Skip to content

Commit

Permalink
Merge pull request #2275 from loft-sh/backport/v0.21/pr-2259
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kosiewski authored Nov 13, 2024
2 parents 0c5dde7 + f388304 commit 7312212
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
8 changes: 7 additions & 1 deletion pkg/controllers/resources/ingresses/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ingresses
import (
"fmt"

"github.com/loft-sh/vcluster/pkg/controllers/resources/services"
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/patcher"
"github.com/loft-sh/vcluster/pkg/pro"
Expand Down Expand Up @@ -31,12 +32,17 @@ func NewSyncer(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
return &ingressSyncer{
GenericTranslator: translator.NewGenericTranslator(ctx, "ingress", &networkingv1.Ingress{}, mapper),
Importer: pro.NewImporter(mapper),

// exclude "field.cattle.io/publicEndpoints" annotation used by Rancher, similar to service syncer
excludedAnnotations: []string{services.RancherPublicEndpointsAnnotation},
}, nil
}

type ingressSyncer struct {
syncertypes.GenericTranslator
syncertypes.Importer

excludedAnnotations []string
}

var _ syncertypes.OptionsProvider = &ingressSyncer{}
Expand Down Expand Up @@ -103,7 +109,7 @@ func (s *ingressSyncer) SyncToVirtual(ctx *synccontext.SyncContext, event *syncc
return patcher.DeleteHostObject(ctx, event.Host, event.VirtualOld, "virtual object was deleted")
}

vIngress := translate.VirtualMetadata(event.Host, s.HostToVirtual(ctx, types.NamespacedName{Name: event.Host.Name, Namespace: event.Host.Namespace}, event.Host))
vIngress := translate.VirtualMetadata(event.Host, s.HostToVirtual(ctx, types.NamespacedName{Name: event.Host.Name, Namespace: event.Host.Namespace}, event.Host), s.excludedAnnotations...)
err := pro.ApplyPatchesVirtualObject(ctx, nil, vIngress, event.Host, ctx.Config.Sync.ToHost.Ingresses.Patches, false)
if err != nil {
return ctrl.Result{}, err
Expand Down
77 changes: 77 additions & 0 deletions pkg/controllers/resources/ingresses/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,83 @@ func TestSync(t *testing.T) {
err = syncCtx.PhysicalClient.Get(syncCtx, types.NamespacedName{Name: createdIngress.Name, Namespace: createdIngress.Namespace}, pIngress)
assert.NilError(t, err)

_, err = syncer.(*ingressSyncer).Sync(syncCtx, synccontext.NewSyncEventWithOld(pIngress, pIngress, baseIngress.DeepCopy(), vIngress))
assert.NilError(t, err)
},
},
{
Name: "Exclude Rancher managed annotations from syncing",
InitialVirtualState: []runtime.Object{
&networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: baseIngress.Name,
Namespace: baseIngress.Namespace,
Labels: baseIngress.Labels,
Annotations: map[string]string{
"nginx.ingress.kubernetes.io/auth-secret": "my-secret",
"nginx.ingress.kubernetes.io/auth-tls-secret": baseIngress.Namespace + "/my-secret",
"field.cattle.io/publicEndpoints": `[{"addresses":["192.168.0.10"],"port":80,"protocol":"HTTP","serviceName":"default:nginx","ingressName":"default:test-ingress","hostname":"my-ingress-endpoint.com","path":"/","allNodes":false}]`,
},
},
},
},
InitialPhysicalState: []runtime.Object{
&networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: createdIngress.Name,
Namespace: createdIngress.Namespace,
Labels: createdIngress.Labels,
},
},
},
ExpectedVirtualState: map[schema.GroupVersionKind][]runtime.Object{
networkingv1.SchemeGroupVersion.WithKind("Ingress"): {
&networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: baseIngress.Name,
Namespace: baseIngress.Namespace,
Labels: baseIngress.Labels,
Annotations: map[string]string{
"nginx.ingress.kubernetes.io/auth-secret": "my-secret",
"nginx.ingress.kubernetes.io/auth-tls-secret": baseIngress.Namespace + "/my-secret",
"field.cattle.io/publicEndpoints": `[{"addresses":["192.168.0.10"],"port":80,"protocol":"HTTP","serviceName":"default:nginx","ingressName":"default:test-ingress","hostname":"my-ingress-endpoint.com","path":"/","allNodes":false}]`,
},
},
},
},
},
ExpectedPhysicalState: map[schema.GroupVersionKind][]runtime.Object{
networkingv1.SchemeGroupVersion.WithKind("Ingress"): {
&networkingv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: createdIngress.Name,
Namespace: createdIngress.Namespace,
Labels: createdIngress.Labels,
Annotations: map[string]string{
"nginx.ingress.kubernetes.io/auth-secret": translate.Default.HostName(nil, "my-secret", baseIngress.Namespace).Name,
"nginx.ingress.kubernetes.io/auth-tls-secret": createdIngress.Namespace + "/" + translate.Default.HostName(nil, "my-secret", baseIngress.Namespace).Name,
"vcluster.loft.sh/object-name": baseIngress.Name,
"vcluster.loft.sh/object-namespace": baseIngress.Namespace,
translate.UIDAnnotation: "",
translate.KindAnnotation: networkingv1.SchemeGroupVersion.WithKind("Ingress").String(),
translate.HostNamespaceAnnotation: createdIngress.Namespace,
translate.HostNameAnnotation: createdIngress.Name,
},
},
},
},
},
Sync: func(registerContext *synccontext.RegisterContext) {
syncCtx, syncer := syncertesting.FakeStartSyncer(t, registerContext, NewSyncer)

vIngress := &networkingv1.Ingress{}
err := syncCtx.VirtualClient.Get(syncCtx, types.NamespacedName{Name: baseIngress.Name, Namespace: baseIngress.Namespace}, vIngress)
assert.NilError(t, err)

pIngress := &networkingv1.Ingress{}
err = syncCtx.PhysicalClient.Get(syncCtx, types.NamespacedName{Name: createdIngress.Name, Namespace: createdIngress.Namespace}, pIngress)
assert.NilError(t, err)

_, err = syncer.(*ingressSyncer).Sync(syncCtx, synccontext.NewSyncEventWithOld(pIngress, pIngress, baseIngress.DeepCopy(), vIngress))
assert.NilError(t, err)
},
Expand Down
10 changes: 9 additions & 1 deletion pkg/controllers/resources/ingresses/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/loft-sh/vcluster/pkg/mappings"
"github.com/loft-sh/vcluster/pkg/mappings/resources"
"github.com/loft-sh/vcluster/pkg/syncer/synccontext"
"github.com/loft-sh/vcluster/pkg/util/stringutil"
"github.com/loft-sh/vcluster/pkg/util/translate"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -21,7 +22,7 @@ const (
)

