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 to service accounts #1945

Merged
merged 2 commits 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
25 changes: 19 additions & 6 deletions pkg/controllers/resources/serviceaccounts/syncer.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package serviceaccounts

import (
"fmt"

"github.com/loft-sh/vcluster/pkg/controllers/syncer"
"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"

synccontext "github.com/loft-sh/vcluster/pkg/controllers/syncer/context"
corev1 "k8s.io/api/core/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -30,12 +34,21 @@ func (s *serviceAccountSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj cli
return s.SyncToHostCreate(ctx, vObj, s.translate(ctx, vObj.(*corev1.ServiceAccount)))
}

func (s *serviceAccountSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (ctrl.Result, error) {
// did the service account change?
newServiceAccount := s.translateUpdate(ctx, pObj.(*corev1.ServiceAccount), vObj.(*corev1.ServiceAccount))
if newServiceAccount != nil {
translator.PrintChanges(pObj, newServiceAccount, ctx.Log)
func (s *serviceAccountSyncer) 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, newServiceAccount)
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.Context, pObj.(*corev1.ServiceAccount), vObj.(*corev1.ServiceAccount))
return ctrl.Result{}, nil
}
16 changes: 4 additions & 12 deletions pkg/controllers/resources/serviceaccounts/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package serviceaccounts
import (
"context"

"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -22,16 +21,9 @@ func (s *serviceAccountSyncer) translate(ctx context.Context, vObj client.Object
return pObj
}

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

func (s *serviceAccountSyncer) translateUpdate(ctx context.Context, pObj, vObj *corev1.ServiceAccount) {
// check annotations & labels
changed, updatedAnnotations, updatedLabels := s.TranslateMetadataUpdate(ctx, vObj, pObj)
if changed {
updated = translator.NewIfNil(updated, pObj)
updated.Labels = updatedLabels
updated.Annotations = updatedAnnotations
}

return updated
_, updatedAnnotations, updatedLabels := s.TranslateMetadataUpdate(ctx, vObj, pObj)
pObj.Labels = updatedLabels
pObj.Annotations = updatedAnnotations
}
Loading