Skip to content

Commit c2f819c

Browse files
authored
[Bugfix] Ensure NodePort wont be duplicated (#1209)
1 parent f585136 commit c2f819c

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- (Bugfix) Change member port discovery
4343
- (Feature) Do not change external service ports
4444
- (Bugfix) Fix Operator Debug mode
45+
- (Bugfix) Ensure NodePort wont be duplicated
4546

4647
## [1.2.20](https://github.com/arangodb/kube-arangodb/tree/1.2.20) (2022-10-25)
4748
- (Feature) Add action progress

pkg/deployment/resources/services.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func (r *Resources) ensureExternalAccessManagedServices(ctx context.Context, cac
375375

376376
apply := func(svc *core.Service) (bool, error) {
377377
return patcher.ServicePatcher(ctx, cachedStatus.ServicesModInterface().V1(), svc, meta.PatchOptions{},
378-
patcher.PatchServiceOnlyPorts(ports...),
378+
patcher.PatchServiceOnlyPortsWithoutNodePort(ports...),
379379
patcher.PatchServiceSelector(selectors))
380380
}
381381

pkg/util/k8sutil/patcher/service.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,58 @@ func PatchServiceOnlyPorts(ports ...core.ServicePort) ServicePatch {
149149
}
150150
}
151151

152+
func PatchServiceOnlyPortsWithoutNodePort(ports ...core.ServicePort) ServicePatch {
153+
return func(in *core.Service) []patch.Item {
154+
psvc := in.Spec.DeepCopy()
155+
cp := psvc.Ports
156+
157+
changed := false
158+
159+
for pid := range ports {
160+
got := false
161+
for id := range cp {
162+
if ports[pid].Name == cp[id].Name {
163+
got = true
164+
165+
// Set ignored fields
166+
if ports[pid].AppProtocol == nil {
167+
ports[pid].AppProtocol = cp[id].AppProtocol
168+
}
169+
if ports[pid].Protocol == "" {
170+
ports[pid].Protocol = cp[id].Protocol
171+
}
172+
if ports[pid].TargetPort.StrVal == "" && ports[pid].TargetPort.IntVal == 0 {
173+
ports[pid].TargetPort = cp[id].TargetPort
174+
}
175+
176+
if !equality.Semantic.DeepEqual(ports[pid], cp[id]) {
177+
q := ports[pid].DeepCopy()
178+
cp[id] = *q
179+
changed = true
180+
break
181+
}
182+
}
183+
}
184+
if !got {
185+
q := ports[pid].DeepCopy()
186+
cp = append(cp, *q)
187+
changed = true
188+
}
189+
}
190+
191+
if !changed {
192+
return nil
193+
}
194+
195+
return []patch.Item{
196+
patch.ItemReplace(patch.NewPath("spec", "ports"), cp),
197+
}
198+
}
199+
}
200+
152201
func PatchServiceSelector(selector map[string]string) ServicePatch {
153202
return func(in *core.Service) []patch.Item {
154-
if equality.Semantic.DeepEqual(in.Spec.Selector, selector) {
203+
if in.Spec.Selector != nil && equality.Semantic.DeepEqual(in.Spec.Selector, selector) {
155204
return nil
156205
}
157206

0 commit comments

Comments
 (0)