Skip to content

Commit c31313f

Browse files
authored
Merge pull request #1371 from LiZhenCheng9527/kmeshctl-version
fix kmeshctl version report error
2 parents ed69807 + 2a297d2 commit c31313f

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

ctl/version/version.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"net/http"
2525
"os"
26+
"regexp"
2627
"strings"
2728

2829
"github.com/spf13/cobra"
@@ -61,7 +62,12 @@ func runVersion(cmd *cobra.Command, args []string) {
6162

6263
if len(args) == 0 {
6364
v := version.Get()
64-
cmd.Printf("client version: %s-%s\n", v.GitVersion, v.GitCommit)
65+
if stringMatch(v.GitVersion) {
66+
cmd.Printf("client version: %s\n", v.GitVersion)
67+
} else {
68+
cmd.Printf("client version: %s-%s\n", v.GitVersion, v.GitCommit)
69+
}
70+
6571
podList, err := cli.PodsForSelector(context.TODO(), utils.KmeshNamespace, utils.KmeshLabel)
6672
if err != nil {
6773
log.Errorf("failed to get kmesh daemon pods: %v", err)
@@ -72,7 +78,11 @@ func runVersion(cmd *cobra.Command, args []string) {
7278
for _, pod := range podList.Items {
7379
v := getVersion(cli, pod.Name)
7480
if v.GitVersion != "" {
75-
daemonVersions[v.GitVersion+"-"+v.GitCommit] = daemonVersions[v.GitVersion+"-"+v.GitCommit] + 1
81+
if stringMatch(v.GitVersion) {
82+
daemonVersions[v.GitVersion] = daemonVersions[v.GitVersion] + 1
83+
} else {
84+
daemonVersions[v.GitVersion+"-"+v.GitCommit] = daemonVersions[v.GitVersion+"-"+v.GitCommit] + 1
85+
}
7686
}
7787
}
7888
counts := []string{}
@@ -129,3 +139,11 @@ func getVersion(client kube.CLIClient, podName string) (version version.Info) {
129139

130140
return
131141
}
142+
143+
// match release version vx.y.z-(alpha)
144+
func stringMatch(str string) bool {
145+
pattern := `^v\d+\.\d+\.\d+(-[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)?$`
146+
regex := regexp.MustCompile(pattern)
147+
148+
return regex.MatchString(str)
149+
}

ctl/version/version_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright The Kmesh Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package version
18+
19+
import "testing"
20+
21+
func Test_stringMatch(t *testing.T) {
22+
type args struct {
23+
str string
24+
}
25+
tests := []struct {
26+
name string
27+
args args
28+
want bool
29+
}{
30+
{
31+
name: "normal test",
32+
args: args{
33+
str: "v1.1.0",
34+
},
35+
want: true,
36+
},
37+
{
38+
name: "alpha-version",
39+
args: args{
40+
str: "v1.1.0-alpha",
41+
},
42+
want: true,
43+
},
44+
{
45+
name: "failed example",
46+
args: args{
47+
str: "7.8.5",
48+
},
49+
want: false,
50+
},
51+
{
52+
name: "alpha.0 Suffix",
53+
args: args{
54+
str: "v1.1.1-alpha.0",
55+
},
56+
want: true,
57+
},
58+
{
59+
name: "alpha.bate suffix",
60+
args: args{
61+
str: "v1.1.1-alpha.beta",
62+
},
63+
want: true,
64+
},
65+
}
66+
for _, tt := range tests {
67+
t.Run(tt.name, func(t *testing.T) {
68+
if got := stringMatch(tt.args.str); got != tt.want {
69+
t.Errorf("stringMatch() = %v, want %v", got, tt.want)
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)