Skip to content

Commit

Permalink
normalize gke versions to be valid.
Browse files Browse the repository at this point in the history
  • Loading branch information
amwat committed Nov 12, 2020
1 parent 6beb03d commit 00fbd26
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion kubetest2-gke/deployer/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package deployer

import (
"fmt"
"regexp"
"strings"
)

Expand All @@ -30,7 +31,11 @@ func (d *deployer) Build() error {
return err
}
version = strings.TrimPrefix(version, "v")
version += ".0+" + d.commonOptions.RunID()
if version, err = normalizeVersion(version); err != nil {
return err
}
// append the kubetest2 run id
version += "+" + d.commonOptions.RunID()
if d.BuildOptions.StageLocation != "" {
if err := d.BuildOptions.Stage(version); err != nil {
return fmt.Errorf("error staging build: %v", err)
Expand All @@ -47,3 +52,31 @@ func (d *deployer) verifyBuildFlags() error {
d.BuildOptions.RepoRoot = d.RepoRoot
return d.BuildOptions.Validate()
}

// ensure that the version is a valid gke version
func normalizeVersion(version string) (string, error) {
// (1.18.10)(-gke)(.601.123)(+existingSuffix)
// capture repeated occurrences of group 3 and don't capture just last
re, err := regexp.Compile(`(\d\.\d+\.\d*)(-\w+)?((?:\.\d+)*)(\+.*)*`)
if err != nil {
return "", err
}
matches := re.FindStringSubmatch(version)
if matches == nil || len(matches) != 5 {
return "", fmt.Errorf("%q is not a valid gke version", version)
}
fmt.Println(matches)
// get the core version as is
finalVersion := matches[1]
// force append -gke, ignore alpha,beta
finalVersion += "-gke"
// add the optional patch number or .0
if matches[3] == "" {
finalVersion += ".0"
} else {
finalVersion += matches[3]
}
// append the optional suffix as is
finalVersion += matches[4]
return finalVersion, nil
}

0 comments on commit 00fbd26

Please sign in to comment.