diff --git a/ctl/version/version.go b/ctl/version/version.go index 7ae1d981b..3ef5ce547 100644 --- a/ctl/version/version.go +++ b/ctl/version/version.go @@ -23,6 +23,7 @@ import ( "io" "net/http" "os" + "regexp" "strings" "github.com/spf13/cobra" @@ -61,7 +62,12 @@ func runVersion(cmd *cobra.Command, args []string) { if len(args) == 0 { v := version.Get() - cmd.Printf("client version: %s-%s\n", v.GitVersion, v.GitCommit) + if stringMatch(v.GitVersion) { + cmd.Printf("client version: %s\n", v.GitVersion) + } else { + cmd.Printf("client version: %s-%s\n", v.GitVersion, v.GitCommit) + } + podList, err := cli.PodsForSelector(context.TODO(), utils.KmeshNamespace, utils.KmeshLabel) if err != nil { log.Errorf("failed to get kmesh daemon pods: %v", err) @@ -72,7 +78,11 @@ func runVersion(cmd *cobra.Command, args []string) { for _, pod := range podList.Items { v := getVersion(cli, pod.Name) if v.GitVersion != "" { - daemonVersions[v.GitVersion+"-"+v.GitCommit] = daemonVersions[v.GitVersion+"-"+v.GitCommit] + 1 + if stringMatch(v.GitVersion) { + daemonVersions[v.GitVersion] = daemonVersions[v.GitVersion] + 1 + } else { + daemonVersions[v.GitVersion+"-"+v.GitCommit] = daemonVersions[v.GitVersion+"-"+v.GitCommit] + 1 + } } } counts := []string{} @@ -129,3 +139,11 @@ func getVersion(client kube.CLIClient, podName string) (version version.Info) { return } + +// match release version vx.y.z-(alpha) +func stringMatch(str string) bool { + pattern := `^v\d+\.\d+\.\d+(-[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)?$` + regex := regexp.MustCompile(pattern) + + return regex.MatchString(str) +} diff --git a/ctl/version/version_test.go b/ctl/version/version_test.go new file mode 100644 index 000000000..1c37376d6 --- /dev/null +++ b/ctl/version/version_test.go @@ -0,0 +1,73 @@ +/* + * Copyright The Kmesh Authors. + * + * Licensed 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 version + +import "testing" + +func Test_stringMatch(t *testing.T) { + type args struct { + str string + } + tests := []struct { + name string + args args + want bool + }{ + { + name: "normal test", + args: args{ + str: "v1.1.0", + }, + want: true, + }, + { + name: "alpha-version", + args: args{ + str: "v1.1.0-alpha", + }, + want: true, + }, + { + name: "failed example", + args: args{ + str: "7.8.5", + }, + want: false, + }, + { + name: "alpha.0 Suffix", + args: args{ + str: "v1.1.1-alpha.0", + }, + want: true, + }, + { + name: "alpha.bate suffix", + args: args{ + str: "v1.1.1-alpha.beta", + }, + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := stringMatch(tt.args.str); got != tt.want { + t.Errorf("stringMatch() = %v, want %v", got, tt.want) + } + }) + } +}