Skip to content

Commit d3f4b2c

Browse files
authored
Merge pull request #9653 from BassinD/bugfix/nil-check-for-service-health-check-node-port-in-last-applied-config
Fix service restore with null healthCheckNodePort in last-applied-configuration label
2 parents baf2491 + dd1def9 commit d3f4b2c

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

changelogs/unreleased/9653-BassinD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix service restore with null healthCheckNodePort in last-applied-configuration label

pkg/restore/actions/service_action.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,18 @@ func deleteHealthCheckNodePort(service *corev1api.Service) error {
9494

9595
// Search HealthCheckNodePort from server's last-applied-configuration
9696
// annotation(HealthCheckNodePort is specified by `kubectl apply` command)
97-
lastAppliedConfig, ok := service.Annotations[annotationLastAppliedConfig]
98-
if ok {
99-
appliedServiceUnstructured := new(map[string]any)
100-
if err := json.Unmarshal([]byte(lastAppliedConfig), appliedServiceUnstructured); err != nil {
101-
return errors.WithStack(err)
97+
if lastAppliedConfig, ok := service.Annotations[annotationLastAppliedConfig]; ok {
98+
var appliedConfig struct {
99+
Spec struct {
100+
HealthCheckNodePort *int32 `json:"healthCheckNodePort"`
101+
} `json:"spec"`
102102
}
103103

104-
healthCheckNodePort, exist, err := unstructured.NestedFloat64(*appliedServiceUnstructured, "spec", "healthCheckNodePort")
105-
if err != nil {
104+
if err := json.Unmarshal([]byte(lastAppliedConfig), &appliedConfig); err != nil {
106105
return errors.WithStack(err)
107106
}
108107

109-
// Found healthCheckNodePort in lastAppliedConfig annotation,
110-
// and the value is not 0. No need to delete, return.
111-
if exist && healthCheckNodePort != 0 {
108+
if appliedConfig.Spec.HealthCheckNodePort != nil && *appliedConfig.Spec.HealthCheckNodePort != 0 {
112109
return nil
113110
}
114111
}

pkg/restore/actions/service_action_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,36 @@ func TestServiceActionExecute(t *testing.T) {
644644
},
645645
},
646646
},
647+
{
648+
name: "If PreserveNodePorts is false and HealthCheckNodePort is null in last-applied-configuration, it should not crash and the port should be cleared.",
649+
obj: corev1api.Service{
650+
ObjectMeta: metav1.ObjectMeta{
651+
Name: "svc-1",
652+
Annotations: map[string]string{
653+
"kubectl.kubernetes.io/last-applied-configuration": `{"spec":{"healthCheckNodePort":null}}`,
654+
},
655+
},
656+
Spec: corev1api.ServiceSpec{
657+
HealthCheckNodePort: 8080,
658+
ExternalTrafficPolicy: corev1api.ServiceExternalTrafficPolicyTypeLocal,
659+
Type: corev1api.ServiceTypeLoadBalancer,
660+
},
661+
},
662+
restore: builder.ForRestore(api.DefaultNamespace, "").PreserveNodePorts(false).Result(),
663+
expectedRes: corev1api.Service{
664+
ObjectMeta: metav1.ObjectMeta{
665+
Name: "svc-1",
666+
Annotations: map[string]string{
667+
"kubectl.kubernetes.io/last-applied-configuration": `{"spec":{"healthCheckNodePort":null}}`,
668+
},
669+
},
670+
Spec: corev1api.ServiceSpec{
671+
HealthCheckNodePort: 0,
672+
ExternalTrafficPolicy: corev1api.ServiceExternalTrafficPolicyTypeLocal,
673+
Type: corev1api.ServiceTypeLoadBalancer,
674+
},
675+
},
676+
},
647677
}
648678

649679
for _, test := range tests {

0 commit comments

Comments
 (0)