Skip to content

Commit

Permalink
refactor: delete object if it was deleted on host
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKramm committed Jul 18, 2024
1 parent 3523d87 commit b8f4b77
Show file tree
Hide file tree
Showing 53 changed files with 342 additions and 265 deletions.
5 changes: 5 additions & 0 deletions pkg/cli/connect_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func ConnectHelm(ctx context.Context, options *ConnectOptions, globalFlags *flag
return err
}

log.Debugf("Found vCluster %s/%s", vCluster.Namespace, vCluster.Name)
return cmd.connect(ctx, vCluster, command)
}

Expand Down Expand Up @@ -285,6 +286,7 @@ func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName
return false, err
} else if len(pods.Items) == 0 {
err = fmt.Errorf("can't find a running vcluster pod in namespace %s", cmd.Namespace)
cmd.Log.Debugf("can't find a running vcluster pod in namespace %s", cmd.Namespace)
return false, nil
}

Expand All @@ -294,6 +296,7 @@ func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName
})
if pods.Items[0].DeletionTimestamp != nil {
err = fmt.Errorf("can't find a running vcluster pod in namespace %s", cmd.Namespace)
cmd.Log.Debugf("can't find a running vcluster pod in namespace %s", cmd.Namespace)
return false, nil
}

Expand All @@ -304,12 +307,14 @@ func (cmd *connectHelm) getVClusterKubeConfig(ctx context.Context, vclusterName
return nil, fmt.Errorf("finding vcluster pod: %w - %w", waitErr, err)
}
}
cmd.Log.Debugf("Successfully found vCluster pod for connecting %s", podName)

// get the kube config from the Secret
kubeConfig, err := clihelper.GetKubeConfig(ctx, cmd.kubeClient, vclusterName, cmd.Namespace, cmd.Log)
if err != nil {
return nil, fmt.Errorf("failed to parse kube config: %w", err)
}
cmd.Log.Debug("Successfully retrieved vCluster kube config")

