Skip to content

Commit 74d4a83

Browse files
authored
Merge pull request #199 from datum-cloud/fix/legacy-cluster-name-decode
fix(gateway): tolerate legacy slash-encoded upstream cluster names
2 parents 62fa475 + 7af857e commit 74d4a83

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

internal/controller/gateway_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ func (r *GatewayReconciler) listGatewaysAttachedByDownstreamHTTPRoute(clusterNam
21112111
Name: string(parentRef.Name),
21122112
},
21132113
},
2114-
ClusterName: multicluster.ClusterName(strings.TrimPrefix(strings.ReplaceAll(httpRoute.Labels[downstreamclient.UpstreamOwnerClusterNameLabel], "_", "/"), "cluster-")),
2114+
ClusterName: multicluster.ClusterName(downstreamclient.UpstreamClusterNameFromLabel(httpRoute.Labels[downstreamclient.UpstreamOwnerClusterNameLabel])),
21152115
})
21162116

21172117
}

internal/downstreamclient/enqueue_upstream_owner.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package downstreamclient
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

87
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
98
"k8s.io/apimachinery/pkg/runtime"
@@ -116,7 +115,7 @@ func (e *enqueueRequestForOwner[object]) getOwnerReconcileRequest(obj metav1.Obj
116115
Namespace: labels[UpstreamOwnerNamespaceLabel],
117116
},
118117
},
119-
ClusterName: multicluster.ClusterName(strings.TrimPrefix(strings.ReplaceAll(labels[UpstreamOwnerClusterNameLabel], "_", "/"), "cluster-")),
118+
ClusterName: multicluster.ClusterName(UpstreamClusterNameFromLabel(labels[UpstreamOwnerClusterNameLabel])),
120119
}
121120
result[request] = empty{}
122121
}

internal/downstreamclient/mappednamespace.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ const (
128128
UpstreamOwnerNamespaceLabel = "meta.datumapis.com/upstream-namespace"
129129
)
130130

131+
// UpstreamClusterNameFromLabel decodes the upstream cluster name from the value
132+
// of UpstreamOwnerClusterNameLabel, reversing the encoding applied when the
133+
// label is written ("cluster-" prefix, slashes encoded as underscores).
134+
//
135+
// It also tolerates labels written before the cluster name format changed in
136+
// #196: those carried a leading slash (e.g. "/project", encoded as
137+
// "cluster-_project") which decodes to "/project". The multicluster provider
138+
// now engages clusters under the slash-less name ("project"), so the leading
139+
// slash is stripped to keep legacy downstream resources resolvable.
140+
func UpstreamClusterNameFromLabel(value string) string {
141+
name := strings.TrimPrefix(strings.ReplaceAll(value, "_", "/"), "cluster-")
142+
return strings.TrimPrefix(name, "/")
143+
}
144+
131145
func (c *mappedNamespaceResourceStrategy) SetControllerReference(ctx context.Context, owner, controlled metav1.Object, opts ...controllerutil.OwnerReferenceOption) error {
132146
// TODO(jreese) add owner validation
133147

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package downstreamclient
2+
3+
import "testing"
4+
5+
func TestUpstreamClusterNameFromLabel(t *testing.T) {
6+
cases := []struct {
7+
name string
8+
label string
9+
want string
10+
}{
11+
{
12+
name: "modern slash-less name",
13+
label: "cluster-my-project-abc123",
14+
want: "my-project-abc123",
15+
},
16+
{
17+
name: "legacy leading-slash name (pre-#196)",
18+
label: "cluster-_my-project-abc123",
19+
want: "my-project-abc123",
20+
},
21+
{
22+
name: "multi-segment name is preserved",
23+
label: "cluster-org_my-project",
24+
want: "org/my-project",
25+
},
26+
{
27+
name: "empty label",
28+
label: "",
29+
want: "",
30+
},
31+
}
32+
33+
for _, tc := range cases {
34+
t.Run(tc.name, func(t *testing.T) {
35+
if got := UpstreamClusterNameFromLabel(tc.label); got != tc.want {
36+
t.Errorf("UpstreamClusterNameFromLabel(%q) = %q, want %q", tc.label, got, tc.want)
37+
}
38+
})
39+
}
40+
}

0 commit comments

Comments
 (0)