Skip to content

Commit

Permalink
Add tests, nil checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Moss committed Mar 27, 2020
1 parent 1eac035 commit a43711f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
8 changes: 5 additions & 3 deletions controlplane/kubeadm/controllers/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (r *KubeadmControlPlaneReconciler) upgradeControlPlane(
}

parsedVersion, err := semver.ParseTolerant(kcp.Spec.Version)
parsedImageRepository := kcp.Spec.KubeadmConfigSpec.ClusterConfiguration.ImageRepository
if err != nil {
return ctrl.Result{}, errors.Wrapf(err, "failed to parse kubernetes version %q", kcp.Spec.Version)
}
Expand All @@ -65,8 +64,11 @@ func (r *KubeadmControlPlaneReconciler) upgradeControlPlane(
return ctrl.Result{}, errors.Wrap(err, "failed to update the kubernetes version in the kubeadm config map")
}

if err := workloadCluster.UpdateImageRepositoryInKubeadmConfigMap(ctx, parsedImageRepository); err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to update the kubernetes version in the kubeadm config map")
if kcp.Spec.KubeadmConfigSpec.ClusterConfiguration != nil {
imageRepository := kcp.Spec.KubeadmConfigSpec.ClusterConfiguration.ImageRepository
if err := workloadCluster.UpdateImageRepositoryInKubeadmConfigMap(ctx, imageRepository); err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to update the kubernetes version in the kubeadm config map")
}
}

if kcp.Spec.KubeadmConfigSpec.ClusterConfiguration != nil && kcp.Spec.KubeadmConfigSpec.ClusterConfiguration.Etcd.Local != nil {
Expand Down
5 changes: 4 additions & 1 deletion controlplane/kubeadm/internal/kubeadm_config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ func (k *kubeadmConfig) UpdateKubernetesVersion(version string) error {
return nil
}

// UpdateImageRepository changes the kubernetes version found in the kubeadm config map
// UpdateImageRepository changes the image repository found in the kubeadm config map
func (k *kubeadmConfig) UpdateImageRepository(imageRepository string) error {
if imageRepository == "" {
return nil
}
data, ok := k.ConfigMap.Data[clusterConfigurationKey]
if !ok {
return errors.Errorf("unable to find %q key in kubeadm ConfigMap", clusterConfigurationKey)
Expand Down
64 changes: 64 additions & 0 deletions controlplane/kubeadm/internal/kubeadm_config_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,67 @@ scheduler: {}`,
})
}
}

func TestUpdateImageRepository(t *testing.T) {

tests := []struct {
name string
clusterConfigurationValue string
imageRepository string
expected string
expectErr error
}{
{
name: "it should set the values, if they were empty",
clusterConfigurationValue: `
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: `
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: `
imageRepository: "cool"
`,
imageRepository: "example.com/k8s",
expectErr: errors.New("Object 'Kind' is missing"),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
g := NewWithT(t)

kconfig := &kubeadmConfig{
ConfigMap: &corev1.ConfigMap{
Data: map[string]string{
clusterConfigurationKey: test.clusterConfigurationValue,
},
},
}

err := kconfig.UpdateImageRepository(test.imageRepository)
if test.expectErr == nil {
g.Expect(err).ToNot(HaveOccurred())
} else {
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring(test.expectErr.Error()))
}

g.Expect(kconfig.ConfigMap.Data[clusterConfigurationKey]).To(ContainSubstring(test.expected))
})
}
}

0 comments on commit a43711f

Please sign in to comment.