From c2f819c0733b81c05ead330d09876e42f61f5826 Mon Sep 17 00:00:00 2001 From: Adam Janikowski <12255597+ajanikow@users.noreply.github.com> Date: Sun, 11 Dec 2022 22:19:21 +0100 Subject: [PATCH] [Bugfix] Ensure NodePort wont be duplicated (#1209) --- CHANGELOG.md | 1 + pkg/deployment/resources/services.go | 2 +- pkg/util/k8sutil/patcher/service.go | 51 +++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 750c9f7e1..43c039852 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ - (Bugfix) Change member port discovery - (Feature) Do not change external service ports - (Bugfix) Fix Operator Debug mode +- (Bugfix) Ensure NodePort wont be duplicated ## [1.2.20](https://github.com/arangodb/kube-arangodb/tree/1.2.20) (2022-10-25) - (Feature) Add action progress diff --git a/pkg/deployment/resources/services.go b/pkg/deployment/resources/services.go index c79343106..ccf442c8c 100644 --- a/pkg/deployment/resources/services.go +++ b/pkg/deployment/resources/services.go @@ -375,7 +375,7 @@ func (r *Resources) ensureExternalAccessManagedServices(ctx context.Context, cac apply := func(svc *core.Service) (bool, error) { return patcher.ServicePatcher(ctx, cachedStatus.ServicesModInterface().V1(), svc, meta.PatchOptions{}, - patcher.PatchServiceOnlyPorts(ports...), + patcher.PatchServiceOnlyPortsWithoutNodePort(ports...), patcher.PatchServiceSelector(selectors)) } diff --git a/pkg/util/k8sutil/patcher/service.go b/pkg/util/k8sutil/patcher/service.go index 803357f98..23e08dfd7 100644 --- a/pkg/util/k8sutil/patcher/service.go +++ b/pkg/util/k8sutil/patcher/service.go @@ -149,9 +149,58 @@ func PatchServiceOnlyPorts(ports ...core.ServicePort) ServicePatch { } } +func PatchServiceOnlyPortsWithoutNodePort(ports ...core.ServicePort) ServicePatch { + return func(in *core.Service) []patch.Item { + psvc := in.Spec.DeepCopy() + cp := psvc.Ports + + changed := false + + for pid := range ports { + got := false + for id := range cp { + if ports[pid].Name == cp[id].Name { + got = true + + // Set ignored fields + if ports[pid].AppProtocol == nil { + ports[pid].AppProtocol = cp[id].AppProtocol + } + if ports[pid].Protocol == "" { + ports[pid].Protocol = cp[id].Protocol + } + if ports[pid].TargetPort.StrVal == "" && ports[pid].TargetPort.IntVal == 0 { + ports[pid].TargetPort = cp[id].TargetPort + } + + if !equality.Semantic.DeepEqual(ports[pid], cp[id]) { + q := ports[pid].DeepCopy() + cp[id] = *q + changed = true + break + } + } + } + if !got { + q := ports[pid].DeepCopy() + cp = append(cp, *q) + changed = true + } + } + + if !changed { + return nil + } + + return []patch.Item{ + patch.ItemReplace(patch.NewPath("spec", "ports"), cp), + } + } +} + func PatchServiceSelector(selector map[string]string) ServicePatch { return func(in *core.Service) []patch.Item { - if equality.Semantic.DeepEqual(in.Spec.Selector, selector) { + if in.Spec.Selector != nil && equality.Semantic.DeepEqual(in.Spec.Selector, selector) { return nil }