From 8d714d38ea2b3640dc085f3c21ef1416ba9acae9 Mon Sep 17 00:00:00 2001 From: Scott Seago Date: Mon, 9 Aug 2021 16:27:32 -0400 Subject: [PATCH] Distinguish between different unnamed node ports when preserving Signed-off-by: Scott Seago --- changelogs/unreleased/4026-sseago | 1 + pkg/restore/service_action.go | 13 ++++++++++--- pkg/restore/service_action_test.go | 4 ++++ 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/4026-sseago diff --git a/changelogs/unreleased/4026-sseago b/changelogs/unreleased/4026-sseago new file mode 100644 index 000000000..951ced8a7 --- /dev/null +++ b/changelogs/unreleased/4026-sseago @@ -0,0 +1 @@ +Distinguish between different unnamed node ports when preserving diff --git a/pkg/restore/service_action.go b/pkg/restore/service_action.go index e8b58f062..01082c7a1 100644 --- a/pkg/restore/service_action.go +++ b/pkg/restore/service_action.go @@ -83,6 +83,7 @@ func deleteNodePorts(service *corev1api.Service) error { // to the last-applied-config annotation. We'll retain these values, and // clear out any other (presumably auto-assigned) NodePort values. explicitNodePorts := sets.NewString() + unnamedPortInts := sets.NewInt() lastAppliedConfig, ok := service.Annotations[annotationLastAppliedConfig] if ok { appliedServiceUnstructured := new(map[string]interface{}) @@ -123,7 +124,7 @@ func deleteNodePorts(service *corev1api.Service) error { portName, ok := p["name"] if !ok { // unnamed port - explicitNodePorts.Insert("") + unnamedPortInts.Insert(nodePortInt) } else { explicitNodePorts.Insert(portName.(string)) } @@ -135,8 +136,14 @@ func deleteNodePorts(service *corev1api.Service) error { } for i, port := range service.Spec.Ports { - if !explicitNodePorts.Has(port.Name) { - service.Spec.Ports[i].NodePort = 0 + if port.Name != "" { + if !explicitNodePorts.Has(port.Name) { + service.Spec.Ports[i].NodePort = 0 + } + } else { + if !unnamedPortInts.Has(int(port.NodePort)) { + service.Spec.Ports[i].NodePort = 0 + } } } diff --git a/pkg/restore/service_action_test.go b/pkg/restore/service_action_test.go index e6b0b1530..54e616a9a 100644 --- a/pkg/restore/service_action_test.go +++ b/pkg/restore/service_action_test.go @@ -196,6 +196,9 @@ func TestServiceActionExecute(t *testing.T) { { NodePort: 8080, }, + { + NodePort: 9090, + }, }, }, }, @@ -212,6 +215,7 @@ func TestServiceActionExecute(t *testing.T) { { NodePort: 8080, }, + {}, }, }, },