diff --git a/changelogs/unreleased/10132-arpitjain099 b/changelogs/unreleased/10132-arpitjain099 new file mode 100644 index 000000000..493c15ba8 --- /dev/null +++ b/changelogs/unreleased/10132-arpitjain099 @@ -0,0 +1 @@ +Fix a panic when a port name in the last-applied-configuration annotation is not a string diff --git a/pkg/restore/actions/service_action.go b/pkg/restore/actions/service_action.go index 1dd712d9f..56697902a 100644 --- a/pkg/restore/actions/service_action.go +++ b/pkg/restore/actions/service_action.go @@ -194,7 +194,7 @@ func deleteNodePorts(service *corev1api.Service) error { // unnamed port unnamedPortInts.Insert(nodePortInt) } else { - explicitNodePorts.Insert(portName.(string)) + explicitNodePorts.Insert(fmt.Sprint(portName)) } } } diff --git a/pkg/restore/actions/service_action_test.go b/pkg/restore/actions/service_action_test.go index f9a01d5d4..89f3b0e69 100644 --- a/pkg/restore/actions/service_action_test.go +++ b/pkg/restore/actions/service_action_test.go @@ -674,6 +674,44 @@ func TestServiceActionExecute(t *testing.T) { }, }, }, + { + name: "If a port name in last-applied-configuration is not a string, it should not crash.", + obj: corev1api.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc-1", + Annotations: map[string]string{ + "kubectl.kubernetes.io/last-applied-configuration": `{"spec":{"ports":[{"nodePort":30001,"name":123}]}}`, + }, + }, + Spec: corev1api.ServiceSpec{ + Type: corev1api.ServiceTypeNodePort, + Ports: []corev1api.ServicePort{ + { + Name: "http", + NodePort: 30001, + }, + }, + }, + }, + restore: builder.ForRestore(api.DefaultNamespace, "").PreserveNodePorts(false).Result(), + expectedRes: corev1api.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "svc-1", + Annotations: map[string]string{ + "kubectl.kubernetes.io/last-applied-configuration": `{"spec":{"ports":[{"nodePort":30001,"name":123}]}}`, + }, + }, + Spec: corev1api.ServiceSpec{ + Type: corev1api.ServiceTypeNodePort, + Ports: []corev1api.ServicePort{ + { + Name: "http", + NodePort: 0, + }, + }, + }, + }, + }, } for _, test := range tests {