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

refactor reconcile/configmap to common pkg #306

Merged
merged 1 commit into from
Apr 12, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 68 additions & 0 deletions shardingsphere-operator/pkg/reconcile/common/configmap.go
Original file line number Diff line number Diff line change
@@ -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
}
68 changes: 20 additions & 48 deletions shardingsphere-operator/pkg/reconcile/computenode/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand All @@ -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]))
})
Expand Down