-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reasons for this enhancement: - A controller cannot set up a watch for a CRD that is not installed on the cluster, trying to set up a watch will panic the operator - There is no known way, that we are aware of, to add a watch later without client cache issue How does the enhancement work around the issue: - On start up, detect which CRD are avail (out of a fixed list) and skip watches for ones that are not avail - At the start each reconcile iteration, revalidate which CRD are now available. If a CRD of interest is now avail, exit the op with a known exit code (42) - Have the pod command detect the exit code and if it is the known exist code (42), restart the process This process will guarantee that the pod does restart when a new CRD of interest becomes available. This in turn helps to avoid the following issue: - Pod will not get into CrushLoopBackoff state - There will be no change that the pod becomes unschedulable after restart as of missing resources Signed-off-by: raaizik <[email protected]> Co-Authored-By: Rewant Soni <[email protected]>
- Loading branch information
1 parent
bb238df
commit 68ed6dc
Showing
5 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package crd | ||
|
||
import ( | ||
"context" | ||
"github.com/go-logr/logr" | ||
"github.com/red-hat-storage/ocs-operator/v4/controllers/util" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/klog/v2" | ||
"reflect" | ||
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" | ||
) | ||
|
||
// CustomResourceDefinitionReconciler reconciles a CustomResourceDefinition object | ||
// nolint:revive | ||
type CustomResourceDefinitionReconciler struct { | ||
Client client.Client | ||
ctx context.Context | ||
Log logr.Logger | ||
AvailableCrds map[string]bool | ||
} | ||
|
||
// Reconcile compares available CRDs maps following either a Create or Delete event | ||
func (r *CustomResourceDefinitionReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { | ||
r.ctx = ctx | ||
r.Log.Info("Reconciling CustomResourceDefinition.", "CRD", klog.KRef(request.Namespace, request.Name)) | ||
|
||
var err error | ||
availableCrds, err := util.MapCRDAvailability(ctx, r.Client, util.CRDList...) | ||
if err != nil { | ||
return reconcile.Result{}, err | ||
} | ||
if !reflect.DeepEqual(availableCrds, r.AvailableCrds) { | ||
r.Log.Info("CustomResourceDefinitions created/deleted. Restarting process.") | ||
panic("CustomResourceDefinitions created/deleted. Restarting process.") | ||
} | ||
return reconcile.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up a controller with a manager | ||
func (r *CustomResourceDefinitionReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
crdPredicate := predicate.Funcs{ | ||
CreateFunc: func(e event.TypedCreateEvent[client.Object]) bool { | ||
crdAvailable, keyExist := r.AvailableCrds[e.Object.GetName()] | ||
if keyExist && !crdAvailable { | ||
r.Log.Info("CustomResourceDefinition %s was Created.", e.Object.GetName()) | ||
return true | ||
} | ||
return false | ||
}, | ||
DeleteFunc: func(e event.TypedDeleteEvent[client.Object]) bool { | ||
crdAvailable, keyExist := r.AvailableCrds[e.Object.GetName()] | ||
if keyExist && crdAvailable { | ||
r.Log.Info("CustomResourceDefinition %s was Deleted.", e.Object.GetName()) | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters