Skip to content

fix kmeshctl version report error #1371

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

Merged
merged 3 commits into from
May 9, 2025
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
22 changes: 20 additions & 2 deletions ctl/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"net/http"
"os"
"regexp"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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)
Expand All @@ -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{}
Expand Down Expand Up @@ -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)
}
73 changes: 73 additions & 0 deletions ctl/version/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading