Skip to content

Commit

Permalink
improves error handling for versions without v prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitropoulos committed Feb 27, 2019
1 parent 8d6a45e commit a2e966b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ func validateKubernetesVersions(old *cfg.Config) string {
if nv == "" {
nv = constants.DefaultKubernetesVersion
}

if nv[0] != version.VersionPrefix {
exit.WithCode(exit.Data, "the --kubernetes-version flag requires the version to start with a 'v'")
}

nvs, err := semver.Make(strings.TrimPrefix(nv, version.VersionPrefix))
if err != nil {
exit.WithCode(exit.Data, "Unable to parse %q: %v", nv, err)
Expand Down
11 changes: 8 additions & 3 deletions pkg/minikube/bootstrapper/kubeadm/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/blang/semver"
"github.com/golang/glog"
"github.com/kubernetes/minikube/pkg/version"
"github.com/pkg/errors"
"k8s.io/kubernetes/cmd/kubeadm/app/features"
"k8s.io/minikube/pkg/minikube/constants"
Expand Down Expand Up @@ -152,9 +153,13 @@ func Supports(featureName string) bool {
return false
}

func ParseKubernetesVersion(version string) (semver.Version, error) {
// Strip leading 'v' prefix from version for semver parsing
v, err := semver.Make(version[1:])
// ParseKubernetesVersion validates the kubernetes version as legitimate
func ParseKubernetesVersion(input string) (semver.Version, error) {
if input[0] != version.VersionPrefix {
return semver.Version{}, fmt.Errorf("version numbers must begin with %v", version.VersionPrefix)
}

v, err := semver.Make(strings.TrimPrefix(input, version.VersionPrefix))
if err != nil {
return semver.Version{}, errors.Wrap(err, "parsing kubernetes version")
}
Expand Down
43 changes: 38 additions & 5 deletions pkg/minikube/bootstrapper/kubeadm/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,45 @@ func TestVersionIsBetween(t *testing.T) {
}

func TestParseKubernetesVersion(t *testing.T) {
version, err := ParseKubernetesVersion("v1.8.0-alpha.5")
if err != nil {
t.Fatalf("Error parsing version: %v", err)
tests := []struct {
description string
version string
comparison string
expected boolean
}{
{
description: "regular 1.x.x version",
version: "1.8.0",
comparison: "1.8.0",
expected: true,
},
{
description: "alpha version",
version: "v1.8.0-alpha.5",
comparison: "1.8.0-alpha.5",
expected: true,
},
{
description: "missing `v` prefix: considered invalid",
version: "1.8.0",
comparison: "1.8.0",
expected: false,
},
}
if version.NE(semver.MustParse("1.8.0-alpha.5")) {
t.Errorf("Expected: %s, Actual:%s", "1.8.0-alpha.5", version)

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
version, err := ParseKubernetesVersion(test.input)
if err != nil {
t.Fatalf("Error parsing version: %v", err)
}

comparison := semver.MustParse(test.comparison)

if version.NE(comparison) == test.expected {
t.Errorf("Expected: %s, Actual: %s", test.expected, version)
}
})
}
}

Expand Down

0 comments on commit a2e966b

Please sign in to comment.