Skip to content

Commit

Permalink
Cleanup older configmaps with the naming format `etcd-bootstrap-UID[:…
Browse files Browse the repository at this point in the history
…6]` (#1014)

* cleanup older configmaps with the naming format etcd-bootstrap-UID[:6]
  • Loading branch information
anveshreddy18 authored Mar 5, 2025
1 parent 9b2b8b7 commit 684dbbe
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions api/core/v1alpha1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func GetServiceAccountName(etcdObjMeta metav1.ObjectMeta) string {
return etcdObjMeta.Name
}

// GetOldConfigMapName returns the name of the old configmap for the Etcd.
// TODO: @anveshreddy18: Remove this function after 3 releases, i.e in v0.31.0
func GetOldConfigMapName(etcdObjMeta metav1.ObjectMeta) string {
return fmt.Sprintf("etcd-bootstrap-%s", string(etcdObjMeta.UID[:6]))
}

// GetConfigMapName returns the name of the configmap for the Etcd.
func GetConfigMapName(etcdObjMeta metav1.ObjectMeta) string {
return fmt.Sprintf("%s-config", etcdObjMeta.Name)
Expand Down
26 changes: 25 additions & 1 deletion internal/component/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,24 @@ func (r _resource) GetExistingResourceNames(ctx component.OperatorContext, etcdO
}

// PreSync is a no-op for the configmap component.
func (r _resource) PreSync(_ component.OperatorContext, _ *druidv1alpha1.Etcd) error {
// TODO: @anveshreddy18: Remove the functionality in the below PreSync method after 3 releases i.e in v0.31.0
// This is a temporary functionality to remove the old configmaps from the cluster that are created by the etcd-druid.
func (r _resource) PreSync(ctx component.OperatorContext, etcd *druidv1alpha1.Etcd) error {
oldConfigMap := emptyConfigMap(getOldObjectKey(etcd.ObjectMeta))
ctx.Logger.Info("PreSync: Deleting old configmap", "name", oldConfigMap.Name)
if err := r.client.Delete(ctx, oldConfigMap); err != nil {
if errors.IsNotFound(err) {
ctx.Logger.Info("No old configmap found, ConfigMap PreSync is a no-op", "name", oldConfigMap.Name)
return nil
}
return druiderr.WrapError(
err,
ErrDeleteConfigMap,
component.OperationPreSync,
fmt.Sprintf("Failed to delete old configmap %s", oldConfigMap.Name),
)
}
ctx.Logger.Info("deleted", "component", "configmap", "name", oldConfigMap.Name)
return nil
}

Expand Down Expand Up @@ -142,6 +159,13 @@ func getObjectKey(obj metav1.ObjectMeta) client.ObjectKey {
}
}

func getOldObjectKey(obj metav1.ObjectMeta) client.ObjectKey {
return client.ObjectKey{
Name: druidv1alpha1.GetOldConfigMapName(obj),
Namespace: obj.Namespace,
}
}

func emptyConfigMap(objectKey client.ObjectKey) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 2 additions & 0 deletions internal/component/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
// However, it can also be used where ever operation context is required to be specified.
// Each component can in turn define their own fine-grained operation labels as well.
const (
// OperationPreSync is the PreSync operation of the Operator.
OperationPreSync = "PreSync"
// OperationSync is the Sync operation of the Operator.
OperationSync = "Sync"
// OperationTriggerDelete is the TriggerDelete operation of the Operator.
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/etcd/reconcile_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ func (r *Reconciler) recordEtcdSpecReconcileSuspension(etcd *druidv1alpha1.Etcd,
}

func (r *Reconciler) getOrderedOperatorsForPreSync() []component.Kind {
return []component.Kind{}
return []component.Kind{
component.ConfigMapKind,
}
}

func (r *Reconciler) getOrderedOperatorsForSync() []component.Kind {
Expand Down

0 comments on commit 684dbbe

Please sign in to comment.