Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openkruise scheme register configurable #80

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions cmd/controller-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -57,6 +56,7 @@ func main() {
var (
printVersion bool
metricsAddr string
enableKruise bool
enableLeaderElection bool
enableAdmissionWebhook bool
probeAddr string
Expand All @@ -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,
Expand All @@ -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())

Expand All @@ -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)
Expand Down
36 changes: 28 additions & 8 deletions pkg/controller/nebulacluster/nebula_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,24 @@ 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}

// ClusterReconciler reconciles a NebulaCluster object
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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}