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 c0b4771  refactor(operator): refactor factory for configmap (#336)
c0b4771 is described below

commit c0b4771671d7ade02776cf608ced0547cab1d3c2
Author: liyao <[email protected]>
AuthorDate: Fri May 5 10:20:59 2023 +0800

    refactor(operator): refactor factory for configmap (#336)
    
    * chore: update unit test
    
    Signed-off-by: mlycore <[email protected]>
    
    * wip: refactor configmap according to gvk
    
    Signed-off-by: mlycore <[email protected]>
    
    * refactor: refactor configmap according to gvk
    
    Signed-off-by: mlycore <[email protected]>
    
    * refactor: update style
    
    Signed-off-by: mlycore <[email protected]>
    
    ---------
    
    Signed-off-by: mlycore <[email protected]>
---
 .../cmd/shardingsphere-operator/manager/option.go  |  11 +-
 .../controllers/shardingsphere_chaos_controller.go |  25 +-
 .../pkg/kubernetes/configmap/builder.go            | 178 -------------
 .../pkg/kubernetes/configmap/builders.go           | 287 +++++++++++++++++++++
 .../pkg/kubernetes/configmap/configmap.go          |   9 +-
 .../pkg/kubernetes/configmap/configmap_test.go     |  48 +++-
 .../pkg/kubernetes/configmap/factory.go            | 131 ++++++++++
 .../pkg/reconcile/common/configmap.go              |  35 ++-
 .../pkg/reconcile/shardingspherechaos/configmap.go |   9 +-
 ...dingsphere_chaos.go => shardingsphere_chaos.go} |   0
 10 files changed, 527 insertions(+), 206 deletions(-)

diff --git 
a/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go 
b/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
index f21d44d..1af158a 100644
--- a/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
+++ b/shardingsphere-operator/cmd/shardingsphere-operator/manager/option.go
@@ -21,22 +21,20 @@ import (
        "flag"
        "strings"
 
-       clientset "k8s.io/client-go/kubernetes"
-
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/job"
-
-       batchV1 "k8s.io/api/batch/v1"
-
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/controllers"
        sschaos 
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/chaosmesh"
        
"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/job"
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/service"
+
        chaosv1alpha1 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
        "go.uber.org/zap/zapcore"
+       batchV1 "k8s.io/api/batch/v1"
        "k8s.io/apimachinery/pkg/runtime"
        utilruntime "k8s.io/apimachinery/pkg/util/runtime"
+       clientset "k8s.io/client-go/kubernetes"
        clientgoscheme "k8s.io/client-go/kubernetes/scheme"
        ctrl "sigs.k8s.io/controller-runtime"
        "sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -127,6 +125,7 @@ var featureGatesHandlers = map[string]FeatureGateHandler{
                        logger.Error(err, "unable to create controller", 
"controller", "ComputeNode")
                        return err
                }
+
                return nil
        },
        "StorageNode": func(mgr manager.Manager) error {
diff --git 
a/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go 
b/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
index 1a56743..9fb4e84 100644
--- a/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
+++ b/shardingsphere-operator/pkg/controllers/shardingsphere_chaos_controller.go
@@ -560,16 +560,27 @@ func (r *ShardingSphereChaosReconciler) 
getConfigMapByNamespacedName(ctx context
        return config, nil
 }
 
-func (r *ShardingSphereChaosReconciler) updateConfigMap(ctx context.Context, 
chao *v1alpha1.ShardingSphereChaos, cur *corev1.ConfigMap) error {
-       exp := reconcile.UpdateConfigMap(chao, cur)
-       return r.Update(ctx, exp)
+func (r *ShardingSphereChaosReconciler) updateConfigMap(ctx context.Context, 
chaos *v1alpha1.ShardingSphereChaos, cur *corev1.ConfigMap) error {
+       // exp := reconcile.UpdateShardingSphereChaosConfigMap(chao, cur)
+       exp := r.ConfigMap.Build(ctx, chaos)
+       exp.ObjectMeta = cur.ObjectMeta
+       exp.ObjectMeta.ResourceVersion = ""
+       exp.Labels = cur.Labels
+       exp.Annotations = cur.Annotations
+       return r.ConfigMap.Update(ctx, exp)
+
+       // return r.Update(ctx, exp)
 }
 
 func (r *ShardingSphereChaosReconciler) createConfigMap(ctx context.Context, 
chaos *v1alpha1.ShardingSphereChaos) error {
-       cm := reconcile.NewSSConfigMap(chaos)
-       if err := ctrl.SetControllerReference(chaos, cm, r.Scheme); err != nil {
-               return err
-       }
+       /*
+               cm := reconcile.NewSSConfigMap(chaos)
+               if err := ctrl.SetControllerReference(chaos, cm, r.Scheme); err 
!= nil {
+                       return err
+               }
+       */
+
+       cm := r.ConfigMap.Build(ctx, chaos)
 
        err := r.Create(ctx, cm)
        if err != nil && apierrors.IsAlreadyExists(err) {
diff --git a/shardingsphere-operator/pkg/kubernetes/configmap/builder.go 
b/shardingsphere-operator/pkg/kubernetes/configmap/builder.go
deleted file mode 100644
index 469c11d..0000000
--- a/shardingsphere-operator/pkg/kubernetes/configmap/builder.go
+++ /dev/null
@@ -1,178 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package configmap
-
-import (
-       "reflect"
-
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/reconcile/common"
-
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
-       "gopkg.in/yaml.v2"
-       v1 "k8s.io/api/core/v1"
-       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
-       "k8s.io/apimachinery/pkg/runtime/schema"
-)
-
-const (
-       // ConfigDataKeyForLogback refers to the configuration file name of 
logback
-       ConfigDataKeyForLogback = "logback.xml"
-       // ConfigDataKeyForServer refers to the configuration file name of 
server
-       ConfigDataKeyForServer = "server.yaml"
-       // ConfigDataKeyForAgent refers to the configuration file name of agent
-       ConfigDataKeyForAgent = "agent.yaml"
-
-       // AnnoClusterRepoConfig refers to the content of logback.xml
-       AnnoLogbackConfig = "computenode.shardingsphere.org/logback"
-
-       // DefaultLogback contains the default logback config
-       DefaultLogback = `<?xml version="1.0"?>
-<configuration>
-    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
-        <encoder>
-            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] 
%logger{36} - %msg%n</pattern>
-        </encoder>
-    </appender>
-    <appender name="sqlConsole" class="ch.qos.logback.core.ConsoleAppender">
-        <encoder>
-            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] 
[%X{database}] [%X{user}] [%X{host}] %logger{36} - %msg%n</pattern>
-        </encoder>
-    </appender>
-    
-    <logger name="ShardingSphere-SQL" level="info" additivity="false">
-        <appender-ref ref="sqlConsole" />
-    </logger>
-    <logger name="org.apache.shardingsphere" level="info" additivity="false">
-        <appender-ref ref="console" />
-    </logger>
-    
-    <logger name="com.zaxxer.hikari" level="error" />
-    
-    <logger name="com.atomikos" level="error" />
-    
-    <logger name="io.netty" level="error" />
-    
-    <root>
-        <level value="info" />
-        <appender-ref ref="console" />
-    </root>
-</configuration> 
-`
-       // DefaultServerConfig contains the default server config
-       DefaultServerConfig = "# Empty file is needed"
-)
-
-// NewConfigMap returns a new ConfigMap
-func NewConfigMap(cn *v1alpha1.ComputeNode) *v1.ConfigMap {
-       builder := NewConfigMapBuilder(cn.GetObjectMeta(), 
cn.GetObjectKind().GroupVersionKind())
-       
builder.SetName(cn.Name).SetNamespace(cn.Namespace).SetLabels(cn.Labels).SetAnnotations(cn.Annotations)
-
-       logback := cn.Annotations[AnnoLogbackConfig]
-       if len(logback) > 0 {
-               builder.SetLogback(logback)
-       } else {
-               builder.SetLogback(string(DefaultLogback))
-       }
-
-       // NOTE: ShardingSphere Proxy 5.3.0 needs a server.yaml no matter if it 
is empty
-       if !reflect.DeepEqual(cn.Spec.Bootstrap.ServerConfig, 
v1alpha1.ServerConfig{}) {
-               servconf := cn.Spec.Bootstrap.ServerConfig.DeepCopy()
-               if y, err := yaml.Marshal(servconf); err == nil {
-                       builder.SetServerConfig(string(y))
-               }
-       } else {
-               builder.SetServerConfig(DefaultServerConfig)
-       }
-
-       // load java agent config to configmap if needed
-       if !reflect.DeepEqual(cn.Spec.Bootstrap.AgentConfig, 
v1alpha1.AgentConfig{}) {
-               agentConf := cn.Spec.Bootstrap.AgentConfig.DeepCopy()
-               if y, err := yaml.Marshal(agentConf); err == nil {
-                       builder.SetAgentConfig(string(y))
-               }
-       }
-
-       return builder.Build()
-}
-
-// TODO: check if changed first, then decide if need to respawn the Pods
-// UpdateConfigMap returns a new ConfigMap
-func UpdateConfigMap(cn *v1alpha1.ComputeNode, cur *v1.ConfigMap) 
*v1.ConfigMap {
-       exp := &v1.ConfigMap{}
-       exp.ObjectMeta = cur.ObjectMeta
-       exp.ObjectMeta.ResourceVersion = ""
-       exp.Labels = cur.Labels
-       exp.Annotations = cur.Annotations
-       exp.Data = NewConfigMap(cn).Data
-       return exp
-}
-
-// CNConfigMapBuilder is a builder for ConfigMap by ComputeNode
-type CNConfigMapBuilder interface {
-       common.ConfigMapBuilder
-       SetLogback(logback string) CNConfigMapBuilder
-       SetServerConfig(serverConfig string) CNConfigMapBuilder
-       SetAgentConfig(agentConfig string) CNConfigMapBuilder
-}
-
-type configmapBuilder struct {
-       common.ConfigMapBuilder
-       configmap *v1.ConfigMap
-}
-
-// NewConfigMapBuilder returns a CNConfigMapBuilder
-func NewConfigMapBuilder(meta metav1.Object, gvk schema.GroupVersionKind) 
CNConfigMapBuilder {
-       configmap := DefaultConfigMap(meta, gvk)
-       return &configmapBuilder{
-               common.NewCommonConfigMapBuilder(configmap),
-               configmap,
-       }
-}
-
-// SetLogback set the ConfigMap data logback
-func (c *configmapBuilder) SetLogback(logback string) CNConfigMapBuilder {
-       c.configmap.Data[ConfigDataKeyForLogback] = logback
-       return c
-}
-
-// SetServerConfig set the ConfigMap data server config
-func (c *configmapBuilder) SetServerConfig(serviceConfig string) 
CNConfigMapBuilder {
-       c.configmap.Data[ConfigDataKeyForServer] = serviceConfig
-       return c
-}
-
-// SetAgentConfig set the ConfigMap data agent config
-func (c *configmapBuilder) SetAgentConfig(agentConfig string) 
CNConfigMapBuilder {
-       c.configmap.Data[ConfigDataKeyForAgent] = agentConfig
-       return c
-}
-
-// DefaultConfigMap returns a ConfigMap filling with default expected values
-func DefaultConfigMap(meta metav1.Object, gvk schema.GroupVersionKind) 
*v1.ConfigMap {
-       return &v1.ConfigMap{
-               ObjectMeta: metav1.ObjectMeta{
-                       Name:      "shardingsphere-proxy",
-                       Namespace: "default",
-                       Labels:    map[string]string{},
-                       OwnerReferences: []metav1.OwnerReference{
-                               *metav1.NewControllerRef(meta, gvk),
-                       },
-               },
-               Data: map[string]string{},
-       }
-}
diff --git a/shardingsphere-operator/pkg/kubernetes/configmap/builders.go 
b/shardingsphere-operator/pkg/kubernetes/configmap/builders.go
new file mode 100644
index 0000000..4c68f14
--- /dev/null
+++ b/shardingsphere-operator/pkg/kubernetes/configmap/builders.go
@@ -0,0 +1,287 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package configmap
+
+import (
+       "reflect"
+
+       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
+       "gopkg.in/yaml.v2"
+       corev1 "k8s.io/api/core/v1"
+       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+       "k8s.io/apimachinery/pkg/runtime"
+       "k8s.io/apimachinery/pkg/runtime/schema"
+)
+
+const (
+       // ConfigDataKeyForLogback refers to the configuration file name of 
logback
+       ConfigDataKeyForLogback = "logback.xml"
+       // ConfigDataKeyForServer refers to the configuration file name of 
server
+       ConfigDataKeyForServer = "server.yaml"
+       // ConfigDataKeyForAgent refers to the configuration file name of agent
+       ConfigDataKeyForAgent = "agent.yaml"
+
+       // AnnoClusterRepoConfig refers to the content of logback.xml
+       AnnoLogbackConfig = "computenode.shardingsphere.org/logback"
+
+       // DefaultLogback contains the default logback config
+       DefaultLogback = `<?xml version="1.0"?>
+<configuration>
+    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] 
%logger{36} - %msg%n</pattern>
+        </encoder>
+    </appender>
+    <appender name="sqlConsole" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] 
[%X{database}] [%X{user}] [%X{host}] %logger{36} - %msg%n</pattern>
+        </encoder>
+    </appender>
+    
+    <logger name="ShardingSphere-SQL" level="info" additivity="false">
+        <appender-ref ref="sqlConsole" />
+    </logger>
+    <logger name="org.apache.shardingsphere" level="info" additivity="false">
+        <appender-ref ref="console" />
+    </logger>
+    
+    <logger name="com.zaxxer.hikari" level="error" />
+    
+    <logger name="com.atomikos" level="error" />
+    
+    <logger name="io.netty" level="error" />
+    
+    <root>
+        <level value="info" />
+        <appender-ref ref="console" />
+    </root>
+</configuration> 
+`
+       // DefaultServerConfig contains the default server config
+       DefaultServerConfig = "# Empty file is needed"
+)
+
+// DefaultConfigMap returns a ConfigMap filling with default expected values
+func DefaultConfigMap(meta metav1.Object, gvk schema.GroupVersionKind) 
*corev1.ConfigMap {
+       return &corev1.ConfigMap{
+               ObjectMeta: metav1.ObjectMeta{
+                       Name:      "shardingsphere-proxy",
+                       Namespace: "default",
+                       Labels:    map[string]string{},
+                       OwnerReferences: []metav1.OwnerReference{
+                               *metav1.NewControllerRef(meta, gvk),
+                       },
+               },
+               Data: map[string]string{},
+       }
+}
+
+// NewConfigMap returns a new ConfigMap
+func NewConfigMap(obj runtime.Object) *corev1.ConfigMap {
+       factory := NewConfigMapFactory(obj)
+       builder := factory.NewConfigMapBuilder()
+       return builder.Build()
+}
+
+// TODO: check if changed first, then decide if need to respawn the Pods
+// UpdateConfigMap returns a new ConfigMap
+func UpdateComputeNodeConfigMap(cn *v1alpha1.ComputeNode, cur 
*corev1.ConfigMap) *corev1.ConfigMap {
+       exp := &corev1.ConfigMap{}
+       exp.ObjectMeta = cur.ObjectMeta
+       exp.ObjectMeta.ResourceVersion = ""
+       exp.Labels = cur.Labels
+       exp.Annotations = cur.Annotations
+       exp.Data = NewConfigMap(cn).Data
+       return exp
+}
+
+type computeNodeConfigMapBuilder struct {
+       configMapBuilder
+       obj runtime.Object
+}
+
+// SetData sets the data of ConfigMap
+func (c *computeNodeConfigMapBuilder) SetData(data map[string]string) 
ConfigMapBuilder {
+       if c.configmap.Data == nil {
+               c.configmap.Data = map[string]string{}
+       }
+
+       if val, ok := data[ConfigDataKeyForServer]; ok {
+               c.configmap.Data[ConfigDataKeyForServer] = val
+       }
+
+       if val, ok := data[ConfigDataKeyForLogback]; ok {
+               c.configmap.Data[ConfigDataKeyForLogback] = val
+       }
+
+       if val, ok := data[ConfigDataKeyForAgent]; ok {
+               c.configmap.Data[ConfigDataKeyForAgent] = val
+       }
+
+       return c
+}
+
+// SetBinaryData sets the binary data of ConfigMap
+func (c *computeNodeConfigMapBuilder) SetBinaryData(binary map[string][]byte) 
ConfigMapBuilder {
+       if c.configmap.BinaryData == nil {
+               c.configmap.BinaryData = map[string][]byte{}
+       }
+       if val, ok := binary[ConfigDataKeyForServer]; ok {
+               c.configmap.BinaryData[ConfigDataKeyForServer] = val
+       }
+
+       if val, ok := binary[ConfigDataKeyForLogback]; ok {
+               c.configmap.BinaryData[ConfigDataKeyForLogback] = val
+       }
+
+       if val, ok := binary[ConfigDataKeyForAgent]; ok {
+               c.configmap.BinaryData[ConfigDataKeyForAgent] = val
+       }
+
+       return c
+}
+
+// Build builds the ConfigMap
+func (c *computeNodeConfigMapBuilder) Build() *corev1.ConfigMap {
+       var (
+               cn *v1alpha1.ComputeNode
+               ok bool
+       )
+       if cn, ok = c.obj.(*v1alpha1.ComputeNode); !ok {
+               return nil
+       }
+       
c.SetName(cn.Name).SetNamespace(cn.Namespace).SetLabels(cn.Labels).SetAnnotations(cn.Annotations)
+
+       data := map[string]string{}
+
+       logback := cn.Annotations[AnnoLogbackConfig]
+       if len(logback) > 0 {
+               data[ConfigDataKeyForLogback] = logback
+       } else {
+               data[ConfigDataKeyForLogback] = DefaultLogback
+       }
+
+       // NOTE: ShardingSphere Proxy 5.3.0 needs a server.yaml no matter if it 
is empty
+       if !reflect.DeepEqual(cn.Spec.Bootstrap.ServerConfig, 
v1alpha1.ServerConfig{}) {
+               servconf := cn.Spec.Bootstrap.ServerConfig.DeepCopy()
+               if y, err := yaml.Marshal(servconf); err == nil {
+                       data[ConfigDataKeyForServer] = string(y)
+               }
+       } else {
+               data[ConfigDataKeyForServer] = DefaultServerConfig
+       }
+
+       // load java agent config to configmap if needed
+       if !reflect.DeepEqual(cn.Spec.Bootstrap.AgentConfig, 
v1alpha1.AgentConfig{}) {
+               agentConf := cn.Spec.Bootstrap.AgentConfig.DeepCopy()
+               if y, err := yaml.Marshal(agentConf); err == nil {
+                       data[ConfigDataKeyForAgent] = string(y)
+               }
+       }
+
+       c.SetData(data)
+       return c.configmap
+}
+
+type shardingsphereChaosConfigMapBuilder struct {
+       configMapBuilder
+       obj runtime.Object
+}
+
+// SetData sets the data of ConfigMap
+func (c *shardingsphereChaosConfigMapBuilder) SetData(data map[string]string) 
ConfigMapBuilder {
+       if c.configmap.Data == nil {
+               c.configmap.Data = map[string]string{}
+       }
+       if val, ok := data[configExperimental]; ok {
+               c.configmap.Data[configExperimental] = val
+       }
+
+       if val, ok := data[configVerify]; ok {
+               c.configmap.Data[configVerify] = val
+       }
+
+       if val, ok := data[configPressure]; ok {
+               c.configmap.Data[configPressure] = val
+       }
+
+       return c
+}
+
+// SetBinaryData sets the binary data of ConfigMap
+func (c *shardingsphereChaosConfigMapBuilder) SetBinaryData(binary 
map[string][]byte) ConfigMapBuilder {
+       if c.configmap.BinaryData == nil {
+               c.configmap.BinaryData = map[string][]byte{}
+       }
+       if val, ok := binary[configExperimental]; ok {
+               c.configmap.BinaryData[configExperimental] = val
+       }
+
+       if val, ok := binary[configVerify]; ok {
+               c.configmap.BinaryData[configVerify] = val
+       }
+
+       if val, ok := binary[configPressure]; ok {
+               c.configmap.BinaryData[configPressure] = val
+       }
+
+       return c
+}
+
+// Build builds the ConfigMap
+func (c *shardingsphereChaosConfigMapBuilder) Build() *corev1.ConfigMap {
+       var (
+               chaos *v1alpha1.ShardingSphereChaos
+               ok    bool
+       )
+       if chaos, ok = c.obj.(*v1alpha1.ShardingSphereChaos); !ok {
+               return nil
+       }
+       
c.SetName(chaos.Name).SetNamespace(chaos.Namespace).SetLabels(chaos.Labels).SetAnnotations(chaos.Annotations)
+
+       data := map[string]string{}
+       data[configExperimental] = string(chaos.Spec.InjectJob.Experimental)
+       data[configPressure] = string(chaos.Spec.InjectJob.Pressure)
+       data[configVerify] = string(chaos.Spec.InjectJob.Verify)
+
+       c.SetData(data)
+
+       return c.configmap
+}
+
+const (
+       configExperimental = "experimental.sh"
+       configPressure     = "pressure.sh"
+       configVerify       = "verify.sh"
+)
+
+const (
+       // DefaultConfigMapName is the data key name
+       DefaultConfigMapName = "ssChaos-configmap"
+)
+
+// UpdateConfigMap returns a new ConfigMap
+func UpdateShardingSphereChaosConfigMap(ssChaos *v1alpha1.ShardingSphereChaos, 
cur *corev1.ConfigMap) *corev1.ConfigMap {
+       exp := &corev1.ConfigMap{}
+       exp.ObjectMeta = cur.ObjectMeta
+       exp.Labels = cur.Labels
+       exp.Annotations = cur.Annotations
+       now := NewConfigMap(ssChaos)
+       exp.Data = now.Data
+       return exp
+}
diff --git a/shardingsphere-operator/pkg/kubernetes/configmap/configmap.go 
b/shardingsphere-operator/pkg/kubernetes/configmap/configmap.go
index 79de803..73081a6 100644
--- a/shardingsphere-operator/pkg/kubernetes/configmap/configmap.go
+++ b/shardingsphere-operator/pkg/kubernetes/configmap/configmap.go
@@ -20,10 +20,9 @@ package configmap
 import (
        "context"
 
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
-
        corev1 "k8s.io/api/core/v1"
        apierrors "k8s.io/apimachinery/pkg/api/errors"
+       "k8s.io/apimachinery/pkg/runtime"
        "k8s.io/apimachinery/pkg/types"
        "sigs.k8s.io/controller-runtime/pkg/client"
 )
@@ -61,7 +60,7 @@ type Setter interface {
 
 // Builder build ConfigMap from given ComputeNode
 type Builder interface {
-       Build(context.Context, *v1alpha1.ComputeNode) *corev1.ConfigMap
+       Build(context.Context, runtime.Object) *corev1.ConfigMap
 }
 
 type configmapClient struct {
@@ -104,6 +103,6 @@ func (cs setter) Update(ctx context.Context, cm 
*corev1.ConfigMap) error {
 type builder struct{}
 
 // Build returns a ConfigMap
-func (b builder) Build(ctx context.Context, cn *v1alpha1.ComputeNode) 
*corev1.ConfigMap {
-       return NewConfigMap(cn)
+func (b builder) Build(ctx context.Context, obj runtime.Object) 
*corev1.ConfigMap {
+       return NewConfigMap(obj)
 }
diff --git a/shardingsphere-operator/pkg/kubernetes/configmap/configmap_test.go 
b/shardingsphere-operator/pkg/kubernetes/configmap/configmap_test.go
index 80415eb..2516b04 100644
--- a/shardingsphere-operator/pkg/kubernetes/configmap/configmap_test.go
+++ b/shardingsphere-operator/pkg/kubernetes/configmap/configmap_test.go
@@ -35,6 +35,10 @@ var _ = Describe("Default ConfigMap", func() {
        var (
                expect = &corev1.ConfigMap{}
                cn     = &v1alpha1.ComputeNode{
+                       TypeMeta: metav1.TypeMeta{
+                               Kind:       "ComputeNode",
+                               APIVersion: fmt.Sprintf("%s/%s", 
v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
+                       },
                        ObjectMeta: metav1.ObjectMeta{
                                Name:      "test_name",
                                Namespace: "test_namespace",
@@ -60,6 +64,7 @@ var _ = Describe("Default ConfigMap", func() {
        Context("Assert ObjectMeta", func() {
                c := configmap.NewConfigMapClient(nil)
                cm := c.Build(context.TODO(), cn)
+               fmt.Printf("cm: %#v\n", cm)
 
                It("name should be equal", func() {
                        Expect(expect.Name).To(Equal(cm.Name))
@@ -88,6 +93,20 @@ var _ = Describe("Default ConfigMap", func() {
        })
 
        Context("Assert Update Spec Data", func() {
+               cn.TypeMeta = metav1.TypeMeta{
+                       Kind:       "ComputeNode",
+                       APIVersion: fmt.Sprintf("%s/%s", 
v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
+               }
+               cn.ObjectMeta = metav1.ObjectMeta{
+                       Name:      "test_name",
+                       Namespace: "test_namespace",
+                       Labels: map[string]string{
+                               "test_key": "test_value",
+                       },
+                       Annotations: map[string]string{
+                               "test_anno_key": "test_anno_value",
+                       },
+               }
                cn.Spec.Bootstrap = v1alpha1.BootstrapConfig{
                        ServerConfig: v1alpha1.ServerConfig{
                                Authority: v1alpha1.ComputeNodeAuthority{
@@ -118,7 +137,7 @@ var _ = Describe("Default ConfigMap", func() {
 
                c := configmap.NewConfigMapClient(nil)
                cm := c.Build(context.TODO(), cn)
-               cm = configmap.UpdateConfigMap(cn, cm)
+               cm = configmap.UpdateComputeNodeConfigMap(cn, cm)
                cfg := &v1alpha1.ServerConfig{}
                err := 
yaml.Unmarshal([]byte(cm.Data[configmap.ConfigDataKeyForServer]), &cfg)
                if err != nil {
@@ -142,6 +161,10 @@ var _ = Describe("Default ConfigMap", func() {
 var _ = Describe("Standalone Server Config", func() {
        Context("Assert Simple Service Config Data", func() {
                cn := &v1alpha1.ComputeNode{
+                       TypeMeta: metav1.TypeMeta{
+                               Kind:       "ComputeNode",
+                               APIVersion: fmt.Sprintf("%s/%s", 
v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
+                       },
                        Spec: v1alpha1.ComputeNodeSpec{
                                Bootstrap: v1alpha1.BootstrapConfig{
                                        ServerConfig: v1alpha1.ServerConfig{
@@ -174,6 +197,10 @@ var _ = Describe("Standalone Server Config", func() {
 
        Context("Assert Full Service Config Data", func() {
                cn := &v1alpha1.ComputeNode{
+                       TypeMeta: metav1.TypeMeta{
+                               Kind:       "ComputeNode",
+                               APIVersion: fmt.Sprintf("%s/%s", 
v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
+                       },
                        Spec: v1alpha1.ComputeNodeSpec{
                                Bootstrap: v1alpha1.BootstrapConfig{
                                        ServerConfig: v1alpha1.ServerConfig{
@@ -206,7 +233,6 @@ var _ = Describe("Standalone Server Config", func() {
                if err != nil {
                        fmt.Printf("Err: %s\n", err)
                }
-
                It("server config authority should be equal", func() {
                        
Expect(expect.Authority).To(Equal(cn.Spec.Bootstrap.ServerConfig.Authority))
                })
@@ -223,6 +249,10 @@ var _ = Describe("Cluster Server Config", func() {
        var (
                expect = &v1alpha1.ServerConfig{}
                cn     = &v1alpha1.ComputeNode{
+                       TypeMeta: metav1.TypeMeta{
+                               Kind:       "ComputeNode",
+                               APIVersion: fmt.Sprintf("%s/%s", 
v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
+                       },
                        ObjectMeta: metav1.ObjectMeta{
                                Name:      "test_name",
                                Namespace: "test_namespace",
@@ -290,6 +320,10 @@ var _ = Describe("Logback Config", func() {
                var (
                        expect = ""
                        cn     = &v1alpha1.ComputeNode{
+                               TypeMeta: metav1.TypeMeta{
+                                       Kind:       "ComputeNode",
+                                       APIVersion: fmt.Sprintf("%s/%s", 
v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
+                               },
                                ObjectMeta: metav1.ObjectMeta{
                                        Annotations: map[string]string{
                                                configmap.AnnoLogbackConfig: 
"test_logback_value",
@@ -316,6 +350,10 @@ var _ = Describe("Logback Config", func() {
                var (
                        expect = ""
                        cn     = &v1alpha1.ComputeNode{
+                               TypeMeta: metav1.TypeMeta{
+                                       Kind:       "ComputeNode",
+                                       APIVersion: fmt.Sprintf("%s/%s", 
v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
+                               },
                                ObjectMeta: metav1.ObjectMeta{
                                        Name:      "test_name",
                                        Namespace: "test_namespace",
@@ -346,6 +384,10 @@ var _ = Describe("Agent Config", func() {
                var (
                        expect = &v1alpha1.AgentConfig{}
                        cn     = &v1alpha1.ComputeNode{
+                               TypeMeta: metav1.TypeMeta{
+                                       Kind:       "ComputeNode",
+                                       APIVersion: fmt.Sprintf("%s/%s", 
v1alpha1.GroupVersion.Group, v1alpha1.GroupVersion.Version),
+                               },
                                ObjectMeta: metav1.ObjectMeta{
                                        Name:      "test_name",
                                        Namespace: "test_namespace",
@@ -395,6 +437,8 @@ var _ = Describe("Agent Config", func() {
                c := configmap.NewConfigMapClient(nil)
                cm := c.Build(context.TODO(), cn)
 
+               fmt.Printf("cm: %s\n", cm.Data[configmap.ConfigDataKeyForAgent])
+
                err := 
yaml.Unmarshal([]byte(cm.Data[configmap.ConfigDataKeyForAgent]), &expect)
                if err != nil {
                        fmt.Printf("Err: %s\n", err)
diff --git a/shardingsphere-operator/pkg/kubernetes/configmap/factory.go 
b/shardingsphere-operator/pkg/kubernetes/configmap/factory.go
new file mode 100644
index 0000000..8c9008d
--- /dev/null
+++ b/shardingsphere-operator/pkg/kubernetes/configmap/factory.go
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package configmap
+
+import (
+       v1 "k8s.io/api/core/v1"
+       "k8s.io/apimachinery/pkg/runtime"
+)
+
+type ConfigMapFactory interface {
+       NewConfigMapBuilder() ConfigMapBuilder
+}
+
+func NewConfigMapFactory(obj runtime.Object) ConfigMapFactory {
+       return &configmapFactory{
+               obj: obj,
+       }
+}
+
+type configmapFactory struct {
+       obj runtime.Object
+}
+
+func (c *configmapFactory) NewConfigMapBuilder() ConfigMapBuilder {
+       gvk := c.obj.GetObjectKind().GroupVersionKind()
+
+       if gvk.Group == "shardingsphere.apache.org" {
+               if gvk.Kind == "ComputeNode" && gvk.Version == "v1alpha1" {
+                       return &computeNodeConfigMapBuilder{
+                               obj: c.obj,
+                               configMapBuilder: configMapBuilder{
+                                       configmap: &v1.ConfigMap{},
+                               },
+                       }
+               }
+
+               if gvk.Kind == "ShardingSphereChaos" && gvk.Version == 
"v1alpha1" {
+                       return &shardingsphereChaosConfigMapBuilder{
+                               obj: c.obj,
+                               configMapBuilder: configMapBuilder{
+                                       configmap: &v1.ConfigMap{},
+                               },
+                       }
+               }
+       }
+
+       return &configMapBuilder{
+               configmap: &v1.ConfigMap{},
+       }
+}
+
+// ConfigMapBuilder generic configmap interface
+type ConfigMapBuilder interface {
+       SetName(name string) ConfigMapBuilder
+       SetNamespace(namespace string) ConfigMapBuilder
+       SetLabels(labels map[string]string) ConfigMapBuilder
+       SetAnnotations(annos map[string]string) ConfigMapBuilder
+       SetBinaryData(binaryData map[string][]byte) ConfigMapBuilder
+       SetData(data map[string]string) ConfigMapBuilder
+       Build() *v1.ConfigMap
+}
+
+// configMapBuilder common configmap implementation
+type configMapBuilder struct {
+       configmap *v1.ConfigMap
+}
+
+// NewConfigMapBuilder Create a new common configmap builder
+func NewConfigMapBuilder(configmap *v1.ConfigMap) ConfigMapBuilder {
+       return &configMapBuilder{configmap}
+}
+
+// SetName set the ConfigMap name
+func (c *configMapBuilder) SetName(name string) ConfigMapBuilder {
+       c.configmap.Name = name
+       return c
+}
+
+// SetNamespace set the ConfigMap namespace
+func (c *configMapBuilder) SetNamespace(namespace string) ConfigMapBuilder {
+       c.configmap.Namespace = namespace
+       return c
+}
+
+// SetLabels set the ConfigMap labels
+func (c *configMapBuilder) SetLabels(labels map[string]string) 
ConfigMapBuilder {
+       c.configmap.Labels = labels
+       return c
+}
+
+// SetAnnotations set the ConfigMap annotations
+func (c *configMapBuilder) SetAnnotations(annos map[string]string) 
ConfigMapBuilder {
+       c.configmap.Annotations = annos
+       return c
+}
+
+func (c *configMapBuilder) SetData(data map[string]string) ConfigMapBuilder {
+       if c.configmap.Data == nil {
+               c.configmap.Data = map[string]string{}
+       }
+       c.configmap.Data = data
+       return c
+}
+
+func (c *configMapBuilder) SetBinaryData(binary map[string][]byte) 
ConfigMapBuilder {
+       if c.configmap.BinaryData == nil {
+               c.configmap.BinaryData = map[string][]byte{}
+       }
+       c.configmap.BinaryData = binary
+       return c
+}
+
+// Build returns a ConfigMap
+func (c *configMapBuilder) Build() *v1.ConfigMap {
+       return c.configmap
+}
diff --git a/shardingsphere-operator/pkg/reconcile/common/configmap.go 
b/shardingsphere-operator/pkg/reconcile/common/configmap.go
index 354ceeb..59eacd5 100644
--- a/shardingsphere-operator/pkg/reconcile/common/configmap.go
+++ b/shardingsphere-operator/pkg/reconcile/common/configmap.go
@@ -17,7 +17,38 @@
 
 package common
 
-import v1 "k8s.io/api/core/v1"
+/*
+
+import (
+       v1 "k8s.io/api/core/v1"
+       "k8s.io/apimachinery/pkg/runtime/schema"
+)
+
+type ConfigMapFactory interface {
+       NewConfigMapBuilderFromGVK(gvk schema.GroupVersionKind) ConfigMapBuilder
+}
+
+type configmapFactory struct{}
+
+func (c configmapFactory) NewConfigMapBuilderFromGVK(gvk 
schema.GroupVersionKind) ConfigMapBuilder {
+       if gvk.Group == "shardingsphere.apache.org" {
+               if gvk.Kind == "ComputeNode" && gvk.Version == "v1alpha1" {
+                       return &commonConfigMapBuilder{}
+               }
+       }
+
+               // if gvk.Group == "shardingsphere.apache.org" && gvk.Kind == 
"ShardingSphereChaos" && gvk.Version == "v1alpha1" {
+               //      return shardingsphereChaosConfigMapBuilder{}
+               // }
+
+       return nil
+}
+
+// type computeNodeConfigMapBuilder struct{}
+
+// func (c computeNodeConfigMapFactory) NewConfigMapBuilderFromGVK()
+
+// type shardingsphereChaosConfigMapFactory struct{}
 
 // ConfigMapBuilder generic configmap interface
 type ConfigMapBuilder interface {
@@ -66,3 +97,5 @@ func (c *commonConfigMapBuilder) SetAnnotations(annos 
map[string]string) ConfigM
 func (c *commonConfigMapBuilder) Build() *v1.ConfigMap {
        return c.configmap
 }
+
+*/
diff --git 
a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/configmap.go 
b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/configmap.go
index 431e6f5..78b383b 100644
--- a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/configmap.go
+++ b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/configmap.go
@@ -17,13 +17,6 @@
 
 package shardingspherechaos
 
-import (
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
-       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/reconcile/common"
-       corev1 "k8s.io/api/core/v1"
-       metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
-)
-
 const (
        configExperimental = "experimental.sh"
        configPressure     = "pressure.sh"
@@ -34,6 +27,7 @@ const (
        DefaultConfigMapName = "ssChaos-configmap"
 )
 
+/*
 // NewSSConfigMap returns a new ConfigMap
 func NewSSConfigMap(chaos *v1alpha1.ShardingSphereChaos) *corev1.ConfigMap {
        cmb := NewSSConfigMapBuilder()
@@ -107,3 +101,4 @@ func UpdateConfigMap(ssChaos *v1alpha1.ShardingSphereChaos, 
cur *corev1.ConfigMa
        exp.Data = now.Data
        return exp
 }
+*/
diff --git 
a/shardingsphere-operator/pkg/reconcile/shardingspherechaos/sahrdingsphere_chaos.go
 
b/shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingsphere_chaos.go
similarity index 100%
rename from 
shardingsphere-operator/pkg/reconcile/shardingspherechaos/sahrdingsphere_chaos.go
rename to 
shardingsphere-operator/pkg/reconcile/shardingspherechaos/shardingsphere_chaos.go


Reply via email to