Skip to content

Commit

Permalink
Ensure only cluster labels for the current cluster are added when pro…
Browse files Browse the repository at this point in the history
…cessing labels
  • Loading branch information
mikenairn committed Jan 22, 2024
1 parent 75f5e95 commit fd70c83
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
22 changes: 13 additions & 9 deletions pkg/utils/gateway_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
48 changes: 26 additions & 22 deletions pkg/utils/gateway_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
Expand All @@ -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",
},
},
Expand All @@ -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",
},
},
Expand All @@ -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",
},
},
Expand All @@ -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{
Expand All @@ -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",
},
},
{
Expand All @@ -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{
Expand All @@ -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",
},
},
}
Expand Down

0 comments on commit fd70c83

Please sign in to comment.