Only accept string port names when clearing node ports

Co-authored-by: kaovilai <11228024+kaovilai@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-09-15 13:57:26 +00:00
committed by GitHub
co-authored by kaovilai
parent 214b63f6f7
commit a4a3d854b8
2 changed files with 43 additions and 2 deletions
+5 -2
View File
@@ -193,8 +193,11 @@ func deleteNodePorts(service *corev1api.Service) error {
if !ok {
// unnamed port
unnamedPortInts.Insert(nodePortInt)
} else {
explicitNodePorts.Insert(fmt.Sprint(portName))
} else if name, ok := portName.(string); ok {
// spec.ports[].name must be a string in a valid Service, so
// silently skip any non-string value rather than coercing it,
// which could otherwise create a false match with a real port name.
explicitNodePorts.Insert(name)
}
}
}
@@ -712,6 +712,44 @@ func TestServiceActionExecute(t *testing.T) {
},
},
},
{
name: "If a port name in last-applied-configuration is a non-string value that stringifies to match a real port name, it should not falsely preserve that port's NodePort.",
obj: corev1api.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "svc-1",
Annotations: map[string]string{
"kubectl.kubernetes.io/last-applied-configuration": `{"spec":{"ports":[{"nodePort":30001,"name":true}]}}`,
},
},
Spec: corev1api.ServiceSpec{
Type: corev1api.ServiceTypeNodePort,
Ports: []corev1api.ServicePort{
{
Name: "true",
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":true}]}}`,
},
},
Spec: corev1api.ServiceSpec{
Type: corev1api.ServiceTypeNodePort,
Ports: []corev1api.ServicePort{
{
Name: "true",
NodePort: 0,
},
},
},
},
},
}
for _, test := range tests {