Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Etcd version #2579

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,13 @@ func (r *KubeadmControlPlaneReconciler) upgradeControlPlane(ctx context.Context,
return ctrl.Result{}, errors.Wrap(err, "failed to update the kubernetes version in the kubeadm config map")
}

if 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 {
return ctrl.Result{}, errors.Wrap(err, "failed to update the etcd version in the kubeadm config map")
}
}

if err := workloadCluster.UpdateKubeletConfigMap(ctx, parsedVersion); err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to upgrade kubelet config map")
}
Expand Down
28 changes: 28 additions & 0 deletions controlplane/kubeadm/internal/kubeadm_config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,34 @@ func (k *kubeadmConfig) UpdateKubernetesVersion(version string) error {
return nil
}

// UpdateEtcdMeta sets the local etcd's configuration's image repository and image tag
func (k *kubeadmConfig) UpdateEtcdMeta(imageRepository, imageTag string) error {
data, ok := k.ConfigMap.Data[clusterConfigurationKey]
if !ok {
return errors.Errorf("could not find key %q in kubeadm config", clusterConfigurationKey)
}
configuration, err := yamlToUnstructured([]byte(data))
if err != nil {
return errors.Wrap(err, "unable to convert YAML to unstructured")
}
if imageRepository != "" {
if err := unstructured.SetNestedField(configuration.UnstructuredContent(), imageRepository, "etcd", "local", "imageRepository"); err != nil {
return errors.Wrap(err, "unable to update image repository on kubeadm configmap")
}
}
if imageTag != "" {
if err := unstructured.SetNestedField(configuration.UnstructuredContent(), imageTag, "etcd", "local", "imageTag"); err != nil {
return errors.Wrap(err, "unable to update image repository on kubeadm configmap")
}
}
updated, err := yaml.Marshal(configuration)
if err != nil {
return errors.Wrap(err, "error encoding kubeadm cluster configuration object")
}
k.ConfigMap.Data[clusterConfigurationKey] = string(updated)
return nil
}

// yamlToUnstructured looks inside a config map for a specific key and extracts the embedded YAML into an
// *unstructured.Unstructured.
func yamlToUnstructured(rawYAML []byte) (*unstructured.Unstructured, error) {
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 @@ -62,6 +62,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 string) error
UpdateEtcdVersionInKubeadmConfigMap(ctx context.Context, imageRepository, imageTag string) error
UpdateKubeletConfigMap(ctx context.Context, version semver.Version) error
RemoveEtcdMemberForMachine(ctx context.Context, machine *clusterv1.Machine) error
RemoveMachineFromKubeadmConfigMap(ctx context.Context, machine *clusterv1.Machine) error
Expand Down Expand Up @@ -257,6 +258,23 @@ func (w *Workload) EtcdIsHealthy(ctx context.Context) (HealthCheckResult, error)
return response, nil
}

// UpdateEtcdVersionInKubeadmConfigMap sets the imageRepository or the imageTag or both in the kubeadm config map.
func (w *Workload) UpdateEtcdVersionInKubeadmConfigMap(ctx context.Context, imageRepository, imageTag 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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we compare first the values in the map vs the ones we want to set before issuing the update?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do a follow-up here as well

if err := config.UpdateEtcdMeta(imageRepository, imageTag); 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 string) error {
configMapKey := ctrlclient.ObjectKey{Name: "kubeadm-config", Namespace: metav1.NamespaceSystem}
Expand Down