From 9384f043b87b3f2ef24a0da71cddf7e068631ed1 Mon Sep 17 00:00:00 2001 From: Travis Raines <571832+rainest@users.noreply.github.com> Date: Mon, 29 Apr 2024 13:04:01 -0700 Subject: [PATCH 1/2] 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 b8f7eb54420592f8ada2078082cb49170cf364e0) --- internal/dataplane/translator/translator.go | 12 +++++++++++- test/integration/ingress_test.go | 5 ++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/dataplane/translator/translator.go b/internal/dataplane/translator/translator.go index 576a941faf..61ded5f876 100644 --- a/internal/dataplane/translator/translator.go +++ b/internal/dataplane/translator/translator.go @@ -449,6 +449,16 @@ func (t *Translator) getUpstreams(serviceMap map[string]kongstate.Service) ([]ko return upstreams, serviceMap } +// targetWeightOrDefault returns the effective value of a target weight pointer. If the pointer is non-nil, it returns +// the pointee. If the pointer is nil, it returns 100, the default Kong target weight. This allows us to sum +// deduplicated targets' weights if one happens to be unset in the controller. +func targetWeightOrDefault(in *int) int { + if in != nil { + return *in + } + return 100 +} + func updateTargetMap(targetMap map[string]kongstate.Target, t kongstate.Target) map[string]kongstate.Target { // See https://github.com/Kong/kubernetes-ingress-controller/issues/5761: // 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) // instead requires t.Target.Target. For consistency, everything below explicitly includes the nested object // name, so t.Target.Weight instead of t.Weight. if existing, ok := targetMap[*t.Target.Target]; ok { - sum := *existing.Target.Weight + *t.Target.Weight + sum := targetWeightOrDefault(existing.Target.Weight) + targetWeightOrDefault(t.Target.Weight) existing.Target.Weight = &sum targetMap[*t.Target.Target] = existing } else { diff --git a/test/integration/ingress_test.go b/test/integration/ingress_test.go index d5469e960b..da81191bd3 100644 --- a/test/integration/ingress_test.go +++ b/test/integration/ingress_test.go @@ -296,7 +296,7 @@ func TestIngressClassNameSpec(t *testing.T) { helpers.EventuallyExpectHTTP404WithNoRoute(t, proxyURL, proxyURL.Host, "/test_ingressclassname_spec", ingressWait, waitTick, nil) } -func TestIngressNamespaces(t *testing.T) { +func TestIngressServiceUpstream(t *testing.T) { t.Parallel() ctx := context.Background() @@ -307,12 +307,15 @@ func TestIngressNamespaces(t *testing.T) { t.Log("deploying a minimal HTTP container deployment to test Ingress routes") container := generators.NewContainer("httpbin", test.HTTPBinImage, test.HTTPBinPort) deployment := generators.NewDeploymentForContainer(container) + deployment.Spec.Replicas = lo.ToPtr(int32(3)) deployment, err := env.Cluster().Client().AppsV1().Deployments(ns.Name).Create(ctx, deployment, metav1.CreateOptions{}) require.NoError(t, err) cleaner.Add(deployment) t.Logf("exposing deployment %s via service", deployment.Name) service := generators.NewServiceForDeployment(deployment, corev1.ServiceTypeLoadBalancer) + service.ObjectMeta.Annotations = map[string]string{} + service.ObjectMeta.Annotations["ingress.kubernetes.io/service-upstream"] = "true" service, err = env.Cluster().Client().CoreV1().Services(ns.Name).Create(ctx, service, metav1.CreateOptions{}) require.NoError(t, err) cleaner.Add(service) From 93356b53c1a5ea5cda7e1ae1d885ec5ee0ac063f Mon Sep 17 00:00:00 2001 From: Yi Tao Date: Wed, 15 May 2024 16:32:29 +0800 Subject: [PATCH 2/2] add changelog entry --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a52b4b920e..6d0d927221 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,7 +93,9 @@ Adding a new version? You'll need three changes: - Support to apply licenses to DB backed Kong gateway from `KongLicense`. [#5648](https://github.com/Kong/kubernetes-ingress-controller/pull/5648) - Redacted values no longer cause collisions in configuration reported to Konnect. - [5964](https://github.com/Kong/kubernetes-ingress-controller/pull/5964) + [#5964](https://github.com/Kong/kubernetes-ingress-controller/pull/5964) +- Assign a default value for `weight` in Kong target if the `weight` is nil. + [#5946](https://github.com/Kong/kubernetes-ingress-controller/pull/5946) ## [3.1.4]