From a016c81db835140de5ad22ceb5f29d01ede50f16 Mon Sep 17 00:00:00 2001 From: pratikjagrut <26519653+pratikjagrut@users.noreply.github.com> Date: Wed, 9 Nov 2022 21:51:55 +0530 Subject: [PATCH] feat: set ownerRef for resources --- plugin/plugin.go | 14 ++++++++++---- translate/translate.go | 4 ++-- translate/util/util.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 translate/util/util.go diff --git a/plugin/plugin.go b/plugin/plugin.go index a720d024..7e1b3b8b 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -4,6 +4,11 @@ import ( "context" "encoding/json" "fmt" + "net" + "os" + "sync" + "time" + "github.com/loft-sh/vcluster-sdk/clienthelper" "github.com/loft-sh/vcluster-sdk/hook" "github.com/loft-sh/vcluster-sdk/log" @@ -11,19 +16,16 @@ import ( "github.com/loft-sh/vcluster-sdk/syncer" synccontext "github.com/loft-sh/vcluster-sdk/syncer/context" "github.com/loft-sh/vcluster-sdk/translate" + "github.com/loft-sh/vcluster-sdk/translate/util" "github.com/pkg/errors" "google.golang.org/grpc" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/tools/clientcmd" "k8s.io/klog" - "net" - "os" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/cache" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/cluster" - "sync" - "time" ) const ( @@ -572,6 +574,10 @@ func (m *manager) start() error { m.context.PhysicalManager.GetCache().WaitForCacheSync(m.context.Context) m.context.VirtualManager.GetCache().WaitForCacheSync(m.context.Context) + err = util.FindOwner(m.context) + if err != nil { + return fmt.Errorf("error in setting owner reference %v", err) + } // start syncers for _, v := range m.syncers { // fake syncer? diff --git a/translate/translate.go b/translate/translate.go index 33673f3f..97775cd6 100644 --- a/translate/translate.go +++ b/translate/translate.go @@ -51,12 +51,12 @@ func IsManaged(obj runtime.Object) bool { } func GetOwnerReference() []metav1.OwnerReference { - if Owner == nil { + if Owner == nil || Owner.GetName() == "" || Owner.GetUID() == "" { return nil } typeAccessor, err := meta.TypeAccessor(Owner) - if err != nil { + if err != nil || typeAccessor.GetAPIVersion() == "" || typeAccessor.GetKind() == "" { return nil } diff --git a/translate/util/util.go b/translate/util/util.go new file mode 100644 index 00000000..a08e2356 --- /dev/null +++ b/translate/util/util.go @@ -0,0 +1,33 @@ +package util + +import ( + "github.com/loft-sh/vcluster-sdk/syncer/context" + "github.com/loft-sh/vcluster-sdk/translate" + "github.com/pkg/errors" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/types" + "k8s.io/klog" +) + +// FindOwner finds the owner of the synced resources and sets it, unless SetOwner is set to false +func FindOwner(ctx *context.RegisterContext) error { + if ctx.CurrentNamespace != ctx.Options.TargetNamespace { + if ctx.Options.SetOwner { + klog.Warningf("Skip setting owner, because current namespace %s != target namespace %s", ctx.CurrentNamespace, ctx.Options.TargetNamespace) + } + return nil + } + + if ctx.Options.SetOwner { + service := &corev1.Service{} + err := ctx.CurrentNamespaceClient.Get(ctx.Context, types.NamespacedName{Namespace: ctx.CurrentNamespace, Name: ctx.Options.ServiceName}, service) + if err != nil { + return errors.Wrap(err, "get vcluster service") + } + + translate.Owner = service + return nil + } + + return nil +}