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

Add released openyurt versions to projectInfo when building binaries #1016

Merged
merged 2 commits into from
Sep 26, 2022
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
30 changes: 30 additions & 0 deletions hack/lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
38 changes: 21 additions & 17 deletions pkg/projectinfo/projectinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
}
}
15 changes: 4 additions & 11 deletions pkg/yurtctl/cmd/yurttest/kindinit/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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": {
Expand Down Expand Up @@ -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
}
Expand Down
44 changes: 23 additions & 21 deletions pkg/yurtctl/cmd/yurttest/kindinit/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"os"
"os/exec"
"strings"
"testing"

v1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -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())
}
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
}

Expand All @@ -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,
},
}

Expand All @@ -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")
}
}
Expand All @@ -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")
}
}
Expand Down