How to use SetNamespace method of config Package

Best Testkube code snippet using config.SetNamespace

reconciler_test.go

Source:reconciler_test.go Github

copy

Full Screen

...105 want: controllerruntime.Result{},106 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {107 cluster := &anywherev1.Cluster{}108 cluster.SetName(name)109 cluster.SetNamespace(namespace)110 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)111 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster.yaml")112 cluster.Spec = spec.Cluster.Spec113 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)114 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {115 clusterSpec := &anywherev1.VSphereDatacenterConfig{}116 if err := yaml.Unmarshal([]byte(vsphereDatacenterConfigSpecPath), clusterSpec); err != nil {117 t.Errorf("unmarshal failed: %v", err)118 }119 cluster := obj.(*anywherev1.VSphereDatacenterConfig)120 cluster.SetName(objectKey.Name)121 cluster.SetNamespace(objectKey.Namespace)122 cluster.Spec = clusterSpec.Spec123 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")124 }).Return(nil)125 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {126 clusterSpec := &anywherev1.VSphereMachineConfig{}127 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {128 t.Errorf("unmarshal failed: %v", err)129 }130 cluster := obj.(*anywherev1.VSphereMachineConfig)131 cluster.SetName(objectKey.Name)132 cluster.SetNamespace(objectKey.Namespace)133 cluster.Spec = clusterSpec.Spec134 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")135 }).Return(nil)136 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {137 clusterSpec := &anywherev1.VSphereMachineConfig{}138 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {139 t.Errorf("unmarshal failed: %v", err)140 }141 cluster := obj.(*anywherev1.VSphereMachineConfig)142 cluster.SetName(objectKey.Name)143 cluster.SetNamespace(objectKey.Namespace)144 cluster.Spec = clusterSpec.Spec145 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")146 }).Return(nil)147 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {148 clusterSpec := &anywherev1.VSphereMachineConfig{}149 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {150 t.Errorf("unmarshal failed: %v", err)151 }152 cluster := obj.(*anywherev1.VSphereMachineConfig)153 cluster.SetName(objectKey.Name)154 cluster.SetNamespace(objectKey.Namespace)155 cluster.Spec = clusterSpec.Spec156 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")157 }).Return(nil)158 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}159 if err := yaml.Unmarshal([]byte(vsphereKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {160 t.Errorf("unmarshal failed: %v", err)161 }162 etcdadmCluster := &etcdv1.EtcdadmCluster{}163 if err := yaml.Unmarshal([]byte(vsphereEtcdadmclusterFile), etcdadmCluster); err != nil {164 t.Errorf("unmarshal failed: %v", err)165 }166 machineDeployment := &clusterv1.MachineDeployment{}167 if err := yaml.Unmarshal([]byte(vsphereMachineDeploymentFile), machineDeployment); err != nil {168 t.Errorf("unmarshalling machinedeployment failed: %v", err)169 }170 workerNodeMachineConfig := &anywherev1.VSphereMachineConfig{171 Spec: anywherev1.VSphereMachineConfigSpec{172 Users: []anywherev1.UserConfiguration{173 {174 Name: "capv",175 SshAuthorizedKeys: []string{"ssh-rsa ssh_key_value"},176 },177 },178 },179 }180 fetcher.EXPECT().MachineDeployment(ctx, gomock.Any(), gomock.Any()).Return(machineDeployment, nil)181 fetcher.EXPECT().Etcd(ctx, gomock.Any()).Return(etcdadmCluster, nil)182 fetcher.EXPECT().ExistingVSphereDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.VSphereDatacenterConfig{}, nil)183 fetcher.EXPECT().ExistingVSphereControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)184 fetcher.EXPECT().ExistingVSphereEtcdMachineConfig(ctx, gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)185 fetcher.EXPECT().ExistingVSphereWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(workerNodeMachineConfig, nil)186 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.WorkerNodeGroupConfiguration{}, nil)187 fetcher.EXPECT().VSphereCredentials(ctx).Return(&corev1.Secret{188 Data: map[string][]byte{"username": []byte("username"), "password": []byte("password")},189 }, nil)190 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))191 resourceUpdater.EXPECT().ApplyPatch(ctx, gomock.Any(), false).Return(nil)192 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {193 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")194 switch template.GetKind() {195 case "VSphereMachineTemplate":196 if strings.Contains(template.GetName(), "worker-node") {197 expectedMachineTemplate := &unstructured.Unstructured{}198 if err := yaml.Unmarshal([]byte(vsphereMachineTemplateFile), expectedMachineTemplate); err != nil {199 t.Errorf("unmarshal failed: %v", err)200 }201 assert.Equal(t, expectedMachineTemplate, template, "values", expectedMachineTemplate, template)202 }203 case "MachineDeployment":204 expectedMCDeployment := &unstructured.Unstructured{}205 if err := yaml.Unmarshal([]byte(expectedVSphereMachineDeploymentFile), expectedMCDeployment); err != nil {206 t.Errorf("unmarshal failed: %v", err)207 }208 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)209 }210 }).AnyTimes().Return(nil)211 },212 },213 {214 name: "worker node reconcile (Vsphere provider) - worker nodes has NO machine-template changes",215 args: args{216 namespace: "namespaceA",217 name: "nameA",218 objectKey: types.NamespacedName{219 Name: "nameA",220 Namespace: "namespaceA",221 },222 },223 want: controllerruntime.Result{},224 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {225 cluster := &anywherev1.Cluster{}226 cluster.SetName(name)227 cluster.SetNamespace(namespace)228 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)229 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster_no_changes.yaml")230 cluster.Spec = spec.Cluster.Spec231 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)232 datacenterSpec := &anywherev1.VSphereDatacenterConfig{}233 if err := yaml.Unmarshal([]byte(vsphereDatacenterConfigSpecPath), datacenterSpec); err != nil {234 t.Errorf("unmarshal failed: %v", err)235 }236 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {237 cluster := obj.(*anywherev1.VSphereDatacenterConfig)238 cluster.SetName(objectKey.Name)239 cluster.SetNamespace(objectKey.Name)240 cluster.Spec = datacenterSpec.Spec241 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")242 }).Return(nil)243 existingVSDatacenter := &anywherev1.VSphereDatacenterConfig{}244 existingVSDatacenter.Spec = datacenterSpec.Spec245 fetcher.EXPECT().ExistingVSphereDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(existingVSDatacenter, nil)246 machineSpec := &anywherev1.VSphereMachineConfig{}247 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), machineSpec); err != nil {248 t.Errorf("unmarshal failed: %v", err)249 }250 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {251 cluster := obj.(*anywherev1.VSphereMachineConfig)252 cluster.SetName(objectKey.Name)253 cluster.SetNamespace(objectKey.Namespace)254 cluster.Spec = machineSpec.Spec255 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")256 }).Return(nil)257 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {258 cluster := obj.(*anywherev1.VSphereMachineConfig)259 cluster.SetName(objectKey.Name)260 cluster.SetNamespace(objectKey.Namespace)261 cluster.Spec = machineSpec.Spec262 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")263 }).Return(nil)264 existingWorkerNodeGroupConfiguration := &anywherev1.WorkerNodeGroupConfiguration{265 Name: "md-0",266 Count: 3,267 MachineGroupRef: nil,268 }269 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(existingWorkerNodeGroupConfiguration, nil)270 existingVSMachine := &anywherev1.VSphereMachineConfig{}271 existingVSMachine.Spec = machineSpec.Spec272 workerNodeMachineConfig := &anywherev1.VSphereMachineConfig{273 Spec: anywherev1.VSphereMachineConfigSpec{274 Users: []anywherev1.UserConfiguration{275 {276 Name: "capv",277 SshAuthorizedKeys: []string{"ssh-rsa ssh_key_value"},278 },279 },280 },281 }282 fetcher.EXPECT().ExistingVSphereControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)283 fetcher.EXPECT().ExistingVSphereWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(workerNodeMachineConfig, nil)284 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}285 if err := yaml.Unmarshal([]byte(vsphereKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {286 t.Errorf("unmarshal failed: %v", err)287 }288 machineDeployment := &clusterv1.MachineDeployment{}289 if err := yaml.Unmarshal([]byte(vsphereMachineDeploymentFile), machineDeployment); err != nil {290 t.Errorf("unmarshal failed: %v", err)291 }292 fetcher.EXPECT().MachineDeployment(ctx, gomock.Any(), gomock.Any()).Return(machineDeployment, nil)293 fetcher.EXPECT().VSphereCredentials(ctx).Return(&corev1.Secret{294 Data: map[string][]byte{"username": []byte("username"), "password": []byte("password")},295 }, nil)296 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))297 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {298 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")299 println(template.GetName(), " : ", template.GetKind())300 switch template.GetKind() {301 case "MachineDeployment":302 expectedMCDeployment := &unstructured.Unstructured{}303 if err := yaml.Unmarshal([]byte(expectedVSphereMachineDeploymentOnlyReplica), expectedMCDeployment); err != nil {304 t.Errorf("unmarshal failed: %v", err)305 }306 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)307 }308 }).AnyTimes().Return(nil)309 },310 },311 {312 name: "worker node reconcile (Vsphere provider) - worker node taints have changed",313 args: args{314 namespace: "namespaceA",315 name: "nameA",316 objectKey: types.NamespacedName{317 Name: "nameA",318 Namespace: "namespaceA",319 },320 },321 want: controllerruntime.Result{},322 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {323 cluster := &anywherev1.Cluster{}324 cluster.SetName(name)325 cluster.SetNamespace(namespace)326 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)327 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster.yaml")328 cluster.Spec = spec.Cluster.Spec329 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)330 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {331 clusterSpec := &anywherev1.VSphereDatacenterConfig{}332 if err := yaml.Unmarshal([]byte(vsphereDatacenterConfigSpecPath), clusterSpec); err != nil {333 t.Errorf("unmarshal failed: %v", err)334 }335 cluster := obj.(*anywherev1.VSphereDatacenterConfig)336 cluster.SetName(objectKey.Name)337 cluster.SetNamespace(objectKey.Namespace)338 cluster.Spec = clusterSpec.Spec339 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")340 }).Return(nil)341 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {342 clusterSpec := &anywherev1.VSphereMachineConfig{}343 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {344 t.Errorf("unmarshal failed: %v", err)345 }346 cluster := obj.(*anywherev1.VSphereMachineConfig)347 cluster.SetName(objectKey.Name)348 cluster.SetNamespace(objectKey.Namespace)349 cluster.Spec = clusterSpec.Spec350 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")351 }).Return(nil)352 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {353 clusterSpec := &anywherev1.VSphereMachineConfig{}354 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {355 t.Errorf("unmarshal failed: %v", err)356 }357 cluster := obj.(*anywherev1.VSphereMachineConfig)358 cluster.SetName(objectKey.Name)359 cluster.SetNamespace(objectKey.Namespace)360 cluster.Spec = clusterSpec.Spec361 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")362 }).Return(nil)363 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {364 clusterSpec := &anywherev1.VSphereMachineConfig{}365 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {366 t.Errorf("unmarshal failed: %v", err)367 }368 cluster := obj.(*anywherev1.VSphereMachineConfig)369 cluster.SetName(objectKey.Name)370 cluster.SetNamespace(objectKey.Namespace)371 cluster.Spec = clusterSpec.Spec372 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")373 }).Return(nil)374 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}375 if err := yaml.Unmarshal([]byte(vsphereKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {376 t.Errorf("unmarshal failed: %v", err)377 }378 etcdadmCluster := &etcdv1.EtcdadmCluster{}379 if err := yaml.Unmarshal([]byte(vsphereEtcdadmclusterFile), etcdadmCluster); err != nil {380 t.Errorf("unmarshal failed: %v", err)381 }382 existingWorkerNodeGroupConfiguration := &anywherev1.WorkerNodeGroupConfiguration{383 Name: "md-0",384 Count: 3,385 MachineGroupRef: nil,386 Taints: []corev1.Taint{387 {388 Key: "key1",389 Value: "val1",390 Effect: "PreferNoSchedule",391 },392 },393 }394 fetcher.EXPECT().Etcd(ctx, gomock.Any()).Return(etcdadmCluster, nil)395 fetcher.EXPECT().ExistingVSphereDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.VSphereDatacenterConfig{}, nil)396 fetcher.EXPECT().ExistingVSphereControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)397 fetcher.EXPECT().ExistingVSphereEtcdMachineConfig(ctx, gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)398 fetcher.EXPECT().ExistingVSphereWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)399 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(existingWorkerNodeGroupConfiguration, nil)400 fetcher.EXPECT().VSphereCredentials(ctx).Return(&corev1.Secret{401 Data: map[string][]byte{"username": []byte("username"), "password": []byte("password")},402 }, nil)403 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))404 resourceUpdater.EXPECT().ApplyPatch(ctx, gomock.Any(), false).Return(nil)405 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {406 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")407 switch template.GetKind() {408 case "KubeadmConfigTemplate":409 existingKubeadmConfigTemplate := &unstructured.Unstructured{}410 if err := yaml.Unmarshal([]byte(vsphereKubeadmconfigTemplateSpecPath), existingKubeadmConfigTemplate); err != nil {411 t.Errorf("unmarshal failed: %v", err)412 }413 assert.Equal(t, existingKubeadmConfigTemplate, template, "values", existingKubeadmConfigTemplate, template)414 case "MachineDeployment":415 expectedMCDeployment := &unstructured.Unstructured{}416 if err := yaml.Unmarshal([]byte(expectedVSphereMachineDeploymentTemplateChanged), expectedMCDeployment); err != nil {417 t.Errorf("unmarshal failed: %v", err)418 }419 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)420 }421 }).AnyTimes().Return(nil)422 },423 },424 {425 name: "worker node reconcile (Vsphere provider) - worker node labels have changed",426 args: args{427 namespace: "namespaceA",428 name: "nameA",429 objectKey: types.NamespacedName{430 Name: "nameA",431 Namespace: "namespaceA",432 },433 },434 want: controllerruntime.Result{},435 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {436 cluster := &anywherev1.Cluster{}437 cluster.SetName(name)438 cluster.SetNamespace(namespace)439 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)440 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster.yaml")441 cluster.Spec = spec.Cluster.Spec442 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)443 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {444 clusterSpec := &anywherev1.VSphereDatacenterConfig{}445 if err := yaml.Unmarshal([]byte(vsphereDatacenterConfigSpecPath), clusterSpec); err != nil {446 t.Errorf("unmarshal failed: %v", err)447 }448 cluster := obj.(*anywherev1.VSphereDatacenterConfig)449 cluster.SetName(objectKey.Name)450 cluster.SetNamespace(objectKey.Namespace)451 cluster.Spec = clusterSpec.Spec452 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")453 }).Return(nil)454 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {455 clusterSpec := &anywherev1.VSphereMachineConfig{}456 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {457 t.Errorf("unmarshal failed: %v", err)458 }459 cluster := obj.(*anywherev1.VSphereMachineConfig)460 cluster.SetName(objectKey.Name)461 cluster.SetNamespace(objectKey.Namespace)462 cluster.Spec = clusterSpec.Spec463 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")464 }).Return(nil)465 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {466 clusterSpec := &anywherev1.VSphereMachineConfig{}467 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {468 t.Errorf("unmarshal failed: %v", err)469 }470 cluster := obj.(*anywherev1.VSphereMachineConfig)471 cluster.SetName(objectKey.Name)472 cluster.SetNamespace(objectKey.Namespace)473 cluster.Spec = clusterSpec.Spec474 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")475 }).Return(nil)476 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {477 clusterSpec := &anywherev1.VSphereMachineConfig{}478 if err := yaml.Unmarshal([]byte(vsphereMachineConfigSpecPath), clusterSpec); err != nil {479 t.Errorf("unmarshal failed: %v", err)480 }481 cluster := obj.(*anywherev1.VSphereMachineConfig)482 cluster.SetName(objectKey.Name)483 cluster.SetNamespace(objectKey.Namespace)484 cluster.Spec = clusterSpec.Spec485 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")486 }).Return(nil)487 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}488 if err := yaml.Unmarshal([]byte(vsphereKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {489 t.Errorf("unmarshal failed: %v", err)490 }491 etcdadmCluster := &etcdv1.EtcdadmCluster{}492 if err := yaml.Unmarshal([]byte(vsphereEtcdadmclusterFile), etcdadmCluster); err != nil {493 t.Errorf("unmarshal failed: %v", err)494 }495 existingWorkerNodeGroupConfiguration := &anywherev1.WorkerNodeGroupConfiguration{496 Name: "md-0",497 Count: 3,498 MachineGroupRef: nil,499 Labels: map[string]string{500 "Key1": "Val1",501 "Key2": "Val2",502 },503 }504 fetcher.EXPECT().Etcd(ctx, gomock.Any()).Return(etcdadmCluster, nil)505 fetcher.EXPECT().ExistingVSphereDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.VSphereDatacenterConfig{}, nil)506 fetcher.EXPECT().ExistingVSphereControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)507 fetcher.EXPECT().ExistingVSphereEtcdMachineConfig(ctx, gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)508 fetcher.EXPECT().ExistingVSphereWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.VSphereMachineConfig{}, nil)509 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(existingWorkerNodeGroupConfiguration, nil)510 fetcher.EXPECT().VSphereCredentials(ctx).Return(&corev1.Secret{511 Data: map[string][]byte{"username": []byte("username"), "password": []byte("password")},512 }, nil)513 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))514 resourceUpdater.EXPECT().ApplyPatch(ctx, gomock.Any(), false).Return(nil)515 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {516 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")517 switch template.GetKind() {518 case "KubeadmConfigTemplate":519 existingKubeadmConfigTemplate := &unstructured.Unstructured{}520 if err := yaml.Unmarshal([]byte(vsphereKubeadmconfigTemplateSpecPath), existingKubeadmConfigTemplate); err != nil {521 t.Errorf("unmarshal failed: %v", err)522 }523 assert.Equal(t, existingKubeadmConfigTemplate, template, "values", existingKubeadmConfigTemplate, template)524 case "MachineDeployment":525 expectedMCDeployment := &unstructured.Unstructured{}526 if err := yaml.Unmarshal([]byte(expectedVSphereMachineDeploymentTemplateChanged), expectedMCDeployment); err != nil {527 t.Errorf("unmarshal failed: %v", err)528 }529 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)530 }531 }).AnyTimes().Return(nil)532 },533 },534 }535 for _, tt := range tests {536 t.Run(tt.name, func(t *testing.T) {537 ctx := context.Background()538 mockCtrl := gomock.NewController(t)539 fetcher := mocks.NewMockResourceFetcher(mockCtrl)540 resourceUpdater := mocks.NewMockResourceUpdater(mockCtrl)541 tt.prepare(ctx, fetcher, resourceUpdater, tt.args.name, tt.args.namespace)542 cor := resource.NewClusterReconciler(fetcher, resourceUpdater, test.FakeNow, logr.Discard())543 if err := cor.Reconcile(ctx, tt.args.objectKey, false); (err != nil) != tt.wantErr {544 t.Errorf("Reconcile() error = %v, wantErr %v", err, tt.wantErr)545 }546 })547 }548}549func TestClusterReconcilerReconcileCloudStack(t *testing.T) {550 type args struct {551 objectKey types.NamespacedName552 name string553 namespace string554 }555 tests := []struct {556 name string557 args args558 want controllerruntime.Result559 wantErr bool560 prepare func(context.Context, *mocks.MockResourceFetcher, *mocks.MockResourceUpdater, string, string)561 }{562 {563 name: "worker node reconcile (Cloudstack provider) - worker nodes has changes",564 args: args{565 namespace: "namespaceA",566 name: "nameA",567 objectKey: types.NamespacedName{568 Name: "nameA",569 Namespace: "namespaceA",570 },571 },572 want: controllerruntime.Result{},573 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {574 cluster := &anywherev1.Cluster{}575 cluster.SetName(name)576 cluster.SetNamespace(namespace)577 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)578 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster-cloudstack.yaml")579 cluster.Spec = spec.Cluster.Spec580 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)581 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {582 clusterSpec := &anywherev1.CloudStackDatacenterConfig{}583 if err := yaml.Unmarshal([]byte(cloudstackDatacenterConfigSpecPath), clusterSpec); err != nil {584 t.Errorf("unmarshal failed: %v", err)585 }586 cluster := obj.(*anywherev1.CloudStackDatacenterConfig)587 cluster.SetName(objectKey.Name)588 cluster.SetNamespace(objectKey.Namespace)589 cluster.Spec = clusterSpec.Spec590 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")591 }).Return(nil)592 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {593 clusterSpec := &anywherev1.CloudStackMachineConfig{}594 if err := yaml.Unmarshal([]byte(cloudstackMachineConfigSpecPath), clusterSpec); err != nil {595 t.Errorf("unmarshal failed: %v", err)596 }597 cluster := obj.(*anywherev1.CloudStackMachineConfig)598 cluster.SetName(objectKey.Name)599 cluster.SetNamespace(objectKey.Namespace)600 cluster.Spec = clusterSpec.Spec601 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")602 }).Return(nil)603 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {604 clusterSpec := &anywherev1.CloudStackMachineConfig{}605 if err := yaml.Unmarshal([]byte(cloudstackMachineConfigSpecPath), clusterSpec); err != nil {606 t.Errorf("unmarshal failed: %v", err)607 }608 cluster := obj.(*anywherev1.CloudStackMachineConfig)609 cluster.SetName(objectKey.Name)610 cluster.SetNamespace(objectKey.Namespace)611 cluster.Spec = clusterSpec.Spec612 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")613 }).Return(nil)614 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}615 if err := yaml.Unmarshal([]byte(cloudstackKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {616 t.Errorf("unmarshal failed: %v", err)617 }618 machineDeployment := &clusterv1.MachineDeployment{}619 if err := yaml.Unmarshal([]byte(cloudstackMachineDeploymentFile), machineDeployment); err != nil {620 t.Errorf("unmarshalling machinedeployment failed: %v", err)621 }622 fetcher.EXPECT().MachineDeployment(ctx, gomock.Any(), gomock.Any()).Return(machineDeployment, nil)623 fetcher.EXPECT().ExistingCloudStackDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackDatacenterConfig{}, nil)624 fetcher.EXPECT().ExistingCloudStackControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)625 fetcher.EXPECT().ExistingCloudStackWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)626 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.WorkerNodeGroupConfiguration{}, nil)627 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))628 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {629 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")630 switch template.GetKind() {631 case "CloudStackMachineTemplate":632 if strings.Contains(template.GetName(), "worker-node") {633 expectedMachineTemplate := &unstructured.Unstructured{}634 if err := yaml.Unmarshal([]byte(cloudstackMachineTemplateFile), expectedMachineTemplate); err != nil {635 t.Errorf("unmarshal failed: %v", err)636 }637 assert.Equal(t, expectedMachineTemplate, template, "values", expectedMachineTemplate, template)638 }639 case "MachineDeployment":640 expectedMCDeployment := &unstructured.Unstructured{}641 if err := yaml.Unmarshal([]byte(expectedCloudStackMachineDeploymentFile), expectedMCDeployment); err != nil {642 t.Errorf("unmarshal failed: %v", err)643 }644 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)645 }646 }).AnyTimes().Return(nil)647 },648 },649 {650 name: "worker node reconcile (Cloudstack provider) - worker nodes has NO machine-template changes",651 args: args{652 namespace: "namespaceA",653 name: "nameA",654 objectKey: types.NamespacedName{655 Name: "nameA",656 Namespace: "namespaceA",657 },658 },659 want: controllerruntime.Result{},660 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {661 cluster := &anywherev1.Cluster{}662 cluster.SetName(name)663 cluster.SetNamespace(namespace)664 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)665 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster-cloudstack_no_changes.yaml")666 cluster.Spec = spec.Cluster.Spec667 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)668 datacenterSpec := &anywherev1.CloudStackDatacenterConfig{}669 if err := yaml.Unmarshal([]byte(cloudstackDatacenterConfigSpecPath), datacenterSpec); err != nil {670 t.Errorf("unmarshal failed: %v", err)671 }672 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {673 cluster := obj.(*anywherev1.CloudStackDatacenterConfig)674 cluster.SetName(objectKey.Name)675 cluster.SetNamespace(objectKey.Name)676 cluster.Spec = datacenterSpec.Spec677 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")678 }).Return(nil)679 machineSpec := &anywherev1.CloudStackMachineConfig{}680 if err := yaml.Unmarshal([]byte(cloudstackMachineConfigSpecPath), machineSpec); err != nil {681 t.Errorf("unmarshal failed: %v", err)682 }683 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {684 cluster := obj.(*anywherev1.CloudStackMachineConfig)685 cluster.SetName(objectKey.Name)686 cluster.SetNamespace(objectKey.Namespace)687 cluster.Spec = machineSpec.Spec688 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")689 }).Return(nil)690 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {691 cluster := obj.(*anywherev1.CloudStackMachineConfig)692 cluster.SetName(objectKey.Name)693 cluster.SetNamespace(objectKey.Namespace)694 cluster.Spec = machineSpec.Spec695 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")696 }).Return(nil)697 existingWorkerNodeGroupConfiguration := &anywherev1.WorkerNodeGroupConfiguration{698 Name: "md-0",699 Count: 3,700 MachineGroupRef: nil,701 }702 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(existingWorkerNodeGroupConfiguration, nil)703 existingCSMachine := &anywherev1.CloudStackMachineConfig{}704 existingCSMachine.Spec = machineSpec.Spec705 fetcher.EXPECT().ExistingCloudStackDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackDatacenterConfig{}, nil)706 fetcher.EXPECT().ExistingCloudStackControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)707 fetcher.EXPECT().ExistingCloudStackWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)708 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}709 if err := yaml.Unmarshal([]byte(cloudstackKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {710 t.Errorf("unmarshal failed: %v", err)711 }712 machineDeployment := &clusterv1.MachineDeployment{}713 if err := yaml.Unmarshal([]byte(cloudstackMachineDeploymentFile), machineDeployment); err != nil {714 t.Errorf("unmarshal failed: %v", err)715 }716 fetcher.EXPECT().MachineDeployment(ctx, gomock.Any(), gomock.Any()).Return(machineDeployment, nil)717 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))718 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {719 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")720 println(template.GetName(), " : ", template.GetKind())721 switch template.GetKind() {722 case "MachineDeployment":723 expectedMCDeployment := &unstructured.Unstructured{}724 if err := yaml.Unmarshal([]byte(expectedCloudStackMachineDeploymentOnlyReplica), expectedMCDeployment); err != nil {725 t.Errorf("unmarshal failed: %v", err)726 }727 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)728 }729 }).AnyTimes().Return(nil)730 },731 },732 {733 name: "worker node reconcile (Cloudstack provider) - worker node taints have changed",734 args: args{735 namespace: "namespaceA",736 name: "nameA",737 objectKey: types.NamespacedName{738 Name: "nameA",739 Namespace: "namespaceA",740 },741 },742 want: controllerruntime.Result{},743 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {744 cluster := &anywherev1.Cluster{}745 cluster.SetName(name)746 cluster.SetNamespace(namespace)747 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)748 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster-cloudstack.yaml")749 cluster.Spec = spec.Cluster.Spec750 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)751 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {752 clusterSpec := &anywherev1.CloudStackDatacenterConfig{}753 if err := yaml.Unmarshal([]byte(cloudstackDatacenterConfigSpecPath), clusterSpec); err != nil {754 t.Errorf("unmarshal failed: %v", err)755 }756 cluster := obj.(*anywherev1.CloudStackDatacenterConfig)757 cluster.SetName(objectKey.Name)758 cluster.SetNamespace(objectKey.Namespace)759 cluster.Spec = clusterSpec.Spec760 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")761 }).Return(nil)762 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {763 clusterSpec := &anywherev1.CloudStackMachineConfig{}764 if err := yaml.Unmarshal([]byte(cloudstackMachineConfigSpecPath), clusterSpec); err != nil {765 t.Errorf("unmarshal failed: %v", err)766 }767 cluster := obj.(*anywherev1.CloudStackMachineConfig)768 cluster.SetName(objectKey.Name)769 cluster.SetNamespace(objectKey.Namespace)770 cluster.Spec = clusterSpec.Spec771 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")772 }).Return(nil)773 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {774 clusterSpec := &anywherev1.CloudStackMachineConfig{}775 if err := yaml.Unmarshal([]byte(cloudstackMachineConfigSpecPath), clusterSpec); err != nil {776 t.Errorf("unmarshal failed: %v", err)777 }778 cluster := obj.(*anywherev1.CloudStackMachineConfig)779 cluster.SetName(objectKey.Name)780 cluster.SetNamespace(objectKey.Namespace)781 cluster.Spec = clusterSpec.Spec782 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")783 }).Return(nil)784 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}785 if err := yaml.Unmarshal([]byte(cloudstackKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {786 t.Errorf("unmarshal failed: %v", err)787 }788 existingWorkerNodeGroupConfiguration := &anywherev1.WorkerNodeGroupConfiguration{789 Name: "md-0",790 Count: 3,791 MachineGroupRef: nil,792 Taints: []corev1.Taint{793 {794 Key: "key1",795 Value: "val1",796 Effect: "PreferNoSchedule",797 },798 },799 }800 oldCloudstackProviderFeatureValue := os.Getenv(features.CloudStackProviderEnvVar)801 os.Unsetenv(features.CloudStackProviderEnvVar)802 defer os.Setenv(features.CloudStackProviderEnvVar, oldCloudstackProviderFeatureValue)803 fetcher.EXPECT().ExistingCloudStackDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackDatacenterConfig{}, nil)804 fetcher.EXPECT().ExistingCloudStackControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)805 fetcher.EXPECT().ExistingCloudStackWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)806 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(existingWorkerNodeGroupConfiguration, nil)807 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))808 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {809 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")810 switch template.GetKind() {811 case "KubeadmConfigTemplate":812 existingKubeadmConfigTemplate := &unstructured.Unstructured{}813 if err := yaml.Unmarshal([]byte(cloudstackKubeadmconfigTemplateSpecPath), existingKubeadmConfigTemplate); err != nil {814 t.Errorf("unmarshal failed: %v", err)815 }816 assert.Equal(t, existingKubeadmConfigTemplate, template, "values", existingKubeadmConfigTemplate, template)817 case "MachineDeployment":818 expectedMCDeployment := &unstructured.Unstructured{}819 if err := yaml.Unmarshal([]byte(expectedCloudStackMachineDeploymentTemplateChanged), expectedMCDeployment); err != nil {820 t.Errorf("unmarshal failed: %v", err)821 }822 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)823 }824 }).AnyTimes().Return(nil)825 },826 },827 {828 name: "worker node reconcile (Cloudstack provider) - worker node labels have changed",829 args: args{830 namespace: "namespaceA",831 name: "nameA",832 objectKey: types.NamespacedName{833 Name: "nameA",834 Namespace: "namespaceA",835 },836 },837 want: controllerruntime.Result{},838 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {839 cluster := &anywherev1.Cluster{}840 cluster.SetName(name)841 cluster.SetNamespace(namespace)842 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)843 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster-cloudstack.yaml")844 cluster.Spec = spec.Cluster.Spec845 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)846 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {847 clusterSpec := &anywherev1.CloudStackDatacenterConfig{}848 if err := yaml.Unmarshal([]byte(cloudstackDatacenterConfigSpecPath), clusterSpec); err != nil {849 t.Errorf("unmarshal failed: %v", err)850 }851 cluster := obj.(*anywherev1.CloudStackDatacenterConfig)852 cluster.SetName(objectKey.Name)853 cluster.SetNamespace(objectKey.Namespace)854 cluster.Spec = clusterSpec.Spec855 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")856 }).Return(nil)857 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {858 clusterSpec := &anywherev1.CloudStackMachineConfig{}859 if err := yaml.Unmarshal([]byte(cloudstackMachineConfigSpecPath), clusterSpec); err != nil {860 t.Errorf("unmarshal failed: %v", err)861 }862 cluster := obj.(*anywherev1.CloudStackMachineConfig)863 cluster.SetName(objectKey.Name)864 cluster.SetNamespace(objectKey.Namespace)865 cluster.Spec = clusterSpec.Spec866 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")867 }).Return(nil)868 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {869 clusterSpec := &anywherev1.CloudStackMachineConfig{}870 if err := yaml.Unmarshal([]byte(cloudstackMachineConfigSpecPath), clusterSpec); err != nil {871 t.Errorf("unmarshal failed: %v", err)872 }873 cluster := obj.(*anywherev1.CloudStackMachineConfig)874 cluster.SetName(objectKey.Name)875 cluster.SetNamespace(objectKey.Namespace)876 cluster.Spec = clusterSpec.Spec877 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")878 }).Return(nil)879 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}880 if err := yaml.Unmarshal([]byte(cloudstackKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {881 t.Errorf("unmarshal failed: %v", err)882 }883 existingWorkerNodeGroupConfiguration := &anywherev1.WorkerNodeGroupConfiguration{884 Name: "md-0",885 Count: 3,886 MachineGroupRef: nil,887 Labels: map[string]string{888 "Key1": "Val1",889 "Key2": "Val2",890 },891 }892 fetcher.EXPECT().ExistingCloudStackDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackDatacenterConfig{}, nil)893 fetcher.EXPECT().ExistingCloudStackControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)894 fetcher.EXPECT().ExistingCloudStackWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)895 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(existingWorkerNodeGroupConfiguration, nil)896 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))897 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {898 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")899 switch template.GetKind() {900 case "KubeadmConfigTemplate":901 existingKubeadmConfigTemplate := &unstructured.Unstructured{}902 if err := yaml.Unmarshal([]byte(cloudstackKubeadmconfigTemplateSpecPath), existingKubeadmConfigTemplate); err != nil {903 t.Errorf("unmarshal failed: %v", err)904 }905 assert.Equal(t, existingKubeadmConfigTemplate, template, "values", existingKubeadmConfigTemplate, template)906 case "MachineDeployment":907 expectedMCDeployment := &unstructured.Unstructured{}908 if err := yaml.Unmarshal([]byte(expectedCloudStackMachineDeploymentTemplateChanged), expectedMCDeployment); err != nil {909 t.Errorf("unmarshal failed: %v", err)910 }911 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)912 }913 }).AnyTimes().Return(nil)914 },915 },916 {917 name: "cp & etcd reconcile (Cloudstack provider) - cp & etcd templates have changed, but don't reconcile",918 args: args{919 namespace: "namespaceA",920 name: "test-cluster",921 objectKey: types.NamespacedName{922 Name: "test-cluster",923 Namespace: "namespaceA",924 },925 },926 want: controllerruntime.Result{},927 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, resourceUpdater *mocks.MockResourceUpdater, name string, namespace string) {928 cluster := &anywherev1.Cluster{}929 cluster.SetName(name)930 cluster.SetNamespace(namespace)931 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)932 spec := test.NewFullClusterSpec(t, "testdata/eksa-cluster-cloudstack-etcd.yaml")933 cluster.Spec = spec.Cluster.Spec934 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)935 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {936 clusterSpec := &anywherev1.CloudStackDatacenterConfig{}937 if err := yaml.Unmarshal([]byte(cloudstackDatacenterConfigSpecPath), clusterSpec); err != nil {938 t.Errorf("unmarshal failed: %v", err)939 }940 cluster := obj.(*anywherev1.CloudStackDatacenterConfig)941 cluster.SetName(objectKey.Name)942 cluster.SetNamespace(objectKey.Namespace)943 cluster.Spec = clusterSpec.Spec944 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")945 }).Return(nil)946 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {947 clusterSpec := &anywherev1.CloudStackMachineConfig{}948 if err := yaml.Unmarshal([]byte(cloudstackMachineConfigSpecPath), clusterSpec); err != nil {949 t.Errorf("unmarshal failed: %v", err)950 }951 cluster := obj.(*anywherev1.CloudStackMachineConfig)952 cluster.SetName(objectKey.Name)953 cluster.SetNamespace(objectKey.Namespace)954 cluster.Spec = clusterSpec.Spec955 assert.Equal(t, objectKey.Name, "test-cluster", "expected Name to be test-cluster")956 }).Return(nil).Times(3)957 kubeAdmControlPlane := &controlplanev1.KubeadmControlPlane{}958 if err := yaml.Unmarshal([]byte(cloudstackKubeadmcontrolplaneFile), kubeAdmControlPlane); err != nil {959 t.Errorf("unmarshal failed: %v", err)960 }961 etcdadmCluster := &etcdv1.EtcdadmCluster{}962 if err := yaml.Unmarshal([]byte(cloudstackEtcdadmclusterFile), etcdadmCluster); err != nil {963 t.Errorf("unmarshal failed: %v", err)964 }965 existingWorkerNodeGroupConfiguration := &anywherev1.WorkerNodeGroupConfiguration{966 Name: "md-0",967 Count: 3,968 MachineGroupRef: nil,969 }970 existingCSDatacenterConfig := &anywherev1.CloudStackDatacenterConfig{971 Spec: anywherev1.CloudStackDatacenterConfigSpec{972 Domain: "root",973 Account: "admin",974 Zones: []anywherev1.CloudStackZone{975 {976 Name: "zone1",977 Network: anywherev1.CloudStackResourceIdentifier{978 Name: "net1",979 },980 },981 {982 Name: "zone2",983 Network: anywherev1.CloudStackResourceIdentifier{984 Name: "net2",985 },986 },987 },988 },989 }990 oldCloudstackProviderFeatureValue := os.Getenv(features.CloudStackProviderEnvVar)991 os.Unsetenv(features.CloudStackProviderEnvVar)992 defer os.Setenv(features.CloudStackProviderEnvVar, oldCloudstackProviderFeatureValue)993 fetcher.EXPECT().Etcd(ctx, gomock.Any()).Return(etcdadmCluster, nil)994 fetcher.EXPECT().ExistingCloudStackDatacenterConfig(ctx, gomock.Any(), gomock.Any()).Return(existingCSDatacenterConfig, nil)995 fetcher.EXPECT().ExistingCloudStackControlPlaneMachineConfig(ctx, gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)996 fetcher.EXPECT().ExistingCloudStackEtcdMachineConfig(ctx, gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)997 fetcher.EXPECT().ExistingCloudStackWorkerMachineConfig(ctx, gomock.Any(), gomock.Any()).Return(&anywherev1.CloudStackMachineConfig{}, nil)998 fetcher.EXPECT().ExistingWorkerNodeGroupConfig(ctx, gomock.Any(), gomock.Any()).Return(existingWorkerNodeGroupConfiguration, nil)999 machineDeployment := &clusterv1.MachineDeployment{}1000 if err := yaml.Unmarshal([]byte(cloudstackMachineDeploymentFile), machineDeployment); err != nil {1001 t.Errorf("unmarshal failed: %v", err)1002 }1003 fetcher.EXPECT().MachineDeployment(ctx, gomock.Any(), gomock.Any()).Return(machineDeployment, nil)1004 fetcher.EXPECT().Fetch(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, errors.NewNotFound(schema.GroupResource{Group: "testgroup", Resource: "testresource"}, ""))1005 resourceUpdater.EXPECT().ApplyPatch(ctx, gomock.Any(), false).Return(nil)1006 resourceUpdater.EXPECT().ForceApplyTemplate(ctx, gomock.Any(), gomock.Any()).Do(func(ctx context.Context, template *unstructured.Unstructured, dryRun bool) {1007 assert.Equal(t, false, dryRun, "Expected dryRun didn't match")1008 switch template.GetKind() {1009 case "CloudStackMachineTemplate":1010 if strings.Contains(template.GetName(), "worker-node") {1011 expectedMachineTemplate := &unstructured.Unstructured{}1012 if err := yaml.Unmarshal([]byte(cloudstackMachineTemplateFile), expectedMachineTemplate); err != nil {1013 t.Errorf("unmarshal failed: %v", err)1014 }1015 assert.Equal(t, expectedMachineTemplate, template, "values", expectedMachineTemplate, template)1016 }1017 if strings.Contains(template.GetName(), "etcd-template") {1018 expectedMachineTemplate := &unstructured.Unstructured{}1019 if err := yaml.Unmarshal([]byte(cloudstackEtcdMachineTemplateFile), expectedMachineTemplate); err != nil {1020 t.Errorf("unmarshal failed: %v", err)1021 }1022 assert.Equal(t, expectedMachineTemplate, template, "values", expectedMachineTemplate, template)1023 }1024 if strings.Contains(template.GetName(), "control-plane-template") {1025 expectedMachineTemplate := &unstructured.Unstructured{}1026 if err := yaml.Unmarshal([]byte(cloudstackCPMachineTemplateFile), expectedMachineTemplate); err != nil {1027 t.Errorf("unmarshal failed: %v", err)1028 }1029 assert.Equal(t, expectedMachineTemplate, template, "values", expectedMachineTemplate, template)1030 }1031 case "MachineDeployment":1032 expectedMCDeployment := &unstructured.Unstructured{}1033 if err := yaml.Unmarshal([]byte(expectedCloudStackMachineDeploymentFile), expectedMCDeployment); err != nil {1034 t.Errorf("unmarshal failed: %v", err)1035 }1036 assert.Equal(t, expectedMCDeployment, template, "values", expectedMCDeployment, template)1037 }1038 }).AnyTimes().Return(nil)1039 },1040 },1041 }1042 oldCloudstackProviderFeatureValue := os.Getenv(features.CloudStackProviderEnvVar)1043 os.Setenv(features.CloudStackProviderEnvVar, "true")1044 defer os.Setenv(features.CloudStackProviderEnvVar, oldCloudstackProviderFeatureValue)1045 for _, tt := range tests {1046 t.Run(tt.name, func(t *testing.T) {1047 ctx := context.Background()1048 mockCtrl := gomock.NewController(t)1049 fetcher := mocks.NewMockResourceFetcher(mockCtrl)1050 resourceUpdater := mocks.NewMockResourceUpdater(mockCtrl)1051 tt.prepare(ctx, fetcher, resourceUpdater, tt.args.name, tt.args.namespace)1052 cor := resource.NewClusterReconciler(fetcher, resourceUpdater, test.FakeNow, logr.Discard())1053 if err := cor.Reconcile(ctx, tt.args.objectKey, false); (err != nil) != tt.wantErr {1054 t.Errorf("Reconcile() error = %v, wantErr %v", err, tt.wantErr)1055 }1056 })1057 }1058}1059func TestClusterReconcilerReconcileNutanix(t *testing.T) {1060 err := os.Setenv(features.NutanixProviderEnvVar, "true")1061 require.NoError(t, err)1062 assert.True(t, features.NutanixProvider().IsActive())1063 type args struct {1064 objectKey types.NamespacedName1065 name string1066 namespace string1067 }1068 tests := []struct {1069 name string1070 args args1071 want controllerruntime.Result1072 wantErr bool1073 prepare func(context.Context, *mocks.MockResourceFetcher, string, string)1074 }{1075 {1076 name: "node reconcile - no error",1077 args: args{1078 namespace: "namespaceA",1079 name: "nameA",1080 objectKey: types.NamespacedName{1081 Name: "nameA",1082 Namespace: "namespaceA",1083 },1084 },1085 want: controllerruntime.Result{},1086 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, name string, namespace string) {1087 cluster := &anywherev1.Cluster{}1088 cluster.SetName(name)1089 cluster.SetNamespace(namespace)1090 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)1091 spec := test.NewFullClusterSpec(t, "testdata/nutanix/eksa-cluster.yaml")1092 cluster.Spec = spec.Cluster.Spec1093 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)1094 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1095 clusterSpec := &anywherev1.NutanixDatacenterConfig{}1096 if err := yaml.Unmarshal([]byte(nutanixDatacenterConfigSpec), clusterSpec); err != nil {1097 t.Errorf("unmarshal failed: %v", err)1098 }1099 cluster := obj.(*anywherev1.NutanixDatacenterConfig)1100 cluster.SetName(objectKey.Name)1101 cluster.SetNamespace(objectKey.Namespace)1102 cluster.Spec = clusterSpec.Spec1103 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1104 }).Return(nil)1105 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1106 clusterSpec := &anywherev1.NutanixMachineConfig{}1107 if err := yaml.Unmarshal([]byte(nutanixCPMachineConfigSpec), clusterSpec); err != nil {1108 t.Errorf("unmarshal failed: %v", err)1109 }1110 cluster := obj.(*anywherev1.NutanixMachineConfig)1111 cluster.SetName(objectKey.Name)1112 cluster.SetNamespace(objectKey.Namespace)1113 cluster.Spec = clusterSpec.Spec1114 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1115 }).Return(nil)1116 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1117 clusterSpec := &anywherev1.NutanixMachineConfig{}1118 if err := yaml.Unmarshal([]byte(nutanixWorkerMachineConfigSpec), clusterSpec); err != nil {1119 t.Errorf("unmarshal failed: %v", err)1120 }1121 cluster := obj.(*anywherev1.NutanixMachineConfig)1122 cluster.SetName(objectKey.Name)1123 cluster.SetNamespace(objectKey.Namespace)1124 cluster.Spec = clusterSpec.Spec1125 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1126 }).Return(nil)1127 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1128 clusterSpec := &anywherev1.NutanixMachineConfig{}1129 if err := yaml.Unmarshal([]byte(nutanixEtcdMachineConfigSpec), clusterSpec); err != nil {1130 t.Errorf("unmarshal failed: %v", err)1131 }1132 cluster := obj.(*anywherev1.NutanixMachineConfig)1133 cluster.SetName(objectKey.Name)1134 cluster.SetNamespace(objectKey.Namespace)1135 cluster.Spec = clusterSpec.Spec1136 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1137 }).Return(nil)1138 },1139 },1140 {1141 name: "node reconcile - failure to fetch cluster",1142 args: args{1143 namespace: "namespaceA",1144 name: "nameA",1145 objectKey: types.NamespacedName{1146 Name: "nameA",1147 Namespace: "namespaceA",1148 },1149 },1150 wantErr: true,1151 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, name string, namespace string) {1152 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error"))1153 },1154 },1155 {1156 name: "node reconcile - failure to fetch applied spec",1157 args: args{1158 namespace: "namespaceA",1159 name: "nameA",1160 objectKey: types.NamespacedName{1161 Name: "nameA",1162 Namespace: "namespaceA",1163 },1164 },1165 wantErr: true,1166 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, name string, namespace string) {1167 cluster := &anywherev1.Cluster{}1168 cluster.SetName(name)1169 cluster.SetNamespace(namespace)1170 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)1171 spec := test.NewFullClusterSpec(t, "testdata/nutanix/eksa-cluster.yaml")1172 cluster.Spec = spec.Cluster.Spec1173 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(nil, fmt.Errorf("error"))1174 },1175 },1176 {1177 name: "node reconcile - failure to fetch datacenter config",1178 args: args{1179 namespace: "namespaceA",1180 name: "nameA",1181 objectKey: types.NamespacedName{1182 Name: "nameA",1183 Namespace: "namespaceA",1184 },1185 },1186 wantErr: true,1187 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, name string, namespace string) {1188 cluster := &anywherev1.Cluster{}1189 cluster.SetName(name)1190 cluster.SetNamespace(namespace)1191 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)1192 spec := test.NewFullClusterSpec(t, "testdata/nutanix/eksa-cluster.yaml")1193 cluster.Spec = spec.Cluster.Spec1194 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)1195 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {}).Return(fmt.Errorf("error"))1196 },1197 },1198 {1199 name: "node reconcile - failure to fetch control plane node machine config",1200 args: args{1201 namespace: "namespaceA",1202 name: "nameA",1203 objectKey: types.NamespacedName{1204 Name: "nameA",1205 Namespace: "namespaceA",1206 },1207 },1208 wantErr: true,1209 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, name string, namespace string) {1210 cluster := &anywherev1.Cluster{}1211 cluster.SetName(name)1212 cluster.SetNamespace(namespace)1213 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)1214 spec := test.NewFullClusterSpec(t, "testdata/nutanix/eksa-cluster.yaml")1215 cluster.Spec = spec.Cluster.Spec1216 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)1217 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1218 clusterSpec := &anywherev1.NutanixDatacenterConfig{}1219 if err := yaml.Unmarshal([]byte(nutanixDatacenterConfigSpec), clusterSpec); err != nil {1220 t.Errorf("unmarshal failed: %v", err)1221 }1222 cluster := obj.(*anywherev1.NutanixDatacenterConfig)1223 cluster.SetName(objectKey.Name)1224 cluster.SetNamespace(objectKey.Namespace)1225 cluster.Spec = clusterSpec.Spec1226 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1227 }).Return(nil)1228 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {}).Return(fmt.Errorf("error"))1229 },1230 },1231 {1232 name: "node reconcile - failure to fetch worker node machine config",1233 args: args{1234 namespace: "namespaceA",1235 name: "nameA",1236 objectKey: types.NamespacedName{1237 Name: "nameA",1238 Namespace: "namespaceA",1239 },1240 },1241 wantErr: true,1242 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, name string, namespace string) {1243 cluster := &anywherev1.Cluster{}1244 cluster.SetName(name)1245 cluster.SetNamespace(namespace)1246 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)1247 spec := test.NewFullClusterSpec(t, "testdata/nutanix/eksa-cluster.yaml")1248 cluster.Spec = spec.Cluster.Spec1249 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)1250 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1251 clusterSpec := &anywherev1.NutanixDatacenterConfig{}1252 if err := yaml.Unmarshal([]byte(nutanixDatacenterConfigSpec), clusterSpec); err != nil {1253 t.Errorf("unmarshal failed: %v", err)1254 }1255 cluster := obj.(*anywherev1.NutanixDatacenterConfig)1256 cluster.SetName(objectKey.Name)1257 cluster.SetNamespace(objectKey.Namespace)1258 cluster.Spec = clusterSpec.Spec1259 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1260 }).Return(nil)1261 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1262 clusterSpec := &anywherev1.NutanixMachineConfig{}1263 if err := yaml.Unmarshal([]byte(nutanixCPMachineConfigSpec), clusterSpec); err != nil {1264 t.Errorf("unmarshal failed: %v", err)1265 }1266 cluster := obj.(*anywherev1.NutanixMachineConfig)1267 cluster.SetName(objectKey.Name)1268 cluster.SetNamespace(objectKey.Namespace)1269 cluster.Spec = clusterSpec.Spec1270 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1271 }).Return(nil)1272 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {}).Return(fmt.Errorf("error"))1273 },1274 },1275 {1276 name: "node reconcile - failure to fetch etcd node machine config",1277 args: args{1278 namespace: "namespaceA",1279 name: "nameA",1280 objectKey: types.NamespacedName{1281 Name: "nameA",1282 Namespace: "namespaceA",1283 },1284 },1285 wantErr: true,1286 prepare: func(ctx context.Context, fetcher *mocks.MockResourceFetcher, name string, namespace string) {1287 cluster := &anywherev1.Cluster{}1288 cluster.SetName(name)1289 cluster.SetNamespace(namespace)1290 fetcher.EXPECT().FetchCluster(gomock.Any(), gomock.Any()).Return(cluster, nil)1291 spec := test.NewFullClusterSpec(t, "testdata/nutanix/eksa-cluster.yaml")1292 cluster.Spec = spec.Cluster.Spec1293 fetcher.EXPECT().FetchAppliedSpec(ctx, gomock.Any()).Return(spec, nil)1294 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1295 clusterSpec := &anywherev1.NutanixDatacenterConfig{}1296 if err := yaml.Unmarshal([]byte(nutanixDatacenterConfigSpec), clusterSpec); err != nil {1297 t.Errorf("unmarshal failed: %v", err)1298 }1299 cluster := obj.(*anywherev1.NutanixDatacenterConfig)1300 cluster.SetName(objectKey.Name)1301 cluster.SetNamespace(objectKey.Namespace)1302 cluster.Spec = clusterSpec.Spec1303 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1304 }).Return(nil)1305 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1306 clusterSpec := &anywherev1.NutanixMachineConfig{}1307 if err := yaml.Unmarshal([]byte(nutanixCPMachineConfigSpec), clusterSpec); err != nil {1308 t.Errorf("unmarshal failed: %v", err)1309 }1310 cluster := obj.(*anywherev1.NutanixMachineConfig)1311 cluster.SetName(objectKey.Name)1312 cluster.SetNamespace(objectKey.Namespace)1313 cluster.Spec = clusterSpec.Spec1314 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1315 }).Return(nil)1316 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {1317 clusterSpec := &anywherev1.NutanixMachineConfig{}1318 if err := yaml.Unmarshal([]byte(nutanixWorkerMachineConfigSpec), clusterSpec); err != nil {1319 t.Errorf("unmarshal failed: %v", err)1320 }1321 cluster := obj.(*anywherev1.NutanixMachineConfig)1322 cluster.SetName(objectKey.Name)1323 cluster.SetNamespace(objectKey.Namespace)1324 cluster.Spec = clusterSpec.Spec1325 assert.Equal(t, objectKey.Name, "eksa-unit-test", "expected Name to be test-cluster")1326 }).Return(nil)1327 fetcher.EXPECT().FetchObject(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(ctx context.Context, objectKey types.NamespacedName, obj client.Object) {}).Return(fmt.Errorf("error"))1328 },1329 },1330 }1331 for _, tt := range tests {1332 t.Run(tt.name, func(t *testing.T) {1333 ctx := context.Background()1334 mockCtrl := gomock.NewController(t)1335 fetcher := mocks.NewMockResourceFetcher(mockCtrl)1336 resourceUpdater := mocks.NewMockResourceUpdater(mockCtrl)1337 tt.prepare(ctx, fetcher, tt.args.name, tt.args.namespace)...

Full Screen

Full Screen

spec_namespace.go

Source:spec_namespace.go Github

copy

Full Screen

1package mgr2import (3 "context"4 "fmt"5 "os"6 "path/filepath"7 "strconv"8 "strings"9 specs "github.com/opencontainers/runtime-spec/specs-go"10)11// isEmpty indicates whether namespace mode is empty.12func isEmpty(mode string) bool {13 return mode == ""14}15// isNone indicates whether container's namespace mode is set to "none".16func isNone(mode string) bool {17 return mode == "none"18}19// isHost indicates whether the container shares the host's corresponding namespace.20func isHost(mode string) bool {21 return mode == "host"22}23// isShareable indicates whether the containers namespace can be shared with another container.24func isShareable(mode string) bool {25 return mode == "shareable"26}27// isContainer indicates whether the container uses another container's corresponding namespace.28func isContainer(mode string) bool {29 parts := strings.SplitN(mode, ":", 2)30 return len(parts) > 1 && parts[0] == "container"31}32// isPrivate indicates whether the container uses its own namespace.33func isPrivate(ns specs.LinuxNamespaceType, mode string) bool {34 switch ns {35 case specs.IPCNamespace:36 return mode == "private"37 case specs.NetworkNamespace, specs.PIDNamespace:38 return !(isHost(mode) || isContainer(mode))39 case specs.UserNamespace, specs.UTSNamespace:40 return !(isHost(mode))41 }42 return false43}44// connectedContainer is the id or name of the container whose namespace this container share with.45func connectedContainer(mode string) string {46 parts := strings.SplitN(mode, ":", 2)47 if len(parts) == 2 {48 return parts[1]49 }50 return ""51}52func getIpcContainer(ctx context.Context, mgr ContainerMgr, id string) (*ContainerMeta, error) {53 // Check whether the container exists.54 c, err := mgr.Get(ctx, id)55 if err != nil {56 return nil, fmt.Errorf("can't join IPC namespace of container %q: %v", id, err)57 }58 // TODO: check whether the container is running and not restarting.59 // TODO: check whether the container's ipc namespace is shareable.60 return c, nil61}62func getPidContainer(ctx context.Context, mgr ContainerMgr, id string) (*ContainerMeta, error) {63 // Check the container exists.64 c, err := mgr.Get(ctx, id)65 if err != nil {66 return nil, fmt.Errorf("can't join PID namespace of %q: %v", id, err)67 }68 // TODO: check whether the container is running and not restarting.69 return c, nil70}71// TODO72func setupUserNamespace(ctx context.Context, meta *ContainerMeta, spec *SpecWrapper) error {73 return nil74}75func setupNetworkNamespace(ctx context.Context, meta *ContainerMeta, spec *SpecWrapper) error {76 s := spec.s77 ns := specs.LinuxNamespace{Type: specs.NetworkNamespace}78 setNamespace(s, ns)79 for _, ns := range s.Linux.Namespaces {80 if ns.Type == "network" && ns.Path == "" && !meta.Config.NetworkDisabled {81 target, err := os.Readlink(filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"))82 if err != nil {83 return err84 }85 s.Hooks = &specs.Hooks{86 Prestart: []specs.Hook{{87 Path: target, // FIXME: cross-platform88 Args: []string{"libnetwork-setkey", meta.ID, spec.netMgr.Controller().ID()},89 }},90 }91 }92 }93 return nil94}95func setupIpcNamespace(ctx context.Context, meta *ContainerMeta, spec *SpecWrapper) error {96 ipcMode := meta.HostConfig.IpcMode97 switch {98 case isContainer(ipcMode):99 ns := specs.LinuxNamespace{Type: specs.IPCNamespace}100 c, err := getIpcContainer(ctx, spec.ctrMgr, connectedContainer(ipcMode))101 if err != nil {102 return fmt.Errorf("setup container ipc namespace mode failed: %v", err)103 }104 ns.Path = fmt.Sprintf("/proc/%d/ns/ipc", c.State.Pid)105 setNamespace(spec.s, ns)106 case isHost(ipcMode):107 removeNamespace(spec.s, specs.IPCNamespace)108 default:109 ns := specs.LinuxNamespace{Type: specs.IPCNamespace}110 setNamespace(spec.s, ns)111 }112 return nil113}114func setupPidNamespace(ctx context.Context, meta *ContainerMeta, spec *SpecWrapper) error {115 pidMode := meta.HostConfig.PidMode116 switch {117 case isContainer(pidMode):118 ns := specs.LinuxNamespace{Type: specs.PIDNamespace}119 c, err := getPidContainer(ctx, spec.ctrMgr, connectedContainer(pidMode))120 if err != nil {121 return fmt.Errorf("setup container pid namespace mode failed: %v", err)122 }123 ns.Path = fmt.Sprintf("/proc/%d/ns/pid", c.State.Pid)124 setNamespace(spec.s, ns)125 case isHost(pidMode):126 removeNamespace(spec.s, specs.PIDNamespace)127 default:128 ns := specs.LinuxNamespace{Type: specs.PIDNamespace}129 setNamespace(spec.s, ns)130 }131 return nil132}133func setupUtsNamespace(ctx context.Context, meta *ContainerMeta, spec *SpecWrapper) error {134 utsMode := meta.HostConfig.UTSMode135 switch {136 case isHost(utsMode):137 removeNamespace(spec.s, specs.UTSNamespace)138 default:139 ns := specs.LinuxNamespace{Type: specs.UTSNamespace}140 setNamespace(spec.s, ns)141 // set hostname142 if hostname := meta.Config.Hostname.String(); hostname != "" {143 spec.s.Hostname = hostname144 }145 }146 return nil147}148func setNamespace(s *specs.Spec, ns specs.LinuxNamespace) {149 for i, n := range s.Linux.Namespaces {150 if n.Type == ns.Type {151 s.Linux.Namespaces[i] = ns152 return153 }154 }155 s.Linux.Namespaces = append(s.Linux.Namespaces, ns)156}157func removeNamespace(s *specs.Spec, nsType specs.LinuxNamespaceType) {158 for i, n := range s.Linux.Namespaces {159 if n.Type == nsType {160 s.Linux.Namespaces = append(s.Linux.Namespaces[:i], s.Linux.Namespaces[i+1:]...)161 return162 }163 }164}...

Full Screen

Full Screen

options.go

Source:options.go Github

copy

Full Screen

...39 if err != nil {40 return err41 }42 if nsFlag != "" {43 o.SetNamespace(nsFlag)44 } else {45 o.SetNamespace(nsConfig)46 }47 return nil48}49// SetNamespace configures the namespace50func (o *Options) SetNamespace(ns string) {51 o.UserNamespace = ns52}53// SetClient configures the client54func (o *Options) SetClient(client client.Client) {55 o.Client = client56}57// SetClientset configures the clientset58func (o *Options) SetClientset(clientset *kubernetes.Clientset) {59 o.Clientset = clientset60}61// GetClientConfig returns the client config62func (o *Options) GetClientConfig() clientcmd.ClientConfig {63 return o.ConfigFlags.ToRawKubeConfigLoader()64}...

Full Screen

Full Screen

SetNamespace

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 config, err := clientcmd.BuildConfigFromFlags("", "/home/username/.kube/config")4 if err != nil {5 log.Fatal(err)6 }7 config.SetNamespace("default")8 clientset, err := kubernetes.NewForConfig(config)9 if err != nil {10 log.Fatal(err)11 }12 pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})13 if err != nil {14 log.Fatal(err)15 }16 for _, pod := range pods.Items {17 fmt.Printf("%s %s18 }19}

Full Screen

Full Screen

SetNamespace

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 config, err := clientcmd.BuildConfigFromFlags("", "/home/username/.kube/config")4 if err != nil {5 panic(err.Error())6 }7 clientset, err := kubernetes.NewForConfig(config)8 if err != nil {9 panic(err.Error())10 }11 fmt.Println("namespace: ", namespace)12 config.SetNamespace("default")13 fmt.Println("namespace: ", namespace)14 namespace = clientset.CoreV1().RESTClient().Get().AbsPath("namespaces").Do().Namespace()15 fmt.Println("namespace: ", namespace)16 clientset.CoreV1().RESTClient().Get().AbsPath("namespaces").Do().SetNamespace("default")17 namespace = clientset.CoreV1().RESTClient().Get().AbsPath("namespaces").Do().Namespace()18 fmt.Println("namespace: ", namespace)19}

Full Screen

Full Screen

SetNamespace

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 viper.SetConfigName("config")4 viper.AddConfigPath(".")5 err := viper.ReadInConfig()6 if err != nil {7 fmt.Println("Error while reading config file", err)8 }9 fmt.Println("config file name:", viper.ConfigFileUsed())10 viper.SetNamespace("database")11 fmt.Println("Namespace: ", viper.Get("name"))12}

