Skip to content

Commit

Permalink
Merge pull request #37 from pratikjagrut/set.owner.ref
Browse files Browse the repository at this point in the history
enchancment: set ownerReference on synced resources in accordance to vcluster behavior
  • Loading branch information
FabianKramm authored Nov 9, 2022
2 parents e41ff3c + a016c81 commit ef7f91e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
14 changes: 10 additions & 4 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ 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"
"github.com/loft-sh/vcluster-sdk/plugin/remote"
"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 (
Expand Down Expand Up @@ -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?
Expand Down
4 changes: 2 additions & 2 deletions translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
33 changes: 33 additions & 0 deletions translate/util/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ef7f91e

Please sign in to comment.