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 endpoints #1939

Merged
merged 3 commits into from
Jul 16, 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
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
}
Loading