You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by mi...@apache.org on 2023/04/12 09:49:20 UTC

[shardingsphere-on-cloud] branch main updated: refactor configmap to common pkg (#306)

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

miaoliyao 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 b69a0da  refactor configmap to common pkg (#306)
b69a0da is described below

commit b69a0da902483174442d34f375ddc456b8f021bb
Author: moomman <85...@users.noreply.github.com>
AuthorDate: Wed Apr 12 17:49:14 2023 +0800

    refactor configmap to common pkg (#306)
    
    Co-authored-by: moonman <ag...@163.com>
---
 .../pkg/controllers/compute_node_controller.go     |  2 +-
 .../pkg/reconcile/common/configmap.go              | 68 ++++++++++++++++++++++
 .../pkg/reconcile/computenode/configmap.go         | 68 +++++++---------------
 .../pkg/reconcile/computenode/configmap_test.go    |  4 +-
 4 files changed, 91 insertions(+), 51 deletions(-)

diff --git a/shardingsphere-operator/pkg/controllers/compute_node_controller.go b/shardingsphere-operator/pkg/controllers/compute_node_controller.go
index feba6df..8e72061 100644
--- a/shardingsphere-operator/pkg/controllers/compute_node_controller.go
+++ b/shardingsphere-operator/pkg/controllers/compute_node_controller.go
@@ -252,7 +252,7 @@ func (r *ComputeNodeReconciler) getServiceByNamespacedName(ctx context.Context,
 }
 
 func (r *ComputeNodeReconciler) createConfigMap(ctx context.Context, cn *v1alpha1.ComputeNode) error {
-	cm := reconcile.NewConfigMap(cn)
+	cm := reconcile.NewCNConfigMap(cn)
 	err := r.Create(ctx, cm)
 	if err != nil && apierrors.IsAlreadyExists(err) || err == nil {
 		return nil
diff --git a/shardingsphere-operator/pkg/reconcile/common/configmap.go b/shardingsphere-operator/pkg/reconcile/common/configmap.go
new file mode 100644
index 0000000..354ceeb
--- /dev/null
+++ b/shardingsphere-operator/pkg/reconcile/common/configmap.go
@@ -0,0 +1,68 @@
+/*
+ * 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 common
+
+import v1 "k8s.io/api/core/v1"
+
+// 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
+	Build() *v1.ConfigMap
+}
+
+// commonConfigMapBuilder common configmap implementation
+type commonConfigMapBuilder struct {
+	configmap *v1.ConfigMap
+}
+
+// NewCommonConfigMapBuilder Create a new common configmap builder
+func NewCommonConfigMapBuilder(configmap *v1.ConfigMap) ConfigMapBuilder {
+	return &commonConfigMapBuilder{configmap}
+}
+
+// SetName set the ConfigMap name
+func (c *commonConfigMapBuilder) SetName(name string) ConfigMapBuilder {
+	c.configmap.Name = name
+	return c
+}
+
+// SetNamespace set the ConfigMap namespace
+func (c *commonConfigMapBuilder) SetNamespace(namespace string) ConfigMapBuilder {
+	c.configmap.Namespace = namespace
+	return c
+}
+
+// SetLabels set the ConfigMap labels
+func (c *commonConfigMapBuilder) SetLabels(labels map[string]string) ConfigMapBuilder {
+	c.configmap.Labels = labels
+	return c
+}
+
+// SetAnnotations set the ConfigMap annotations
+func (c *commonConfigMapBuilder) SetAnnotations(annos map[string]string) ConfigMapBuilder {
+	c.configmap.Annotations = annos
+	return c
+}
+
+// Build returns a ConfigMap
+func (c *commonConfigMapBuilder) Build() *v1.ConfigMap {
+	return c.configmap
+}
diff --git a/shardingsphere-operator/pkg/reconcile/computenode/configmap.go b/shardingsphere-operator/pkg/reconcile/computenode/configmap.go
index 3735541..fdc0fc7 100644
--- a/shardingsphere-operator/pkg/reconcile/computenode/configmap.go
+++ b/shardingsphere-operator/pkg/reconcile/computenode/configmap.go
@@ -21,6 +21,8 @@ import (
 	"encoding/json"
 	"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"
@@ -42,8 +44,8 @@ const (
 	AnnoLogbackConfig = "computenode.shardingsphere.org/logback"
 )
 
-// NewConfigMap returns a new ConfigMap
-func NewConfigMap(cn *v1alpha1.ComputeNode) *v1.ConfigMap {
+// NewCNConfigMap returns a new ConfigMap
+func NewCNConfigMap(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)
 
@@ -89,76 +91,46 @@ func updateConfigMapServerConf(cluster string, servconf *v1alpha1.ServerConfig,
 	return string(y), err
 }
 
-// ConfigMapBuilder is a builder for ConfigMap by ComputeNode
-type ConfigMapBuilder interface {
-	SetName(name string) ConfigMapBuilder
-	SetNamespace(namespace string) ConfigMapBuilder
-	SetLabels(labels map[string]string) ConfigMapBuilder
-	SetAnnotations(annos map[string]string) ConfigMapBuilder
-	SetLogback(logback string) ConfigMapBuilder
-	SetServerConfig(serverConfig string) ConfigMapBuilder
-	SetAgentConfig(agentConfig string) ConfigMapBuilder
-	Build() *v1.ConfigMap
+// 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 ConfigMapBuilder
-func NewConfigMapBuilder(meta metav1.Object, gvk schema.GroupVersionKind) ConfigMapBuilder {
+// NewConfigMapBuilder returns a CNConfigMapBuilder
+func NewConfigMapBuilder(meta metav1.Object, gvk schema.GroupVersionKind) CNConfigMapBuilder {
+	configmap := DefaultConfigMap(meta, gvk)
 	return &configmapBuilder{
-		configmap: DefaultConfigMap(meta, gvk),
+		common.NewCommonConfigMapBuilder(configmap),
+		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
-}
-
 // SetLogback set the ConfigMap data logback
-func (c *configmapBuilder) SetLogback(logback string) ConfigMapBuilder {
+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) ConfigMapBuilder {
+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) ConfigMapBuilder {
+func (c *configmapBuilder) SetAgentConfig(agentConfig string) CNConfigMapBuilder {
 	c.configmap.Data[ConfigDataKeyForAgent] = agentConfig
 	return c
 }
 
-// Build returns a ConfigMap
-func (c *configmapBuilder) Build() *v1.ConfigMap {
-	return c.configmap
-}
-
 // DefaultConfigMap returns a ConfigMap filling with default expected values
 func DefaultConfigMap(meta metav1.Object, gvk schema.GroupVersionKind) *v1.ConfigMap {
 	return &v1.ConfigMap{
@@ -181,7 +153,7 @@ func UpdateConfigMap(cn *v1alpha1.ComputeNode, cur *v1.ConfigMap) *v1.ConfigMap
 	exp.ObjectMeta.ResourceVersion = ""
 	exp.Labels = cur.Labels
 	exp.Annotations = cur.Annotations
-	exp.Data = NewConfigMap(cn).Data
+	exp.Data = NewCNConfigMap(cn).Data
 	return exp
 }
 
diff --git a/shardingsphere-operator/pkg/reconcile/computenode/configmap_test.go b/shardingsphere-operator/pkg/reconcile/computenode/configmap_test.go
index 3574a68..d778a77 100644
--- a/shardingsphere-operator/pkg/reconcile/computenode/configmap_test.go
+++ b/shardingsphere-operator/pkg/reconcile/computenode/configmap_test.go
@@ -56,7 +56,7 @@ var _ = Describe("ConfigMap", func() {
 	})
 
 	Context("Assert ObjectMeta", func() {
-		cm := computenode.NewConfigMap(cn)
+		cm := computenode.NewCNConfigMap(cn)
 		It("name should be equal", func() {
 			Expect(expect.Name).To(Equal(cm.Name))
 		})
@@ -69,7 +69,7 @@ var _ = Describe("ConfigMap", func() {
 	})
 
 	Context("Assert Default Spec Data", func() {
-		cm := computenode.NewConfigMap(cn)
+		cm := computenode.NewCNConfigMap(cn)
 		It("default logback should be equal", func() {
 			Expect(expect.Data[computenode.AnnoLogbackConfig]).To(Equal(cm.Data[computenode.AnnoLogbackConfig]))
 		})