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

migrated to new patchers and syncers #1924

Merged
merged 6 commits into from
Jul 12, 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
30 changes: 20 additions & 10 deletions pkg/controllers/resources/namespaces/syncer.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package namespaces

import (
"fmt"

"github.com/loft-sh/vcluster/pkg/constants"
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"
syncertypes "github.com/loft-sh/vcluster/pkg/types"
"github.com/loft-sh/vcluster/pkg/util/translate"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -68,18 +72,24 @@ func (s *namespaceSyncer) SyncToHost(ctx *synccontext.SyncContext, vObj client.O
return ctrl.Result{}, s.EnsureWorkloadServiceAccount(ctx, newNamespace.Name)
}

func (s *namespaceSyncer) Sync(ctx *synccontext.SyncContext, pObj client.Object, vObj client.Object) (ctrl.Result, error) {
updated := s.translateUpdate(ctx.Context, pObj.(*corev1.Namespace), vObj.(*corev1.Namespace))
if updated != nil {
ctx.Log.Infof("updating physical namespace %s, because virtual namespace has changed", updated.Name)
translator.PrintChanges(pObj, updated, ctx.Log)
err := ctx.PhysicalClient.Update(ctx.Context, updated)
if err != nil {
return ctrl.Result{}, err
}
func (s *namespaceSyncer) 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 ctrl.Result{}, s.EnsureWorkloadServiceAccount(ctx, pObj.GetName())
facchettos marked this conversation as resolved.
Show resolved Hide resolved
defer func() {
if err := patch.Patch(ctx, pObj, vObj); err != nil {
retErr = utilerrors.NewAggregate([]error{retErr, err})
}
}()

// cast objects
pNamespace, vNamespace, _, _ := synccontext.Cast[*corev1.Namespace](ctx, pObj, vObj)

s.translateUpdate(ctx.Context, pNamespace, vNamespace)

return ctrl.Result{}, s.EnsureWorkloadServiceAccount(ctx, pNamespace.Name)
}

func (s *namespaceSyncer) EnsureWorkloadServiceAccount(ctx *synccontext.SyncContext, pNamespace string) error {
Expand Down
16 changes: 5 additions & 11 deletions pkg/controllers/resources/namespaces/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package namespaces

import (
"context"
"maps"

"github.com/loft-sh/vcluster/pkg/controllers/syncer/translator"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -24,9 +23,7 @@ func (s *namespaceSyncer) translate(ctx context.Context, vObj client.Object) *co
return newNamespace
}

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

func (s *namespaceSyncer) translateUpdate(ctx context.Context, pObj, vObj *corev1.Namespace) {
_, updatedAnnotations, updatedLabels := s.TranslateMetadataUpdate(ctx, vObj, pObj)
if updatedLabels == nil {
updatedLabels = map[string]string{}
Expand All @@ -38,11 +35,8 @@ func (s *namespaceSyncer) translateUpdate(ctx context.Context, pObj, vObj *corev
// set the kubernetes.io/metadata.name label
updatedLabels[corev1.LabelMetadataName] = pObj.Name
// check if any labels or annotations changed
if !equality.Semantic.DeepEqual(updatedAnnotations, pObj.GetAnnotations()) || !equality.Semantic.DeepEqual(updatedLabels, pObj.GetLabels()) {
updated = translator.NewIfNil(updated, pObj)
updated.Annotations = updatedAnnotations
updated.Labels = updatedLabels
if !maps.Equal(updatedAnnotations, pObj.GetAnnotations()) || !maps.Equal(updatedLabels, pObj.GetLabels()) {
pObj.Annotations = updatedAnnotations
pObj.Labels = updatedLabels
}

return updated
}
Loading