kubeadm: refactor validateStableVersion()

Currently the function `cmd/kubeadm/app/util.validateStableVersion()`
doesn't validate remote versions in the special case when the client
version is empty. This makes the code more difficult to reason about,
because the function may successfully return a string which isn't a valid version.

Move handling the special case outside of the function to the place
where its meaning is more obvious.
pull/564/head
Dmitry Rozhkov 2018-12-14 12:26:46 +02:00
parent 4b6d91c2c3
commit b9c2139ccc
2 changed files with 12 additions and 9 deletions

View File

@ -79,9 +79,11 @@ func KubernetesReleaseVersion(version string) (string, error) {
// kubeReleaseLabelRegex matches labels such as: latest, latest-1, latest-1.10
if kubeReleaseLabelRegex.MatchString(versionLabel) {
var clientVersion string
// Try to obtain a client version.
clientVersion, _ = kubeadmVersion(pkgversion.Get().String())
// pkgversion.Get().String() should always return a correct version added by the golang
// linker and the build system. The version can still be missing when doing unit tests
// on individual packages.
clientVersion, clientVersionErr := kubeadmVersion(pkgversion.Get().String())
// Fetch version from the internet.
url := fmt.Sprintf("%s/%s.txt", bucketURL, versionLabel)
body, err := fetchFromURL(url, getReleaseVersionTimeout)
@ -95,6 +97,12 @@ func KubernetesReleaseVersion(version string) (string, error) {
klog.Infof("falling back to the local client version: %s", clientVersion)
return KubernetesReleaseVersion(clientVersion)
}
if clientVersionErr != nil {
klog.Warningf("could not obtain client version; using remote version: %s", body)
return KubernetesReleaseVersion(body)
}
// both the client and the remote version are obtained; validate them and pick a stable version
body, err = validateStableVersion(body, clientVersion)
if err != nil {
@ -216,11 +224,6 @@ func kubeadmVersion(info string) (string, error) {
// This is done to conform with "stable-X" and only allow remote versions from
// the same Patch level release.
func validateStableVersion(remoteVersion, clientVersion string) (string, error) {
if clientVersion == "" {
klog.Infof("could not obtain client version; using remote version: %s", remoteVersion)
return remoteVersion, nil
}
verRemote, err := versionutil.ParseGeneric(remoteVersion)
if err != nil {
return "", pkgerrors.Wrap(err, "remote version error")

View File

@ -421,10 +421,10 @@ func TestValidateStableVersion(t *testing.T) {
output: "v1.11.0",
},
{
name: "valid: client version is empty; use remote version",
name: "invalid: client version is empty",
remoteVersion: "v1.12.1",
clientVersion: "",
output: "v1.12.1",
expectedError: true,
},
{
name: "invalid: error parsing the remote version",