Skip to content

Commit

Permalink
Available CRDs
Browse files Browse the repository at this point in the history
Signed-off-by: raaizik <[email protected]>
Co-Authored-By: Rewant Soni <[email protected]>
  • Loading branch information
raaizik and rewantsoni committed Jul 24, 2024
1 parent bb238df commit df31699
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ COPY --from=builder workspace/provider-api /usr/local/bin/provider-api
COPY --from=builder workspace/onboarding-validation-keys-gen /usr/local/bin/onboarding-validation-keys-gen
COPY --from=builder workspace/metrics/deploy/*rules*.yaml /ocs-prometheus-rules/
COPY --from=builder workspace/ux-backend-server /usr/local/bin/ux-backend-server
COPY --from=builder workspace/hack/crdavail.sh /usr/local/bin/crdavail

RUN chmod +x /usr/local/bin/ocs-operator /usr/local/bin/provider-api
RUN chmod +x /usr/local/bin/ocs-operator /usr/local/bin/provider-api /usr/local/bin/crdavail

USER operator

ENTRYPOINT ["/usr/local/bin/ocs-operator"]
ENTRYPOINT ["/usr/local/bin/crdavail"]
5 changes: 1 addition & 4 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ spec:
serviceAccountName: ocs-operator
containers:
- command:
- ocs-operator
args:
- --enable-leader-election
- "--health-probe-bind-address=:8081"
- crdavail
image: ocs-dev/ocs-operator:latest
imagePullPolicy: Always
name: ocs-operator
Expand Down
57 changes: 57 additions & 0 deletions controllers/crd/crd_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package crd

import (
"context"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/klog/v2"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"slices"
)

// CustomResourceDefinitionReconciler reconciles a CustomResourceDefinition object
// nolint:revive
type CustomResourceDefinitionReconciler struct {
}

// Reconcile exits immediately following either a Create or Delete event
func (r *CustomResourceDefinitionReconciler) Reconcile(_ context.Context, _ reconcile.Request) (reconcile.Result, error) {
os.Exit(42)
return reconcile.Result{}, nil
}

// SetupWithManager sets up a controller with a manager
func (r *CustomResourceDefinitionReconciler) SetupWithManager(crds []string, mgr ctrl.Manager) error {
crdPredicate := predicate.Funcs{
CreateFunc: func(e event.TypedCreateEvent[client.Object]) bool {
name := string(e.Object.GetName())
if slices.Contains(crds, name) {
klog.Errorf("CustomResourceDefinition %s was Created. Restarting process.", name)
return true
}
return false
},
DeleteFunc: func(e event.TypedDeleteEvent[client.Object]) bool {
name := string(e.Object.GetName())
if slices.Contains(crds, name) {
klog.Errorf("CustomResourceDefinition %s was Deleted. Restarting process.", name)
return true
}
return false
},
UpdateFunc: func(e event.TypedUpdateEvent[client.Object]) bool {
return false
},
GenericFunc: func(e event.TypedGenericEvent[client.Object]) bool {
return false
},
}
return ctrl.NewControllerManagedBy(mgr).
For(&apiextensionsv1.CustomResourceDefinition{}, builder.WithPredicates(crdPredicate)).
Complete(r)
}
14 changes: 14 additions & 0 deletions controllers/util/k8sutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
ocsv1 "github.com/red-hat-storage/ocs-operator/api/v4/v1"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -149,3 +150,16 @@ func GenerateNameForNonResilientCephBlockPoolSC(initData *ocsv1.StorageCluster)
}
return fmt.Sprintf("%s-ceph-non-resilient-rbd", initData.Name)
}

func MapCRDAvailability(ctx context.Context, clnt client.Client, crdNames ...string) (map[string]bool, error) {
crdExist := map[string]bool{}
for _, crdName := range crdNames {
crd := &apiextensionsv1.CustomResourceDefinition{}
crd.Name = crdName
if err := clnt.Get(ctx, client.ObjectKeyFromObject(crd), crd); client.IgnoreNotFound(err) != nil {
return nil, fmt.Errorf("error getting CRD, %v", err)
}
crdExist[crdName] = crd.UID != ""
}
return crdExist, nil
}
7 changes: 2 additions & 5 deletions deploy/csv-templates/ocs-operator.csv.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,8 @@ spec:
name: ocs-operator
spec:
containers:
- args:
- --enable-leader-election
- --health-probe-bind-address=:8081
command:
- ocs-operator
- command:
- crdavail
env:
- name: WATCH_NAMESPACE
valueFrom:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,8 @@ spec:
name: ocs-operator
spec:
containers:
- args:
- --enable-leader-election
- --health-probe-bind-address=:8081
command:
- ocs-operator
- command:
- crdavail
env:
- name: WATCH_NAMESPACE
valueFrom:
Expand Down
11 changes: 11 additions & 0 deletions hack/crdavail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

RESTART_EXIT_CODE=42

while true; do
./usr/local/bin/ocs-operator --enable-leader-election --health-probe-bind-address=:8081
EXIT_CODE=$?
if [ $EXIT_CODE -ne $RESTART_EXIT_CODE ]; then
exit $EXIT_CODE
fi
done
14 changes: 14 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"flag"
"fmt"
"github.com/red-hat-storage/ocs-operator/v4/controllers/crd"
"os"
"runtime"

Expand Down Expand Up @@ -252,6 +253,19 @@ func main() {
os.Exit(1)
}

var crds []string
_, err = util.MapCRDAvailability(context.Background(), apiClient, crds...)
if err != nil {
setupLog.Error(err, "Unable to get CRD")
os.Exit(1)
}
if len(crds) > 0 {
if err = (&crd.CustomResourceDefinitionReconciler{}).SetupWithManager(crds, mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CustomResourceDefinitionReconciler")
os.Exit(1)
}
}

// Set OperatorCondition Upgradeable to True
// We have to at least default the condition to True or
// OLM will use the Readiness condition via our readiness probe instead:
Expand Down

0 comments on commit df31699

Please sign in to comment.