Skip to content

Commit

Permalink
Fix metadata transformer to remove owner ref
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed May 23, 2024
1 parent 5156857 commit 935b916
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/transformer/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ package transformer

import (
"fmt"
"reflect"
"slices"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1"
"github.com/cosmo-workspace/cosmo/pkg/instance"
Expand Down Expand Up @@ -51,6 +56,22 @@ func (t *MetadataTransformer) Transform(src *unstructured.Unstructured) (*unstru
if err != nil {
return nil, fmt.Errorf("failed to set owner reference on %s: %w", obj.GetObjectKind().GroupVersionKind(), err)
}
} else {
// Remove owner reference
if len(obj.GetOwnerReferences()) > 0 {
gvk, _ := apiutil.GVKForObject(t.inst, t.scheme)
refs := slices.DeleteFunc(obj.GetOwnerReferences(), func(v metav1.OwnerReference) bool {
return reflect.DeepEqual(v, metav1.OwnerReference{
APIVersion: gvk.GroupVersion().String(),
Kind: gvk.Kind,
Name: t.inst.GetName(),
UID: t.inst.GetUID(),
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
})
})
obj.SetOwnerReferences(refs)
}
}
return obj, nil
}
134 changes: 134 additions & 0 deletions pkg/transformer/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,140 @@ metadata:
namespace: cosmo-user-tom
spec:
host: example.com
`,
wantErr: false,
},
{
name: "Remove owner ref when delete-policy: keep annotated on Template resource",
fields: fields{
inst: &cosmov1alpha1.Instance{
ObjectMeta: metav1.ObjectMeta{
Name: "cs1",
Namespace: "cosmo-user-tom",
UID: "xxxxxxxxx",
},
Spec: cosmov1alpha1.InstanceSpec{
Template: cosmov1alpha1.TemplateRef{
Name: "code-server",
},
Override: cosmov1alpha1.OverrideSpec{},
Vars: map[string]string{"{{TEST}}": "OK"},
},
},
disableNamePrefix: false,
scheme: scheme,
},
args: args{
src: `apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cosmo/ingress-patch-enable: "true"
kubernetes.io/ingress.class: alb
cosmo-workspace.github.io/delete-policy: keep
labels:
cosmo-workspace.github.io/instance: cs1
cosmo-workspace.github.io/template: code-server
name: cs1-test
namespace: cosmo-user-tom
ownerReferences:
- apiVersion: cosmo-workspace.github.io/v1alpha1
blockOwnerDeletion: true
controller: true
kind: Instance
name: cs1
uid: "xxxxxxxxx"
- apiVersion: cosmo-workspace.github.io/v1alpha1
blockOwnerDeletion: true
controller: true
kind: Instance
name: cs1
uid: "yyyyyyyyy"
spec:
host: example.com
`,
},
want: `apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cosmo/ingress-patch-enable: "true"
kubernetes.io/ingress.class: alb
cosmo-workspace.github.io/delete-policy: keep
labels:
cosmo-workspace.github.io/instance: cs1
cosmo-workspace.github.io/template: code-server
name: cs1-test
namespace: cosmo-user-tom
ownerReferences:
- apiVersion: cosmo-workspace.github.io/v1alpha1
blockOwnerDeletion: true
controller: true
kind: Instance
name: cs1
uid: "yyyyyyyyy"
spec:
host: example.com
`,
wantErr: false,
},

{
name: "Remove owner ref when delete-policy: keep annotated on Instance",
fields: fields{
inst: &cosmov1alpha1.Instance{
ObjectMeta: metav1.ObjectMeta{
Name: "cs1",
Namespace: "cosmo-user-tom",
UID: "xxxxxxxxx",
Annotations: map[string]string{
cosmov1alpha1.ResourceAnnKeyDeletePolicy: cosmov1alpha1.ResourceAnnEnumDeletePolicyKeep,
},
},
Spec: cosmov1alpha1.InstanceSpec{
Template: cosmov1alpha1.TemplateRef{
Name: "code-server",
},
Override: cosmov1alpha1.OverrideSpec{},
Vars: map[string]string{"{{TEST}}": "OK"},
},
},
disableNamePrefix: false,
scheme: scheme,
},
args: args{
src: `apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
labels:
cosmo-workspace.github.io/instance: cs1
cosmo-workspace.github.io/template: code-server
name: cs1-test
namespace: cosmo-user-tom
ownerReferences:
- apiVersion: cosmo-workspace.github.io/v1alpha1
blockOwnerDeletion: true
controller: true
kind: Instance
name: cs1
uid: "xxxxxxxxx"
spec:
ingressClassName: alb
host: example.com
`,
},
want: `apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
labels:
cosmo-workspace.github.io/instance: cs1
cosmo-workspace.github.io/template: code-server
name: cs1-test
namespace: cosmo-user-tom
ownerReferences: []
spec:
ingressClassName: alb
host: example.com
`,
wantErr: false,
},
Expand Down

0 comments on commit 935b916

Please sign in to comment.