Skip to content

Commit

Permalink
kubetest2-gke automatically resolves the release channel if it's not …
Browse files Browse the repository at this point in the history
…specified
  • Loading branch information
chizhg committed Jun 18, 2021
1 parent 4ed77e6 commit c5ad1e8
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 51 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ require (
github.com/spf13/pflag v1.0.5
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/sys v0.0.0-20201221093633-bc327ba9c2f0 // indirect
google.golang.org/api v0.36.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/apimachinery v0.18.8
k8s.io/klog v1.0.0
k8s.io/release v0.7.1-0.20210204090829-09fb5e3883b8
sigs.k8s.io/boskos v0.0.0-20200710214748-f5935686c7fc
Expand Down
34 changes: 0 additions & 34 deletions kubetest2-gke/deployer/commandutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"path/filepath"
"strings"

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

"sigs.k8s.io/kubetest2/pkg/exec"
Expand Down Expand Up @@ -122,39 +121,6 @@ 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)
}

func containerArgs(args ...string) []string {
return append(append([]string{}, "container"), args...)
}
Expand Down
25 changes: 8 additions & 17 deletions kubetest2-gke/deployer/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -164,6 +163,11 @@ func (d *Deployer) CreateCluster(project string, cluster cluster, subNetworkArgs
}
} else {
args = append(args, "--cluster-version="+d.Version)
releaseChannel, err := resolveReleaseChannelForClusterVersion(d.Version, locationArg)
if err != nil {
return err
}
args = append(args, "--release-channel="+releaseChannel)
}
args = append(args, subNetworkArgs...)
args = append(args, privateClusterArgs...)
Expand Down Expand Up @@ -307,6 +311,9 @@ func (d *Deployer) VerifyUpFlags() error {
if err := validateVersion(d.Version); err != nil {
return err
}
if err := validateReleaseChannel(d.ReleaseChannel); err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -335,19 +342,3 @@ func generateClusterNames(numClusters int, uid string) []string {
}
return clusters
}

func validateVersion(version string) error {
switch version {
case "latest", "":
return nil
default:
re, err := regexp.Compile(`(\d)\.(\d)+(\.(\d)*(.*))?`)
if err != nil {
return err
}
if !re.MatchString(version) {
return fmt.Errorf("unknown version %q", version)
}
}
return nil
}
164 changes: 164 additions & 0 deletions kubetest2-gke/deployer/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
Copyright 2021 The Kubernetes 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 deployer

import (
"fmt"
"regexp"
"strings"

"google.golang.org/api/container/v1"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/kubetest2/pkg/exec"
)

var (
noneReleaseChannel = "None"
rapidReleaseChannel = "rapid"
regularReleaseChannel = "regular"
stableReleaseChannel = "stable"

validReleaseChannels = sets.NewString(noneReleaseChannel, rapidReleaseChannel, regularReleaseChannel, stableReleaseChannel)
)

func validateVersion(version string) error {
switch version {
case "latest", "":
return nil
default:
re, err := regexp.Compile(`(\d)\.(\d)+(\.(\d)*(.*))?`)
if err != nil {
return err
}
if !re.MatchString(version) {
return fmt.Errorf("unknown version %q", version)
}
}
return nil
}

func validateReleaseChannel(releaseChannel string) error {
if releaseChannel != "" && !validReleaseChannels.Has(releaseChannel) {
return fmt.Errorf("%q is not one of the valid release channels %v", releaseChannel, validReleaseChannels)
}
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.
cfg, err := getServerConfig(loc)
if err != nil {
return "", fmt.Errorf("error getting server config: %w", err)
}
for _, channel := range cfg.Channels {
if strings.EqualFold(channel.Channel, 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)
}

// Resolve the valid release channel for the given cluster version.
func resolveReleaseChannelForClusterVersion(clusterVersion, loc string) (string, error) {
if clusterVersion == "" || clusterVersion == "latest" {
// For latest or non cluster version, always use none release channel.
return noneReleaseChannel, nil
}

// Get the server config.
cfg, err := getServerConfig(loc)
if err != nil {
return "", err
}

// Look through the versions not associated with a channel.
for _, v := range cfg.ValidMasterVersions {
if isClusterVersionMatch(clusterVersion, v) {
// Use None release channel if there is a match in the valid master versions.
return noneReleaseChannel, nil
}
}

// Look through all the channels.
for _, channel := range cfg.Channels {
for _, v := range channel.ValidVersions {
if isClusterVersionMatch(clusterVersion, v) {
return toReleaseChannel(channel.Channel)
}
}
}

return noneReleaseChannel, fmt.Errorf("no matched release channel found for cluster version %q", clusterVersion)
}

func toReleaseChannel(channelName string) (string, error) {
switch channelName {
case "RAPID":
return rapidReleaseChannel, nil
case "REGULAR":
return regularReleaseChannel, nil
case "STABLE":
return stableReleaseChannel, nil
default:
return "", fmt.Errorf("selected unknown release channel: %s", channelName)
}
}

// isClusterVersionMatch compares a cluster version against a match version. The match version can
// be used to match against various patch versions. For example, the match version 1.7 will match
// against versions 1.7.3 and 1.7.10.
func isClusterVersionMatch(match, version string) bool {
if match == version {
return true
}

matchParts := strings.Split(match, ".")
parts := strings.Split(version, ".")

if len(parts) < len(matchParts) {
return false
}

for i := 0; i < len(matchParts); i++ {
if matchParts[i] != parts[i] {
return false
}
}
return true
}

func getServerConfig(loc string) (*container.ServerConfig, error) {
// List the available versions for each release channel.
out, err := exec.Output(exec.RawCommand((fmt.Sprintf("gcloud container get-server-config --format=yaml %s", loc))))
if err != nil {
return nil, err
}

// Parse the YAML into a struct.
cfg := &container.ServerConfig{}
if err := yaml.Unmarshal(out, cfg); err != nil {
return nil, err
}

return cfg, nil
}
Loading

0 comments on commit c5ad1e8

Please sign in to comment.