mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
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.
This commit is contained in:
parent
4b6d91c2c3
commit
b9c2139ccc
@ -79,9 +79,11 @@ func KubernetesReleaseVersion(version string) (string, error) {
|
|||||||
|
|
||||||
// kubeReleaseLabelRegex matches labels such as: latest, latest-1, latest-1.10
|
// kubeReleaseLabelRegex matches labels such as: latest, latest-1, latest-1.10
|
||||||
if kubeReleaseLabelRegex.MatchString(versionLabel) {
|
if kubeReleaseLabelRegex.MatchString(versionLabel) {
|
||||||
var clientVersion string
|
|
||||||
// Try to obtain a client version.
|
// 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.
|
// Fetch version from the internet.
|
||||||
url := fmt.Sprintf("%s/%s.txt", bucketURL, versionLabel)
|
url := fmt.Sprintf("%s/%s.txt", bucketURL, versionLabel)
|
||||||
body, err := fetchFromURL(url, getReleaseVersionTimeout)
|
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)
|
klog.Infof("falling back to the local client version: %s", clientVersion)
|
||||||
return KubernetesReleaseVersion(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
|
// both the client and the remote version are obtained; validate them and pick a stable version
|
||||||
body, err = validateStableVersion(body, clientVersion)
|
body, err = validateStableVersion(body, clientVersion)
|
||||||
if err != nil {
|
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
|
// This is done to conform with "stable-X" and only allow remote versions from
|
||||||
// the same Patch level release.
|
// the same Patch level release.
|
||||||
func validateStableVersion(remoteVersion, clientVersion string) (string, error) {
|
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)
|
verRemote, err := versionutil.ParseGeneric(remoteVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", pkgerrors.Wrap(err, "remote version error")
|
return "", pkgerrors.Wrap(err, "remote version error")
|
||||||
|
@ -421,10 +421,10 @@ func TestValidateStableVersion(t *testing.T) {
|
|||||||
output: "v1.11.0",
|
output: "v1.11.0",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "valid: client version is empty; use remote version",
|
name: "invalid: client version is empty",
|
||||||
remoteVersion: "v1.12.1",
|
remoteVersion: "v1.12.1",
|
||||||
clientVersion: "",
|
clientVersion: "",
|
||||||
output: "v1.12.1",
|
expectedError: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid: error parsing the remote version",
|
name: "invalid: error parsing the remote version",
|
||||||
|
Loading…
Reference in New Issue
Block a user