Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use new v0.21 vCluster version #110

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ concurrency:
cancel-in-progress: true

env:
VCLUSTER_VERSION: v0.20.1
VCLUSTER_VERSION: v0.21.0
VCLUSTER_SUFFIX: vcluster
VCLUSTER_NAME: vcluster
VCLUSTER_NAMESPACE: vcluster
Expand Down
22 changes: 11 additions & 11 deletions e2e/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var _ = ginkgo.Describe("Plugin test", func() {
var err error
var virtualDeployments *appsv1.DeploymentList
gomega.Eventually(func() int {
virtualDeployments, err = f.VclusterClient.AppsV1().Deployments("default").List(f.Context, metav1.ListOptions{})
virtualDeployments, err = f.VClusterClient.AppsV1().Deployments("default").List(f.Context, metav1.ListOptions{})
framework.ExpectNoError(err)
return len(virtualDeployments.Items)
}).
Expand All @@ -39,7 +39,7 @@ var _ = ginkgo.Describe("Plugin test", func() {
// wait for pod to become ready
var podList *corev1.PodList
gomega.Eventually(func() bool {
podList, err = f.VclusterClient.CoreV1().Pods("default").List(f.Context, metav1.ListOptions{})
podList, err = f.VClusterClient.CoreV1().Pods("default").List(f.Context, metav1.ListOptions{})
framework.ExpectNoError(err)
return len(podList.Items) == 1 && podList.Items[0].Status.Phase == corev1.PodRunning
}).
Expand All @@ -50,7 +50,7 @@ var _ = ginkgo.Describe("Plugin test", func() {
// get pod in host cluster
pod := &podList.Items[0]
hostPod := &corev1.Pod{}
err = f.HostCRClient.Get(f.Context, types.NamespacedName{Name: translate.Default.PhysicalName(pod.Name, pod.Namespace), Namespace: translate.Default.PhysicalNamespace(pod.Namespace)}, hostPod)
err = f.HostCRClient.Get(f.Context, translate.Default.HostName(nil, pod.Name, pod.Namespace), hostPod)
framework.ExpectNoError(err)

// check if hook worked
Expand All @@ -71,7 +71,7 @@ var _ = ginkgo.Describe("Plugin test", func() {

// create car in vcluster
gomega.Eventually(func() bool {
err := f.VclusterCRClient.Create(f.Context, car)
err := f.VClusterCRClient.Create(f.Context, car)
return err == nil
}).
WithPolling(pollingInterval).
Expand All @@ -80,7 +80,7 @@ var _ = ginkgo.Describe("Plugin test", func() {

// wait for car to become synced
hostCar := &examplev1.Car{}
carName := types.NamespacedName{Name: translate.Default.PhysicalName(car.Name, car.Namespace), Namespace: translate.Default.PhysicalNamespace(car.Namespace)}
carName := translate.Default.HostName(nil, car.Name, car.Namespace)
gomega.Eventually(func() bool {
err := f.HostCRClient.Get(f.Context, carName, hostCar)
return err == nil
Expand Down Expand Up @@ -113,13 +113,13 @@ var _ = ginkgo.Describe("Plugin test", func() {
}

// create service
err := f.VclusterCRClient.Create(f.Context, service)
err := f.VClusterCRClient.Create(f.Context, service)
framework.ExpectNoError(err)

// wait for service to become synced
hostService := &corev1.Service{}
gomega.Eventually(func() bool {
err := f.HostCRClient.Get(f.Context, types.NamespacedName{Name: translate.Default.PhysicalName(service.Name, service.Namespace), Namespace: f.VclusterNamespace}, hostService)
err := f.HostCRClient.Get(f.Context, translate.Default.HostName(nil, service.Name, service.Namespace), hostService)
return err == nil
}).
WithPolling(pollingInterval).
Expand Down Expand Up @@ -154,7 +154,7 @@ var _ = ginkgo.Describe("Plugin test", func() {
// wait for secret to become synced
vSecret := &corev1.Secret{}
gomega.Eventually(func() bool {
err := f.VclusterCRClient.Get(f.Context, types.NamespacedName{Name: "test", Namespace: "test"}, vSecret)
err := f.VClusterCRClient.Get(f.Context, types.NamespacedName{Name: "test", Namespace: "test"}, vSecret)
return err == nil
}).
WithPolling(pollingInterval).
Expand All @@ -172,7 +172,7 @@ var _ = ginkgo.Describe("Plugin test", func() {

// wait for update
gomega.Eventually(func() bool {
err := f.VclusterCRClient.Get(f.Context, types.NamespacedName{Name: "test", Namespace: "test"}, vSecret)
err := f.VClusterCRClient.Get(f.Context, types.NamespacedName{Name: "test", Namespace: "test"}, vSecret)
return err == nil && string(vSecret.Data["test"]) == "newtest"
}).
WithPolling(pollingInterval).
Expand All @@ -185,7 +185,7 @@ var _ = ginkgo.Describe("Plugin test", func() {

// wait for delete within vCluster
gomega.Eventually(func() bool {
err := f.VclusterCRClient.Get(f.Context, types.NamespacedName{Name: "test", Namespace: "test"}, vSecret)
err := f.VClusterCRClient.Get(f.Context, types.NamespacedName{Name: "test", Namespace: "test"}, vSecret)
return kerrors.IsNotFound(err)
}).
WithPolling(pollingInterval).
Expand All @@ -196,7 +196,7 @@ var _ = ginkgo.Describe("Plugin test", func() {
ginkgo.It("check the interceptor", func() {
// wait for secret to become synced
vPod := &corev1.Pod{}
err := f.VclusterCRClient.Get(f.Context, types.NamespacedName{Name: "stuff", Namespace: "test"}, vPod)
err := f.VClusterCRClient.Get(f.Context, types.NamespacedName{Name: "stuff", Namespace: "test"}, vPod)
framework.ExpectNoError(err)

// check if secret is synced correctly
Expand Down
19 changes: 16 additions & 3 deletions e2e/test_plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
examplev1 "github.com/loft-sh/vcluster-sdk/e2e/test_plugin/apis/v1"
"github.com/loft-sh/vcluster-sdk/e2e/test_plugin/syncers"
"github.com/loft-sh/vcluster-sdk/plugin"
"github.com/loft-sh/vcluster/pkg/mappings/resources"
"github.com/loft-sh/vcluster/pkg/scheme"
"k8s.io/klog/v2"
)
Expand All @@ -20,7 +21,11 @@ type PluginConfig struct {

func main() {
_ = examplev1.AddToScheme(scheme.Scheme)
ctx := plugin.MustInit()
ctx := plugin.MustInitWithOptions(plugin.Options{
RegisterMappings: []resources.BuildMapper{
resources.CreateSecretsMapper,
},
})
err := validateConfig()
if err != nil {
klog.Fatalf("validate config: %v", err)
Expand All @@ -29,8 +34,16 @@ func main() {
plugin.MustRegister(syncers.NewPodHook())
plugin.MustRegister(syncers.NewSecretHook())
plugin.MustRegister(syncers.NewMyDeploymentSyncer(ctx))
plugin.MustRegister(syncers.NewCarSyncer(ctx))
plugin.MustRegister(syncers.NewImportSecrets(ctx))
carSyncer, err := syncers.NewCarSyncer(ctx)
if err != nil {
klog.Fatalf("new car syncer: %v", err)
}
plugin.MustRegister(carSyncer)
importSecretSyncer, err := syncers.NewImportSecrets(ctx)
if err != nil {
klog.Fatalf("import secret syncer: %w", err)
}
plugin.MustRegister(importSecretSyncer)
plugin.MustRegister(syncers.DummyInterceptor{})

klog.Info("finished registering the plugins")
Expand Down
105 changes: 61 additions & 44 deletions e2e/test_plugin/syncers/car.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,91 @@
package syncers

import (
"context"
"os"
_ "embed"
"fmt"

examplev1 "github.com/loft-sh/vcluster-sdk/e2e/test_plugin/apis/v1"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
synctypes "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/mappings/generic"
"github.com/loft-sh/vcluster/pkg/patcher"
"github.com/loft-sh/vcluster/pkg/syncer"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
"github.com/loft-sh/vcluster/pkg/syncer/translator"
syncertypes "github.com/loft-sh/vcluster/pkg/syncer/types"
"github.com/loft-sh/vcluster/pkg/util"
"github.com/loft-sh/vcluster/pkg/util/translate"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func NewCarSyncer(ctx *synccontext.RegisterContext) synctypes.Base {
return &carSyncer{
NamespacedTranslator: translator.NewNamespacedTranslator(ctx, "car", &examplev1.Car{}),
}
}

type carSyncer struct {
translator.NamespacedTranslator
}
//go:embed car.crd.yaml
var carCRD string

var _ synctypes.Initializer = &carSyncer{}
func NewCarSyncer(ctx *synccontext.RegisterContext) (syncertypes.Base, error) {
err := util.EnsureCRD(ctx.Context, ctx.PhysicalManager.GetConfig(), []byte(carCRD), examplev1.SchemeGroupVersion.WithKind("Car"))
if err != nil {
return nil, err
}

func (s *carSyncer) Init(ctx *synccontext.RegisterContext) error {
out, err := os.ReadFile("manifests/crds.yaml")
err = util.EnsureCRD(ctx.Context, ctx.VirtualManager.GetConfig(), []byte(carCRD), examplev1.SchemeGroupVersion.WithKind("Car"))
if err != nil {
return err
return nil, err
}

gvk := examplev1.GroupVersion.WithKind("Car")
err = util.EnsureCRD(ctx.Context, ctx.PhysicalManager.GetConfig(), out, gvk)
mapper, err := generic.NewMapper(ctx, &examplev1.Car{}, translate.Default.HostName)
if err != nil {
return err
return nil, err
}

_, _, err = translate.EnsureCRDFromPhysicalCluster(ctx.Context, ctx.PhysicalManager.GetConfig(), ctx.VirtualManager.GetConfig(), gvk)
return err
return &carSyncer{
GenericTranslator: translator.NewGenericTranslator(ctx, "car", &examplev1.Car{}, mapper),
}, nil
}

var _ synctypes.Syncer = &carSyncer{}

func (s *carSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Object) (ctrl.Result, error) {
return s.SyncToHostCreate(ctx, vObj, s.TranslateMetadata(ctx.Context, vObj).(*examplev1.Car))
type carSyncer struct {
syncertypes.GenericTranslator
}

func (s *carSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (ctrl.Result, error) {
return s.SyncToHostUpdate(ctx, vObj, s.translateUpdate(ctx.Context, pObj.(*examplev1.Car), vObj.(*examplev1.Car)))
var _ syncertypes.Syncer = &carSyncer{}

func (s *carSyncer) Syncer() syncertypes.Sync[client.Object] {
return syncer.ToGenericSyncer[*examplev1.Car](s)
}

func (s *carSyncer) translateUpdate(ctx context.Context, pObj, vObj *examplev1.Car) *examplev1.Car {
var updated *examplev1.Car
func (s *carSyncer) SyncToHost(ctx *synccontext.SyncContext, event *synccontext.SyncToHostEvent[*examplev1.Car]) (ctrl.Result, error) {
pObj := translate.HostMetadata(event.Virtual, s.VirtualToHost(ctx, types.NamespacedName{Name: event.Virtual.Name, Namespace: event.Virtual.Namespace}, event.Virtual))
return patcher.CreateHostObject(ctx, event.Virtual, pObj, s.EventRecorder(), true)
}

// check annotations & labels
changed, updatedAnnotations, updatedLabels := s.TranslateMetadataUpdate(ctx, vObj, pObj)
if changed {
updated = translator.NewIfNil(updated, pObj)
updated.Labels = updatedLabels
updated.Annotations = updatedAnnotations
func (s *carSyncer) Sync(ctx *synccontext.SyncContext, event *synccontext.SyncEvent[*examplev1.Car]) (_ ctrl.Result, retErr error) {
patchHelper, err := patcher.NewSyncerPatcher(ctx, event.Host, event.Virtual)
if err != nil {
return ctrl.Result{}, fmt.Errorf("new syncer patcher: %w", err)
}

// check spec
if !equality.Semantic.DeepEqual(vObj.Spec, pObj.Spec) {
updated = translator.NewIfNil(updated, pObj)
updated.Spec = vObj.Spec
}
defer func() {
if err := patchHelper.Patch(ctx, event.Host, event.Virtual); err != nil {
retErr = errors.NewAggregate([]error{retErr, err})
}
if retErr != nil {
s.EventRecorder().Eventf(event.Virtual, "Warning", "SyncError", "Error syncing: %v", retErr)
}
}()

// any changes made below here are correctly synced

// sync metadata
event.Host.Annotations = translate.HostAnnotations(event.Virtual, event.Host)
event.Host.Labels = translate.HostLabels(event.Virtual, event.Host)

// sync virtual to host
event.Host.Spec = event.Virtual.Spec

return ctrl.Result{}, nil
}

return updated
func (s *carSyncer) SyncToVirtual(ctx *synccontext.SyncContext, event *synccontext.SyncToVirtualEvent[*examplev1.Car]) (ctrl.Result, error) {
// virtual object is not here anymore, so we delete
return patcher.DeleteHostObject(ctx, event.Host, nil, "virtual object was deleted")
}
Loading
Loading