Skip to content

Commit 1372ad2

Browse files
bonifaidopepov
authored andcommitted
fixing numeric pointer diffs (#18)
* add test for deployment replicas diff * fix int marshalling and IntOrStr behaviour Signed-off-by: Nandor Kracser <bonifaido@gmail.com>
1 parent cf8ca3d commit 1372ad2

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ require (
99
github.com/googleapis/gnostic v0.3.0 // indirect
1010
github.com/goph/emperror v0.17.2
1111
github.com/imdario/mergo v0.3.7 // indirect
12+
github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74be
13+
github.com/modern-go/reflect2 v1.0.1
1214
github.com/pkg/errors v0.8.1
1315
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
1416
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
@@ -22,5 +24,5 @@ require (
2224
k8s.io/client-go v0.15.7
2325
k8s.io/klog v0.4.0
2426
k8s.io/kube-openapi v0.0.0-20190722073852-5e22f3d471e6 // indirect
25-
k8s.io/utils v0.0.0-20190809000727-6c36bc71fc4a // indirect
27+
k8s.io/utils v0.0.0-20190809000727-6c36bc71fc4a
2628
)

integration_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3333
"k8s.io/apimachinery/pkg/runtime/schema"
3434
"k8s.io/apimachinery/pkg/util/intstr"
35+
"k8s.io/utils/pointer"
3536
)
3637

3738
func TestIntegration(t *testing.T) {
@@ -453,6 +454,33 @@ func TestIntegration(t *testing.T) {
453454
},
454455
},
455456
}),
457+
NewTestDiff("deployment does not match when replicas changes",
458+
&appsv1.Deployment{
459+
ObjectMeta: standardObjectMeta(),
460+
Spec: appsv1.DeploymentSpec{
461+
Selector: &metav1.LabelSelector{
462+
MatchLabels: map[string]string{
463+
"a": "b",
464+
},
465+
},
466+
Template: v1.PodTemplateSpec{
467+
ObjectMeta: metaWithLabels(map[string]string{
468+
"a": "b",
469+
}),
470+
Spec: v1.PodSpec{
471+
Containers: []v1.Container{
472+
{
473+
Name: "test-container", Image: "test-image",
474+
},
475+
},
476+
},
477+
},
478+
},
479+
}).
480+
withLocalChange(func(i interface{}) {
481+
pod := i.(*appsv1.Deployment)
482+
pod.Spec.Replicas = pointer.Int32Ptr(0)
483+
}),
456484
NewTestMatch("hpa match",
457485
&v2beta1.HorizontalPodAutoscaler{
458486
ObjectMeta: standardObjectMeta(),

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestMain(m *testing.M) {
6868
if *integration {
6969
err := testContext.Setup()
7070
if err != nil {
71-
panic("Failed to setup test namespace")
71+
panic("Failed to setup test namespace: " + err.Error())
7272
}
7373
}
7474
result := m.Run()

patch/annotation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package patch
1616

1717
import (
18-
"encoding/json"
18+
json "github.com/json-iterator/go"
1919

2020
"k8s.io/apimachinery/pkg/api/meta"
2121
"k8s.io/apimachinery/pkg/runtime"

patch/deletenull.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,39 @@
1515
package patch
1616

1717
import (
18-
"encoding/json"
1918
"reflect"
19+
"unsafe"
2020

2121
"github.com/goph/emperror"
22+
json "github.com/json-iterator/go"
2223
"github.com/pkg/errors"
24+
"k8s.io/apimachinery/pkg/util/intstr"
2325
)
2426

27+
func init() {
28+
// k8s.io/apimachinery/pkg/util/intstr.IntOrString behaves really badly
29+
// from JSON marshaling point of view, it can't be empty basically.
30+
// So we need to override the defined marshaling behaviour and write nil
31+
// instead of 0, because usually (in all observed cases) 0 means "not set"
32+
// for IntOrStr types.
33+
// To make this happen we need to pull in json-iterator and override the
34+
// factory marshaling overrides.
35+
json.RegisterTypeEncoderFunc("intstr.IntOrString",
36+
func(ptr unsafe.Pointer, stream *json.Stream) {
37+
i := (*intstr.IntOrString)(ptr)
38+
if i.IntValue() == 0 {
39+
stream.WriteNil()
40+
} else {
41+
stream.WriteInt(i.IntValue())
42+
}
43+
},
44+
func(ptr unsafe.Pointer) bool {
45+
i := (*intstr.IntOrString)(ptr)
46+
return i.IntValue() == 0
47+
},
48+
)
49+
}
50+
2551
func DeleteNullInJson(jsonBytes []byte) ([]byte, map[string]interface{}, error) {
2652
var patchMap map[string]interface{}
2753

@@ -115,6 +141,8 @@ func isZero(v reflect.Value) bool {
115141
default:
116142
z := reflect.Zero(v.Type())
117143
return v.Interface() == z.Interface()
144+
case reflect.Float64, reflect.Int64:
145+
return false
118146
case reflect.Func, reflect.Map, reflect.Slice:
119147
return v.IsNil()
120148
case reflect.Array:

patch/patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
package patch
1616

1717
import (
18-
"encoding/json"
1918
"fmt"
19+
json "github.com/json-iterator/go"
2020

2121
"github.com/goph/emperror"
2222
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

0 commit comments

Comments
 (0)