Skip to content

Commit

Permalink
Allow specifying the latest version for a release channel
Browse files Browse the repository at this point in the history
  • Loading branch information
chizhg committed Oct 13, 2020
1 parent 4e57931 commit 79cd29f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e
gopkg.in/yaml.v2 v2.2.8
k8s.io/klog v1.0.0
sigs.k8s.io/boskos v0.0.0-20200710214748-f5935686c7fc
)
34 changes: 34 additions & 0 deletions kubetest2-gke/deployer/commandutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"strings"

"gopkg.in/yaml.v2"
"k8s.io/klog"

"sigs.k8s.io/kubetest2/pkg/exec"
Expand Down Expand Up @@ -116,3 +117,36 @@ func getClusterCredentials(project, loc, cluster string) error {

return nil
}

// Resolve the current latest version in the given release channel.
func resolveLatestVersionInChannel(loc, channelName string) (string, error) {
// Get the server config for the current location.
out, err := exec.Output(exec.RawCommand(
fmt.Sprintf("gcloud container get-server-config --format=\"yaml(channels:format='yaml(channel,validVersions)')\" %s", loc)))
if err != nil {
return "", fmt.Errorf("failed to get the server config: %w", err)
}

type Channel struct {
Name string `yaml:"channel"`
ValidVersions []string `yaml:"validVersions"`
}
type Channels struct {
Channels []Channel `yaml:"channels"`
}
var cs Channels
if err = yaml.Unmarshal(out, &cs); err != nil {
return "", fmt.Errorf("failed to unmarshal the server config: %w", err)
}

for _, channel := range cs.Channels {
if strings.EqualFold(channel.Name, channelName) {
if len(channel.ValidVersions) == 0 {
return "", fmt.Errorf("no valid versions for channel %q", channelName)
}
return channel.ValidVersions[0], nil
}
}

return "", fmt.Errorf("channel %q does not exist in the server config", channelName)
}
5 changes: 3 additions & 2 deletions kubetest2-gke/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type deployer struct {

RepoRoot string `desc:"Path to root of the kubernetes repo. Used with --build and for dumping cluster logs."`
ReleaseChannel string `desc:"Use a GKE release channel, could be one of empty, rapid, regular and stable - https://cloud.google.com/kubernetes-engine/docs/concepts/release-channels"`
Version string `desc:"Use a specific GKE version e.g. 1.16.13.gke-400 or 'latest'. If --build is specified it will default to building kubernetes from source."`
Version string `desc:"Use a specific GKE version e.g. 1.16.13.gke-400, 'latest' or ''. If --build is specified it will default to building kubernetes from source."`

// doInit helps to make sure the initialization is performed only once
doInit sync.Once
Expand Down Expand Up @@ -166,7 +166,8 @@ func New(opts types.Options) (types.Deployer, *pflag.FlagSet) {
NumClusters: 1,
},
localLogsDir: filepath.Join(opts.ArtifactsDir(), "logs"),
Version: "latest",
// Leave Version as empty to use the default cluster version.
Version: "",
}

// register flags
Expand Down
13 changes: 10 additions & 3 deletions kubetest2-gke/deployer/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,15 @@ func (d *deployer) Up() error {
}
if d.ReleaseChannel != "" {
args = append(args, "--release-channel="+d.ReleaseChannel)
// --cluster-version must be a specific valid version if --release-channel is not empty.
if d.Version != "latest" {
if d.Version == "latest" {
// If latest is specified, get the latest version from server config for this channel.
actualVersion, err := resolveLatestVersionInChannel(loc, d.ReleaseChannel)
if err != nil {
return err
}
klog.V(0).Infof("Using the latest version %q in %q channel", actualVersion, d.ReleaseChannel)
args = append(args, "--cluster-version="+actualVersion)
} else {
args = append(args, "--cluster-version="+d.Version)
}
} else {
Expand Down Expand Up @@ -264,7 +271,7 @@ func generateClusterNames(numClusters int, uid string) []string {

func validateVersion(version string) error {
switch version {
case "latest":
case "latest", "":
return nil
default:
re, err := regexp.Compile(`(\d)\.(\d)+\.(\d)*(.*)`)
Expand Down

0 comments on commit 79cd29f

Please sign in to comment.