Skip to content

Commit

Permalink
modify setOwnerReference to accept unstructred.Unstructured
Browse files Browse the repository at this point in the history
Signed-off-by: varshaprasad96 <varshaprasad96@gmail.com>
  • Loading branch information
varshaprasad96 committed Oct 21, 2021
1 parent 2f04ded commit 9f8beb0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func watchDependentResources(mgr manager.Manager, r *HelmOperatorReconciler, c c
}

restMapper := mgr.GetRESTMapper()
useOwnerRef, err := controllerutil.SupportsOwnerReference(restMapper, owner, dependent)
useOwnerRef, err := controllerutil.SupportsOwnerReference(restMapper, owner, unstructuredObj)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/actionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (pr *ownerPostRenderer) Run(in *bytes.Buffer) (*bytes.Buffer, error) {
return err
}
u := &unstructured.Unstructured{Object: objMap}
useOwnerRef, err := controllerutil.SupportsOwnerReference(pr.rm, pr.owner, u)
useOwnerRef, err := controllerutil.SupportsOwnerReference(pr.rm, pr.owner.(*unstructured.Unstructured), u)
if err != nil {
return err
}
Expand Down
18 changes: 4 additions & 14 deletions pkg/sdk/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -57,34 +57,24 @@ func WaitForDeletion(ctx context.Context, cl client.Reader, o client.Object) err
}, ctx.Done())
}

func SupportsOwnerReference(restMapper meta.RESTMapper, owner, dependent runtime.Object) (bool, error) {
func SupportsOwnerReference(restMapper meta.RESTMapper, owner, dependent *unstructured.Unstructured) (bool, error) {
ownerGVK := owner.GetObjectKind().GroupVersionKind()
ownerMapping, err := restMapper.RESTMapping(ownerGVK.GroupKind(), ownerGVK.Version)
if err != nil {
return false, err
}

mOwner, err := meta.Accessor(owner)
if err != nil {
return false, err
}

depGVK := dependent.GetObjectKind().GroupVersionKind()
depMapping, err := restMapper.RESTMapping(depGVK.GroupKind(), depGVK.Version)
if err != nil {
return false, err
}

mDep, err := meta.Accessor(dependent)
if err != nil {
return false, err
}

ownerClusterScoped := ownerMapping.Scope.Name() == meta.RESTScopeNameRoot
ownerNamespace := mOwner.GetNamespace()
ownerNamespace := owner.GetNamespace()
depClusterScoped := depMapping.Scope.Name() == meta.RESTScopeNameRoot

depNamespace := mDep.GetNamespace()
depNamespace := dependent.GetNamespace()

if ownerClusterScoped {
return true, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/controllerutil/controllerutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ var _ = Describe("Controllerutil", func() {
Describe("SupportsOwnerReference", func() {
var (
rm *meta.DefaultRESTMapper
owner client.Object
dependent client.Object
owner *unstructured.Unstructured
dependent *unstructured.Unstructured
clusterScoped = schema.GroupVersionKind{Group: "example.com", Version: "v1", Kind: "ClusterScoped"}
namespaceScoped = schema.GroupVersionKind{Group: "example.com", Version: "v1", Kind: "NamespaceScoped"}
)
Expand Down

0 comments on commit 9f8beb0

Please sign in to comment.