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 ef909ce  feat(operator): introduce SemVer helpers to ComputeNode (#436)
ef909ce is described below

commit ef909cea15902b512e08044dd242495224bf01dd
Author: liyao <[email protected]>
AuthorDate: Thu Jul 13 19:13:09 2023 +0800

    feat(operator): introduce SemVer helpers to ComputeNode (#436)
    
    * chore: add semver go module
    
    Signed-off-by: mlycore <[email protected]>
    
    * feat: add semver handle in computenode
    
    Signed-off-by: mlycore <[email protected]>
    
    * fix: remove useless log
    
    Signed-off-by: mlycore <[email protected]>
    
    * feat: add SemVer helpers
    
    Signed-off-by: mlycore <[email protected]>
    
    * feat: inject JAVA env to 5.4.0
    
    Signed-off-by: mlycore <[email protected]>
    
    * chore: add test for semver helpers
    
    Signed-off-by: mlycore <[email protected]>
    
    ---------
    
    Signed-off-by: mlycore <[email protected]>
---
 shardingsphere-operator/go.mod                     |  1 +
 shardingsphere-operator/go.sum                     |  2 +
 .../pkg/controllers/compute_node_controller.go     |  1 -
 shardingsphere-operator/pkg/kubernetes/helpers.go  | 83 ++++++++++++++++++++++
 .../pkg/kubernetes/helpers_test.go                 | 69 ++++++++++++++++++
 .../pkg/reconcile/computenode/deployment.go        | 20 ++++--
 6 files changed, 168 insertions(+), 8 deletions(-)

diff --git a/shardingsphere-operator/go.mod b/shardingsphere-operator/go.mod
index a673821..ea6bab1 100644
--- a/shardingsphere-operator/go.mod
+++ b/shardingsphere-operator/go.mod
@@ -17,6 +17,7 @@ require (
        github.com/prometheus/client_golang v1.14.0
        github.com/stretchr/testify v1.8.1
        go.uber.org/zap v1.24.0
+       golang.org/x/mod v0.9.0
        gopkg.in/yaml.v2 v2.4.0
        k8s.io/api v0.26.4
        k8s.io/apimachinery v0.26.4
diff --git a/shardingsphere-operator/go.sum b/shardingsphere-operator/go.sum
index 031d1f2..fc94874 100644
--- a/shardingsphere-operator/go.sum
+++ b/shardingsphere-operator/go.sum
@@ -268,6 +268,8 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod 
h1:6SW0HCj/g11FgYtHl
 golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
 golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
 golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
+golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
+golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
 golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod 
h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod 
h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
 golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod 
h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
diff --git a/shardingsphere-operator/pkg/controllers/compute_node_controller.go 
b/shardingsphere-operator/pkg/controllers/compute_node_controller.go
index 44a0472..a08f32b 100644
--- a/shardingsphere-operator/pkg/controllers/compute_node_controller.go
+++ b/shardingsphere-operator/pkg/controllers/compute_node_controller.go
@@ -125,7 +125,6 @@ func (r *ComputeNodeReconciler) reconcileDeployment(ctx 
context.Context, cn *v1a
        }
 
        if deploy != nil {
-               fmt.Printf("deploy selector: %s\n", 
deploy.Spec.Selector.String())
                return r.updateDeployment(ctx, cn, deploy)
        }
        return r.createDeployment(ctx, cn)
diff --git a/shardingsphere-operator/pkg/kubernetes/helpers.go 
b/shardingsphere-operator/pkg/kubernetes/helpers.go
new file mode 100644
index 0000000..8a4ba93
--- /dev/null
+++ b/shardingsphere-operator/pkg/kubernetes/helpers.go
@@ -0,0 +1,83 @@
+/*
+ * 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 kubernetes
+
+import (
+       "fmt"
+
+       "golang.org/x/mod/semver"
+)
+
+var (
+       supportedShardingSphereVersion = []string{"5.3.0", "5.3.1", "5.3.2", 
"5.4.0"}
+)
+
+func patchHeadingV(version string) string {
+       return fmt.Sprintf("v%s", version)
+}
+
+func IsSupportedShardingSphereVersion(v string) bool {
+       if !semver.IsValid(patchHeadingV(v)) {
+               return false
+       }
+
+       for i := range supportedShardingSphereVersion {
+               if supportedShardingSphereVersion[i] == v {
+                       return true
+               }
+       }
+
+       return false
+}
+
+func VersionBetween(version, left, right string) bool {
+       if !IsSupportedShardingSphereVersion(version) || 
!IsSupportedShardingSphereVersion(left) || 
!IsSupportedShardingSphereVersion(right) {
+               return false
+       }
+
+       version = patchHeadingV(version)
+       left = patchHeadingV(left)
+       right = patchHeadingV(right)
+
+       if semver.Compare(version, left) >= 0 && semver.Compare(version, right) 
<= 0 {
+               return true
+       }
+       return false
+}
+
+func VersionGreaterAndEqualThan(version, target string) bool {
+       if !IsSupportedShardingSphereVersion(target) || 
!IsSupportedShardingSphereVersion(version) {
+               return false
+       }
+
+       target = patchHeadingV(target)
+       version = patchHeadingV(version)
+
+       return semver.Compare(version, target) >= 0
+}
+
+func VersionExactEqualTo(version, target string) bool {
+       if !IsSupportedShardingSphereVersion(target) || 
!IsSupportedShardingSphereVersion(version) {
+               return false
+       }
+
+       target = patchHeadingV(target)
+       version = patchHeadingV(version)
+
+       return semver.Compare(version, target) == 0
+}
diff --git a/shardingsphere-operator/pkg/kubernetes/helpers_test.go 
b/shardingsphere-operator/pkg/kubernetes/helpers_test.go
new file mode 100644
index 0000000..f783beb
--- /dev/null
+++ b/shardingsphere-operator/pkg/kubernetes/helpers_test.go
@@ -0,0 +1,69 @@
+/*
+ * 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 kubernetes
+
+import (
+       "fmt"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func Test_IsSupportedShardingSphereVersion(t *testing.T) {
+       cases := []struct {
+               version string
+               exp     bool
+       }{
+               {
+                       version: "v5.3.0",
+                       exp:     false,
+               },
+               {
+                       version: "5.3.0",
+                       exp:     true,
+               },
+               {
+                       version: "5.3.2",
+                       exp:     true,
+               },
+       }
+
+       for _, c := range cases {
+               act := IsSupportedShardingSphereVersion(c.version)
+               assert.Equal(t, c.exp, act, fmt.Sprintf("%s should be valid", 
c.version))
+       }
+}
+
+func Test_VersionBetween(t *testing.T) {
+       cases := []struct {
+               version      string
+               versionRange []string
+               exp          bool
+       }{
+               {
+                       version:      "5.3.0",
+                       exp:          true,
+                       versionRange: []string{"5.3.0", "5.3.2"},
+               },
+       }
+
+       for _, c := range cases {
+               act := VersionBetween(c.version, c.versionRange[0], 
c.versionRange[1])
+               assert.Equal(t, c.exp, act, fmt.Sprintf("%s should be valid", 
c.version))
+       }
+}
diff --git a/shardingsphere-operator/pkg/reconcile/computenode/deployment.go 
b/shardingsphere-operator/pkg/reconcile/computenode/deployment.go
index 4757b87..d4952bc 100644
--- a/shardingsphere-operator/pkg/reconcile/computenode/deployment.go
+++ b/shardingsphere-operator/pkg/reconcile/computenode/deployment.go
@@ -22,6 +22,7 @@ import (
        "fmt"
 
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
+       
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes"
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/configmap"
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/container"
        
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/deployment"
@@ -203,12 +204,16 @@ func (d *shardingsphereDeploymentBuilder) SetAgentBin(cn 
*v1alpha1.ComputeNode)
        d.deployment.Spec.Template.Annotations = metricsAnnos
 
        proxy := d.FindContainerByName("shardingsphere-proxy")
-       proxy.AppendEnv([]corev1.EnvVar{
-               {
-                       Name:  defaultJavaToolOptionsName,
-                       Value: fmt.Sprintf(defaultJavaAgentEnvValue, 
cn.Spec.ServerVersion),
-               },
-       })
+       if kubernetes.VersionBetween(cn.Spec.ServerVersion, "5.3.0", "5.4.0") {
+               proxy.AppendEnv([]corev1.EnvVar{
+                       {
+                               Name:  defaultJavaToolOptionsName,
+                               Value: fmt.Sprintf(defaultJavaAgentEnvValue, 
cn.Spec.ServerVersion),
+                       },
+               })
+       } else if kubernetes.VersionGreaterAndEqualThan(cn.Spec.ServerVersion, 
"5.4.0") {
+               proxy.SetArgs([]string{"-g"})
+       }
 
        vbAgentConf := deployment.NewSharedVolumeAndMountBuilder().
                SetVolumeMountSize(1).
@@ -242,9 +247,10 @@ func (d *shardingsphereDeploymentBuilder) SetAgentBin(cn 
*v1alpha1.ComputeNode)
 
        proxy.AppendVolumeMounts([]corev1.VolumeMount{*vmc[0], *vma[0]})
 
-       if cn.Spec.ServerVersion == "5.3.2" {
+       if kubernetes.VersionExactEqualTo(cn.Spec.ServerVersion, "5.3.2") {
                d.SetAgentScript(cn)
        }
+
        d.UpdateContainerByName(proxy.BuildContainer())
        return d
 }

Reply via email to