Skip to content

Commit ef909ce

Browse files
authored
feat(operator): introduce SemVer helpers to ComputeNode (#436)
* chore: add semver go module Signed-off-by: mlycore <maxwell92@126.com> * feat: add semver handle in computenode Signed-off-by: mlycore <maxwell92@126.com> * fix: remove useless log Signed-off-by: mlycore <maxwell92@126.com> * feat: add SemVer helpers Signed-off-by: mlycore <maxwell92@126.com> * feat: inject JAVA env to 5.4.0 Signed-off-by: mlycore <maxwell92@126.com> * chore: add test for semver helpers Signed-off-by: mlycore <maxwell92@126.com> --------- Signed-off-by: mlycore <maxwell92@126.com>
1 parent b79a1b3 commit ef909ce

6 files changed

Lines changed: 168 additions & 8 deletions

File tree

shardingsphere-operator/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/prometheus/client_golang v1.14.0
1818
github.com/stretchr/testify v1.8.1
1919
go.uber.org/zap v1.24.0
20+
golang.org/x/mod v0.9.0
2021
gopkg.in/yaml.v2 v2.4.0
2122
k8s.io/api v0.26.4
2223
k8s.io/apimachinery v0.26.4

shardingsphere-operator/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl
268268
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
269269
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
270270
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
271+
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
272+
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
271273
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
272274
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
273275
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

shardingsphere-operator/pkg/controllers/compute_node_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ func (r *ComputeNodeReconciler) reconcileDeployment(ctx context.Context, cn *v1a
125125
}
126126

127127
if deploy != nil {
128-
fmt.Printf("deploy selector: %s\n", deploy.Spec.Selector.String())
129128
return r.updateDeployment(ctx, cn, deploy)
130129
}
131130
return r.createDeployment(ctx, cn)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package kubernetes
19+
20+
import (
21+
"fmt"
22+
23+
"golang.org/x/mod/semver"
24+
)
25+
26+
var (
27+
supportedShardingSphereVersion = []string{"5.3.0", "5.3.1", "5.3.2", "5.4.0"}
28+
)
29+
30+
func patchHeadingV(version string) string {
31+
return fmt.Sprintf("v%s", version)
32+
}
33+
34+
func IsSupportedShardingSphereVersion(v string) bool {
35+
if !semver.IsValid(patchHeadingV(v)) {
36+
return false
37+
}
38+
39+
for i := range supportedShardingSphereVersion {
40+
if supportedShardingSphereVersion[i] == v {
41+
return true
42+
}
43+
}
44+
45+
return false
46+
}
47+
48+
func VersionBetween(version, left, right string) bool {
49+
if !IsSupportedShardingSphereVersion(version) || !IsSupportedShardingSphereVersion(left) || !IsSupportedShardingSphereVersion(right) {
50+
return false
51+
}
52+
53+
version = patchHeadingV(version)
54+
left = patchHeadingV(left)
55+
right = patchHeadingV(right)
56+
57+
if semver.Compare(version, left) >= 0 && semver.Compare(version, right) <= 0 {
58+
return true
59+
}
60+
return false
61+
}
62+
63+
func VersionGreaterAndEqualThan(version, target string) bool {
64+
if !IsSupportedShardingSphereVersion(target) || !IsSupportedShardingSphereVersion(version) {
65+
return false
66+
}
67+
68+
target = patchHeadingV(target)
69+
version = patchHeadingV(version)
70+
71+
return semver.Compare(version, target) >= 0
72+
}
73+
74+
func VersionExactEqualTo(version, target string) bool {
75+
if !IsSupportedShardingSphereVersion(target) || !IsSupportedShardingSphereVersion(version) {
76+
return false
77+
}
78+
79+
target = patchHeadingV(target)
80+
version = patchHeadingV(version)
81+
82+
return semver.Compare(version, target) == 0
83+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package kubernetes
19+
20+
import (
21+
"fmt"
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func Test_IsSupportedShardingSphereVersion(t *testing.T) {
28+
cases := []struct {
29+
version string
30+
exp bool
31+
}{
32+
{
33+
version: "v5.3.0",
34+
exp: false,
35+
},
36+
{
37+
version: "5.3.0",
38+
exp: true,
39+
},
40+
{
41+
version: "5.3.2",
42+
exp: true,
43+
},
44+
}
45+
46+
for _, c := range cases {
47+
act := IsSupportedShardingSphereVersion(c.version)
48+
assert.Equal(t, c.exp, act, fmt.Sprintf("%s should be valid", c.version))
49+
}
50+
}
51+
52+
func Test_VersionBetween(t *testing.T) {
53+
cases := []struct {
54+
version string
55+
versionRange []string
56+
exp bool
57+
}{
58+
{
59+
version: "5.3.0",
60+
exp: true,
61+
versionRange: []string{"5.3.0", "5.3.2"},
62+
},
63+
}
64+
65+
for _, c := range cases {
66+
act := VersionBetween(c.version, c.versionRange[0], c.versionRange[1])
67+
assert.Equal(t, c.exp, act, fmt.Sprintf("%s should be valid", c.version))
68+
}
69+
}

shardingsphere-operator/pkg/reconcile/computenode/deployment.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323

2424
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/api/v1alpha1"
25+
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes"
2526
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/configmap"
2627
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/container"
2728
"github.com/apache/shardingsphere-on-cloud/shardingsphere-operator/pkg/kubernetes/deployment"
@@ -203,12 +204,16 @@ func (d *shardingsphereDeploymentBuilder) SetAgentBin(cn *v1alpha1.ComputeNode)
203204
d.deployment.Spec.Template.Annotations = metricsAnnos
204205

205206
proxy := d.FindContainerByName("shardingsphere-proxy")
206-
proxy.AppendEnv([]corev1.EnvVar{
207-
{
208-
Name: defaultJavaToolOptionsName,
209-
Value: fmt.Sprintf(defaultJavaAgentEnvValue, cn.Spec.ServerVersion),
210-
},
211-
})
207+
if kubernetes.VersionBetween(cn.Spec.ServerVersion, "5.3.0", "5.4.0") {
208+
proxy.AppendEnv([]corev1.EnvVar{
209+
{
210+
Name: defaultJavaToolOptionsName,
211+
Value: fmt.Sprintf(defaultJavaAgentEnvValue, cn.Spec.ServerVersion),
212+
},
213+
})
214+
} else if kubernetes.VersionGreaterAndEqualThan(cn.Spec.ServerVersion, "5.4.0") {
215+
proxy.SetArgs([]string{"-g"})
216+
}
212217

213218
vbAgentConf := deployment.NewSharedVolumeAndMountBuilder().
214219
SetVolumeMountSize(1).
@@ -242,9 +247,10 @@ func (d *shardingsphereDeploymentBuilder) SetAgentBin(cn *v1alpha1.ComputeNode)
242247

243248
proxy.AppendVolumeMounts([]corev1.VolumeMount{*vmc[0], *vma[0]})
244249

245-
if cn.Spec.ServerVersion == "5.3.2" {
250+
if kubernetes.VersionExactEqualTo(cn.Spec.ServerVersion, "5.3.2") {
246251
d.SetAgentScript(cn)
247252
}
253+
248254
d.UpdateContainerByName(proxy.BuildContainer())
249255
return d
250256
}

0 commit comments

Comments
 (0)