Skip to content

Commit

Permalink
Add dynamically start crd reconciler
Browse files Browse the repository at this point in the history
Signed-off-by: SK Ali Arman <[email protected]>
  • Loading branch information
sheikh-arman committed Dec 14, 2023
1 parent 1099422 commit 12a47bd
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
18 changes: 18 additions & 0 deletions apis/register_crd.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 7 additions & 1 deletion cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"context"
"k8s.io/klog/v2"
"os"
"path/filepath"
"sigs.k8s.io/controller-runtime/pkg/cache"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}
91 changes: 91 additions & 0 deletions internal/controller/crd_controller.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 12a47bd

Please sign in to comment.