From 53a99357a8d6807c130fbdca567d66143173ba52 Mon Sep 17 00:00:00 2001 From: "kevin.qiao" Date: Wed, 27 Oct 2021 13:10:04 +0800 Subject: [PATCH 1/2] openkruise scheme register configurable --- cmd/controller-manager/main.go | 12 +++-- .../nebula_cluster_controller.go | 44 +++++++++++++------ 2 files changed, 39 insertions(+), 17 deletions(-) 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..2cfdc6ab 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,17 +158,17 @@ 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 ReconcileWaitResult, errorsutil.ReconcileErrorf("openkruise scheme not registered") + } + if err := r.syncNebulaCluster(nebulaCluster.DeepCopy()); err != nil { if strings.Contains(err.Error(), registry.OptimisticLockErrorMsg) { return ReconcileWaitResult, nil } - isReconcileError := func(err error) (b bool) { - defer func() { - if b { - log.Info("reconcile failed", "error", err) - } - }() + isReconcileError := func(err error) bool { + defer log.Info("reconcile failed", "error", err) return errorsutil.IsReconcileError(err) } @@ -183,12 +189,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) } From e17c3bf4a7c8e2e3c1757ca8716bc30cc933af5c Mon Sep 17 00:00:00 2001 From: "kevin.qiao" Date: Wed, 27 Oct 2021 13:22:48 +0800 Subject: [PATCH 2/2] defer log --- .../nebulacluster/nebula_cluster_controller.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/controller/nebulacluster/nebula_cluster_controller.go b/pkg/controller/nebulacluster/nebula_cluster_controller.go index 2cfdc6ab..7859b13d 100644 --- a/pkg/controller/nebulacluster/nebula_cluster_controller.go +++ b/pkg/controller/nebulacluster/nebula_cluster_controller.go @@ -159,7 +159,7 @@ 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 ReconcileWaitResult, errorsutil.ReconcileErrorf("openkruise scheme not registered") + return ctrl.Result{}, errorsutil.ReconcileErrorf("openkruise scheme not registered") } if err := r.syncNebulaCluster(nebulaCluster.DeepCopy()); err != nil { @@ -167,8 +167,12 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (re return ReconcileWaitResult, nil } - isReconcileError := func(err error) bool { - defer log.Info("reconcile failed", "error", err) + isReconcileError := func(err error) (b bool) { + defer func() { + if b { + log.Info("reconcile failed", "error", err) + } + }() return errorsutil.IsReconcileError(err) }