Skip to content

Commit

Permalink
Update kubeadm configmap imageRepository
Browse files Browse the repository at this point in the history
Signed-off-by: Naadir Jeewa <[email protected]>
  • Loading branch information
Naadir Jeewa committed Mar 27, 2020
1 parent 2cc8570 commit 1eac035
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
5 changes: 5 additions & 0 deletions controlplane/kubeadm/controllers/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
38 changes: 30 additions & 8 deletions controlplane/kubeadm/internal/kubeadm_config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]
Expand Down
18 changes: 18 additions & 0 deletions controlplane/kubeadm/internal/workload_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 1eac035

Please sign in to comment.