Skip to content

Commit

Permalink
added patcher logic for network policies
Browse files Browse the repository at this point in the history
  • Loading branch information
facchettos committed Jul 19, 2024
1 parent 175c2ff commit 931d20a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
25 changes: 20 additions & 5 deletions pkg/controllers/resources/networkpolicies/syncer.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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
}
20 changes: 5 additions & 15 deletions pkg/controllers/resources/networkpolicies/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 931d20a

Please sign in to comment.