Skip to content

Commit fe430c8

Browse files
Merge pull request #1577 from openshift-cherrypick-robot/cherry-pick-1566-to-release-0.6
[release-0.6] syncer/namespace-locator: Rename path to workspace
2 parents 47e2320 + 769a730 commit fe430c8

File tree

4 files changed

+239
-41
lines changed

4 files changed

+239
-41
lines changed

pkg/syncer/shared/namespace.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,30 @@ type NamespaceLocator struct {
4141
}
4242

4343
type SyncTargetLocator struct {
44-
Path logicalcluster.Name `json:"path"`
45-
Name string `json:"name"`
46-
UID types.UID `json:"uid"`
44+
Workspace string `json:"workspace,omitempty"`
45+
DeprecatedPath string `json:"path,omitempty"`
46+
Name string `json:"name"`
47+
UID types.UID `json:"uid"`
4748
}
4849

4950
func NewNamespaceLocator(workspace, syncTargetWorkspace logicalcluster.Name, syncTargetUID types.UID, workloadLogicalClusterName, upstreamNamespace string) NamespaceLocator {
5051
return NamespaceLocator{
5152
SyncTarget: SyncTargetLocator{
52-
Path: syncTargetWorkspace,
53-
Name: workloadLogicalClusterName,
54-
UID: syncTargetUID,
53+
Workspace: syncTargetWorkspace.String(),
54+
Name: workloadLogicalClusterName,
55+
UID: syncTargetUID,
56+
},
57+
Workspace: workspace,
58+
Namespace: upstreamNamespace,
59+
}
60+
}
61+
62+
func NewNamespaceLocatorV060(workspace, syncTargetWorkspace logicalcluster.Name, syncTargetUID types.UID, workloadLogicalClusterName, upstreamNamespace string) NamespaceLocator {
63+
return NamespaceLocator{
64+
SyncTarget: SyncTargetLocator{
65+
DeprecatedPath: syncTargetWorkspace.String(),
66+
Name: workloadLogicalClusterName,
67+
UID: syncTargetUID,
5568
},
5669
Workspace: workspace,
5770
Namespace: upstreamNamespace,
@@ -67,6 +80,13 @@ func LocatorFromAnnotations(annotations map[string]string) (*NamespaceLocator, b
6780
if err := json.Unmarshal([]byte(annotation), &locator); err != nil {
6881
return nil, false, err
6982
}
83+
84+
// get us from v0.6.0 locators (using syncTarget.path) to v0.6.1+ (using syncTarget.workspace)
85+
if locator.SyncTarget.Workspace == "" {
86+
locator.SyncTarget.Workspace = locator.SyncTarget.DeprecatedPath
87+
}
88+
locator.SyncTarget.DeprecatedPath = ""
89+
7090
return &locator, true, nil
7191
}
7292

pkg/syncer/shared/namespace_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Copyright 2022 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package shared
18+
19+
import (
20+
"reflect"
21+
"strings"
22+
"testing"
23+
24+
"github.com/kcp-dev/logicalcluster"
25+
)
26+
27+
func TestLocatorFromAnnotations(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
annotations map[string]string
31+
want *NamespaceLocator
32+
wantFound bool
33+
wantErrs []string
34+
}{
35+
{
36+
name: "no annotation",
37+
wantFound: false,
38+
},
39+
{
40+
name: "garbage",
41+
annotations: map[string]string{
42+
NamespaceLocatorAnnotation: "garbage",
43+
},
44+
wantErrs: []string{"invalid character"},
45+
},
46+
{
47+
name: "happy case",
48+
annotations: map[string]string{
49+
NamespaceLocatorAnnotation: `{"syncTarget":{"workspace":"test-workspace","name":"test-name","uid":"test-uid"},"workspace":"test-workspace","namespace":"test-namespace"}`,
50+
},
51+
want: &NamespaceLocator{
52+
SyncTarget: SyncTargetLocator{
53+
Workspace: "test-workspace",
54+
Name: "test-name",
55+
UID: "test-uid",
56+
},
57+
Workspace: logicalcluster.New("test-workspace"),
58+
Namespace: "test-namespace",
59+
},
60+
wantFound: true,
61+
},
62+
{
63+
name: "format up to v0.6.0",
64+
annotations: map[string]string{
65+
NamespaceLocatorAnnotation: `{"syncTarget":{"path":"test-workspace","name":"test-name","uid":"test-uid"},"workspace":"test-workspace","namespace":"test-namespace"}`,
66+
},
67+
want: &NamespaceLocator{
68+
SyncTarget: SyncTargetLocator{
69+
Workspace: "test-workspace",
70+
Name: "test-name",
71+
UID: "test-uid",
72+
},
73+
Workspace: logicalcluster.New("test-workspace"),
74+
Namespace: "test-namespace",
75+
},
76+
wantFound: true,
77+
},
78+
}
79+
for _, tt := range tests {
80+
t.Run(tt.name, func(t *testing.T) {
81+
got, gotFound, err := LocatorFromAnnotations(tt.annotations)
82+
if (err != nil) != (len(tt.wantErrs) > 0) {
83+
t.Errorf("LocatorFromAnnotations() error = %q, wantErrs %v", err.Error(), tt.wantErrs)
84+
return
85+
} else if err != nil {
86+
for _, wantErr := range tt.wantErrs {
87+
if !strings.Contains(err.Error(), wantErr) {
88+
t.Errorf("LocatorFromAnnotations() error = %q, wantErrs %q", err.Error(), wantErr)
89+
return
90+
}
91+
}
92+
}
93+
if !reflect.DeepEqual(got, tt.want) {
94+
t.Errorf("LocatorFromAnnotations() got = %v, want %v", got, tt.want)
95+
}
96+
if gotFound != tt.wantFound {
97+
t.Errorf("LocatorFromAnnotations() gotFound = %v, want %v", gotFound, tt.wantFound)
98+
}
99+
})
100+
}
101+
}

pkg/syncer/spec/spec_process.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,30 @@ func (c *Controller) process(ctx context.Context, gvr schema.GroupVersionResourc
114114
}
115115
clusterName, name := clusters.SplitClusterAwareKey(clusterAwareName)
116116

117+
namespaceGvr := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}
118+
117119
desiredNSLocator := shared.NewNamespaceLocator(clusterName, c.syncTargetClusterName, c.syncTargetUID, c.syncTargetName, upstreamNamespace)
118120
jsonNSLocator, err := json.Marshal(desiredNSLocator)
119121
if err != nil {
120122
return err
121123
}
122-
123-
namespaceGvr := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}
124124
downstreamNamespaces, err := c.downstreamInformers.ForResource(namespaceGvr).Informer().GetIndexer().ByIndex(byNamespaceLocatorIndexName, string(jsonNSLocator))
125125
if err != nil {
126126
return err
127127
}
128+
if len(downstreamNamespaces) == 0 {
129+
// case for up to v0.6.0 where we used syncTarget.path in namespace locators
130+
desiredNSLocator := shared.NewNamespaceLocatorV060(clusterName, c.syncTargetClusterName, c.syncTargetUID, c.syncTargetName, upstreamNamespace)
131+
jsonNSLocator, err := json.Marshal(desiredNSLocator)
132+
if err != nil {
133+
return err
134+
}
135+
downstreamNamespaces, err = c.downstreamInformers.ForResource(namespaceGvr).Informer().GetIndexer().ByIndex(byNamespaceLocatorIndexName, string(jsonNSLocator))
136+
if err != nil {
137+
return err
138+
}
139+
}
140+
128141
var downstreamNamespace string
129142

130143
if len(downstreamNamespaces) == 1 {

0 commit comments

Comments
 (0)