Skip to content

Commit

Permalink
fix: config-related resource cannot be cleaned up after disable addons (
Browse files Browse the repository at this point in the history
  • Loading branch information
sophon-zt committed Sep 23, 2024
1 parent afd68c8 commit d74cbe3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions controllers/apps/componentdefinition_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (r *ComponentDefinitionReconciler) deletionHandler(rctx intctrlutil.Request
recordEvent, &appsv1alpha1.ComponentList{}); res != nil || err != nil {
return res, err
}
_ = appsconfig.DeleteConfigMapFinalizer(r.Client, rctx, cmpd)
return nil, nil
}
}
Expand Down
30 changes: 24 additions & 6 deletions controllers/apps/configuration/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,49 @@ func DeleteConfigMapFinalizer(cli client.Client, ctx intctrlutil.RequestCtx, obj

func validateConfigMapOwners(cli client.Client, ctx intctrlutil.RequestCtx, labels client.MatchingLabels, check func(obj client.Object) bool, objLists ...client.ObjectList) (bool, error) {
for _, objList := range objLists {
if err := cli.List(ctx.Ctx, objList, labels, client.Limit(2)); err != nil {
if err := cli.List(ctx.Ctx, objList, labels); err != nil {
return false, err
}
v, err := conversion.EnforcePtr(objList)
if err != nil {
return false, err
}
items := v.FieldByName("Items")
if !items.IsValid() || items.Kind() != reflect.Slice || items.Len() > 1 {
if !items.IsValid() || items.Kind() != reflect.Slice {
return false, nil
}
if items.Len() == 0 {
continue
}
if !checkEnabledDelete(items, check) {
return false, nil
}
}
return true, nil
}

val := items.Index(0)
func checkEnabledDelete(items reflect.Value, check func(obj client.Object) bool) bool {
for i := 0; i < items.Len(); i++ {
val := items.Index(i)
// fetch object pointer
if val.CanAddr() {
val = val.Addr()
}
if !val.CanInterface() || !check(val.Interface().(client.Object)) {
return false, nil
if !val.CanInterface() {
return false
}
obj, ok := val.Interface().(client.Object)
if !ok {
return false
}
if obj.GetDeletionTimestamp() != nil {
continue
}
if !check(obj) {
return false
}
}
return true, nil
return true
}

func batchDeleteConfigMapFinalizer(cli client.Client, ctx intctrlutil.RequestCtx, configSpecs []appsv1alpha1.ComponentConfigSpec, cr client.Object) error {
Expand Down

0 comments on commit d74cbe3

Please sign in to comment.