From fd70c832dc4609f0da06f4710846aa76c2b3b00b Mon Sep 17 00:00:00 2001 From: Michael Nairn Date: Mon, 22 Jan 2024 09:16:52 +0000 Subject: [PATCH] Ensure only cluster labels for the current cluster are added when processing labels --- pkg/utils/gateway_wrapper.go | 22 ++++++++------ pkg/utils/gateway_wrapper_test.go | 48 +++++++++++++++++-------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/pkg/utils/gateway_wrapper.go b/pkg/utils/gateway_wrapper.go index dc9acfde..3094050b 100644 --- a/pkg/utils/gateway_wrapper.go +++ b/pkg/utils/gateway_wrapper.go @@ -9,8 +9,10 @@ import ( ) const ( - MultiClusterIPAddressType gatewayapiv1.AddressType = "kuadrant.io/MultiClusterIPAddress" - MultiClusterHostnameAddressType gatewayapiv1.AddressType = "kuadrant.io/MultiClusterHostnameAddress" + LabelPrefix = "kuadrant.io/" + ClustersLabelPrefix = "clusters." + LabelPrefix + MultiClusterIPAddressType gatewayapiv1.AddressType = LabelPrefix + "MultiClusterIPAddress" + MultiClusterHostnameAddressType gatewayapiv1.AddressType = LabelPrefix + "MultiClusterHostnameAddress" ) type GatewayWrapper struct { @@ -92,14 +94,16 @@ func (g *GatewayWrapper) GetClusterGatewayLabels(clusterName string) map[string] labels := map[string]string{} for k, v := range g.GetLabels() { - attr, found := strings.CutPrefix(k, "clusters.kuadrant.io/"+clusterName+"_") - if found { - labels["kuadrant.io/"+attr] = v - } else { - // Only add a label if we haven't already found a cluster specific version of it - if _, ok := labels[k]; !ok { - labels[k] = v + if strings.HasPrefix(k, ClustersLabelPrefix) { + attr, found := strings.CutPrefix(k, ClustersLabelPrefix+clusterName+"_") + if found { + labels[LabelPrefix+attr] = v } + continue + } + // Only add a label if we haven't already found a cluster specific version of it + if _, ok := labels[k]; !ok { + labels[k] = v } } return labels diff --git a/pkg/utils/gateway_wrapper_test.go b/pkg/utils/gateway_wrapper_test.go index 49407f0a..77c6f248 100644 --- a/pkg/utils/gateway_wrapper_test.go +++ b/pkg/utils/gateway_wrapper_test.go @@ -241,6 +241,7 @@ func TestGatewayWrapper_GetClusterGatewayLabels(t *testing.T) { Labels: map[string]string{ "kuadrant.io/lb-attribute-weight": "TSTATTR", "kuadrant.io/lb-attribute-geo-code": "EU", + "kuadrant.io/foo": "bar", "foo": "bar", }, }, @@ -249,6 +250,7 @@ func TestGatewayWrapper_GetClusterGatewayLabels(t *testing.T) { want: map[string]string{ "kuadrant.io/lb-attribute-weight": "TSTATTR", "kuadrant.io/lb-attribute-geo-code": "EU", + "kuadrant.io/foo": "bar", "foo": "bar", }, }, @@ -260,6 +262,7 @@ func TestGatewayWrapper_GetClusterGatewayLabels(t *testing.T) { Labels: map[string]string{ "kuadrant.io/lb-attribute-weight": "TSTATTR", "kuadrant.io/lb-attribute-geo-code": "EU", + "kuadrant.io/foo": "bar", "foo": "bar", }, }, @@ -280,6 +283,7 @@ func TestGatewayWrapper_GetClusterGatewayLabels(t *testing.T) { want: map[string]string{ "kuadrant.io/lb-attribute-weight": "TSTATTR", "kuadrant.io/lb-attribute-geo-code": "EU", + "kuadrant.io/foo": "bar", "foo": "bar", }, }, @@ -289,11 +293,12 @@ func TestGatewayWrapper_GetClusterGatewayLabels(t *testing.T) { ObjectMeta: v1.ObjectMeta{ Name: "testgw", Labels: map[string]string{ - "kuadrant.io/kind-mgc-control-plane_lb-attribute-weight": "TSTATTR", - "kuadrant.io/kind-mgc-control-plane_lb-attribute-geo-code": "EU", - "kuadrant.io/kind-mgc-workload-1_lb-attribute-weight": "TSTATTR2", - "kuadrant.io/kind-mgc-workload-1_lb-attribute-geo-code": "US", - "foo": "bar", + "clusters.kuadrant.io/kind-mgc-control-plane_lb-attribute-weight": "TSTATTR", + "clusters.kuadrant.io/kind-mgc-control-plane_lb-attribute-geo-code": "EU", + "clusters.kuadrant.io/kind-mgc-workload-1_lb-attribute-weight": "TSTATTR2", + "clusters.kuadrant.io/kind-mgc-workload-1_lb-attribute-geo-code": "US", + "kuadrant.io/foo": "bar", + "foo": "bar", }, }, Status: gatewayapiv1.GatewayStatus{ @@ -311,11 +316,10 @@ func TestGatewayWrapper_GetClusterGatewayLabels(t *testing.T) { }, clusterName: "kind-mgc-control-plane", want: map[string]string{ - "kuadrant.io/lb-attribute-weight": "TSTATTR", - "kuadrant.io/lb-attribute-geo-code": "EU", - "kuadrant.io/kind-mgc-workload-1_lb-attribute-weight": "TSTATTR2", - "kuadrant.io/kind-mgc-workload-1_lb-attribute-geo-code": "US", - "foo": "bar", + "kuadrant.io/lb-attribute-weight": "TSTATTR", + "kuadrant.io/lb-attribute-geo-code": "EU", + "kuadrant.io/foo": "bar", + "foo": "bar", }, }, { @@ -324,13 +328,14 @@ func TestGatewayWrapper_GetClusterGatewayLabels(t *testing.T) { ObjectMeta: v1.ObjectMeta{ Name: "testgw", Labels: map[string]string{ - "kuadrant.io/kind-mgc-control-plane_lb-attribute-weight": "TSTATTR", - "kuadrant.io/kind-mgc-control-plane_lb-attribute-geo-code": "EU", - "kuadrant.io/kind-mgc-workload-1_lb-attribute-weight": "TSTATTR2", - "kuadrant.io/kind-mgc-workload-1_lb-attribute-geo-code": "US", - "kuadrant.io/lb-attribute-weight": "TSTATTR3", - "kuadrant.io/lb-attribute-geo-code": "ES", - "foo": "bar", + "clusters.kuadrant.io/kind-mgc-control-plane_lb-attribute-weight": "TSTATTR", + "clusters.kuadrant.io/kind-mgc-control-plane_lb-attribute-geo-code": "EU", + "clusters.kuadrant.io/kind-mgc-workload-1_lb-attribute-weight": "TSTATTR2", + "clusters.kuadrant.io/kind-mgc-workload-1_lb-attribute-geo-code": "US", + "kuadrant.io/lb-attribute-weight": "TSTATTR3", + "kuadrant.io/lb-attribute-geo-code": "ES", + "kuadrant.io/foo": "bar", + "foo": "bar", }, }, Status: gatewayapiv1.GatewayStatus{ @@ -348,11 +353,10 @@ func TestGatewayWrapper_GetClusterGatewayLabels(t *testing.T) { }, clusterName: "kind-mgc-control-plane", want: map[string]string{ - "kuadrant.io/lb-attribute-weight": "TSTATTR", - "kuadrant.io/lb-attribute-geo-code": "EU", - "kuadrant.io/kind-mgc-workload-1_lb-attribute-weight": "TSTATTR2", - "kuadrant.io/kind-mgc-workload-1_lb-attribute-geo-code": "US", - "foo": "bar", + "kuadrant.io/lb-attribute-weight": "TSTATTR", + "kuadrant.io/lb-attribute-geo-code": "EU", + "kuadrant.io/foo": "bar", + "foo": "bar", }, }, }