-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4163 from CecileRobertMichon/version-util
🌱 Clean up kube version parsing
- Loading branch information
Showing
9 changed files
with
223 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
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 version | ||
|
||
import ( | ||
"github.com/blang/semver" | ||
"github.com/pkg/errors" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
var ( | ||
// KubeSemver is the regex for Kubernetes versions. It requires the "v" prefix. | ||
KubeSemver = regexp.MustCompile(`^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)([-0-9a-zA-Z_\.+]*)?$`) | ||
// KubeSemverTolerant is the regex for Kubernetes versions with an optional "v" prefix. | ||
KubeSemverTolerant = regexp.MustCompile(`^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)([-0-9a-zA-Z_\.+]*)?$`) | ||
) | ||
|
||
// ParseMajorMinorPatch returns a semver.Version from the string provided | ||
// by looking only at major.minor.patch and stripping everything else out. | ||
// It requires the version to have a "v" prefix. | ||
func ParseMajorMinorPatch(version string) (semver.Version, error) { | ||
return parseMajorMinorPatch(version, false) | ||
} | ||
|
||
// ParseMajorMinorPatchTolerant returns a semver.Version from the string provided | ||
// by looking only at major.minor.patch and stripping everything else out. | ||
// It does not require the version to have a "v" prefix. | ||
func ParseMajorMinorPatchTolerant(version string) (semver.Version, error) { | ||
return parseMajorMinorPatch(version, true) | ||
} | ||
|
||
// parseMajorMinorPatch returns a semver.Version from the string provided | ||
// by looking only at major.minor.patch and stripping everything else out. | ||
func parseMajorMinorPatch(version string, tolerant bool) (semver.Version, error) { | ||
groups := KubeSemver.FindStringSubmatch(version) | ||
if tolerant { | ||
groups = KubeSemverTolerant.FindStringSubmatch(version) | ||
} | ||
if len(groups) < 4 { | ||
return semver.Version{}, errors.Errorf("failed to parse major.minor.patch from %q", version) | ||
} | ||
major, err := strconv.ParseUint(groups[1], 10, 64) | ||
if err != nil { | ||
return semver.Version{}, errors.Wrapf(err, "failed to parse major version from %q", version) | ||
} | ||
minor, err := strconv.ParseUint(groups[2], 10, 64) | ||
if err != nil { | ||
return semver.Version{}, errors.Wrapf(err, "failed to parse minor version from %q", version) | ||
} | ||
patch, err := strconv.ParseUint(groups[3], 10, 64) | ||
if err != nil { | ||
return semver.Version{}, errors.Wrapf(err, "failed to parse patch version from %q", version) | ||
} | ||
return semver.Version{ | ||
Major: major, | ||
Minor: minor, | ||
Patch: patch, | ||
}, nil | ||
} |
Oops, something went wrong.