Skip to content

Commit

Permalink
fix: Extension Finalization
Browse files Browse the repository at this point in the history
The metadataGoTemplate was not properly rendered in case of finalization leading to misbehavior on finalization
  • Loading branch information
nexus49 committed Jan 23, 2025
1 parent be97e18 commit ec46ef8
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion pkg/subroutines/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kcp-dev/logicalcluster/v3"
"github.com/openmfp/golang-commons/controller/lifecycle"
"github.com/openmfp/golang-commons/errors"
"github.com/openmfp/golang-commons/logger"
v1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -144,6 +145,7 @@ func RenderExtensionSpec(ctx context.Context, keyValues map[string]any, account
}

func (e *ExtensionSubroutine) Finalize(ctx context.Context, instance lifecycle.RuntimeObject) (ctrl.Result, errors.OperatorError) {
log := logger.LoadLoggerFromContext(ctx)
account := instance.(*v1alpha1.Account)

lookupNamespace := account.GetNamespace()
Expand All @@ -157,11 +159,31 @@ func (e *ExtensionSubroutine) Finalize(ctx context.Context, instance lifecycle.R
us := unstructured.Unstructured{}
us.SetGroupVersionKind(extension.GroupVersionKind())

us.SetName(strings.ToLower(extension.Kind))
if len(extension.MetadataGoTemplate.Raw) > 0 {
var metadataKeyValues map[string]any
err := json.NewDecoder(bytes.NewReader(extension.MetadataGoTemplate.Raw)).Decode(&metadataKeyValues)
if err != nil {
return ctrl.Result{}, errors.NewOperatorError(err, true, false)
}
err = RenderExtensionSpec(ctx, metadataKeyValues, account, &us, []string{"metadata"})
if err != nil {
return ctrl.Result{}, errors.NewOperatorError(err, true, false)
}
}

if us.GetName() == "" {
us.SetName(strings.ToLower(extension.Kind))
}
if namespaced, err := e.client.IsObjectNamespaced(&us); err == nil && namespaced {
us.SetNamespace(*account.Status.Namespace)
}

log.Info().
Str("name", us.GetName()).
Str("kind", us.GetKind()).
Str("namespace", us.GetNamespace()).
Msg("Deleting extension")

err := e.client.Delete(ctx, &us)
if kerrors.IsNotFound(err) {
continue
Expand Down

0 comments on commit ec46ef8

Please sign in to comment.