diff --git a/controlplane/kubeadm/internal/kubeadm_config_map_test.go b/controlplane/kubeadm/internal/kubeadm_config_map_test.go index 4e6ccddeef50..aeac896a240c 100644 --- a/controlplane/kubeadm/internal/kubeadm_config_map_test.go +++ b/controlplane/kubeadm/internal/kubeadm_config_map_test.go @@ -47,7 +47,7 @@ kubernetesVersion: v1.16.1 delete(kubeadmConfigNoKey.Data, clusterConfigurationKey) kubeadmConfigBadData := kconf.DeepCopy() - kubeadmConfigBadData.Data[clusterConfigurationKey] = `foobar` + kubeadmConfigBadData.Data[clusterConfigurationKey] = `something` tests := []struct { name string @@ -94,6 +94,7 @@ kubernetesVersion: v1.16.1 }) } } + func Test_kubeadmConfig_RemoveAPIEndpoint(t *testing.T) { g := NewWithT(t) original := &corev1.ConfigMap{ @@ -378,40 +379,49 @@ scheduler: {}`, func TestUpdateImageRepository(t *testing.T) { tests := []struct { - name string - clusterConfigurationValue string - imageRepository string - expected string - expectErr error + name string + data map[string]string + imageRepository string + expected string + expectErr error }{ { name: "it should set the values, if they were empty", - clusterConfigurationValue: ` + data: map[string]string{ + clusterConfigurationKey: ` apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration imageRepository: k8s.gcr.io -`, +`}, imageRepository: "example.com/k8s", expected: "example.com/k8s", }, { name: "it shouldn't write empty strings", - clusterConfigurationValue: ` + data: map[string]string{ + clusterConfigurationKey: ` apiVersion: kubeadm.k8s.io/v1beta2 kind: ClusterConfiguration imageRepository: k8s.gcr.io -`, +`}, imageRepository: "", expected: "k8s.gcr.io", }, { name: "it should error if it's not a valid k8s object", - clusterConfigurationValue: ` + data: map[string]string{ + clusterConfigurationKey: ` imageRepository: "cool" -`, +`}, imageRepository: "example.com/k8s", expectErr: errors.New("Object 'Kind' is missing"), }, + { + name: "returns an error if config map doesn't have the cluster config data key", + data: map[string]string{}, + imageRepository: "example.com/k8s", + expectErr: errors.New("unable to find \"ClusterConfiguration\" key in kubeadm ConfigMap"), + }, } for _, test := range tests { @@ -420,9 +430,7 @@ imageRepository: "cool" kconfig := &kubeadmConfig{ ConfigMap: &corev1.ConfigMap{ - Data: map[string]string{ - clusterConfigurationKey: test.clusterConfigurationValue, - }, + Data: test.data, }, } diff --git a/controlplane/kubeadm/internal/workload_cluster_test.go b/controlplane/kubeadm/internal/workload_cluster_test.go index 2e007298f41f..ba44efbff0a4 100644 --- a/controlplane/kubeadm/internal/workload_cluster_test.go +++ b/controlplane/kubeadm/internal/workload_cluster_test.go @@ -474,6 +474,85 @@ kubernetesVersion: v1.16.1 } } +func TestUpdateImageRepositoryInKubeadmConfigMap(t *testing.T) { + kubeadmConfig := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: kubeadmConfigKey, + Namespace: metav1.NamespaceSystem, + }, + Data: map[string]string{ + clusterConfigurationKey: ` +apiVersion: kubeadm.k8s.io/v1beta2 +kind: ClusterConfiguration +imageRepository: k8s.gcr.io +`, + }, + } + + kubeadmConfigNoKey := kubeadmConfig.DeepCopy() + delete(kubeadmConfigNoKey.Data, clusterConfigurationKey) + + kubeadmConfigBadData := kubeadmConfig.DeepCopy() + kubeadmConfigBadData.Data[clusterConfigurationKey] = `foobar` + + g := NewWithT(t) + scheme := runtime.NewScheme() + g.Expect(corev1.AddToScheme(scheme)).To(Succeed()) + tests := []struct { + name string + imageRepository string + objs []runtime.Object + expectErr bool + }{ + { + name: "updates the config map", + imageRepository: "myspecialrepo.io", + objs: []runtime.Object{kubeadmConfig}, + expectErr: false, + }, + { + name: "returns error if cannot find config map", + expectErr: true, + }, + { + name: "returns error if config has bad data", + objs: []runtime.Object{kubeadmConfigBadData}, + imageRepository: "myspecialrepo.io", + expectErr: true, + }, + { + name: "returns error if config doesn't have cluster config key", + objs: []runtime.Object{kubeadmConfigNoKey}, + imageRepository: "myspecialrepo.io", + expectErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + fakeClient := fake.NewFakeClientWithScheme(scheme, tt.objs...) + w := &Workload{ + Client: fakeClient, + } + ctx := context.TODO() + err := w.UpdateImageRepositoryInKubeadmConfigMap(ctx, tt.imageRepository) + if tt.expectErr { + g.Expect(err).To(HaveOccurred()) + return + } + g.Expect(err).ToNot(HaveOccurred()) + var actualConfig corev1.ConfigMap + g.Expect(w.Client.Get( + ctx, + ctrlclient.ObjectKey{Name: kubeadmConfigKey, Namespace: metav1.NamespaceSystem}, + &actualConfig, + )).To(Succeed()) + g.Expect(actualConfig.Data[clusterConfigurationKey]).To(ContainSubstring(tt.imageRepository)) + }) + } +} + func TestClusterStatus(t *testing.T) { node1 := &corev1.Node{ ObjectMeta: metav1.ObjectMeta{