diff --git a/cmd/controller-manager/main.go b/cmd/controller-manager/main.go index 27031acd..b3d5851e 100644 --- a/cmd/controller-manager/main.go +++ b/cmd/controller-manager/main.go @@ -46,7 +46,6 @@ var ( func init() { utilruntime.Must(clientgoscheme.AddToScheme(scheme)) utilruntime.Must(v1alpha1.AddToScheme(clientgoscheme.Scheme)) - utilruntime.Must(kruise.AddToScheme(scheme)) utilruntime.Must(v1alpha1.AddToScheme(scheme)) //+kubebuilder:scaffold:scheme } @@ -57,6 +56,7 @@ func main() { var ( printVersion bool metricsAddr string + enableKruise bool enableLeaderElection bool enableAdmissionWebhook bool probeAddr string @@ -66,10 +66,11 @@ func main() { pflag.BoolVar(&printVersion, "version", false, "Show version and quit") pflag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") pflag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") + pflag.BoolVar(&enableKruise, "enable-kruise", false, "Enable openkruise scheme for controller manager.") pflag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") - pflag.BoolVar(&enableAdmissionWebhook, "admission-webhook", false, "Enable admission webhook for controller manager. ") + pflag.BoolVar(&enableAdmissionWebhook, "admission-webhook", false, "Enable admission webhook for controller manager.") pflag.IntVar(&maxConcurrentReconciles, "max-concurrent-reconciles", 2, "The max concurrent reconciles.") opts := logging.Options{ Development: true, @@ -89,6 +90,11 @@ func main() { os.Exit(0) } + if enableKruise { + utilruntime.Must(kruise.AddToScheme(scheme)) + log.Info("register openkruise scheme") + } + log.Info("Welcome to Nebula Operator.") log.Info("Nebula Operator Version", "version", version.Version()) @@ -105,7 +111,7 @@ func main() { os.Exit(1) } - nebulaClusterReconciler, err := nebulacluster.NewClusterReconciler(mgr) + nebulaClusterReconciler, err := nebulacluster.NewClusterReconciler(mgr, enableKruise) if err != nil { log.Error(err, "unable to create nebula cluster reconciler", "controller", "NebulaCluster") os.Exit(1) diff --git a/pkg/controller/nebulacluster/nebula_cluster_controller.go b/pkg/controller/nebulacluster/nebula_cluster_controller.go index 937f5a18..7859b13d 100644 --- a/pkg/controller/nebulacluster/nebula_cluster_controller.go +++ b/pkg/controller/nebulacluster/nebula_cluster_controller.go @@ -43,7 +43,11 @@ import ( errorsutil "github.com/vesoft-inc/nebula-operator/pkg/util/errors" ) -const reconcileTimeOut = 10 * time.Second +const ( + reconcileTimeOut = 10 * time.Second + + KruiseReferenceName = "statefulsets.apps.kruise.io" +) var ReconcileWaitResult = reconcile.Result{RequeueAfter: reconcileTimeOut} @@ -51,11 +55,12 @@ var ReconcileWaitResult = reconcile.Result{RequeueAfter: reconcileTimeOut} type ClusterReconciler struct { Control ControlInterface client.Client - Log logr.Logger - Scheme *runtime.Scheme + Log logr.Logger + Scheme *runtime.Scheme + EnableKruise bool } -func NewClusterReconciler(mgr ctrl.Manager) (*ClusterReconciler, error) { +func NewClusterReconciler(mgr ctrl.Manager, enableKruise bool) (*ClusterReconciler, error) { clientSet, err := kube.NewClientSet(mgr.GetConfig()) if err != nil { return nil, err @@ -103,9 +108,10 @@ func NewClusterReconciler(mgr ctrl.Manager) (*ClusterReconciler, error) { reclaimer.NewPVCReclaimer(clientSet), NewClusterConditionUpdater(), ), - Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("NebulaCluster"), - Scheme: mgr.GetScheme(), + Client: mgr.GetClient(), + Log: ctrl.Log.WithName("controllers").WithName("NebulaCluster"), + Scheme: mgr.GetScheme(), + EnableKruise: enableKruise, }, nil } @@ -152,6 +158,10 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re } log.Info("Start to reconcile") + if !r.EnableKruise && nebulaCluster.Spec.Reference.Name == KruiseReferenceName { + return ctrl.Result{}, errorsutil.ReconcileErrorf("openkruise scheme not registered") + } + if err := r.syncNebulaCluster(nebulaCluster.DeepCopy()); err != nil { if strings.Contains(err.Error(), registry.OptimisticLockErrorMsg) { return ReconcileWaitResult, nil @@ -183,12 +193,22 @@ func (r *ClusterReconciler) syncNebulaCluster(nc *v1alpha1.NebulaCluster) error // SetupWithManager sets up the controller with the Manager. func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager, opts controller.Options) error { + if r.EnableKruise { + return ctrl.NewControllerManagedBy(mgr). + For(&v1alpha1.NebulaCluster{}). + Owns(&corev1.ConfigMap{}). + Owns(&corev1.Service{}). + Owns(&appsv1.StatefulSet{}). + Owns(&kruisev1alpha1.StatefulSet{}). + WithOptions(opts). + Complete(r) + } + return ctrl.NewControllerManagedBy(mgr). For(&v1alpha1.NebulaCluster{}). Owns(&corev1.ConfigMap{}). Owns(&corev1.Service{}). Owns(&appsv1.StatefulSet{}). - Owns(&kruisev1alpha1.StatefulSet{}). WithOptions(opts). Complete(r) }