diff --git a/apis/register_crd.go b/apis/register_crd.go new file mode 100644 index 0000000..a655129 --- /dev/null +++ b/apis/register_crd.go @@ -0,0 +1,18 @@ +package apis + +import ( + extentionapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apimachinery/pkg/runtime" +) + +func init() { + // Register the types with the Scheme so the components can map objects to GroupVersionKinds and back + AddToSchemes = append(AddToSchemes, + extentionapi.AddToScheme, + ) +} + +// AddToScheme adds all Resources to the Scheme +func AddToSchemeCrd(s *runtime.Scheme) error { + return AddToSchemes.AddToScheme(s) +} diff --git a/cmd/provider/main.go b/cmd/provider/main.go index 132a12f..6e37b83 100644 --- a/cmd/provider/main.go +++ b/cmd/provider/main.go @@ -6,6 +6,7 @@ package main import ( "context" + "k8s.io/klog/v2" "os" "path/filepath" "sigs.k8s.io/controller-runtime/pkg/cache" @@ -77,6 +78,7 @@ func main() { }) kingpin.FatalIfError(err, "Cannot create controller manager") kingpin.FatalIfError(apis.AddToScheme(mgr.GetScheme()), "Cannot add Azure APIs to scheme") + kingpin.FatalIfError(apis.AddToSchemeCrd(mgr.GetScheme()), "Cannot add Azure APIs to scheme") o := tjcontroller.Options{ Options: xpcontroller.Options{ Logger: log, @@ -115,6 +117,10 @@ func main() { log.Info("Alpha feature enabled", "flag", features.EnableBetaManagementPolicies) } - kingpin.FatalIfError(controller.Setup(mgr, o), "Cannot setup Azure controllers") + if err := controller.NewCustomResourceReconciler(mgr, o).SetupWithManager(mgr); err != nil { + klog.Error(err, "unable to create controller", "controller", "CustomResourceReconciler") + os.Exit(1) + } + //kingpin.FatalIfError(controller.Setup(mgr, o), "Cannot setup Azure controllers") kingpin.FatalIfError(mgr.Start(ctrl.SetupSignalHandler()), "Cannot start controller manager") } diff --git a/internal/controller/crd_controller.go b/internal/controller/crd_controller.go new file mode 100644 index 0000000..16fc66b --- /dev/null +++ b/internal/controller/crd_controller.go @@ -0,0 +1,91 @@ +/* +Copyright AppsCode Inc. and Contributors + +Licensed under the AppsCode Community License 1.0.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md + +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 controller + +import ( + "context" + "github.com/crossplane/upjet/pkg/controller" + apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + "kubedb.dev/provider-azure/internal/controller/azure/providerregistration" + "kubedb.dev/provider-azure/internal/controller/azure/resourcegroup" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + "sync" +) + +var ( + gk2 = schema.GroupKind{"azure.kubedb.com", "ResourceGroup"} + gk3 = schema.GroupKind{"azure.kubedb.com", "ProviderConfig"} + setupFns = map[schema.GroupKind]func(ctrl.Manager, controller.Options) error{ + gk2: resourcegroup.Setup, + gk3: providerregistration.Setup, + } + setupDone = map[schema.GroupKind]bool{} + mu sync.RWMutex +) + +//func SetupControllerList(mgr ctrl.Manager, o controller.Options) error { +// +//} + +type CustomResourceReconciler struct { + mgr ctrl.Manager + o controller.Options +} + +func NewCustomResourceReconciler(mgr ctrl.Manager, o controller.Options) *CustomResourceReconciler { + //if err := SetupControllerList(mgr, o); err != nil { + // log.Error(err, "unable to fetch CustomResourceDefinition") + //} + return &CustomResourceReconciler{mgr: mgr, o: o} +} + +func (r *CustomResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + log := log.FromContext(ctx) + + var crd apiextensions.CustomResourceDefinition + if err := r.mgr.GetClient().Get(ctx, req.NamespacedName, &crd); err != nil { + log.Error(err, "unable to fetch CustomResourceDefinition") + return ctrl.Result{}, client.IgnoreNotFound(err) + } + + gk := schema.GroupKind{ + Group: crd.Spec.Group, + Kind: crd.Spec.Names.Kind, + } + mu.Lock() + defer mu.Unlock() + _, found := setupDone[gk] + if found { + return ctrl.Result{}, nil + } + setup, found := setupFns[gk] + if found { + setup(r.mgr, r.o) + setupDone[gk] = true + } + + return ctrl.Result{}, nil +} + +func (r *CustomResourceReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&apiextensions.CustomResourceDefinition{}). + Complete(r) +}