From 37ab45cee52a8a3400f1259c6da5037bb95489eb Mon Sep 17 00:00:00 2001 From: justinsb Date: Wed, 5 Jun 2024 12:59:28 -0400 Subject: [PATCH] chore: Refactor registry to be more reusable We need to move things around to avoid import cycles as we add IAM support. --- .../singleresourceiamclient.go | 6 +- .../direct/alloydb/cluster_controller.go | 7 +- .../direct/apikeys/apikeyskey_controller.go | 7 +- .../directbase/directbase_controller.go | 55 +-------- .../direct/{registry.go => export.go} | 28 +---- .../gkehub/featuremembership_controller.go | 7 +- .../direct/logging/logmetric_controller.go | 7 +- pkg/controller/direct/registry/registry.go | 107 ++++++++++++++++++ .../resourcemanager/tagkey_controller.go | 7 +- pkg/controller/kccmanager/kccmanager.go | 10 ++ .../registration/registration_controller.go | 16 ++- .../controller/reconciler/testreconciler.go | 17 ++- pkg/test/resourcefixture/contexts/register.go | 10 +- 13 files changed, 184 insertions(+), 100 deletions(-) rename pkg/controller/direct/{registry.go => export.go} (72%) create mode 100644 pkg/controller/direct/registry/registry.go diff --git a/pkg/cli/cmd/bulkexport/singleresourceiamclient/singleresourceiamclient.go b/pkg/cli/cmd/bulkexport/singleresourceiamclient/singleresourceiamclient.go index a839591c05..a49022f0c5 100644 --- a/pkg/cli/cmd/bulkexport/singleresourceiamclient/singleresourceiamclient.go +++ b/pkg/cli/cmd/bulkexport/singleresourceiamclient/singleresourceiamclient.go @@ -20,7 +20,7 @@ import ( "reflect" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/iam/v1beta1" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" kcciamclient "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/iamclient" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/deepcopy" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/krmtotf" @@ -49,8 +49,8 @@ func New(tfProvider *schema.Provider, smloader *servicemappingloader.ServiceMapp func (i *iamClient) SupportsIAM(unstructured *unstructured.Unstructured) (bool, error) { groundKind := unstructured.GroupVersionKind().GroupKind() - if direct.IsDirect(groundKind) { - return direct.SupportsIAM(groundKind) + if registry.IsDirectByGK(groundKind) { + return registry.SupportsIAM(groundKind) } rc, err := i.smLoader.GetResourceConfig(unstructured) diff --git a/pkg/controller/direct/alloydb/cluster_controller.go b/pkg/controller/direct/alloydb/cluster_controller.go index d8c009e968..bd3b06cccc 100644 --- a/pkg/controller/direct/alloydb/cluster_controller.go +++ b/pkg/controller/direct/alloydb/cluster_controller.go @@ -32,14 +32,15 @@ import ( krm "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/alloydb/v1beta1" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" ) func init() { - directbase.ControllerBuilder.RegisterModel(krm.AlloyDBClusterGVK, NewModel) + registry.RegisterModel(krm.AlloyDBClusterGVK, NewModel) } -func NewModel(config *config.ControllerConfig) directbase.Model { - return &clusterModel{config: config} +func NewModel(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) { + return &clusterModel{config: config}, nil } type clusterModel struct { diff --git a/pkg/controller/direct/apikeys/apikeyskey_controller.go b/pkg/controller/direct/apikeys/apikeyskey_controller.go index 1fbc55d3d5..5d39a7a422 100644 --- a/pkg/controller/direct/apikeys/apikeyskey_controller.go +++ b/pkg/controller/direct/apikeys/apikeyskey_controller.go @@ -31,16 +31,17 @@ import ( krm "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/apikeys/v1alpha1" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" . "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/mappings" //nolint:revive ) func init() { - directbase.ControllerBuilder.RegisterModel(krm.APIKeysKeyGVK, newAPIKeysModel) + registry.RegisterModel(krm.APIKeysKeyGVK, newAPIKeysModel) } -func newAPIKeysModel(config *config.ControllerConfig) directbase.Model { - return &model{config: *config} +func newAPIKeysModel(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) { + return &model{config: *config}, nil } type model struct { diff --git a/pkg/controller/direct/directbase/directbase_controller.go b/pkg/controller/direct/directbase/directbase_controller.go index 4cfb37a224..f0884b4fef 100644 --- a/pkg/controller/direct/directbase/directbase_controller.go +++ b/pkg/controller/direct/directbase/directbase_controller.go @@ -23,7 +23,6 @@ import ( "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/apis/core/v1beta1" "github.com/GoogleCloudPlatform/k8s-config-connector/operator/pkg/kccstate" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" kcciamclient "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/iamclient" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/jitter" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/lifecyclehandler" @@ -37,7 +36,6 @@ import ( "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/util" "golang.org/x/sync/semaphore" - apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -55,66 +53,21 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" ) -var ControllerBuilder directControllerBuilder - -func init() { - ControllerBuilder = directControllerBuilder{} -} - -type directControllerBuilder struct { - modelMapper map[schema.GroupVersionKind]func(*config.ControllerConfig) Model -} - -func (c *directControllerBuilder) RegisterModel(gvk schema.GroupVersionKind, modelFn func(*config.ControllerConfig) Model) { - if c.modelMapper == nil { - c.modelMapper = map[schema.GroupVersionKind]func(*config.ControllerConfig) Model{} - } - c.modelMapper[gvk] = modelFn -} - -func (c *directControllerBuilder) AddController(mgr manager.Manager, config *config.ControllerConfig, crd *apiextensions.CustomResourceDefinition, deps Deps) error { +func AddController(mgr manager.Manager, gvk schema.GroupVersionKind, model Model, deps Deps) error { immediateReconcileRequests := make(chan event.GenericEvent, k8s.ImmediateReconcileRequestsBufferSize) resourceWatcherRoutines := semaphore.NewWeighted(k8s.MaxNumResourceWatcherRoutines) - reconciler, err := c.NewReconciler(mgr, config, immediateReconcileRequests, resourceWatcherRoutines, crd, deps.JitterGenerator) + reconciler, err := NewReconciler(mgr, immediateReconcileRequests, resourceWatcherRoutines, gvk, model, deps.JitterGenerator) if err != nil { return err } return add(mgr, reconciler) } -func (c *directControllerBuilder) IsDirectByGK(gk schema.GroupKind) bool { - for gvk, _ := range c.modelMapper { - if gvk.Group == gk.Group && gvk.Kind == gk.Kind { - return true - } - } - return false -} - -func (c *directControllerBuilder) gvkByCrd(crd *apiextensions.CustomResourceDefinition) schema.GroupVersionKind { - for gvk, _ := range c.modelMapper { - if crd.Spec.Group == gvk.Group && crd.Spec.Names.Kind == gvk.Kind { - return gvk - } - } - return schema.GroupVersionKind{} -} - // NewReconciler returns a new reconcile.Reconciler. -func (c *directControllerBuilder) NewReconciler(mgr manager.Manager, config *config.ControllerConfig, immediateReconcileRequests chan event.GenericEvent, resourceWatcherRoutines *semaphore.Weighted, - crd *apiextensions.CustomResourceDefinition, jg jitter.Generator) (*DirectReconciler, error) { - gvk := c.gvkByCrd(crd) - if gvk.Empty() { - return nil, fmt.Errorf("CRD %s is not registered on direct controllers", crd.Name) - } +func NewReconciler(mgr manager.Manager, immediateReconcileRequests chan event.GenericEvent, resourceWatcherRoutines *semaphore.Weighted, + gvk schema.GroupVersionKind, model Model, jg jitter.Generator) (*DirectReconciler, error) { controllerName := strings.ToLower(gvk.Kind) + "-controller" - modelFn, ok := c.modelMapper[gvk] - if !ok { - return nil, fmt.Errorf("no direct controller is registered for GroupVersionKind %s", gvk) - } - model := modelFn(config) - if jg == nil { return nil, fmt.Errorf("jitter generator is not initialized") } diff --git a/pkg/controller/direct/registry.go b/pkg/controller/direct/export.go similarity index 72% rename from pkg/controller/direct/registry.go rename to pkg/controller/direct/export.go index e6e2f3c6bd..3ec714a5d8 100644 --- a/pkg/controller/direct/registry.go +++ b/pkg/controller/direct/export.go @@ -20,31 +20,12 @@ import ( "strings" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/logging" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/controller-runtime/pkg/client" ) -// IsDirect returns true if this resource uses the direct-reconciliation model. -func IsDirect(groupKind schema.GroupKind) bool { - switch groupKind { - case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}: - return true - } - return false -} - -// SupportsIAM returns true if this resource supports IAM (not all GCP resources do). -// An error will be returned if IsDirect(groupKind) is not true. -func SupportsIAM(groupKind schema.GroupKind) (bool, error) { - switch groupKind { - case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}: - return false, nil - } - return false, fmt.Errorf("groupKind %v is not recognized as a direct kind", groupKind) -} - // Export attempts to export the resource specified by url. // The url format should match the Cloud-Asset-Inventory format: https://cloud.google.com/asset-inventory/docs/resource-name-format // If url is not recognized or not implemented by a direct controller, this returns (nil, nil) @@ -52,7 +33,10 @@ func Export(ctx context.Context, url string, config *config.ControllerConfig) (* if strings.HasPrefix(url, "//logging.googleapis.com/") { tokens := strings.Split(strings.TrimPrefix(url, "//logging.googleapis.com/"), "/") if len(tokens) == 4 && tokens[0] == "projects" && tokens[2] == "metrics" { - m := logging.NewLogMetricModel(config) + model, err := registry.GetModel(schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}) + if err != nil { + return nil, err + } in := &unstructured.Unstructured{} in.SetName(tokens[3]) if err := unstructured.SetNestedField(in.Object, tokens[1], "spec", "projectRef", "external"); err != nil { @@ -60,7 +44,7 @@ func Export(ctx context.Context, url string, config *config.ControllerConfig) (* } var reader client.Reader // TODO: Create erroring reader? - a, err := m.AdapterForObject(ctx, reader, in) + a, err := model.AdapterForObject(ctx, reader, in) if err != nil { return nil, err } diff --git a/pkg/controller/direct/gkehub/featuremembership_controller.go b/pkg/controller/direct/gkehub/featuremembership_controller.go index 39ab5c97c0..ca5d71e616 100644 --- a/pkg/controller/direct/gkehub/featuremembership_controller.go +++ b/pkg/controller/direct/gkehub/featuremembership_controller.go @@ -30,16 +30,17 @@ import ( "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/references" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" ) const ctrlName = "gkehubfeaturemembership-controller" func init() { - directbase.ControllerBuilder.RegisterModel(krm.GKEHubFeatureMembershipGVK, GetModel) + registry.RegisterModel(krm.GKEHubFeatureMembershipGVK, getGkeHubModel) } -func GetModel(config *config.ControllerConfig) directbase.Model { - return &gkeHubModel{config: config} +func getGkeHubModel(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) { + return &gkeHubModel{config: config}, nil } type gkeHubModel struct { diff --git a/pkg/controller/direct/logging/logmetric_controller.go b/pkg/controller/direct/logging/logmetric_controller.go index 681e8b4f64..4d8fa584e5 100644 --- a/pkg/controller/direct/logging/logmetric_controller.go +++ b/pkg/controller/direct/logging/logmetric_controller.go @@ -31,16 +31,17 @@ import ( "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/references" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" ) const ctrlName = "logmetric-controller" func init() { - directbase.ControllerBuilder.RegisterModel(krm.LoggingLogMetricGVK, NewLogMetricModel) + registry.RegisterModel(krm.LoggingLogMetricGVK, NewLogMetricModel) } -func NewLogMetricModel(config *config.ControllerConfig) directbase.Model { - return &logMetricModel{config: config} +func NewLogMetricModel(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) { + return &logMetricModel{config: config}, nil } type logMetricModel struct { diff --git a/pkg/controller/direct/registry/registry.go b/pkg/controller/direct/registry/registry.go new file mode 100644 index 0000000000..3fbc9d4065 --- /dev/null +++ b/pkg/controller/direct/registry/registry.go @@ -0,0 +1,107 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package registry + +import ( + "context" + "fmt" + + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +var singleton = registry{} + +type registry struct { + registrations map[schema.GroupKind]*registration +} + +type registration struct { + gvk schema.GroupVersionKind + factory ModelFactoryFunc + model directbase.Model +} + +type ModelFactoryFunc func(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) + +func GetModel(gk schema.GroupKind) (directbase.Model, error) { + registration := singleton.registrations[gk] + if registration == nil { + return nil, fmt.Errorf("no model registered for %s", gk) + } + return registration.model, nil +} + +func PreferredGVK(gk schema.GroupKind) (schema.GroupVersionKind, bool) { + registration := singleton.registrations[gk] + if registration == nil { + return schema.GroupVersionKind{}, false + } + return registration.gvk, true +} + +func Init(ctx context.Context, config *config.ControllerConfig) error { + for _, registration := range singleton.registrations { + model, err := registration.factory(ctx, config) + if err != nil { + return err + } + + registration.model = model + } + return nil +} + +func RegisterModel(gvk schema.GroupVersionKind, modelFn ModelFactoryFunc) { + if singleton.registrations == nil { + singleton.registrations = make(map[schema.GroupKind]*registration) + } + singleton.registrations[gvk.GroupKind()] = ®istration{ + gvk: gvk, + factory: modelFn, + } +} + +func IsDirectByGK(gk schema.GroupKind) bool { + registration := singleton.registrations[gk] + return registration != nil +} + +// IsIAMDirect returns true if this resource uses the direct-reconciliation model for IAM. +func IsIAMDirect(groupKind schema.GroupKind) bool { + registration := singleton.registrations[groupKind] + if registration == nil { + return false + } + + // TODO: Move to registration somehow? + switch groupKind { + case schema.GroupKind{Group: "privateca.cnrm.cloud.google.com", Kind: "PrivateCACAPool"}: + return true + } + return false +} + +// SupportsIAM returns true if this resource supports IAM (not all GCP resources do). +// An error will be returned if IsDirect(groupKind) is not true. +func SupportsIAM(groupKind schema.GroupKind) (bool, error) { + // TODO: Move to registration somehow? + switch groupKind { + case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}: + return false, nil + } + return false, fmt.Errorf("groupKind %v is not recognized as a direct kind", groupKind) +} diff --git a/pkg/controller/direct/resourcemanager/tagkey_controller.go b/pkg/controller/direct/resourcemanager/tagkey_controller.go index 59817ae219..39bd057340 100644 --- a/pkg/controller/direct/resourcemanager/tagkey_controller.go +++ b/pkg/controller/direct/resourcemanager/tagkey_controller.go @@ -32,14 +32,15 @@ import ( krm "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/tags/v1beta1" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" ) func init() { - directbase.ControllerBuilder.RegisterModel(krm.TagsTagKeyGVK, newTagKeyModel) + registry.RegisterModel(krm.TagsTagKeyGVK, newTagKeyModel) } -func newTagKeyModel(config *config.ControllerConfig) directbase.Model { - return &tagKeyModel{config: config} +func newTagKeyModel(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) { + return &tagKeyModel{config: config}, nil } type tagKeyModel struct { diff --git a/pkg/controller/kccmanager/kccmanager.go b/pkg/controller/kccmanager/kccmanager.go index 6e2016ef8f..ac31e68939 100644 --- a/pkg/controller/kccmanager/kccmanager.go +++ b/pkg/controller/kccmanager/kccmanager.go @@ -23,6 +23,7 @@ import ( "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/kccmanager/nocache" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/ratelimiter" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/registration" @@ -40,6 +41,9 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/manager" + + // Register direct controllers + _ "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct" ) type Config struct { @@ -142,6 +146,12 @@ func New(ctx context.Context, restConfig *rest.Config, cfg Config) (manager.Mana HTTPClient: cfg.HTTPClient, UserAgent: gcp.KCCUserAgent, } + + // Initialize direct controllers + if err := registry.Init(ctx, controllerConfig); err != nil { + return nil, err + } + rd := controller.Deps{ TfProvider: provider, TfLoader: smLoader, diff --git a/pkg/controller/registration/registration_controller.go b/pkg/controller/registration/registration_controller.go index a7bf970417..87f0c507f7 100644 --- a/pkg/controller/registration/registration_controller.go +++ b/pkg/controller/registration/registration_controller.go @@ -25,6 +25,7 @@ import ( dclcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/dcl" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/deletiondefender" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/gsakeysecretgenerator" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/auditconfig" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/partialpolicy" @@ -195,8 +196,14 @@ func registerDefaultController(r *ReconcileRegistration, config *config.Controll } var schemaUpdater k8s.SchemaReferenceUpdater if kccfeatureflags.UseDirectReconciler(gvk.GroupKind()) { - err := directbase.ControllerBuilder.AddController(r.mgr, config, crd, directbase.Deps{JitterGenerator: r.jitterGenerator}) + groupKind := gvk.GroupKind() + + model, err := registry.GetModel(groupKind) if err != nil { + return nil, err + } + + if err := directbase.AddController(r.mgr, gvk, model, directbase.Deps{JitterGenerator: r.jitterGenerator}); err != nil { return nil, fmt.Errorf("error adding direct controller for %v to a manager: %w", crd.Spec.Names.Kind, err) } return schemaUpdater, nil @@ -247,9 +254,12 @@ func registerDefaultController(r *ReconcileRegistration, config *config.Controll return su, nil } // register controllers for direct CRDs - if directbase.ControllerBuilder.IsDirectByGK(schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}) { - err := directbase.ControllerBuilder.AddController(r.mgr, config, crd, directbase.Deps{JitterGenerator: r.jitterGenerator}) + if registry.IsDirectByGK(gvk.GroupKind()) { + model, err := registry.GetModel(gvk.GroupKind()) if err != nil { + return nil, err + } + if err := directbase.AddController(r.mgr, gvk, model, directbase.Deps{JitterGenerator: r.jitterGenerator}); err != nil { return nil, fmt.Errorf("error adding direct controller for %v to a manager: %w", crd.Spec.Names.Kind, err) } return schemaUpdater, nil diff --git a/pkg/test/controller/reconciler/testreconciler.go b/pkg/test/controller/reconciler/testreconciler.go index 07f69ccaa7..0ee7507538 100644 --- a/pkg/test/controller/reconciler/testreconciler.go +++ b/pkg/test/controller/reconciler/testreconciler.go @@ -23,9 +23,9 @@ import ( "testing" "time" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" dclcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/dcl" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/auditconfig" partialpolicy "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/partialpolicy" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/iam/policy" @@ -239,9 +239,18 @@ func (r *TestReconciler) newReconcilerForCRD(crd *apiextensions.CustomResourceDe if crd.GetLabels()[k8s.DCL2CRDLabel] == "true" { return dclcontroller.NewReconciler(r.mgr, crd, r.dclConverter, r.dclConfig, r.smLoader, immediateReconcileRequests, resourceWatcherRoutines, defaulters, jg) } - gv := schema.GroupKind{Group: crd.Spec.Group, Kind: crd.Spec.Names.Kind} - if directbase.ControllerBuilder.IsDirectByGK(gv) { - return directbase.ControllerBuilder.NewReconciler(r.mgr, &config.ControllerConfig{HTTPClient: r.httpClient}, immediateReconcileRequests, resourceWatcherRoutines, crd, jg) + gk := schema.GroupKind{Group: crd.Spec.Group, Kind: crd.Spec.Names.Kind} + if registry.IsDirectByGK(gk) { + model, err := registry.GetModel(gk) + if err != nil { + return nil, err + } + gvk, found := registry.PreferredGVK(gk) + if !found { + return nil, fmt.Errorf("no preferred GVK for %v", gk) + } + + return directbase.NewReconciler(r.mgr, immediateReconcileRequests, resourceWatcherRoutines, gvk, model, jg) } } return nil, fmt.Errorf("CRD format not recognized") diff --git a/pkg/test/resourcefixture/contexts/register.go b/pkg/test/resourcefixture/contexts/register.go index b929e302b7..61eb72a79c 100644 --- a/pkg/test/resourcefixture/contexts/register.go +++ b/pkg/test/resourcefixture/contexts/register.go @@ -139,9 +139,12 @@ func (rc ResourceContext) Get(ctx context.Context, _ *testing.T, u *unstructured // direct controllers switch u.GroupVersionKind().GroupKind() { case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}: - m := logging.NewLogMetricModel(&config.ControllerConfig{ + m, err := logging.NewLogMetricModel(ctx, &config.ControllerConfig{ HTTPClient: httpClient, }) + if err != nil { + return nil, err + } a, err := m.AdapterForObject(ctx, c, u) if err != nil { return nil, err @@ -172,9 +175,12 @@ func (rc ResourceContext) Delete(ctx context.Context, _ *testing.T, u *unstructu // direct controllers switch u.GroupVersionKind().GroupKind() { case schema.GroupKind{Group: "logging.cnrm.cloud.google.com", Kind: "LoggingLogMetric"}: - m := logging.NewLogMetricModel(&config.ControllerConfig{ + m, err := logging.NewLogMetricModel(ctx, &config.ControllerConfig{ HTTPClient: httpClient, }) + if err != nil { + return err + } a, err := m.AdapterForObject(ctx, c, u) if err != nil { return err