-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
134 lines (111 loc) · 4.8 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
MIT License
(C) Copyright 2023 Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
package csm
import (
"fmt"
"github.com/spf13/viper"
"golang.org/x/mod/semver"
"log"
"os"
"path"
"regexp"
)
// MinimumVersion defines the minimum CSI API version that this application is back-wards compatible
// with.
// NOTE: This should never change to a higher version, support should grow backwards or remain
//
// the same (e.g. changing to v1.2 is acceptable, changing to v1.5 is not).
const MinimumVersion = "v1.4"
// APIKeyName defines the name of the parameter and input file's key that defines the target API version.
const APIKeyName = "csm-version"
// APIEnvName defines the environment variable to read the CSM installation version from for checking the API
// version against.
const APIEnvName = "CSM_RELEASE"
// normalize ensures that any given string is not a URL, does not have any suffix, has a leading "v".
func normalize(version string) string {
exp := regexp.MustCompile(`^(v?)`)
csm := regexp.MustCompile(`(?i)^v?(csm-?)`)
tar := regexp.MustCompile(`(\.tar\.[bxg]z2?)$`)
v := path.Base(version)
v = csm.ReplaceAllString(v, "")
v = tar.ReplaceAllString(v, "")
v = exp.ReplaceAllString(v, "v")
return v
}
// currentVersion returns the currently targeted API version for the current set of inputs into CSI.
func currentVersion() (string, error) {
v := viper.GetViper()
version := v.GetString(APIKeyName)
if version == "" {
return "", fmt.Errorf("unable to resolve %s from given parameters; key/parameter was undefined", APIKeyName)
}
return normalize(version), nil
}
// IsCompatible returns whether the currently set version is compatible with this variant of CSI or not.
func IsCompatible() (string, error) {
version, err := currentVersion()
if err != nil {
return version, err
}
min := semver.MajorMinor(semver.Canonical(normalize(MinimumVersion)))
set := semver.MajorMinor(semver.Canonical(normalize(version)))
if semver.Compare(set, min) == -1 {
return version, fmt.Errorf("[%s < %s] this edition of `csi` is only compatible with CSM %s and higher but the given parameters have %s set to: %s", set, min, min, APIKeyName, version)
}
return version, nil
}
// DetectedVersion returns the version of CSM detected in the environment by the APIEnvName.
func DetectedVersion() (string, error) {
csmRelease, exists := os.LookupEnv(APIEnvName)
if exists {
csmVersion := normalize(csmRelease)
log.Printf("Interpreting [%s=%s] as %s", APIEnvName, csmRelease, csmVersion)
if !semver.IsValid(csmVersion) {
return "", fmt.Errorf("%s must be a semver version string r'v?[0-9]+\\.[0-9]+(\\.[0-9]+)?(-alpha|beta|rc\\.[0-9]+])?' but was %s", APIEnvName, csmRelease)
}
return csmVersion, nil
}
// note this does not check Kubernetes, it is assumed to be running in an installation environment.
return "", fmt.Errorf("CSM installation or upgrade presence is unknown; %s was not found in the environment", APIEnvName)
}
// Compare does a semver.Compare of the CurrentVersion against a given version.
func Compare(version string) (string, int) {
currentVersion, err := currentVersion()
if err != nil {
return "", 1
}
c := semver.Canonical(normalize(currentVersion))
v := semver.Canonical(normalize(version))
cmp := semver.Compare(c, v)
return currentVersion, cmp
}
// CompareMajorMinor is the same as Compare but only considers the major and minor version numbers.
// Useful for resolving whether a given full-release equal to a parent release.
// (e.g. v1.2.3 vs v1.2.4 evaluates as equal).
func CompareMajorMinor(version string) (string, int) {
currentVersion, err := currentVersion()
if err != nil {
return "", 1
}
c := semver.MajorMinor(semver.Canonical(normalize(currentVersion)))
v := semver.MajorMinor(semver.Canonical(normalize(version)))
cmp := semver.Compare(c, v)
return currentVersion, cmp
}