-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from FabianKramm/main
refactor: use new v0.21 vCluster version
- Loading branch information
Showing
2,938 changed files
with
154,324 additions
and
76,518 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.