This is an automated email from the ASF dual-hosted git repository.

sunnianjun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new c2573bb  refactor: update FeatureGate handlers (#219)
c2573bb is described below

commit c2573bbf34d3a801f05e24dd01718e226cee591b
Author: liyao <[email protected]>
AuthorDate: Wed Feb 22 13:09:06 2023 +0800

    refactor: update FeatureGate handlers (#219)
    
    Signed-off-by: mlycore <[email protected]>
---
 .../cmd/shardingsphere-operator/manager/manager.go | 18 +++-----
 .../cmd/shardingsphere-operator/manager/option.go  | 52 +++++++++++++++++++---
 2 files changed, 50 insertions(+), 20 deletions(-)

diff --git 
a/shardingsphere-operator/cmd/shardingsphere-operator/manager/manager.go 
b/shardingsphere-operator/cmd/shardingsphere-operator/manager/manager.go
index c9063d8..254cb95 100644
--- a/shardingsphere-operator/cmd/shardingsphere-operator/manager/manager.go
+++ b/shardingsphere-operator/cmd/shardingsphere-operator/manager/manager.go
@@ -21,9 +21,6 @@ import (
        "context"
        "os"
 
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/configmap"
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/deployment"
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/service"
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/metrics"
 
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/controllers"
@@ -67,16 +64,11 @@ func SetupWithOptions(opts *Options) *Manager {
                os.Exit(1)
        }
 
-       if opts.ComputeNode {
-               if err = (&controllers.ComputeNodeReconciler{
-                       Client:     mgr.GetClient(),
-                       Scheme:     mgr.GetScheme(),
-                       Log:        mgr.GetLogger(),
-                       Deployment: deployment.NewDeployment(mgr.GetClient()),
-                       Service:    service.NewService(mgr.GetClient()),
-                       ConfigMap:  configmap.NewConfigMap(mgr.GetClient()),
-               }).SetupWithManager(mgr); err != nil {
-                       logger.Error(err, "unable to create controller", 
"controller", "ComputeNode")
+       // feature gates handling
+       handlers := opts.ParseFeatureGates()
+       for _, h := range handlers {
+               //FIXME: this will cause panic if there is no handler found
+               if err := h(mgr); err != nil {
                        os.Exit(1)
                }
        }
diff --git 
a/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go 
b/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
index 62cd0db..48216e1 100644
--- a/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
+++ b/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
@@ -19,14 +19,20 @@ package manager
 
 import (
        "flag"
+       "strings"
 
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
+       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/controllers"
+       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/configmap"
+       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/deployment"
+       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/service"
        "go.uber.org/zap/zapcore"
        "k8s.io/apimachinery/pkg/runtime"
        utilruntime "k8s.io/apimachinery/pkg/util/runtime"
        clientgoscheme "k8s.io/client-go/kubernetes/scheme"
        ctrl "sigs.k8s.io/controller-runtime"
        "sigs.k8s.io/controller-runtime/pkg/log/zap"
+       "sigs.k8s.io/controller-runtime/pkg/manager"
 )
 
 var (
@@ -40,12 +46,8 @@ func init() {
 
 type Options struct {
        ctrl.Options
-       FeatureGateOptions
-       ZapOptions zap.Options
-}
-
-type FeatureGateOptions struct {
-       ComputeNode bool
+       FeatureGates string
+       ZapOptions   zap.Options
 }
 
 func ParseOptionsFromCmdFlags() *Options {
@@ -66,7 +68,7 @@ func ParseOptionsFromCmdFlags() *Options {
        flag.BoolVar(&opt.LeaderElection, "leader-elect", false,
                "Enable leader election for controller manager. "+
                        "Enabling this will ensure there is only one active 
controller manager.")
-       flag.BoolVar(&opt.ComputeNode, "feature-gate-compute-node", false, 
"Enable support for CustomResourceDefinition ComputeNode.")
+       flag.StringVar(&opt.FeatureGates, "feature-gates", "", "A set of 
key=value pairs that describe feature gates for alpha/experimental features.")
 
        opt.ZapOptions.BindFlags(flag.CommandLine)
 
@@ -74,3 +76,39 @@ func ParseOptionsFromCmdFlags() *Options {
 
        return opt
 }
+
+func (opts *Options) ParseFeatureGates() []FeatureGateHandler {
+       handlers := []FeatureGateHandler{}
+       if gatesVal := strings.Split(opts.FeatureGates, ","); len(gatesVal) > 0 
{
+               for i := range gatesVal {
+                       gate, enable := func() (string, bool) {
+                               gval := strings.Split(gatesVal[i], "=")
+                               return gval[0], gval[1] == "true"
+                       }()
+
+                       if h, ok := featureGatesHandlers[gate]; ok && enable {
+                               handlers = append(handlers, h)
+                       }
+               }
+       }
+       return handlers
+}
+
+type FeatureGateHandler func(mgr manager.Manager) error
+
+var featureGatesHandlers = map[string]FeatureGateHandler{
+       "ComputeNode": func(mgr manager.Manager) error {
+               if err := (&controllers.ComputeNodeReconciler{
+                       Client:     mgr.GetClient(),
+                       Scheme:     mgr.GetScheme(),
+                       Log:        mgr.GetLogger(),
+                       Deployment: deployment.NewDeployment(mgr.GetClient()),
+                       Service:    service.NewService(mgr.GetClient()),
+                       ConfigMap:  configmap.NewConfigMap(mgr.GetClient()),
+               }).SetupWithManager(mgr); err != nil {
+                       logger.Error(err, "unable to create controller", 
"controller", "ComputeNode")
+                       return err
+               }
+               return nil
+       },
+}

Reply via email to