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 patcher logic for pdb #1947

Merged
merged 1 commit into from
Jul 18, 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
26 changes: 21 additions & 5 deletions pkg/controllers/resources/poddisruptionbudgets/syncer.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package poddisruptionbudgets

import (
"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"
policyv1 "k8s.io/api/policy/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -29,13 +33,25 @@ func (pdb *pdbSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.Objec
return pdb.SyncToHostCreate(ctx, vObj, pdb.translate(ctx, vObj.(*policyv1.PodDisruptionBudget)))
}

func (pdb *pdbSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (ctrl.Result, error) {
func (pdb *pdbSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (_ ctrl.Result, retErr error) {
vPDB := vObj.(*policyv1.PodDisruptionBudget)
pPDB := pObj.(*policyv1.PodDisruptionBudget)
newPDB := pdb.translateUpdate(ctx, pPDB, vPDB)
if newPDB != nil {
translator.PrintChanges(pObj, newPDB, ctx.Log)

patch, err := patcher.NewSyncerPatcher(ctx, pPDB, vPDB)
if err != nil {
return ctrl.Result{}, fmt.Errorf("new syncer patcher: %w", err)
}

return pdb.SyncToHostUpdate(ctx, vObj, newPDB)
defer func() {
if err := patch.Patch(ctx, pPDB, vPDB); err != nil {
retErr = utilerrors.NewAggregate([]error{retErr, err})
}
if retErr != nil {
pdb.EventRecorder().Eventf(vObj, "Warning", "SyncError", "Error syncing: %v", retErr)
}
}()

pdb.translateUpdate(ctx.Context, pPDB, vPDB)

return ctrl.Result{}, nil
}
6 changes: 3 additions & 3 deletions pkg/controllers/resources/poddisruptionbudgets/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*pdbSyncer).SyncToHost(syncCtx, vclusterPDB)
_, err := syncer.(*pdbSyncer).SyncToHost(syncCtx, vclusterPDB.DeepCopy())
assert.NilError(t, err)
},
},
Expand All @@ -113,7 +113,7 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*pdbSyncer).Sync(syncCtx, hostClusterSyncedPDB, vclusterUpdatedPDB)
_, err := syncer.(*pdbSyncer).Sync(syncCtx, hostClusterSyncedPDB.DeepCopy(), vclusterUpdatedPDB.DeepCopy())
assert.NilError(t, err)
},
},
Expand All @@ -133,7 +133,7 @@ func TestSync(t *testing.T) {
},
Sync: func(ctx *synccontext.RegisterContext) {
syncCtx, syncer := generictesting.FakeStartSyncer(t, ctx, New)
_, err := syncer.(*pdbSyncer).Sync(syncCtx, hostClusterSyncedPDB, vclusterUpdatedSelectorPDB)
_, err := syncer.(*pdbSyncer).Sync(syncCtx, hostClusterSyncedPDB.DeepCopy(), vclusterUpdatedSelectorPDB.DeepCopy())
assert.NilError(t, err)
},
},
Expand Down
32 changes: 7 additions & 25 deletions pkg/controllers/resources/poddisruptionbudgets/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package poddisruptionbudgets
import (
"context"

"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
"github.com/loft-sh/vcluster/pkg/util/translate"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/equality"
)

func (pdb *pdbSyncer) translate(ctx context.Context, vObj *policyv1.PodDisruptionBudget) *policyv1.PodDisruptionBudget {
Expand All @@ -17,31 +15,15 @@ func (pdb *pdbSyncer) translate(ctx context.Context, vObj *policyv1.PodDisruptio
return newPDB
}

func (pdb *pdbSyncer) translateUpdate(ctx context.Context, pObj, vObj *policyv1.PodDisruptionBudget) *policyv1.PodDisruptionBudget {
var updated *policyv1.PodDisruptionBudget

// check max available and min available in spec
if !equality.Semantic.DeepEqual(vObj.Spec.MaxUnavailable, pObj.Spec.MaxUnavailable) ||
!equality.Semantic.DeepEqual(vObj.Spec.MinAvailable, pObj.Spec.MinAvailable) {
updated = translator.NewIfNil(updated, pObj)
updated.Spec.MaxUnavailable = vObj.Spec.MaxUnavailable
updated.Spec.MinAvailable = vObj.Spec.MinAvailable
}
func (pdb *pdbSyncer) translateUpdate(ctx context.Context, pObj, vObj *policyv1.PodDisruptionBudget) {
pObj.Spec.MaxUnavailable = vObj.Spec.MaxUnavailable
pObj.Spec.MinAvailable = vObj.Spec.MinAvailable

// check annotations
changed, updatedAnnotations, updatedLabels := pdb.TranslateMetadataUpdate(ctx, vObj, pObj)
if changed {
updated = translator.NewIfNil(updated, pObj)
updated.Annotations = updatedAnnotations
updated.Labels = updatedLabels
}
_, updatedAnnotations, updatedLabels := pdb.TranslateMetadataUpdate(ctx, vObj, pObj)
pObj.Annotations = updatedAnnotations
pObj.Labels = updatedLabels

// check LabelSelector
vObjLabelSelector := translate.Default.TranslateLabelSelector(vObj.Spec.Selector)
if !equality.Semantic.DeepEqual(vObjLabelSelector, pObj.Spec.Selector) {
updated = translator.NewIfNil(updated, pObj)
updated.Spec.Selector = vObjLabelSelector
}

return updated
pObj.Spec.Selector = translate.Default.TranslateLabelSelector(vObj.Spec.Selector)
}
Loading