Skip to content

Commit 684dbbe

Browse files
Cleanup older configmaps with the naming format etcd-bootstrap-UID[:6] (#1014)
* cleanup older configmaps with the naming format etcd-bootstrap-UID[:6]
1 parent 9b2b8b7 commit 684dbbe

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

api/core/v1alpha1/helper.go

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ func GetServiceAccountName(etcdObjMeta metav1.ObjectMeta) string {
2929
return etcdObjMeta.Name
3030
}
3131

32+
// GetOldConfigMapName returns the name of the old configmap for the Etcd.
33+
// TODO: @anveshreddy18: Remove this function after 3 releases, i.e in v0.31.0
34+
func GetOldConfigMapName(etcdObjMeta metav1.ObjectMeta) string {
35+
return fmt.Sprintf("etcd-bootstrap-%s", string(etcdObjMeta.UID[:6]))
36+
}
37+
3238
// GetConfigMapName returns the name of the configmap for the Etcd.
3339
func GetConfigMapName(etcdObjMeta metav1.ObjectMeta) string {
3440
return fmt.Sprintf("%s-config", etcdObjMeta.Name)

internal/component/configmap/configmap.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,24 @@ func (r _resource) GetExistingResourceNames(ctx component.OperatorContext, etcdO
6464
}
6565

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

@@ -142,6 +159,13 @@ func getObjectKey(obj metav1.ObjectMeta) client.ObjectKey {
142159
}
143160
}
144161

162+
func getOldObjectKey(obj metav1.ObjectMeta) client.ObjectKey {
163+
return client.ObjectKey{
164+
Name: druidv1alpha1.GetOldConfigMapName(obj),
165+
Namespace: obj.Namespace,
166+
}
167+
}
168+
145169
func emptyConfigMap(objectKey client.ObjectKey) *corev1.ConfigMap {
146170
return &corev1.ConfigMap{
147171
ObjectMeta: metav1.ObjectMeta{

internal/component/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
// However, it can also be used where ever operation context is required to be specified.
1818
// Each component can in turn define their own fine-grained operation labels as well.
1919
const (
20+
// OperationPreSync is the PreSync operation of the Operator.
21+
OperationPreSync = "PreSync"
2022
// OperationSync is the Sync operation of the Operator.
2123
OperationSync = "Sync"
2224
// OperationTriggerDelete is the TriggerDelete operation of the Operator.

internal/controller/etcd/reconcile_spec.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ func (r *Reconciler) recordEtcdSpecReconcileSuspension(etcd *druidv1alpha1.Etcd,
169169
}
170170

171171
func (r *Reconciler) getOrderedOperatorsForPreSync() []component.Kind {
172-
return []component.Kind{}
172+
return []component.Kind{
173+
component.ConfigMapKind,
174+
}
173175
}
174176

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

0 commit comments

Comments
 (0)