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

feat(operator): introduce SemVer helpers to ComputeNode #436

Merged
merged 6 commits into from
Jul 13, 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
1 change: 1 addition & 0 deletions shardingsphere-operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions shardingsphere-operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
83 changes: 83 additions & 0 deletions shardingsphere-operator/pkg/kubernetes/helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
69 changes: 69 additions & 0 deletions shardingsphere-operator/pkg/kubernetes/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
20 changes: 13 additions & 7 deletions shardingsphere-operator/pkg/reconcile/computenode/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
}
Expand Down
Loading