From 214b63f6f7e49b7766e0547b314eec2a9daf93c7 Mon Sep 17 00:00:00 2001 From: Arpit Jain <3242828+arpitjain099@users.noreply.github.com> Date: Tue, 15 Sep 2026 09:50:10 -0400 Subject: [PATCH] Do not assume a port name is a string when clearing node ports (#10132) * Do not assume a port name is a string when clearing node ports deleteNodePorts reads the last-applied-configuration annotation, which is free-form JSON controlled by whoever produced the backup, and cast p["name"] to string without checking. A port whose name is a number crashed the restore of that Service with an interface conversion panic. Every sibling in the same loop already uses the comma-ok form. Signed-off-by: Arpit Jain * Add changelog file Signed-off-by: Arpit Jain * Convert name to string by Sprint. Signed-off-by: Xun Jiang --------- Signed-off-by: Arpit Jain Signed-off-by: Xun Jiang Co-authored-by: Xun Jiang --- changelogs/unreleased/10132-arpitjain099 | 1 + pkg/restore/actions/service_action.go | 2 +- pkg/restore/actions/service_action_test.go | 38 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/10132-arpitjain099 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 {