From 1eac03509a921c8ce5971c7e9b6248d360fa5f30 Mon Sep 17 00:00:00 2001 From: Naadir Jeewa Date: Fri, 27 Mar 2020 19:10:02 +0000 Subject: [PATCH] Update kubeadm configmap imageRepository Signed-off-by: Naadir Jeewa --- controlplane/kubeadm/controllers/upgrade.go | 5 +++ .../kubeadm/internal/kubeadm_config_map.go | 38 +++++++++++++++---- .../kubeadm/internal/workload_cluster.go | 18 +++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/controlplane/kubeadm/controllers/upgrade.go b/controlplane/kubeadm/controllers/upgrade.go index 2670adfcfd9c..337592dee09f 100644 --- a/controlplane/kubeadm/controllers/upgrade.go +++ b/controlplane/kubeadm/controllers/upgrade.go @@ -48,6 +48,7 @@ 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) } @@ -64,6 +65,10 @@ 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 && kcp.Spec.KubeadmConfigSpec.ClusterConfiguration.Etcd.Local != nil { meta := kcp.Spec.KubeadmConfigSpec.ClusterConfiguration.Etcd.Local.ImageMeta if err := workloadCluster.UpdateEtcdVersionInKubeadmConfigMap(ctx, meta.ImageRepository, meta.ImageTag); err != nil { diff --git a/controlplane/kubeadm/internal/kubeadm_config_map.go b/controlplane/kubeadm/internal/kubeadm_config_map.go index 79b0ea22cbe8..ea3e745bf02f 100644 --- a/controlplane/kubeadm/internal/kubeadm_config_map.go +++ b/controlplane/kubeadm/internal/kubeadm_config_map.go @@ -27,14 +27,15 @@ import ( ) const ( - clusterStatusKey = "ClusterStatus" - clusterConfigurationKey = "ClusterConfiguration" - statusAPIEndpointsKey = "apiEndpoints" - configVersionKey = "kubernetesVersion" - dnsKey = "dns" - dnsTypeKey = "type" - dnsImageRepositoryKey = "imageRepository" - dnsImageTagKey = "imageTag" + clusterStatusKey = "ClusterStatus" + clusterConfigurationKey = "ClusterConfiguration" + statusAPIEndpointsKey = "apiEndpoints" + configVersionKey = "kubernetesVersion" + dnsKey = "dns" + dnsTypeKey = "type" + dnsImageRepositoryKey = "imageRepository" + dnsImageTagKey = "imageTag" + configImageRepositoryKey = "imageRepository" ) // kubeadmConfig wraps up interactions necessary for modifying the kubeadm config during an upgrade. @@ -89,6 +90,27 @@ func (k *kubeadmConfig) UpdateKubernetesVersion(version string) error { return nil } +// UpdateImageRepository changes the kubernetes version found in the kubeadm config map +func (k *kubeadmConfig) UpdateImageRepository(imageRepository string) error { + data, ok := k.ConfigMap.Data[clusterConfigurationKey] + if !ok { + return errors.Errorf("unable to find %q key in kubeadm ConfigMap", clusterConfigurationKey) + } + configuration, err := yamlToUnstructured([]byte(data)) + if err != nil { + return errors.Wrapf(err, "unable to decode kubeadm ConfigMap's %q to Unstructured object", clusterConfigurationKey) + } + if err := unstructured.SetNestedField(configuration.UnstructuredContent(), imageRepository, configImageRepositoryKey); err != nil { + return errors.Wrapf(err, "unable to update %q on kubeadm ConfigMap's %q", configVersionKey, clusterConfigurationKey) + } + updated, err := yaml.Marshal(configuration) + if err != nil { + return errors.Wrapf(err, "unable to encode kubeadm ConfigMap's %q to YAML", clusterConfigurationKey) + } + k.ConfigMap.Data[clusterConfigurationKey] = string(updated) + return nil +} + // UpdateEtcdMeta sets the local etcd's configuration's image repository and image tag func (k *kubeadmConfig) UpdateEtcdMeta(imageRepository, imageTag string) (bool, error) { data, ok := k.ConfigMap.Data[clusterConfigurationKey] diff --git a/controlplane/kubeadm/internal/workload_cluster.go b/controlplane/kubeadm/internal/workload_cluster.go index a0e7e292fa47..6779f931af30 100644 --- a/controlplane/kubeadm/internal/workload_cluster.go +++ b/controlplane/kubeadm/internal/workload_cluster.go @@ -68,6 +68,7 @@ type WorkloadCluster interface { ReconcileKubeletRBACBinding(ctx context.Context, version semver.Version) error ReconcileKubeletRBACRole(ctx context.Context, version semver.Version) error UpdateKubernetesVersionInKubeadmConfigMap(ctx context.Context, version semver.Version) error + UpdateImageRepositoryInKubeadmConfigMap(ctx context.Context, imageRepository string) error UpdateEtcdVersionInKubeadmConfigMap(ctx context.Context, imageRepository, imageTag string) error UpdateKubeletConfigMap(ctx context.Context, version semver.Version) error UpdateKubeProxyImageInfo(ctx context.Context, kcp *controlplanev1.KubeadmControlPlane) error @@ -308,6 +309,23 @@ func (w *Workload) UpdateEtcdVersionInKubeadmConfigMap(ctx context.Context, imag return nil } +// UpdateKubernetesVersionInKubeadmConfigMap updates the kubernetes version in the kubeadm config map. +func (w *Workload) UpdateImageRepositoryInKubeadmConfigMap(ctx context.Context, imageRepository string) error { + configMapKey := ctrlclient.ObjectKey{Name: "kubeadm-config", Namespace: metav1.NamespaceSystem} + kubeadmConfigMap, err := w.getConfigMap(ctx, configMapKey) + if err != nil { + return err + } + config := &kubeadmConfig{ConfigMap: kubeadmConfigMap} + if err := config.UpdateImageRepository(imageRepository); err != nil { + return err + } + if err := w.Client.Update(ctx, config.ConfigMap); err != nil { + return errors.Wrap(err, "error updating kubeadm ConfigMap") + } + return nil +} + // UpdateKubernetesVersionInKubeadmConfigMap updates the kubernetes version in the kubeadm config map. func (w *Workload) UpdateKubernetesVersionInKubeadmConfigMap(ctx context.Context, version semver.Version) error { configMapKey := ctrlclient.ObjectKey{Name: "kubeadm-config", Namespace: metav1.NamespaceSystem}