From ef146f027bb2e65a6fdd0d9b26f3de7bf7b506d2 Mon Sep 17 00:00:00 2001 From: Fabian Kramm Date: Wed, 21 Aug 2024 15:53:49 +0200 Subject: [PATCH 1/2] test: make fake mapper globally available --- pkg/mappings/generic/mapper_test.go | 2 +- pkg/mappings/generic/recorder_test.go | 27 +-------------------- pkg/util/testing/client.go | 6 ++--- pkg/util/testing/mapper.go | 34 +++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 pkg/util/testing/mapper.go diff --git a/pkg/mappings/generic/mapper_test.go b/pkg/mappings/generic/mapper_test.go index a22e723f7f..4cad090495 100644 --- a/pkg/mappings/generic/mapper_test.go +++ b/pkg/mappings/generic/mapper_test.go @@ -300,7 +300,7 @@ func TestTryToTranslateBackByStore(t *testing.T) { } for _, mapping := range testCase.Mappings { if !syncContext.Mappings.Has(mapping.GroupVersionKind) { - err = syncContext.Mappings.AddMapper(&fakeMapper{gvk: mapping.GroupVersionKind}) + err = syncContext.Mappings.AddMapper(testingutil.NewFakeMapper(mapping.GroupVersionKind)) assert.NilError(t, err) } } diff --git a/pkg/mappings/generic/recorder_test.go b/pkg/mappings/generic/recorder_test.go index 47d6418e61..175cbfe2fe 100644 --- a/pkg/mappings/generic/recorder_test.go +++ b/pkg/mappings/generic/recorder_test.go @@ -13,7 +13,6 @@ import ( "gotest.tools/assert" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" @@ -265,7 +264,7 @@ func TestRecorder(t *testing.T) { } // create mapper - recorderMapper := WithRecorder(&fakeMapper{gvk: gvk}) + recorderMapper := WithRecorder(testingutil.NewFakeMapper(gvk)) // record mapping vTest := types.NamespacedName{ @@ -346,27 +345,3 @@ func TestRecorder(t *testing.T) { assert.NilError(t, err) assert.Equal(t, isManaged, false) } - -var _ synccontext.Mapper = &fakeMapper{} - -type fakeMapper struct { - gvk schema.GroupVersionKind -} - -func (f *fakeMapper) Migrate(_ *synccontext.RegisterContext, _ synccontext.Mapper) error { - return nil -} - -func (f *fakeMapper) GroupVersionKind() schema.GroupVersionKind { return f.gvk } - -func (f *fakeMapper) VirtualToHost(_ *synccontext.SyncContext, req types.NamespacedName, _ client.Object) types.NamespacedName { - return req -} - -func (f *fakeMapper) HostToVirtual(_ *synccontext.SyncContext, req types.NamespacedName, _ client.Object) types.NamespacedName { - return req -} - -func (f *fakeMapper) IsManaged(_ *synccontext.SyncContext, _ client.Object) (bool, error) { - return false, nil -} diff --git a/pkg/util/testing/client.go b/pkg/util/testing/client.go index d3cd5ee32a..46ec19bcf5 100644 --- a/pkg/util/testing/client.go +++ b/pkg/util/testing/client.go @@ -22,7 +22,7 @@ func NewFakeClient(scheme *runtime.Scheme, objs ...runtime.Object) *FakeIndexCli co = append(co, o.(client.Object)) } - fakeMapper := NewFakeMapper(scheme).(*meta.DefaultRESTMapper) + fakeMapper := NewFakeRESTMapper(scheme).(*meta.DefaultRESTMapper) for groupVersionKind := range scheme.AllKnownTypes() { // we just assume everything is namespace scoped, if this causes issues we can // exclude certain resources here @@ -37,8 +37,8 @@ func NewFakeClient(scheme *runtime.Scheme, objs ...runtime.Object) *FakeIndexCli } } -// NewFakeMapper creates a new fake mapper -func NewFakeMapper(scheme *runtime.Scheme) meta.RESTMapper { +// NewFakeRESTMapper creates a new fake mapper +func NewFakeRESTMapper(scheme *runtime.Scheme) meta.RESTMapper { return meta.NewDefaultRESTMapper(scheme.PreferredVersionAllGroups()) } diff --git a/pkg/util/testing/mapper.go b/pkg/util/testing/mapper.go new file mode 100644 index 0000000000..8b72852f89 --- /dev/null +++ b/pkg/util/testing/mapper.go @@ -0,0 +1,34 @@ +package testing + +import ( + "github.com/loft-sh/vcluster/pkg/syncer/synccontext" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func NewFakeMapper(gvk schema.GroupVersionKind) synccontext.Mapper { + return &fakeMapper{gvk: gvk} +} + +type fakeMapper struct { + gvk schema.GroupVersionKind +} + +func (f *fakeMapper) Migrate(_ *synccontext.RegisterContext, _ synccontext.Mapper) error { + return nil +} + +func (f *fakeMapper) GroupVersionKind() schema.GroupVersionKind { return f.gvk } + +func (f *fakeMapper) VirtualToHost(_ *synccontext.SyncContext, req types.NamespacedName, _ client.Object) types.NamespacedName { + return req +} + +func (f *fakeMapper) HostToVirtual(_ *synccontext.SyncContext, req types.NamespacedName, _ client.Object) types.NamespacedName { + return req +} + +func (f *fakeMapper) IsManaged(_ *synccontext.SyncContext, _ client.Object) (bool, error) { + return false, nil +} From e76331a4601bebcd690de37d5ca5e00fd316096e Mon Sep 17 00:00:00 2001 From: Fabian Kramm Date: Wed, 21 Aug 2024 15:54:04 +0200 Subject: [PATCH 2/2] feat: add kindPath and apiVersionPath --- chart/values.schema.json | 12 ++++++++++-- config/config.go | 11 ++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/chart/values.schema.json b/chart/values.schema.json index d84a0871b8..eef5f75a9e 100755 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -3086,17 +3086,25 @@ "type": "string", "description": "APIVersion is the apiVersion of the referenced object." }, + "apiVersionPath": { + "type": "string", + "description": "APIVersionPath is optional relative path to use to determine the kind. If APIVersionPath is not found, will fallback to apiVersion." + }, "kind": { "type": "string", "description": "Kind is the kind of the referenced object." }, + "kindPath": { + "type": "string", + "description": "KindPath is the optional relative path to use to determine the kind. If KindPath is not found, will fallback to kind." + }, "namePath": { "type": "string", - "description": "NamePath is the optional path to the reference name within the object. If omitted namePath equals to the\ntranslate patch path." + "description": "NamePath is the optional relative path to the reference name within the object." }, "namespacePath": { "type": "string", - "description": "NamespacePath is the optional path to the reference namespace within the object. If omitted namespacePath equals to the\nmetadata.namespace path of the object." + "description": "NamespacePath is the optional relative path to the reference namespace within the object. If omitted or not found, namespacePath equals to the\nmetadata.namespace path of the object." } }, "additionalProperties": false, diff --git a/config/config.go b/config/config.go index c7bb3beef6..9755aac987 100644 --- a/config/config.go +++ b/config/config.go @@ -468,14 +468,19 @@ type TranslatePatchReference struct { // APIVersion is the apiVersion of the referenced object. APIVersion string `json:"apiVersion,omitempty" jsonschema:"required"` + // APIVersionPath is optional relative path to use to determine the kind. If APIVersionPath is not found, will fallback to apiVersion. + APIVersionPath string `json:"apiVersionPath,omitempty"` + // Kind is the kind of the referenced object. Kind string `json:"kind,omitempty" jsonschema:"required"` - // NamePath is the optional path to the reference name within the object. If omitted namePath equals to the - // translate patch path. + // KindPath is the optional relative path to use to determine the kind. If KindPath is not found, will fallback to kind. + KindPath string `json:"kindPath,omitempty"` + + // NamePath is the optional relative path to the reference name within the object. NamePath string `json:"namePath,omitempty"` - // NamespacePath is the optional path to the reference namespace within the object. If omitted namespacePath equals to the + // NamespacePath is the optional relative path to the reference namespace within the object. If omitted or not found, namespacePath equals to the // metadata.namespace path of the object. NamespacePath string `json:"namespacePath,omitempty"` }