// find out port we should listen to locally
if len(kubeConfig.Clusters) != 1 {
Expand Down
23 changes: 14 additions & 9 deletions pkg/controllers/generic/export_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/loft-sh/vcluster/pkg/config"
syncertypes "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/mappings/generic"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/loft-sh/vcluster/pkg/controllers/syncer"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
util "github.com/loft-sh/vcluster/pkg/util/context"
"github.com/loft-sh/vcluster/pkg/util/translate"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -105,11 +105,11 @@ func createExporterFromConfig(ctx *synccontext.RegisterContext, config *vcluster
}

return &exporter{
GenericTranslator: translator.NewGenericTranslator(ctx, controllerID, obj, mapper),
ObjectPatcher: &exportPatcher{
config: config,
gvk: gvk,
},
NamespacedTranslator: translator.NewNamespacedTranslator(ctx, controllerID, obj, mapper),

patcher: NewPatcher(ctx.VirtualManager.GetClient(), ctx.PhysicalManager.GetClient(), hasStatusSubresource, log.New(controllerID)),
gvk: gvk,
Expand All @@ -125,7 +125,7 @@ func BuildCustomExporter(
controllerID string,
objectPatcher ObjectPatcher,
gvk schema.GroupVersionKind,
namespacedTranslator translator.NamespacedTranslator,
genericTranslator syncertypes.GenericTranslator,
replaceWhenInvalid bool,
) (syncertypes.Object, error) {
_, hasStatusSubresource, err := translate.EnsureCRDFromPhysicalCluster(
Expand All @@ -138,8 +138,8 @@ func BuildCustomExporter(
}

return &exporter{
ObjectPatcher: objectPatcher,
NamespacedTranslator: namespacedTranslator,
ObjectPatcher: objectPatcher,
GenericTranslator: genericTranslator,

patcher: NewPatcher(registerCtx.VirtualManager.GetClient(), registerCtx.PhysicalManager.GetClient(), hasStatusSubresource, log.New(controllerID)),
gvk: gvk,
Expand All @@ -152,7 +152,7 @@ func BuildCustomExporter(
var _ syncertypes.Syncer = &exporter{}

type exporter struct {
translator.NamespacedTranslator
syncertypes.GenericTranslator
ObjectPatcher

patcher *Patcher
Expand All @@ -169,6 +169,11 @@ func (f *exporter) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object)
return ctrl.Result{}, nil
}

// delete object if host was deleted
if ctx.IsDelete {
return syncer.DeleteVirtualObject(ctx, vObj, "host object was deleted")
}

// apply object to physical cluster
ctx.Log.Infof("Create physical %s %s/%s, since it is missing, but virtual object exists", f.gvk.Kind, vObj.GetNamespace(), vObj.GetName())
pObj, err := f.patcher.ApplyPatches(ctx, vObj, nil, f)
Expand Down Expand Up @@ -295,15 +300,15 @@ func (f *exporter) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj c
var _ syncertypes.ToVirtualSyncer = &exporter{}

func (f *exporter) SyncToVirtual(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
isManaged, err := f.NamespacedTranslator.IsManaged(ctx, pObj)
isManaged, err := f.GenericTranslator.IsManaged(ctx, pObj)
if err != nil {
return ctrl.Result{}, err
} else if !isManaged {
return ctrl.Result{}, nil
}

// delete physical object because virtual one is missing
return syncer.DeleteObject(ctx, pObj, fmt.Sprintf("delete physical %s because virtual is missing", pObj.GetName()))
return syncer.DeleteHostObject(ctx, pObj, fmt.Sprintf("delete physical %s because virtual is missing", pObj.GetName()))
}

func (f *exporter) Name() string {
Expand All @@ -312,7 +317,7 @@ func (f *exporter) Name() string {

// TranslateMetadata converts the virtual object into a physical object
func (f *exporter) TranslateMetadata(ctx context.Context, vObj client.Object) client.Object {
pObj := f.NamespacedTranslator.TranslateMetadata(ctx, vObj)
pObj := f.GenericTranslator.TranslateMetadata(ctx, vObj)
if pObj == nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/generic/import_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

vclusterconfig "github.com/loft-sh/vcluster/config"
"github.com/loft-sh/vcluster/pkg/config"
syncertypes "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/scheme"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/loft-sh/vcluster/pkg/controllers/syncer"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/log"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/clienthelper"
util "github.com/loft-sh/vcluster/pkg/util/context"
"github.com/loft-sh/vcluster/pkg/util/translate"
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *importer) Name() string {

var _ syncertypes.OptionsProvider = &importer{}

func (s *importer) WithOptions() *syncertypes.Options {
func (s *importer) Options() *syncertypes.Options {
return s.syncerOptions
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/generic/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/log"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/equality"
Expand All @@ -18,7 +18,7 @@ import (
var fieldManager = "vcluster-syncer"

type ObjectPatcherAndMetadataTranslator interface {
translator.MetadataTranslator
types.MetadataTranslator
ObjectPatcher
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/loft-sh/vcluster/pkg/controllers/generic"
"github.com/loft-sh/vcluster/pkg/controllers/servicesync"
"github.com/loft-sh/vcluster/pkg/controllers/syncer"
syncertypes "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/util/blockingcacheclient"
util "github.com/loft-sh/vcluster/pkg/util/context"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -22,7 +23,6 @@ import (
"github.com/loft-sh/vcluster/pkg/controllers/coredns"
"github.com/loft-sh/vcluster/pkg/controllers/k8sdefaultendpoint"
"github.com/loft-sh/vcluster/pkg/controllers/podsecurity"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/loghelper"
"github.com/pkg/errors"
)
Expand Down
17 changes: 11 additions & 6 deletions pkg/controllers/resources/configmaps/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"strings"

"github.com/loft-sh/vcluster/pkg/constants"
"github.com/loft-sh/vcluster/pkg/controllers/syncer"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
syncertypes "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/mappings"
syncer "github.com/loft-sh/vcluster/pkg/types"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -21,21 +22,21 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func New(ctx *synccontext.RegisterContext) (syncer.Object, error) {
func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
return &configMapSyncer{
NamespacedTranslator: translator.NewNamespacedTranslator(ctx, "configmap", &corev1.ConfigMap{}, mappings.ConfigMaps()),
GenericTranslator: translator.NewGenericTranslator(ctx, "configmap", &corev1.ConfigMap{}, mappings.ConfigMaps()),

syncAllConfigMaps: ctx.Config.Sync.ToHost.ConfigMaps.All,
}, nil
}

type configMapSyncer struct {
translator.NamespacedTranslator
syncertypes.GenericTranslator

syncAllConfigMaps bool
}

var _ syncer.IndicesRegisterer = &configMapSyncer{}
var _ syncertypes.IndicesRegisterer = &configMapSyncer{}

func (s *configMapSyncer) RegisterIndices(ctx *synccontext.RegisterContext) error {
// index pods by their used config maps
Expand All @@ -45,7 +46,7 @@ func (s *configMapSyncer) RegisterIndices(ctx *synccontext.RegisterContext) erro
})
}

var _ syncer.ControllerModifier = &configMapSyncer{}
var _ syncertypes.ControllerModifier = &configMapSyncer{}

func (s *configMapSyncer) ModifyController(_ *synccontext.RegisterContext, builder *builder.Builder) (*builder.Builder, error) {
return builder.Watches(&corev1.Pod{}, handler.EnqueueRequestsFromMapFunc(mapPods)), nil
Expand All @@ -59,6 +60,10 @@ func (s *configMapSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.O
return ctrl.Result{}, nil
}

if ctx.IsDelete {
return syncer.DeleteVirtualObject(ctx, vObj, "host object was deleted")
}

return s.SyncToHostCreate(ctx, vObj, s.translate(ctx, vObj.(*corev1.ConfigMap)))
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/resources/csidrivers/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
syncer "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
syncer "github.com/loft-sh/vcluster/pkg/types"
storagev1 "k8s.io/api/storage/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -21,7 +21,7 @@ func New(_ *synccontext.RegisterContext) (syncer.Object, error) {
}

type csidriverSyncer struct {
translator.Translator
syncer.Translator
}

var _ syncer.ToVirtualSyncer = &csidriverSyncer{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/resources/csinodes/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
syncertypes "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -25,7 +25,7 @@ func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
}

type csinodeSyncer struct {
translator.Translator
syncertypes.Translator
virtualClient client.Client
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/csistoragecapacities/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"

synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
syncertypes "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"testing"

"github.com/loft-sh/vcluster/pkg/config"
syncer "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
testingutil "github.com/loft-sh/vcluster/pkg/util/testing"
"github.com/loft-sh/vcluster/pkg/util/translate"

"github.com/loft-sh/vcluster/pkg/controllers/resources/storageclasses"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
syncer "github.com/loft-sh/vcluster/pkg/types"
"gotest.tools/assert"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"fmt"

syncer "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/mappings"
syncer "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/equality"
Expand Down
17 changes: 11 additions & 6 deletions pkg/controllers/resources/endpoints/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"errors"
"fmt"

"github.com/loft-sh/vcluster/pkg/controllers/syncer"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
syncertypes "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
"github.com/loft-sh/vcluster/pkg/specialservices"
syncer "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -19,17 +20,21 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func New(ctx *synccontext.RegisterContext) (syncer.Object, error) {
func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
return &endpointsSyncer{
NamespacedTranslator: translator.NewNamespacedTranslator(ctx, "endpoints", &corev1.Endpoints{}, mappings.Endpoints()),
GenericTranslator: translator.NewGenericTranslator(ctx, "endpoints", &corev1.Endpoints{}, mappings.Endpoints()),
}, nil
}

type endpointsSyncer struct {
translator.NamespacedTranslator
syncertypes.GenericTranslator
}

func (s *endpointsSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
if ctx.IsDelete {
return syncer.DeleteVirtualObject(ctx, vObj, "host object was deleted")
}

return s.SyncToHostCreate(ctx, vObj, s.translate(ctx, vObj))
}

Expand All @@ -44,7 +49,7 @@ func (s *endpointsSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object,
}

if retErr != nil {
s.NamespacedTranslator.EventRecorder().Eventf(pObj, "Warning", "SyncError", "Error syncing: %v", retErr)
s.EventRecorder().Eventf(vObj, "Warning", "SyncError", "Error syncing: %v", retErr)
}
}()

Expand All @@ -56,7 +61,7 @@ func (s *endpointsSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object,
return ctrl.Result{}, nil
}

var _ syncer.Starter = &endpointsSyncer{}
var _ syncertypes.Starter = &endpointsSyncer{}

func (s *endpointsSyncer) ReconcileStart(ctx *synccontext.SyncContext, req ctrl.Request) (bool, error) {
if req.NamespacedName == specialservices.DefaultKubernetesSvcKey {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/events/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"

synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
syncer "github.com/loft-sh/vcluster/pkg/controllers/syncer/types"
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/mappings/resources"
"github.com/loft-sh/vcluster/pkg/patcher"
syncer "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down
Loading

0 comments on commit b8f4b77

Please sign in to comment.