Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit ea5b34a

Browse files
authored
Merge pull request #1347 from mars1024/feat/retain_service
retain healthCheckNodePort for service when updating
2 parents 7653009 + 4f4675a commit ea5b34a

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

pkg/controller/sync/dispatch/retain.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ func RetainClusterFields(targetKind string, desiredObj, clusterObj, fedObj *unst
4646
}
4747

4848
func retainServiceFields(desiredObj, clusterObj *unstructured.Unstructured) error {
49+
// healthCheckNodePort is allocated by APIServer and unchangeable, so it should be retained while updating
50+
healthCheckNodePort, ok, err := unstructured.NestedInt64(clusterObj.Object, "spec", "healthCheckNodePort")
51+
if err != nil {
52+
return errors.Wrap(err, "Error retrieving healthCheckNodePort from service")
53+
}
54+
if ok && healthCheckNodePort > 0 {
55+
if err = unstructured.SetNestedField(desiredObj.Object, healthCheckNodePort, "spec", "healthCheckNodePort"); err != nil {
56+
return errors.Wrap(err, "Error setting healthCheckNodePort for service")
57+
}
58+
}
59+
4960
// ClusterIP and NodePort are allocated to Service by cluster, so retain the same if any while updating
5061

5162
// Retain clusterip

pkg/controller/sync/dispatch/retain_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"testing"
2121

2222
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23+
"k8s.io/utils/pointer"
2324

2425
"sigs.k8s.io/kubefed/pkg/controller/util"
2526
)
@@ -85,3 +86,88 @@ func TestRetainClusterFields(t *testing.T) {
8586
})
8687
}
8788
}
89+
90+
func TestRetainHealthCheckNodePortInServiceFields(t *testing.T) {
91+
tests := []struct {
92+
name string
93+
desiredObj *unstructured.Unstructured
94+
clusterObj *unstructured.Unstructured
95+
retainSucceed bool
96+
expectedValue *int64
97+
}{
98+
{
99+
"cluster object has no healthCheckNodePort",
100+
&unstructured.Unstructured{
101+
Object: map[string]interface{}{},
102+
},
103+
&unstructured.Unstructured{
104+
Object: map[string]interface{}{},
105+
},
106+
true,
107+
nil,
108+
},
109+
{
110+
"cluster object has invalid healthCheckNodePort",
111+
&unstructured.Unstructured{
112+
Object: map[string]interface{}{},
113+
},
114+
&unstructured.Unstructured{
115+
Object: map[string]interface{}{
116+
"spec": map[string]interface{}{
117+
"healthCheckNodePort": "invalid string",
118+
},
119+
},
120+
},
121+
false,
122+
nil,
123+
},
124+
{
125+
"cluster object has healthCheckNodePort 0",
126+
&unstructured.Unstructured{
127+
Object: map[string]interface{}{},
128+
},
129+
&unstructured.Unstructured{
130+
Object: map[string]interface{}{
131+
"spec": map[string]interface{}{
132+
"healthCheckNodePort": int64(0),
133+
},
134+
},
135+
},
136+
true,
137+
nil,
138+
},
139+
{
140+
"cluster object has healthCheckNodePort 1000",
141+
&unstructured.Unstructured{
142+
Object: map[string]interface{}{},
143+
},
144+
&unstructured.Unstructured{
145+
Object: map[string]interface{}{
146+
"spec": map[string]interface{}{
147+
"healthCheckNodePort": int64(1000),
148+
},
149+
},
150+
},
151+
true,
152+
pointer.Int64Ptr(1000),
153+
},
154+
}
155+
for _, test := range tests {
156+
t.Run(test.name, func(t *testing.T) {
157+
if err := retainServiceFields(test.desiredObj, test.clusterObj); (err == nil) != test.retainSucceed {
158+
t.Fatalf("test %s fails: unexpected returned error %v", test.name, err)
159+
}
160+
161+
currentValue, ok, err := unstructured.NestedInt64(test.desiredObj.Object, "spec", "healthCheckNodePort")
162+
if err != nil {
163+
t.Fatalf("test %s fails: %v", test.name, err)
164+
}
165+
if !ok && test.expectedValue != nil {
166+
t.Fatalf("test %s fails: expect specified healthCheckNodePort but not found", test.name)
167+
}
168+
if ok && (test.expectedValue == nil || *test.expectedValue != currentValue) {
169+
t.Fatalf("test %s fails: unexpected current healthCheckNodePort %d", test.name, currentValue)
170+
}
171+
})
172+
}
173+
}

0 commit comments

Comments
 (0)