Skip to content

Commit

Permalink
Merge pull request #1507 from FabianKramm/main
Browse files Browse the repository at this point in the history
refactor: syncup & syncdown naming
  • Loading branch information
FabianKramm authored Feb 1, 2024
2 parents 0084a2b + 793604a commit f69e2ea
Show file tree
Hide file tree
Showing 63 changed files with 172 additions and 172 deletions.
8 changes: 4 additions & 4 deletions docs/pages/advanced-topics/plugins-development.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {

Let's break down what is happening in the `main()` function above.

`ctx := plugin.MustInit("sync-all-configmaps-plugin")` - SDK will contact the vCluster backend server and retrieve it's configuration. The returned struct of type [`RegisterContext`](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/syncer/context#RegisterContext) contains information about vCluster flags, namespace, vCluster client config, controller manager objects, etc.
`ctx := plugin.MustInit("sync-all-configmaps-plugin")` - SDK will contact the vCluster backend server and retrieve it's configuration. The returned struct of type [`RegisterContext`](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/syncer/context#RegisterContext) contains information about vCluster flags, namespace, vCluster client config, controller manager objects, etc.

`plugin.MustRegister(syncers.NewConfigMapSyncer(ctx))` - we will implement the `NewConfigMapSyncer` function below, but for now, all we need to know is that it should return a struct that implements [`Base`](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/syncer#Base) interface, which is accepted by the [`MustRegister`](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/plugin#MustRegister) function. We should call [`MustRegister`](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/plugin#MustRegister) function for each syncer that we wish to be managed by the plugins controller manager.

Expand Down Expand Up @@ -77,15 +77,15 @@ type configMapSyncer struct {

After an import block, we see the `NewConfigMapSyncer` function, which is being called from the `main.go`. It returns a new instance of the `configMapSyncer` struct, which is defined by a single nested anonymous struct of type [`NamespacedTranslator`](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/syncer/translator#NamespacedTranslator). The [`NamespacedTranslator`](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/syncer/translator#NamespacedTranslator) implements many functions of the [`Syncer`](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/syncer#Syncer) interface for us, and we will implement the remaining ones - `SyncDown` and `Sync`.

:::info
:::info
You can get more familiar with the interfaces mentioned above by reading the SDK source files on GitHub - [vcluster-sdk/syncer/types.go](https://github.com/loft-sh/vcluster-sdk/blob/main/syncer/types.go) and [vcluster-sdk/syncer/translator/translator.go](https://github.com/loft-sh/vcluster-sdk/blob/main/syncer/translator/translator.go), or by using pkg.go.dev website - [Syncer](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/syncer#Syncer) and [NamespacedTranslator](https://pkg.go.dev/github.com/loft-sh/vcluster-sdk/syncer/translator#NamespacedTranslator).
:::


The `SyncDown` function mentioned above is called by the vCluster SDK when a given resource, e.g. a ConfigMap, is created in the vCluster, but it doesn't exist in the host cluster yet. To create a ConfigMap in the host cluster we will call the `SyncDownCreate` function with the output of the `translate` function as a third parameter. This demonstrates a typical pattern used in the vCluster syncer implementations.

```
func (s *configMapSyncer) SyncDown(ctx *syncercontext.syncercontext, vObj client.Object) (ctrl.Result, error) {
func (s *configMapSyncer) SyncToHost(ctx *syncercontext.syncercontext, vObj client.Object) (ctrl.Result, error) {
return s.SyncDownCreate(ctx, vObj, s.translate(vObj.(*corev1.ConfigMap)))
}
Expand Down Expand Up @@ -298,7 +298,7 @@ You can now change a file locally in your IDE and then restart the command in th

DevSpace will create a development vCluster which will execute your plugin. Any changes made within the vCluster created by DevSpace will execute against your plugin.
```
vcluster list
vcluster list
NAME NAMESPACE STATUS CONNECTED CREATED AGE
vcluster vcluster Running True 2022-09-06 20:33:20 +1000 AEST 2h26m8s
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/plugins/tutorial.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ After an import block, we see the `NewCarSyncer` function, which is being called
The `SyncDown` function mentioned above is called by the vCluster SDK when a given resource, e.g. a Car, is created in the vCluster, but it doesn't exist in the host cluster yet. To create a ConfigMap in the host cluster we will call the `SyncDownCreate` function with the output of the `translate` function as third parameter. This demonstrates a typical pattern used in the vCluster syncer implementations.

```
func (s *carSyncer) SyncDown(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
func (s *carSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
return s.SyncDownCreate(ctx, vObj, s.TranslateMetadata(ctx.Context, vObj).(*examplev1.Car))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/generic/export_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type exporter struct {
name string
}

func (f *exporter) SyncDown(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
func (f *exporter) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
// check if selector matches
if !f.objectMatches(vObj) {
return ctrl.Result{}, nil
Expand Down Expand Up @@ -261,9 +261,9 @@ func (f *exporter) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj c
return ctrl.Result{}, nil
}

var _ syncertypes.UpSyncer = &exporter{}
var _ syncertypes.ToVirtualSyncer = &exporter{}

func (f *exporter) SyncUp(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
func (f *exporter) SyncToVirtual(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
if !translate.Default.IsManaged(pObj) {
return ctrl.Result{}, nil
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/controllers/generic/import_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func (s *importer) excludeObject(obj client.Object) bool {
return false
}

var _ syncertypes.UpSyncer = &importer{}
var _ syncertypes.ToVirtualSyncer = &importer{}

func (s *importer) SyncUp(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
func (s *importer) SyncToVirtual(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
// check if annotation is already present
if pObj.GetAnnotations() != nil {
if pObj.GetAnnotations()[translate.ControllerLabel] == s.Name() &&
Expand Down Expand Up @@ -234,7 +234,7 @@ func (s *importer) SyncUp(ctx *synccontext.SyncContext, pObj client.Object) (ctr

var _ syncertypes.Syncer = &importer{}

func (s *importer) SyncDown(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
func (s *importer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
// ignore all virtual resources that were not created by this controller
if !s.IsVirtualManaged(vObj) {
return ctrl.Result{}, nil
Expand Down Expand Up @@ -354,11 +354,11 @@ func (s *importer) IsVirtualManaged(vObj client.Object) bool {
return vObj.GetAnnotations() != nil && vObj.GetAnnotations()[translate.ControllerLabel] != "" && vObj.GetAnnotations()[translate.ControllerLabel] == s.Name()
}

func (s *importer) VirtualToPhysical(_ context.Context, req types.NamespacedName, _ client.Object) types.NamespacedName {
func (s *importer) VirtualToHost(_ context.Context, req types.NamespacedName, _ client.Object) types.NamespacedName {
return types.NamespacedName{Name: translate.Default.PhysicalName(req.Name, req.Namespace), Namespace: translate.Default.PhysicalNamespace(req.Namespace)}
}

func (s *importer) PhysicalToVirtual(ctx context.Context, req types.NamespacedName, _ client.Object) types.NamespacedName {
func (s *importer) HostToVirtual(ctx context.Context, req types.NamespacedName, _ client.Object) types.NamespacedName {
if s.syncerOptions.IsClusterScopedCRD {
return types.NamespacedName{
Name: req.Name,
Expand All @@ -382,7 +382,7 @@ func (s *importer) TranslateMetadata(ctx context.Context, pObj client.Object) cl
vObj.SetOwnerReferences(nil)
vObj.SetFinalizers(nil)
vObj.SetAnnotations(s.updateVirtualAnnotations(vObj.GetAnnotations()))
nn := s.PhysicalToVirtual(ctx, types.NamespacedName{Name: pObj.GetName(), Namespace: pObj.GetNamespace()}, pObj)
nn := s.HostToVirtual(ctx, types.NamespacedName{Name: pObj.GetName(), Namespace: pObj.GetNamespace()}, pObj)
vObj.SetName(nn.Name)
vObj.SetNamespace(nn.Namespace)

Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/resources/configmaps/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *configMapSyncer) ModifyController(_ *synccontext.RegisterContext, build
return builder.Watches(&corev1.Pod{}, handler.EnqueueRequestsFromMapFunc(mapPods)), nil
}

func (s *configMapSyncer) SyncDown(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
func (s *configMapSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
createNeeded, err := s.isConfigMapUsed(ctx, vObj)
if err != nil {
return ctrl.Result{}, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/resources/configmaps/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*configMapSyncer).SyncDown(syncCtx, baseConfigMap)
_, err := syncer.(*configMapSyncer).SyncToHost(syncCtx, baseConfigMap)
assert.NilError(t, err)
},
},
Expand All @@ -94,7 +94,7 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*configMapSyncer).SyncDown(syncCtx, baseConfigMap)
_, err := syncer.(*configMapSyncer).SyncToHost(syncCtx, baseConfigMap)
assert.NilError(t, err)
},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/resources/csidrivers/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type csidriverSyncer struct {
translator.Translator
}

var _ syncer.UpSyncer = &csidriverSyncer{}
var _ syncer.ToVirtualSyncer = &csidriverSyncer{}
var _ syncer.Syncer = &csidriverSyncer{}

func (s *csidriverSyncer) SyncUp(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
func (s *csidriverSyncer) SyncToVirtual(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
vObj := s.translateBackwards(ctx.Context, pObj.(*storagev1.CSIDriver))
ctx.Log.Infof("create CSIDriver %s, because it does not exist in virtual cluster", vObj.Name)
return ctrl.Result{}, ctx.VirtualClient.Create(ctx.Context, vObj)
Expand All @@ -40,7 +40,7 @@ func (s *csidriverSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object,
return ctrl.Result{}, nil
}

func (s *csidriverSyncer) SyncDown(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
func (s *csidriverSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
ctx.Log.Infof("delete virtual CSIDriver %s, because physical object is missing", vObj.GetName())
return ctrl.Result{}, ctx.VirtualClient.Delete(ctx.Context, vObj)
}
4 changes: 2 additions & 2 deletions pkg/controllers/resources/csidrivers/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*csidriverSyncer).SyncUp(syncCtx, pObj)
_, err := syncer.(*csidriverSyncer).SyncToVirtual(syncCtx, pObj)
assert.NilError(t, err)
},
},
Expand All @@ -115,7 +115,7 @@ func TestSync(t *testing.T) {
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*csidriverSyncer).SyncDown(syncCtx, vObj)
_, err := syncer.(*csidriverSyncer).SyncToHost(syncCtx, vObj)
assert.NilError(t, err)
},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/resources/csinodes/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type csinodeSyncer struct {
virtualClient client.Client
}

var _ syncertypes.UpSyncer = &csinodeSyncer{}
var _ syncertypes.ToVirtualSyncer = &csinodeSyncer{}
var _ syncertypes.Syncer = &csinodeSyncer{}

func (s *csinodeSyncer) SyncUp(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
func (s *csinodeSyncer) SyncToVirtual(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
// look up matching node name, don't sync if not found
node := &corev1.Node{}
err := s.virtualClient.Get(ctx.Context, types.NamespacedName{Name: pObj.GetName()}, node)
Expand Down Expand Up @@ -62,7 +62,7 @@ func (s *csinodeSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, v
return ctrl.Result{}, nil
}

func (s *csinodeSyncer) SyncDown(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
func (s *csinodeSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
ctx.Log.Infof("delete virtual CSINode %s, because physical object is missing", vObj.GetName())
return ctrl.Result{}, ctx.VirtualClient.Delete(ctx.Context, vObj)
}
4 changes: 2 additions & 2 deletions pkg/controllers/resources/csinodes/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*csinodeSyncer).SyncUp(syncCtx, pObj)
_, err := syncer.(*csinodeSyncer).SyncToVirtual(syncCtx, pObj)
assert.NilError(t, err)
},
},
Expand All @@ -118,7 +118,7 @@ func TestSync(t *testing.T) {
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*csinodeSyncer).SyncDown(syncCtx, vObj)
_, err := syncer.(*csinodeSyncer).SyncToHost(syncCtx, vObj)
assert.NilError(t, err)
},
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/resources/csistoragecapacities/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ type csistoragecapacitySyncer struct {
physicalClient client.Client
}

var _ syncertypes.UpSyncer = &csistoragecapacitySyncer{}
var _ syncertypes.ToVirtualSyncer = &csistoragecapacitySyncer{}
var _ syncertypes.Syncer = &csistoragecapacitySyncer{}

func (s *csistoragecapacitySyncer) SyncUp(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
func (s *csistoragecapacitySyncer) SyncToVirtual(ctx *synccontext.SyncContext, pObj client.Object) (ctrl.Result, error) {
vObj, shouldSkip, err := s.translateBackwards(ctx, pObj.(*storagev1.CSIStorageCapacity))
if err != nil || shouldSkip {
return ctrl.Result{}, err
Expand Down Expand Up @@ -67,7 +67,7 @@ func (s *csistoragecapacitySyncer) Sync(ctx *synccontext.SyncContext, pObj clien
return ctrl.Result{}, nil
}

func (s *csistoragecapacitySyncer) SyncDown(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
func (s *csistoragecapacitySyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
ctx.Log.Infof("delete virtual CSIStorageCapacity %s, because physical object is missing", vObj.GetName())
return ctrl.Result{}, ctx.VirtualClient.Delete(ctx.Context, vObj)
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func (s *csistoragecapacitySyncer) enqueuePhysical(ctx context.Context, obj clie
return
}

name := s.PhysicalToVirtual(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
name := s.HostToVirtual(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj)
if name.Name != "" && name.Namespace != "" {
q.Add(reconcile.Request{NamespacedName: name})
}
Expand Down
Loading

0 comments on commit f69e2ea

Please sign in to comment.