Skip to content

Commit 779cad2

Browse files
randmonkeyrainest
andauthored
[Backport release/3.1.x] deduplicate nil-weight targets (#5946) (#6025)
* fix(targets) deduplicate nil-weight targets (#5946) Assume the Kong default 100 weight for targets that have nil weight when being deduplicated. (cherry picked from commit b8f7eb5) * add changelog entry --------- Co-authored-by: Travis Raines <[email protected]>
1 parent bff96c9 commit 779cad2

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ Adding a new version? You'll need three changes:
9393
- Support to apply licenses to DB backed Kong gateway from `KongLicense`.
9494
[#5648](https://github.com/Kong/kubernetes-ingress-controller/pull/5648)
9595
- Redacted values no longer cause collisions in configuration reported to Konnect.
96-
[5964](https://github.com/Kong/kubernetes-ingress-controller/pull/5964)
96+
[#5964](https://github.com/Kong/kubernetes-ingress-controller/pull/5964)
97+
- Assign a default value for `weight` in Kong target if the `weight` is nil.
98+
[#5946](https://github.com/Kong/kubernetes-ingress-controller/pull/5946)
9799

98100
## [3.1.4]
99101

internal/dataplane/translator/translator.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,16 @@ func (t *Translator) getUpstreams(serviceMap map[string]kongstate.Service) ([]ko
449449
return upstreams, serviceMap
450450
}
451451

452+
// targetWeightOrDefault returns the effective value of a target weight pointer. If the pointer is non-nil, it returns
453+
// the pointee. If the pointer is nil, it returns 100, the default Kong target weight. This allows us to sum
454+
// deduplicated targets' weights if one happens to be unset in the controller.
455+
func targetWeightOrDefault(in *int) int {
456+
if in != nil {
457+
return *in
458+
}
459+
return 100
460+
}
461+
452462
func updateTargetMap(targetMap map[string]kongstate.Target, t kongstate.Target) map[string]kongstate.Target {
453463
// See https://github.com/Kong/kubernetes-ingress-controller/issues/5761:
454464
// Duplicate targets will appear in configurations that use Services with the same selector, which are used
@@ -459,7 +469,7 @@ func updateTargetMap(targetMap map[string]kongstate.Target, t kongstate.Target)
459469
// instead requires t.Target.Target. For consistency, everything below explicitly includes the nested object
460470
// name, so t.Target.Weight instead of t.Weight.
461471
if existing, ok := targetMap[*t.Target.Target]; ok {
462-
sum := *existing.Target.Weight + *t.Target.Weight
472+
sum := targetWeightOrDefault(existing.Target.Weight) + targetWeightOrDefault(t.Target.Weight)
463473
existing.Target.Weight = &sum
464474
targetMap[*t.Target.Target] = existing
465475
} else {

test/integration/ingress_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func TestIngressClassNameSpec(t *testing.T) {
296296
helpers.EventuallyExpectHTTP404WithNoRoute(t, proxyURL, proxyURL.Host, "/test_ingressclassname_spec", ingressWait, waitTick, nil)
297297
}
298298

299-
func TestIngressNamespaces(t *testing.T) {
299+
func TestIngressServiceUpstream(t *testing.T) {
300300
t.Parallel()
301301

302302
ctx := context.Background()
@@ -307,12 +307,15 @@ func TestIngressNamespaces(t *testing.T) {
307307
t.Log("deploying a minimal HTTP container deployment to test Ingress routes")
308308
container := generators.NewContainer("httpbin", test.HTTPBinImage, test.HTTPBinPort)
309309
deployment := generators.NewDeploymentForContainer(container)
310+
deployment.Spec.Replicas = lo.ToPtr(int32(3))
310311
deployment, err := env.Cluster().Client().AppsV1().Deployments(ns.Name).Create(ctx, deployment, metav1.CreateOptions{})
311312
require.NoError(t, err)
312313
cleaner.Add(deployment)
313314

314315
t.Logf("exposing deployment %s via service", deployment.Name)
315316
service := generators.NewServiceForDeployment(deployment, corev1.ServiceTypeLoadBalancer)
317+
service.ObjectMeta.Annotations = map[string]string{}
318+
service.ObjectMeta.Annotations["ingress.kubernetes.io/service-upstream"] = "true"
316319
service, err = env.Cluster().Client().CoreV1().Services(ns.Name).Create(ctx, service, metav1.CreateOptions{})
317320
require.NoError(t, err)
318321
cleaner.Add(service)

0 commit comments

Comments
 (0)