@@ -18,7 +18,6 @@ package cluster
18
18
19
19
import (
20
20
"fmt"
21
- "math/rand"
22
21
"os"
23
22
"path/filepath"
24
23
"strings"
@@ -30,6 +29,7 @@ import (
30
29
apierrors "k8s.io/apimachinery/pkg/api/errors"
31
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
31
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
32
+ "k8s.io/apimachinery/pkg/util/sets"
33
33
"k8s.io/utils/pointer"
34
34
"sigs.k8s.io/controller-runtime/pkg/client"
35
35
@@ -50,6 +50,32 @@ var moveTests = []struct {
50
50
wantMoveGroups [][]string
51
51
wantErr bool
52
52
}{
53
+ {
54
+ name : "Cluster with ClusterClass" ,
55
+ fields : moveTestsFields {
56
+ objs : func () []client.Object {
57
+ objs := test .NewFakeClusterClass ("ns1" , "class1" ).Objs ()
58
+ objs = append (objs , test .NewFakeCluster ("ns1" , "foo" ).WithTopologyClass ("class1" ).Objs ()... )
59
+ return deduplicateObjects (objs )
60
+ }(),
61
+ },
62
+ wantMoveGroups : [][]string {
63
+ { // group 1
64
+ "cluster.x-k8s.io/v1beta1, Kind=ClusterClass, ns1/class1" ,
65
+ },
66
+ { // group 2
67
+ "infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureClusterTemplate, ns1/class1" ,
68
+ "controlplane.cluster.x-k8s.io/v1beta1, Kind=GenericControlPlaneTemplate, ns1/class1" ,
69
+ "cluster.x-k8s.io/v1beta1, Kind=Cluster, ns1/foo" ,
70
+ },
71
+ { // group 3
72
+ "/v1, Kind=Secret, ns1/foo-ca" ,
73
+ "/v1, Kind=Secret, ns1/foo-kubeconfig" ,
74
+ "infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureCluster, ns1/foo" ,
75
+ },
76
+ },
77
+ wantErr : false ,
78
+ },
53
79
{
54
80
name : "Cluster" ,
55
81
fields : moveTestsFields {
@@ -1163,9 +1189,82 @@ func Test_objectMover_move_dryRun(t *testing.T) {
1163
1189
}
1164
1190
1165
1191
func Test_objectMover_move (t * testing.T ) {
1192
+ // NB. we are testing the move and move sequence using the same set of moveTests, but checking the results at different stages of the move process
1193
+ for _ , tt := range moveTests {
1194
+ t .Run (tt .name , func (t * testing.T ) {
1195
+ g := NewWithT (t )
1196
+
1197
+ // Create an objectGraph bound a source cluster with all the CRDs for the types involved in the test.
1198
+ graph := getObjectGraphWithObjs (tt .fields .objs )
1199
+
1200
+ // Get all the types to be considered for discovery
1201
+ g .Expect (getFakeDiscoveryTypes (graph )).To (Succeed ())
1202
+
1203
+ // trigger discovery the content of the source cluster
1204
+ g .Expect (graph .Discovery ("" )).To (Succeed ())
1205
+
1206
+ // gets a fakeProxy to an empty cluster with all the required CRDs
1207
+ toProxy := getFakeProxyWithCRDs ()
1208
+
1209
+ // Run move
1210
+ mover := objectMover {
1211
+ fromProxy : graph .proxy ,
1212
+ }
1213
+ err := mover .move (graph , toProxy )
1214
+
1215
+ if tt .wantErr {
1216
+ g .Expect (err ).To (HaveOccurred ())
1217
+ return
1218
+ }
1219
+
1220
+ g .Expect (err ).NotTo (HaveOccurred ())
1221
+
1222
+ // check that the objects are removed from the source cluster and are created in the target cluster
1223
+ csFrom , err := graph .proxy .NewClient ()
1224
+ g .Expect (err ).NotTo (HaveOccurred ())
1225
+
1226
+ csTo , err := toProxy .NewClient ()
1227
+ g .Expect (err ).NotTo (HaveOccurred ())
1228
+
1229
+ for _ , node := range graph .uidToNode {
1230
+ key := client.ObjectKey {
1231
+ Namespace : node .identity .Namespace ,
1232
+ Name : node .identity .Name ,
1233
+ }
1234
+
1235
+ // objects are deleted from the source cluster
1236
+ oFrom := & unstructured.Unstructured {}
1237
+ oFrom .SetAPIVersion (node .identity .APIVersion )
1238
+ oFrom .SetKind (node .identity .Kind )
1239
+
1240
+ err := csFrom .Get (ctx , key , oFrom )
1241
+ if err == nil {
1242
+ if ! node .isGlobal && ! node .isGlobalHierarchy {
1243
+ t .Errorf ("%v not deleted in source cluster" , key )
1244
+ continue
1245
+ }
1246
+ } else if ! apierrors .IsNotFound (err ) {
1247
+ t .Errorf ("error = %v when checking for %v deleted in source cluster" , err , key )
1248
+ continue
1249
+ }
1250
+
1251
+ // objects are created in the target cluster
1252
+ oTo := & unstructured.Unstructured {}
1253
+ oTo .SetAPIVersion (node .identity .APIVersion )
1254
+ oTo .SetKind (node .identity .Kind )
1255
+
1256
+ if err := csTo .Get (ctx , key , oTo ); err != nil {
1257
+ t .Errorf ("error = %v when checking for %v created in target cluster" , err , key )
1258
+ continue
1259
+ }
1260
+ }
1261
+ })
1262
+ }
1263
+ }
1264
+
1265
+ func Test_objectMover_move_with_Mutator (t * testing.T ) {
1166
1266
// NB. we are testing the move and move sequence using the same set of moveTests, but checking the results at different stages of the move process
1167
1267
// we use same mutator function for all tests and validate outcome based on input.
1168
- randSource := rand .NewSource (time .Now ().Unix ())
1169
1268
for _ , tt := range moveTests {
1170
1269
t .Run (tt .name , func (t * testing.T ) {
1171
1270
g := NewWithT (t )
@@ -1216,20 +1315,12 @@ func Test_objectMover_move(t *testing.T) {
1216
1315
// gets a fakeProxy to an empty cluster with all the required CRDs
1217
1316
toProxy := getFakeProxyWithCRDs ()
1218
1317
1219
- // Run move
1318
+ // Run move with mutators
1220
1319
mover := objectMover {
1221
1320
fromProxy : graph .proxy ,
1222
1321
}
1223
1322
1224
- // choose to include/exclude mutator randomly
1225
- includeMutator := randSource .Int63 ()% 2 == 0
1226
- var mutators []ResourceMutatorFunc
1227
- if includeMutator {
1228
- mutators = append (mutators , namespaceMutator )
1229
- }
1230
-
1231
- err := mover .move (graph , toProxy , mutators ... )
1232
-
1323
+ err := mover .move (graph , toProxy , namespaceMutator )
1233
1324
if tt .wantErr {
1234
1325
g .Expect (err ).To (HaveOccurred ())
1235
1326
return
@@ -1270,24 +1361,20 @@ func Test_objectMover_move(t *testing.T) {
1270
1361
oTo := & unstructured.Unstructured {}
1271
1362
oTo .SetAPIVersion (node .identity .APIVersion )
1272
1363
oTo .SetKind (node .identity .Kind )
1273
- if includeMutator {
1274
- if key .Namespace != "" {
1275
- key .Namespace = toNamespace
1276
- }
1364
+ if ! node .isGlobal {
1365
+ key .Namespace = toNamespace
1277
1366
}
1278
1367
1279
1368
if err := csTo .Get (ctx , key , oTo ); err != nil {
1280
1369
t .Errorf ("error = %v when checking for %v created in target cluster" , err , key )
1281
1370
continue
1282
1371
}
1283
- if includeMutator {
1284
- if fields , knownKind := updateKnownKinds [oTo .GetKind ()]; knownKind {
1285
- for _ , nsField := range fields {
1286
- value , exists , err := unstructured .NestedFieldNoCopy (oTo .Object , nsField ... )
1287
- g .Expect (err ).To (BeNil ())
1288
- if exists {
1289
- g .Expect (value ).To (Equal (toNamespace ))
1290
- }
1372
+ if fields , knownKind := updateKnownKinds [oTo .GetKind ()]; knownKind {
1373
+ for _ , nsField := range fields {
1374
+ value , exists , err := unstructured .NestedFieldNoCopy (oTo .Object , nsField ... )
1375
+ g .Expect (err ).To (BeNil ())
1376
+ if exists {
1377
+ g .Expect (value ).To (Equal (toNamespace ))
1291
1378
}
1292
1379
}
1293
1380
}
@@ -1998,7 +2085,7 @@ func Test_createTargetObject(t *testing.T) {
1998
2085
fromProxy : tt .args .fromProxy ,
1999
2086
}
2000
2087
2001
- err := mover .createTargetObject (tt .args .node , tt .args .toProxy , nil , nil )
2088
+ err := mover .createTargetObject (tt .args .node , tt .args .toProxy , nil , sets . New [ string ]() )
2002
2089
if tt .wantErr {
2003
2090
g .Expect (err ).To (HaveOccurred ())
2004
2091
return
0 commit comments