From c744679d626a79e552972331d0b3a7296a0a2cc9 Mon Sep 17 00:00:00 2001 From: facchettos Date: Fri, 19 Jul 2024 11:21:16 +0200 Subject: [PATCH] added patcher logic for network policies --- .../resources/networkpolicies/syncer.go | 25 +++++++++++++++---- .../resources/networkpolicies/translate.go | 20 ++++----------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/pkg/controllers/resources/networkpolicies/syncer.go b/pkg/controllers/resources/networkpolicies/syncer.go index 8688389ba..b239a27aa 100644 --- a/pkg/controllers/resources/networkpolicies/syncer.go +++ b/pkg/controllers/resources/networkpolicies/syncer.go @@ -1,12 +1,16 @@ package networkpolicies 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" networkingv1 "k8s.io/api/networking/v1" + utilerrors "k8s.io/apimachinery/pkg/util/errors" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -31,11 +35,22 @@ func (s *networkPolicySyncer) SyncToHost(ctx *synccontext.SyncContext, vObj clie return s.SyncToHostCreate(ctx, vObj, s.translate(ctx, vObj.(*networkingv1.NetworkPolicy))) } -func (s *networkPolicySyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (ctrl.Result, error) { - newNetworkPolicy := s.translateUpdate(ctx, pObj.(*networkingv1.NetworkPolicy), vObj.(*networkingv1.NetworkPolicy)) - if newNetworkPolicy != nil { - translator.PrintChanges(pObj, newNetworkPolicy, ctx.Log) +func (s *networkPolicySyncer) 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) } - return s.SyncToHostUpdate(ctx, vObj, newNetworkPolicy) + defer func() { + if err := patch.Patch(ctx, pObj, vObj); err != nil { + retErr = utilerrors.NewAggregate([]error{retErr, err}) + } + if retErr != nil { + s.EventRecorder().Eventf(vObj, "Warning", "SyncError", "Error syncing: %v", retErr) + } + }() + + s.translateUpdate(ctx, pObj.(*networkingv1.NetworkPolicy), vObj.(*networkingv1.NetworkPolicy)) + + return ctrl.Result{}, nil } diff --git a/pkg/controllers/resources/networkpolicies/translate.go b/pkg/controllers/resources/networkpolicies/translate.go index ade1e9779..c061d5179 100644 --- a/pkg/controllers/resources/networkpolicies/translate.go +++ b/pkg/controllers/resources/networkpolicies/translate.go @@ -4,10 +4,8 @@ import ( "context" podstranslate "github.com/loft-sh/vcluster/pkg/controllers/resources/pods/translate" - "github.com/loft-sh/vcluster/pkg/controllers/syncer/translator" "github.com/loft-sh/vcluster/pkg/util/translate" networkingv1 "k8s.io/api/networking/v1" - "k8s.io/apimachinery/pkg/api/equality" ) func (s *networkPolicySyncer) translate(ctx context.Context, vNetworkPolicy *networkingv1.NetworkPolicy) *networkingv1.NetworkPolicy { @@ -19,23 +17,15 @@ func (s *networkPolicySyncer) translate(ctx context.Context, vNetworkPolicy *net } func (s *networkPolicySyncer) translateUpdate(ctx context.Context, pObj, vObj *networkingv1.NetworkPolicy) *networkingv1.NetworkPolicy { - var updated *networkingv1.NetworkPolicy - if translatedSpec := translateSpec(&vObj.Spec, vObj.GetNamespace()); translatedSpec != nil { - if !equality.Semantic.DeepEqual(translatedSpec, pObj.Spec) { - updated = translator.NewIfNil(updated, pObj) - updated.Spec = *translatedSpec - } + pObj.Spec = *translatedSpec } - changed, translatedAnnotations, translatedLabels := s.TranslateMetadataUpdate(ctx, vObj, pObj) - if changed { - updated = translator.NewIfNil(updated, pObj) - updated.Labels = translatedLabels - updated.Annotations = translatedAnnotations - } + _, translatedAnnotations, translatedLabels := s.TranslateMetadataUpdate(ctx, vObj, pObj) + pObj.Labels = translatedLabels + pObj.Annotations = translatedAnnotations - return updated + return pObj } func translateSpec(spec *networkingv1.NetworkPolicySpec, namespace string) *networkingv1.NetworkPolicySpec {