Skip to content

Commit

Permalink
Check all crds are present to setup multiregister
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Jun 24, 2024
1 parent aecaa9b commit f258d6e
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions apiextensions/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ import (
"sync"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/meta"
"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)
TestFn func(*apiextensionsv1.CustomResourceDefinition) bool
SetupFn func(context.Context, ctrl.Manager)
TestFn func(meta.RESTMapper, *apiextensionsv1.CustomResourceDefinition) bool
)

type setupGroup struct {
Expand Down Expand Up @@ -77,7 +78,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
if !setupFnExists {
return ctrl.Result{}, nil
}
if !testFns[gk](&crd) {
if !testFns[gk](r.mgr.GetRESTMapper(), &crd) {
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -111,7 +112,7 @@ func MultiRegisterSetup(gks []schema.GroupKind, fn SetupFn, tn ...TestFn) {
mu.Lock()
defer mu.Unlock()

testFN := andTestFn(tn...)
testFN := andTestFn(append(tn, allCRDPresent(gks))...)
for _, gk := range gks {
setupFns[gk] = setupGroup{
gks: gks,
Expand All @@ -121,10 +122,26 @@ func MultiRegisterSetup(gks []schema.GroupKind, fn SetupFn, tn ...TestFn) {
}
}

func allCRDPresent(gks []schema.GroupKind) TestFn {
return func(mapper meta.RESTMapper, definition *apiextensionsv1.CustomResourceDefinition) bool {
for _, gk := range gks {
if !crdFound(mapper, gk) {
return false
}
}
return true
}
}

func crdFound(mapper meta.RESTMapper, gk schema.GroupKind) bool {
_, err := mapper.RESTMappings(gk)
return err == nil
}

func andTestFn(fns ...TestFn) TestFn {
return func(crd *apiextensionsv1.CustomResourceDefinition) bool {
return func(mapper meta.RESTMapper, crd *apiextensionsv1.CustomResourceDefinition) bool {
for _, fn := range fns {
if !fn(crd) {
if !fn(mapper, crd) {
return false
}
}
Expand Down

0 comments on commit f258d6e

Please sign in to comment.