func (s *ingressSyncer) translate(ctx *synccontext.SyncContext, vIngress *networkingv1.Ingress) (*networkingv1.Ingress, error) {
newIngress := translate.HostMetadata(vIngress, s.VirtualToHost(ctx, types.NamespacedName{Name: vIngress.Name, Namespace: vIngress.Namespace}, vIngress))
newIngress := translate.HostMetadata(vIngress, s.VirtualToHost(ctx, types.NamespacedName{Name: vIngress.Name, Namespace: vIngress.Namespace}, vIngress), s.excludedAnnotations...)
newIngress.Spec = *translateSpec(ctx, vIngress.Namespace, &vIngress.Spec)
newIngress.Annotations = updateAnnotations(ctx, newIngress.Annotations, vIngress.Namespace)
return newIngress, nil
Expand All @@ -42,6 +43,9 @@ func (s *ingressSyncer) translateUpdate(ctx *synccontext.SyncContext, event *syn
if strings.HasPrefix(key, AlbActionsAnnotation) || strings.HasPrefix(key, AlbConditionAnnotation) {
return "", nil
}
if stringutil.Contains(s.excludedAnnotations, key) {
return "", nil
}
return key, value
},
func(key string, value interface{}) (string, interface{}) {
Expand All @@ -50,6 +54,10 @@ func (s *ingressSyncer) translateUpdate(ctx *synccontext.SyncContext, event *syn
if !ok {
return key, value
}
// we ignore excluded annotations
if stringutil.Contains(s.excludedAnnotations, key) {
return "", nil
}

// translate the annotation
translatedAnnotations := updateAnnotations(ctx, map[string]string{
Expand Down
7 changes: 5 additions & 2 deletions pkg/controllers/resources/services/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

var ServiceBlockDeletion = "vcluster.loft.sh/block-deletion"
var (
ServiceBlockDeletion = "vcluster.loft.sh/block-deletion"
RancherPublicEndpointsAnnotation = "field.cattle.io/publicEndpoints"
)

func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
mapper, err := ctx.Mappings.ByGVK(mappings.Services())
Expand All @@ -39,7 +42,7 @@ func New(ctx *synccontext.RegisterContext) (syncertypes.Object, error) {
Importer: pro.NewImporter(mapper),

excludedAnnotations: []string{
"field.cattle.io/publicEndpoints",
RancherPublicEndpointsAnnotation,
},

serviceName: ctx.Config.WorkloadService,
Expand Down

0 comments on commit 7312212

Please sign in to comment.