Skip to content

Commit

Permalink
Add TestFn to crd reconciler setup
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jun 17, 2024
1 parent af86ba6 commit d2381f7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
28 changes: 17 additions & 11 deletions apiextensions/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ import (
"context"
"sync"

apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

type SetupFn func(ctx context.Context, mgr ctrl.Manager)

var setupFns = map[schema.GroupKind]SetupFn{
// schema.GroupKind{"compute.gcp.kubedb.com", "Firewall"}: firewall.Setup,
}
type (
SetupFn func(ctx context.Context, mgr ctrl.Manager)
TestFn func(*apiextensionsv1.CustomResourceDefinition) bool
)

var (
setupFns = make(map[schema.GroupKind]SetupFn)
testFns = make(map[schema.GroupKind]TestFn)
setupDone = map[schema.GroupKind]bool{}
mu sync.Mutex
)
Expand All @@ -49,7 +50,7 @@ func NewReconciler(ctx context.Context, mgr ctrl.Manager) *Reconciler {

func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
var crd apiextensions.CustomResourceDefinition
var crd apiextensionsv1.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)
Expand All @@ -65,8 +66,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
if found {
return ctrl.Result{}, nil
}
setup, found := setupFns[gk]
if found {

setup, setupFnExists := setupFns[gk]
test, testFnExists := testFns[gk]
if setupFnExists && (!testFnExists || test(&crd)) {
setup(r.ctx, r.mgr)
setupDone[gk] = true
}
Expand All @@ -75,13 +78,16 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu

func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&apiextensions.CustomResourceDefinition{}).
For(&apiextensionsv1.CustomResourceDefinition{}).
Complete(r)
}

func RegisterSetup(gk schema.GroupKind, fn SetupFn) {
func RegisterSetup(gk schema.GroupKind, fn SetupFn, tn ...TestFn) {
mu.Lock()
defer mu.Unlock()

setupFns[gk] = fn
if len(tn) == 1 {
testFns[gk] = tn[0]
}
}
16 changes: 16 additions & 0 deletions cluster/host_detector.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright AppsCode Inc. and Contributors
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 cluster

import (
Expand Down

0 comments on commit d2381f7

Please sign in to comment.