2016-11-21 14:56:10 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 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 util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-08-14 17:19:36 +00:00
|
|
|
kubeReleaseBucketURL = "https://dl.k8s.io"
|
2017-07-05 16:41:57 +00:00
|
|
|
kubeReleaseRegex = regexp.MustCompile(`^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)([-0-9a-zA-Z_\.+]*)?$`)
|
2016-11-21 14:56:10 +00:00
|
|
|
kubeReleaseLabelRegex = regexp.MustCompile(`^[[:lower:]]+(-[-\w_\.]+)?$`)
|
2017-08-14 17:19:36 +00:00
|
|
|
kubeBucketPrefixes = regexp.MustCompile(`^((release|ci|ci-cross)/)?([-\w_\.+]+)$`)
|
2016-11-21 14:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// KubernetesReleaseVersion is helper function that can fetch
|
|
|
|
// available version information from release servers based on
|
|
|
|
// label names, like "stable" or "latest".
|
|
|
|
//
|
|
|
|
// If argument is already semantic version string, it
|
|
|
|
// will return same string.
|
|
|
|
//
|
|
|
|
// In case of labels, it tries to fetch from release
|
|
|
|
// servers and then return actual semantic version.
|
|
|
|
//
|
|
|
|
// Available names on release servers:
|
|
|
|
// stable (latest stable release)
|
|
|
|
// stable-1 (latest stable release in 1.x)
|
|
|
|
// stable-1.0 (and similarly 1.1, 1.2, 1.3, ...)
|
|
|
|
// latest (latest release, including alpha/beta)
|
|
|
|
// latest-1 (latest release in 1.x, including alpha/beta)
|
|
|
|
// latest-1.0 (and similarly 1.1, 1.2, 1.3, ...)
|
|
|
|
func KubernetesReleaseVersion(version string) (string, error) {
|
|
|
|
if kubeReleaseRegex.MatchString(version) {
|
2017-07-05 16:41:57 +00:00
|
|
|
if strings.HasPrefix(version, "v") {
|
|
|
|
return version, nil
|
|
|
|
}
|
|
|
|
return "v" + version, nil
|
2017-08-14 17:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bucketURL, versionLabel, err := splitVersion(version)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if kubeReleaseLabelRegex.MatchString(versionLabel) {
|
|
|
|
url := fmt.Sprintf("%s/%s.txt", bucketURL, versionLabel)
|
|
|
|
body, err := fetchFromURL(url)
|
2016-11-21 14:56:10 +00:00
|
|
|
if err != nil {
|
2017-08-14 17:19:36 +00:00
|
|
|
return "", err
|
2016-11-21 14:56:10 +00:00
|
|
|
}
|
|
|
|
// Re-validate received version and return.
|
2017-08-14 17:19:36 +00:00
|
|
|
return KubernetesReleaseVersion(body)
|
2016-11-21 14:56:10 +00:00
|
|
|
}
|
2016-12-09 20:16:37 +00:00
|
|
|
return "", fmt.Errorf("version %q doesn't match patterns for neither semantic version nor labels (stable, latest, ...)", version)
|
2016-11-21 14:56:10 +00:00
|
|
|
}
|
2017-07-12 11:43:29 +00:00
|
|
|
|
|
|
|
// KubernetesVersionToImageTag is helper function that replaces all
|
|
|
|
// non-allowed symbols in tag strings with underscores.
|
|
|
|
// Image tag can only contain lowercase and uppercase letters, digits,
|
|
|
|
// underscores, periods and dashes.
|
|
|
|
// Current usage is for CI images where all of symbols except '+' are valid,
|
|
|
|
// but function is for generic usage where input can't be always pre-validated.
|
|
|
|
func KubernetesVersionToImageTag(version string) string {
|
|
|
|
allowed := regexp.MustCompile(`[^-a-zA-Z0-9_\.]`)
|
|
|
|
return allowed.ReplaceAllString(version, "_")
|
|
|
|
}
|
2017-08-14 17:19:36 +00:00
|
|
|
|
|
|
|
// KubernetesIsCIVersion checks if user requested CI version
|
|
|
|
func KubernetesIsCIVersion(version string) bool {
|
|
|
|
subs := kubeBucketPrefixes.FindAllStringSubmatch(version, 1)
|
|
|
|
if len(subs) == 1 && len(subs[0]) == 4 && strings.HasPrefix(subs[0][2], "ci") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal helper: split version parts,
|
|
|
|
// Return base URL and cleaned-up version
|
|
|
|
func splitVersion(version string) (string, string, error) {
|
|
|
|
var urlSuffix string
|
|
|
|
subs := kubeBucketPrefixes.FindAllStringSubmatch(version, 1)
|
|
|
|
if len(subs) != 1 || len(subs[0]) != 4 {
|
|
|
|
return "", "", fmt.Errorf("invalid version %q", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(subs[0][2], "ci"):
|
|
|
|
// Special case. CI images populated only by ci-cross area
|
|
|
|
urlSuffix = "ci-cross"
|
|
|
|
default:
|
|
|
|
urlSuffix = "release"
|
|
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", kubeReleaseBucketURL, urlSuffix)
|
|
|
|
return url, subs[0][3], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal helper: return content of URL
|
|
|
|
func fetchFromURL(url string) (string, error) {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to get URL %q: %s", url, err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return "", fmt.Errorf("unable to fetch file. URL: %q Status: %v", url, resp.Status)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to read content of URL %q: %s", url, err.Error())
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(body)), nil
|
|
|
|
}
|