Skip to content

Commit

Permalink
fix: Extension Finalization (#179)
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 authored Jan 23, 2025
1 parent be97e18 commit 6c1b7dd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ go.work
*.swp
*.swo
*~
.DS_Store
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
12 changes: 11 additions & 1 deletion pkg/subroutines/extensions_ready_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestExtensionReadySubroutine(t *testing.T) {
expectedResult ctrl.Result
}{
{
name: "should respect ready condition and return sucessfully",
name: "should respect ready condition and return successfully",
k8sMocks: func(c *mocks.Client) {
c.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).Once().Return(kerrors.NewNotFound(schema.GroupResource{}, "Namespace"))
c.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, nn types.NamespacedName, o client.Object, opts ...client.GetOption) error {
Expand Down Expand Up @@ -74,6 +75,15 @@ func TestExtensionReadySubroutine(t *testing.T) {
Spec: v1alpha1.AccountSpec{
Extensions: []v1alpha1.Extension{
{
MetadataGoTemplate: apiextensionsv1.JSON{
Raw: []byte(`{
"annotations": {
"account.core.openmfp.io/owner": "{{ .Account.metadata.name }}",
"account.core.openmfp.io/owner-namespace": "{{ .Account.metadata.namespace }}"
},
"name": "{{ .Account.metadata.name }}"
}`),
},
TypeMeta: metav1.TypeMeta{
Kind: "AccountExtension",
APIVersion: "core.openmfp.io/v1alpha1",
Expand Down
9 changes: 9 additions & 0 deletions pkg/subroutines/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,15 @@ func TestExtensionSubroutine_Finalize(t *testing.T) {
Kind: "AccountExtension",
APIVersion: "core.openmfp.io/v1alpha1",
},
MetadataGoTemplate: apiextensionsv1.JSON{
Raw: []byte(`{
"annotations": {
"account.core.openmfp.io/owner": "{{ .Account.metadata.name }}",
"account.core.openmfp.io/owner-namespace": "{{ .Account.metadata.namespace }}"
},
"name": "{{ .Account.metadata.name }}"
}`),
},
SpecGoTemplate: apiextensionsv1.JSON{},
},
},
Expand Down

0 comments on commit 6c1b7dd

Please sign in to comment.