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

added logic for patcher snapshot class #1962

Merged
merged 1 commit into from
Jul 19, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package volumesnapshotclasses

import (
"fmt"

"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"
"github.com/loft-sh/vcluster/pkg/util"
utilerrors "k8s.io/apimachinery/pkg/util/errors"

volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
Expand Down Expand Up @@ -51,16 +55,19 @@ func (s *volumeSnapshotClassSyncer) SyncToHost(ctx *synccontext.SyncContext, vOb
return ctrl.Result{}, nil
}

func (s *volumeSnapshotClassSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (ctrl.Result, error) {
updated := s.translateUpdateBackwards(ctx, pObj.(*volumesnapshotv1.VolumeSnapshotClass), vObj.(*volumesnapshotv1.VolumeSnapshotClass))
if updated != nil {
ctx.Log.Infof("updating virtual VolumeSnapshotClass %s, because it differs from the physical one", updated.Name)
translator.PrintChanges(vObj, updated, ctx.Log)
err := ctx.VirtualClient.Update(ctx, updated)
if err != nil {
return ctrl.Result{}, err
}
func (s *volumeSnapshotClassSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (_ ctrl.Result, retErr error) {
patch, err := patcher.NewSyncerPatcher(ctx, pObj, vObj)
if err != nil {
return ctrl.Result{}, fmt.Errorf("new syncer patcher: %w", err)
}

defer func() {
if err := patch.Patch(ctx, pObj, vObj); err != nil {
retErr = utilerrors.NewAggregate([]error{retErr, err})
}
}()

s.translateUpdateBackwards(ctx, pObj.(*volumesnapshotv1.VolumeSnapshotClass), vObj.(*volumesnapshotv1.VolumeSnapshotClass))

return ctrl.Result{}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,29 @@ import (
"context"

volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
"k8s.io/apimachinery/pkg/api/equality"
)

func (s *volumeSnapshotClassSyncer) translateBackwards(ctx context.Context, pVSC *volumesnapshotv1.VolumeSnapshotClass) *volumesnapshotv1.VolumeSnapshotClass {
return s.TranslateMetadata(ctx, pVSC).(*volumesnapshotv1.VolumeSnapshotClass)
}

func (s *volumeSnapshotClassSyncer) translateUpdateBackwards(ctx context.Context, pVSC *volumesnapshotv1.VolumeSnapshotClass, vVSC *volumesnapshotv1.VolumeSnapshotClass) *volumesnapshotv1.VolumeSnapshotClass {
var updated *volumesnapshotv1.VolumeSnapshotClass

func (s *volumeSnapshotClassSyncer) translateUpdateBackwards(ctx context.Context, pVSC *volumesnapshotv1.VolumeSnapshotClass, vVSC *volumesnapshotv1.VolumeSnapshotClass) {
changed, updatedAnnotations, updatedLabels := s.TranslateMetadataUpdate(ctx, vVSC, pVSC)
if changed {
updated = translator.NewIfNil(updated, vVSC)
updated.Labels = updatedLabels
updated.Annotations = updatedAnnotations
vVSC.Labels = updatedLabels
vVSC.Annotations = updatedAnnotations
}

if !equality.Semantic.DeepEqual(vVSC.Driver, pVSC.Driver) {
updated = translator.NewIfNil(updated, vVSC)
updated.Driver = pVSC.Driver
vVSC.Driver = pVSC.Driver
}

if !equality.Semantic.DeepEqual(vVSC.Parameters, pVSC.Parameters) {
updated = translator.NewIfNil(updated, vVSC)
updated.Parameters = pVSC.Parameters
vVSC.Parameters = pVSC.Parameters
}

if !equality.Semantic.DeepEqual(vVSC.DeletionPolicy, pVSC.DeletionPolicy) {
updated = translator.NewIfNil(updated, vVSC)
updated.DeletionPolicy = pVSC.DeletionPolicy
vVSC.DeletionPolicy = pVSC.DeletionPolicy
}

return updated
}
Loading