Full Screen

Full Screen

SetNamespace

Using AI Code Generation

copy

Full Screen

1func main() {2 config, err := clientcmd.BuildConfigFromFlags("", "/home/username/.kube/config")3 if err != nil {4 panic(err.Error())5 }6 clientset, err := kubernetes.NewForConfig(config)7 if err != nil {8 panic(err.Error())9 }10 config.SetNamespace(namespace)11 pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})12 if err != nil {13 panic(err.Error())14 }15 fmt.Printf("There are %d pods in the cluster16", len(pods.Items))17 for _, pod := range pods.Items {18 fmt.Printf("Pod Name: %s19 }20}

Full Screen

Full Screen

SetNamespace

Using AI Code Generation

copy

Full Screen

1func main() {2 config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(3 clientcmd.NewDefaultClientConfigLoadingRules(),4 &clientcmd.ConfigOverrides{})5 config.SetNamespace("default")6 client, err := kubernetes.NewForConfig(config.ClientConfig())7 if err != nil {8 panic(err.Error())9 }10 pods, err := client.CoreV1().Pods("").List(metav1.ListOptions{})11 if err != nil {12 panic(err.Error())13 }14 fmt.Printf("There are %d pods in the cluster15", len(pods.Items))16}

Full Screen

Full Screen

SetNamespace

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 config := viper.New()4 config.SetNamespace("dev")5 config.SetConfigFile("config.yml")6 err := config.ReadInConfig()7 if err != nil {8 fmt.Println("Error in reading the config file", err)9 }10 fmt.Println(config.GetString("url"))11}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful