You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2022/10/07 15:21:34 UTC

[shardingsphere-on-cloud] branch main updated: chore(reconcile): add unit test for ConstructCascadingDeployment (#70)

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 519e61e  chore(reconcile): add unit test for ConstructCascadingDeployment (#70)
519e61e is described below

commit 519e61e7a05cf63f35f63c43ebce4314979b2ea8
Author: liyao <ma...@126.com>
AuthorDate: Fri Oct 7 23:21:29 2022 +0800

    chore(reconcile): add unit test for ConstructCascadingDeployment (#70)
    
    * fix(reconcile): add nil spec attributions handle
    
    Signed-off-by: mlycore <ma...@126.com>
    
    * chore(test): add unit test for ConstructCascadingDeployment
    
    Signed-off-by: mlycore <ma...@126.com>
---
 .../pkg/reconcile/reconcile_test.go                | 230 +++++++++++++++++++++
 shardingsphere-operator/pkg/reconcile/resource.go  |  21 +-
 2 files changed, 247 insertions(+), 4 deletions(-)

diff --git a/shardingsphere-operator/pkg/reconcile/reconcile_test.go b/shardingsphere-operator/pkg/reconcile/reconcile_test.go
index 55b5e35..2c97d31 100644
--- a/shardingsphere-operator/pkg/reconcile/reconcile_test.go
+++ b/shardingsphere-operator/pkg/reconcile/reconcile_test.go
@@ -18,9 +18,13 @@
 package reconcile
 
 import (
+	"fmt"
+	"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
 	"github.com/stretchr/testify/assert"
+	appsv1 "k8s.io/api/apps/v1"
 	"k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"strconv"
 	"testing"
 )
 
@@ -267,3 +271,229 @@ func Test_CountingReadyPods(t *testing.T) {
 		assert.Equal(t, c.exp, act, c.message)
 	}
 }
+
+func Test_ConstructCascadingDeployment(t *testing.T) {
+	cases := []struct {
+		proxy   *v1alpha1.ShardingSphereProxy
+		exp     *appsv1.Deployment
+		message string
+	}{
+		{
+			exp:     &appsv1.Deployment{},
+			message: "Nil ShardingSphereProxy definition should lead to empty Deployment",
+		},
+		{
+			proxy:   &v1alpha1.ShardingSphereProxy{},
+			exp:     &appsv1.Deployment{},
+			message: "Empty ShardingSphereProxy definition should lead to empty Deployment",
+		},
+		{
+			proxy: &v1alpha1.ShardingSphereProxy{
+				ObjectMeta: metav1.ObjectMeta{
+					Name:      "testname",
+					Namespace: "testnamespace",
+				},
+				Spec: v1alpha1.ProxySpec{
+					Version: "5.1.2",
+					// ServiceType: ServiceTypeNodePort,
+					Replicas:         3,
+					AutomaticScaling: &v1alpha1.AutomaticScaling{},
+					ImagePullSecrets: []v1.LocalObjectReference{},
+					ProxyConfigName:  "shardingsphere-proxy-config",
+					Port:             3307,
+					MySQLDriver:      &v1alpha1.MySQLDriver{},
+					Resources:        &v1.ResourceRequirements{},
+					LivenessProbe: &v1.Probe{
+						ProbeHandler: v1.ProbeHandler{
+							TCPSocket: &v1.TCPSocketAction{},
+						},
+						InitialDelaySeconds: 30,
+						TimeoutSeconds:      3,
+						PeriodSeconds:       5,
+						SuccessThreshold:    1,
+						FailureThreshold:    3,
+					},
+					ReadinessProbe: &v1.Probe{
+						ProbeHandler: v1.ProbeHandler{
+							TCPSocket: &v1.TCPSocketAction{},
+						},
+						InitialDelaySeconds: 30,
+						TimeoutSeconds:      3,
+						PeriodSeconds:       5,
+						SuccessThreshold:    1,
+						FailureThreshold:    3,
+					},
+					StartupProbe: &v1.Probe{
+						ProbeHandler: v1.ProbeHandler{
+							TCPSocket: &v1.TCPSocketAction{},
+						},
+						InitialDelaySeconds: 30,
+						TimeoutSeconds:      3,
+						PeriodSeconds:       5,
+						SuccessThreshold:    1,
+						FailureThreshold:    3,
+					},
+				},
+			},
+			exp: &appsv1.Deployment{
+				ObjectMeta: metav1.ObjectMeta{
+					Name:      "testname",
+					Namespace: "testnamespace",
+				},
+				Spec: appsv1.DeploymentSpec{
+					Strategy: appsv1.DeploymentStrategy{
+						Type: appsv1.RecreateDeploymentStrategyType,
+					},
+					Selector: &metav1.LabelSelector{
+						MatchLabels: map[string]string{
+							"apps": "testname",
+						},
+					},
+					Template: v1.PodTemplateSpec{
+						ObjectMeta: metav1.ObjectMeta{
+							Labels: map[string]string{
+								"apps": "testname",
+							},
+						},
+						Spec: v1.PodSpec{
+							Containers: []v1.Container{
+								{
+									Name:            "proxy",
+									Image:           fmt.Sprintf("%s:%s", "apache/shardingsphere-proxy", "5.1.2"),
+									ImagePullPolicy: v1.PullIfNotPresent,
+									Ports: []v1.ContainerPort{
+										{
+											ContainerPort: 3307,
+										},
+									},
+									Env: []v1.EnvVar{
+										{
+											Name:  "PORT",
+											Value: strconv.FormatInt(int64(3307), 10),
+										},
+									},
+									LivenessProbe: &v1.Probe{
+										ProbeHandler: v1.ProbeHandler{
+											TCPSocket: &v1.TCPSocketAction{},
+										},
+										InitialDelaySeconds: 30,
+										TimeoutSeconds:      3,
+										PeriodSeconds:       5,
+										SuccessThreshold:    1,
+										FailureThreshold:    3,
+									},
+									ReadinessProbe: &v1.Probe{
+										ProbeHandler: v1.ProbeHandler{
+											TCPSocket: &v1.TCPSocketAction{},
+										},
+										InitialDelaySeconds: 30,
+										TimeoutSeconds:      3,
+										PeriodSeconds:       5,
+										SuccessThreshold:    1,
+										FailureThreshold:    3,
+									},
+									StartupProbe: &v1.Probe{
+										ProbeHandler: v1.ProbeHandler{
+											TCPSocket: &v1.TCPSocketAction{},
+										},
+										InitialDelaySeconds: 30,
+										TimeoutSeconds:      3,
+										PeriodSeconds:       5,
+										SuccessThreshold:    1,
+										FailureThreshold:    3,
+									},
+									VolumeMounts: []v1.VolumeMount{
+										{
+											Name:      "config",
+											MountPath: "/opt/shardingsphere-proxy/conf",
+										},
+									},
+								},
+							},
+							Volumes: []v1.Volume{
+								{
+									Name: "config",
+									VolumeSource: v1.VolumeSource{
+										ConfigMap: &v1.ConfigMapVolumeSource{
+											LocalObjectReference: v1.LocalObjectReference{
+												Name: "shardingsphere-proxy-config",
+											},
+										},
+									},
+								},
+							},
+						},
+					},
+				},
+			},
+			message: "Normal ShardingSphereProxy definition should lead to normal Deployment",
+		},
+	}
+
+	for _, c := range cases {
+		act := ConstructCascadingDeployment(c.proxy)
+		assert.Equal(t, c.exp.ObjectMeta.Name, act.ObjectMeta.Name, c.message)
+		assert.Equal(t, c.exp.ObjectMeta.Namespace, act.ObjectMeta.Namespace, c.message)
+		// assert.EqualValues(t, c.exp.Spec, act.Spec, c.message)
+		if c.proxy != nil {
+			if c.proxy.Spec.AutomaticScaling != nil {
+				assert.Equal(t, c.exp.Spec.Replicas, act.Spec.Replicas, c.message)
+			}
+			if c.proxy.Spec.Resources != nil {
+				assert.EqualValues(t, c.exp.Spec.Template.Spec.Containers[0].Resources, act.Spec.Template.Spec.Containers[0].Resources, c.message)
+			}
+			if c.proxy.Spec.LivenessProbe != nil {
+				assert.EqualValues(t, c.exp.Spec.Template.Spec.Containers[0].LivenessProbe, act.Spec.Template.Spec.Containers[0].LivenessProbe, c.message)
+			}
+			if c.proxy.Spec.ReadinessProbe != nil {
+				assert.EqualValues(t, c.exp.Spec.Template.Spec.Containers[0].ReadinessProbe, act.Spec.Template.Spec.Containers[0].ReadinessProbe, c.message)
+			}
+			if c.proxy.Spec.StartupProbe != nil {
+				assert.EqualValues(t, c.exp.Spec.Template.Spec.Containers[0].StartupProbe, act.Spec.Template.Spec.Containers[0].StartupProbe, c.message)
+			}
+			if len(c.proxy.Spec.ImagePullSecrets) > 0 {
+				assert.EqualValues(t, c.exp.Spec.Template.Spec.ImagePullSecrets, act.Spec.Template.Spec.ImagePullSecrets, c.message)
+			}
+		}
+	}
+}
+
+func Test_ConstructCascadingService(t *testing.T) {
+
+}
+
+func Test_addInitContaienr(t *testing.T) {
+
+}
+
+func Test_processOptionalParameter(t *testing.T) {
+
+}
+
+func Test_ConstructCascadingConfigmap(t *testing.T) {
+
+}
+
+func Test_ConstructHPA(t *testing.T) {
+
+}
+
+func Test_ToYAML(t *testing.T) {
+
+}
+
+func Test_UpdateDeployment(t *testing.T) {
+
+}
+
+func Test_UpdateService(t *testing.T) {
+
+}
+
+func Test_UpdateHPA(t *testing.T) {
+
+}
+
+func Test_fromInt32(t *testing.T) {
+
+}
diff --git a/shardingsphere-operator/pkg/reconcile/resource.go b/shardingsphere-operator/pkg/reconcile/resource.go
index 4a49fcf..c7d8dd1 100644
--- a/shardingsphere-operator/pkg/reconcile/resource.go
+++ b/shardingsphere-operator/pkg/reconcile/resource.go
@@ -20,6 +20,7 @@ package reconcile
 import (
 	"fmt"
 	"html/template"
+	"reflect"
 	"strconv"
 	"strings"
 
@@ -68,6 +69,10 @@ var logback = `<?xml version="1.0"?>
 `
 
 func ConstructCascadingDeployment(proxy *v1alpha1.ShardingSphereProxy) *appsv1.Deployment {
+	if proxy == nil || reflect.DeepEqual(proxy, &v1alpha1.ShardingSphereProxy{}) {
+		return &appsv1.Deployment{}
+	}
+
 	dp := &appsv1.Deployment{
 		ObjectMeta: metav1.ObjectMeta{
 			Name:      proxy.Name,
@@ -135,10 +140,18 @@ func ConstructCascadingDeployment(proxy *v1alpha1.ShardingSphereProxy) *appsv1.D
 	if proxy.Spec.AutomaticScaling == nil {
 		dp.Spec.Replicas = &proxy.Spec.Replicas
 	}
-	dp.Spec.Template.Spec.Containers[0].Resources = *proxy.Spec.Resources
-	dp.Spec.Template.Spec.Containers[0].LivenessProbe = proxy.Spec.LivenessProbe
-	dp.Spec.Template.Spec.Containers[0].ReadinessProbe = proxy.Spec.ReadinessProbe
-	dp.Spec.Template.Spec.Containers[0].StartupProbe = proxy.Spec.StartupProbe
+	if proxy.Spec.Resources != nil {
+		dp.Spec.Template.Spec.Containers[0].Resources = *proxy.Spec.Resources
+	}
+	if proxy.Spec.LivenessProbe != nil {
+		dp.Spec.Template.Spec.Containers[0].LivenessProbe = proxy.Spec.LivenessProbe
+	}
+	if proxy.Spec.ReadinessProbe != nil {
+		dp.Spec.Template.Spec.Containers[0].ReadinessProbe = proxy.Spec.ReadinessProbe
+	}
+	if proxy.Spec.StartupProbe != nil {
+		dp.Spec.Template.Spec.Containers[0].StartupProbe = proxy.Spec.StartupProbe
+	}
 	if len(proxy.Spec.ImagePullSecrets) > 0 {
 		dp.Spec.Template.Spec.ImagePullSecrets = proxy.Spec.ImagePullSecrets
 	}