diff --git a/hack/lib/common.sh b/hack/lib/common.sh index 1573ae49281..07ed4371a0f 100644 --- a/hack/lib/common.sh +++ b/hack/lib/common.sh @@ -64,10 +64,40 @@ project_info() { echo "-X ${PROJECT_INFO_PKG}.gitVersion=${GIT_VERSION}" echo "-X ${PROJECT_INFO_PKG}.gitCommit=${GIT_COMMIT}" echo "-X ${PROJECT_INFO_PKG}.buildDate=${BUILD_DATE}" + + maintainingVersions=$(get_maintained_versions | tr " " ",") + versionSeparator="," + echo "-X ${PROJECT_INFO_PKG}.separator=${versionSeparator}" + echo "-X ${PROJECT_INFO_PKG}.maintainingVersions=${maintainingVersions}" } # get_binary_dir_with_arch generated the binary's directory with GOOS and GOARCH. # eg: ./_output/bin/darwin/arm64/ get_binary_dir_with_arch(){ echo $1/$(go env GOOS)/$(go env GOARCH) +} + +# get openyurt versions we still maintain +# returned versions are separated by space +get_maintained_versions() { + # we currently maintain latest 3 versions including all their maintained releases, + # such as v1.0.0-rc1 v1.0.0 v0.7.0 v0.7.1 v0.6.0 v0.6.1 + MAINTAINED_VERSION_NUM=${MAINTAINED_VERSION_NUM:-3} + allVersions=$(git for-each-ref refs/tags --sort=authordate | awk '{print $3}' | awk -F '/' '{print $3}') + latestVersion=$(git for-each-ref refs/tags --sort=authordate | awk 'END{print}' |awk '{print $3}' | awk -F '/' '{print $3}') + major=$(echo $latestVersion | awk -F '.' '{print $1}') + major=${major#v} + minor=$(echo $latestVersion | awk -F '.' '{print $2}') + versions="" + + for ((cnt=0;cnt<$MAINTAINED_VERSION_NUM;cnt++)); do + versions+=" "$(echo $allVersions | tr " " "\n" | grep -E "v$major\.$minor\..*") + if [ $minor -eq 0 ]; then + major=$[$major-1] + minor=$(echo $allVersions | tr " " "\n" | grep -E -o "v$major\.[0-9]+\..*" | awk 'END{print}' | awk -F '.' '{print $2}') + else + minor=$[$minor-1] + fi + done + echo $versions } \ No newline at end of file diff --git a/pkg/projectinfo/projectinfo.go b/pkg/projectinfo/projectinfo.go index c20548b69f8..efd1c0c5e97 100644 --- a/pkg/projectinfo/projectinfo.go +++ b/pkg/projectinfo/projectinfo.go @@ -23,11 +23,13 @@ import ( ) var ( - projectPrefix = "yurt" - labelPrefix = "openyurt.io" - gitVersion = "v0.0.0" - gitCommit = "unknown" - buildDate = "1970-01-01T00:00:00Z" + projectPrefix = "yurt" + labelPrefix = "openyurt.io" + gitVersion = "v0.0.0" + gitCommit = "unknown" + buildDate = "1970-01-01T00:00:00Z" + maintainingVersions = "unknown" + separator = "," ) func ShortAgentVersion() string { @@ -114,22 +116,24 @@ func normalizeGitCommit(commit string) string { // Info contains version information. type Info struct { - GitVersion string `json:"gitVersion"` - GitCommit string `json:"gitCommit"` - BuildDate string `json:"buildDate"` - GoVersion string `json:"goVersion"` - Compiler string `json:"compiler"` - Platform string `json:"platform"` + GitVersion string `json:"gitVersion"` + GitCommit string `json:"gitCommit"` + BuildDate string `json:"buildDate"` + GoVersion string `json:"goVersion"` + Compiler string `json:"compiler"` + Platform string `json:"platform"` + AllVersions []string `json:"allVersions"` } // Get returns the overall codebase version. func Get() Info { return Info{ - GitVersion: gitVersion, - GitCommit: normalizeGitCommit(gitCommit), - BuildDate: buildDate, - GoVersion: runtime.Version(), - Compiler: runtime.Compiler, - Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + GitVersion: gitVersion, + GitCommit: normalizeGitCommit(gitCommit), + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + AllVersions: strings.Split(maintainingVersions, separator), } } diff --git a/pkg/yurtctl/cmd/yurttest/kindinit/init.go b/pkg/yurtctl/cmd/yurttest/kindinit/init.go index 9d36d9faf50..ae10f67e9ef 100644 --- a/pkg/yurtctl/cmd/yurttest/kindinit/init.go +++ b/pkg/yurtctl/cmd/yurttest/kindinit/init.go @@ -36,6 +36,7 @@ import ( "k8s.io/klog/v2" nodeservant "github.com/openyurtio/openyurt/pkg/node-servant" + "github.com/openyurtio/openyurt/pkg/projectinfo" strutil "github.com/openyurtio/openyurt/pkg/util/strings" tmplutil "github.com/openyurtio/openyurt/pkg/util/templates" "github.com/openyurtio/openyurt/pkg/yurtctl/constants" @@ -53,19 +54,11 @@ var ( "v1.22", "v1.23", } - validOpenYurtVersions = []string{ - "v0.5.0", - "v0.6.0", - "v0.6.1", - "v0.7.0", - "v0.7.1", - "v1.0.0", - "latest", - } validKindVersions = []string{ "v0.11.1", "v0.12.0", } + AllValidOpenYurtVersions = append(projectinfo.Get().AllVersions, "latest") kindNodeImageMap = map[string]map[string]string{ "v0.11.1": { @@ -656,9 +649,9 @@ func validateKubernetesVersion(ver string) error { } func validateOpenYurtVersion(ver string, ignoreError bool) error { - if !strutil.IsInStringLst(validOpenYurtVersions, ver) && !ignoreError { + if !strutil.IsInStringLst(AllValidOpenYurtVersions, ver) && !ignoreError { return fmt.Errorf("%s is not a valid openyurt version, all valid versions are %s. If you know what you're doing, you can set --ignore-error", - ver, strings.Join(validOpenYurtVersions, ",")) + ver, strings.Join(AllValidOpenYurtVersions, ",")) } return nil } diff --git a/pkg/yurtctl/cmd/yurttest/kindinit/init_test.go b/pkg/yurtctl/cmd/yurttest/kindinit/init_test.go index c529fda49c1..0b8ffee2ffc 100644 --- a/pkg/yurtctl/cmd/yurttest/kindinit/init_test.go +++ b/pkg/yurtctl/cmd/yurttest/kindinit/init_test.go @@ -21,7 +21,6 @@ import ( "io" "os" "os/exec" - "strings" "testing" v1 "k8s.io/api/apps/v1" @@ -75,35 +74,30 @@ func TestValidateOpenYurtVersion(t *testing.T) { cases := map[string]struct { version string ignore bool - want string + wantErr bool }{ "valid": { "v0.6.0", false, - "", + false, }, "unsupported": { "0.5.10", false, - fmt.Sprintf("0.5.10 is not a valid openyurt version, all valid versions are %s. If you know what you're doing, you can set --ignore-error", - strings.Join(validOpenYurtVersions, ",")), + true, }, "ignoreError": { "0.5.10", true, - "", + false, }, } for name, c := range cases { err := validateOpenYurtVersion(c.version, c.ignore) if err == nil { - if c.want != "" { - t.Errorf("validateOpenYurtVersion failed at case %s, want: %s, got: nil", name, c.want) + if c.wantErr { + t.Errorf("validateOpenYurtVersion failed at case %s, wantErr: %v, got: nil", name, c.wantErr) } - continue - } - if err.Error() != c.want { - t.Errorf("validateOpenYurtVersion failed at case %s, want: %s, got: %s", name, c.want, err.Error()) } } } @@ -190,6 +184,7 @@ nodes: } func TestKindOptions_Validate(t *testing.T) { + AllValidOpenYurtVersions = []string{"v0.6.0", "v0.7.0"} cases1 := []struct { nodeNum int kubernetesVersion string @@ -217,10 +212,10 @@ func TestKindOptions_Validate(t *testing.T) { { 3, "v1.22", - "v0.1.0", + "v0.0.0", false, true, - "v0.1.0 is not a valid openyurt version", + "v0.0.0 is not a valid openyurt version", }, } @@ -229,28 +224,35 @@ func TestKindOptions_Validate(t *testing.T) { kubernetesVersion string openyurtVersion string ignoreErr bool - want error + wantErr bool }{ { 2, "v1.22", "v0.6.0", false, - nil, + false, }, { 2, "v1.22", - "v0.1.0", + "v0.6.0", true, - nil, + false, }, { 1, "v1.22", "v0.100.0", true, - nil, + false, + }, + { + 1, + "v1.22", + "v0.100.0", + false, + true, }, } @@ -261,7 +263,7 @@ func TestKindOptions_Validate(t *testing.T) { o.OpenYurtVersion = v.openyurtVersion o.IgnoreError = v.ignoreErr err := o.Validate() - if v.wantErr && err == nil { + if (v.wantErr && err == nil) || (!v.wantErr && err != nil) { t.Errorf("failed vaildate") } } @@ -272,7 +274,7 @@ func TestKindOptions_Validate(t *testing.T) { o.OpenYurtVersion = v.openyurtVersion o.IgnoreError = v.ignoreErr err := o.Validate() - if err != v.want { + if (v.wantErr && err == nil) || (!v.wantErr && err != nil) { t.Errorf("failed vaildate") } }