Skip to content

Commit

Permalink
added patcher logic for endpoints (#1939)
Browse files Browse the repository at this point in the history
* added patcher logic for endpoints

* added defer for consistency

* added event warning
  • Loading branch information
facchettos authored Jul 16, 2024
1 parent d5475d4 commit 5024ecc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
21 changes: 16 additions & 5 deletions pkg/controllers/resources/endpoints/syncer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package endpoints

import (
"fmt"

synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
"github.com/loft-sh/vcluster/pkg/patcher"
"github.com/loft-sh/vcluster/pkg/specialservices"
syncer "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
Expand All @@ -28,13 +31,21 @@ func (s *endpointsSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.O
return s.SyncToHostCreate(ctx, vObj, s.translate(ctx.Context, vObj))
}

func (s *endpointsSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (ctrl.Result, error) {
newEndpoints := s.translateUpdate(ctx.Context, pObj.(*corev1.Endpoints), vObj.(*corev1.Endpoints))
if newEndpoints != nil {
translator.PrintChanges(pObj, newEndpoints, ctx.Log)
func (s *endpointsSyncer) 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 {
s.NamespacedTranslator.EventRecorder().Eventf(pObj, "Warning", "SyncError", "Error syncing: %v", err)
retErr = err
}
}()

s.translateUpdate(ctx.Context, pObj.(*corev1.Endpoints), vObj.(*corev1.Endpoints))

return s.SyncToHostUpdate(ctx, vObj, newEndpoints)
return ctrl.Result{}, nil
}

var _ syncer.Starter = &endpointsSyncer{}
Expand Down
15 changes: 4 additions & 11 deletions pkg/controllers/resources/endpoints/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package endpoints
import (
"context"

"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
Expand Down Expand Up @@ -49,25 +48,19 @@ func (s *endpointsSyncer) translateSpec(endpoints *corev1.Endpoints) {
}
}

func (s *endpointsSyncer) translateUpdate(ctx context.Context, pObj, vObj *corev1.Endpoints) *corev1.Endpoints {
var updated *corev1.Endpoints

func (s *endpointsSyncer) translateUpdate(ctx context.Context, pObj, vObj *corev1.Endpoints) {
// check subsets
translated := vObj.DeepCopy()
s.translateSpec(translated)
if !equality.Semantic.DeepEqual(translated.Subsets, pObj.Subsets) {
updated = translator.NewIfNil(updated, pObj)
updated.Subsets = translated.Subsets
pObj.Subsets = translated.Subsets
}

// check annotations & labels
_, annotations, labels := s.TranslateMetadataUpdate(ctx, vObj, pObj)
delete(annotations, "control-plane.alpha.kubernetes.io/leader")
if !equality.Semantic.DeepEqual(annotations, pObj.Annotations) || !equality.Semantic.DeepEqual(labels, pObj.Labels) {
updated = translator.NewIfNil(updated, pObj)
updated.Annotations = annotations
updated.Labels = labels
pObj.Annotations = annotations
pObj.Labels = labels
}

return updated
}

0 comments on commit 5024ecc

Please sign in to comment.