forked from Azure/aks-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
395 lines (369 loc) · 11.3 KB
/
versions.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package common
import (
"fmt"
"sort"
"strings"
"github.com/blang/semver"
)
// AllKubernetesSupportedVersions is a whitelist map of all supported Kubernetes version strings
// The bool value indicates if creating new clusters with this version is allowed
var AllKubernetesSupportedVersions = map[string]bool{
"1.6.6": false,
"1.6.9": true, // need to keep 1.6.9 version support for v20160930
"1.6.11": false,
"1.6.12": false,
"1.6.13": false,
"1.7.0": false,
"1.7.1": false,
"1.7.2": false,
"1.7.4": false,
"1.7.5": false,
"1.7.7": false,
"1.7.9": false,
"1.7.10": false,
"1.7.12": false,
"1.7.13": false,
"1.7.14": false,
"1.7.15": true,
"1.7.16": true,
"1.8.0": false,
"1.8.1": false,
"1.8.2": false,
"1.8.4": false,
"1.8.6": false,
"1.8.7": false,
"1.8.8": false,
"1.8.9": false,
"1.8.10": false,
"1.8.11": false,
"1.8.12": false,
"1.8.13": false,
"1.8.14": true,
"1.8.15": true,
"1.9.0": false,
"1.9.1": false,
"1.9.2": false,
"1.9.3": false,
"1.9.4": false,
"1.9.5": false,
"1.9.6": false,
"1.9.7": false,
"1.9.8": false,
"1.9.9": false,
"1.9.10": true,
"1.9.11": true,
"1.10.0-beta.2": false,
"1.10.0-beta.4": false,
"1.10.0-rc.1": false,
"1.10.0": false,
"1.10.1": false,
"1.10.2": false,
"1.10.3": false,
"1.10.4": false,
"1.10.5": false,
"1.10.6": false,
"1.10.7": false,
"1.10.8": false,
"1.10.9": true,
"1.10.12": true,
"1.11.0-alpha.1": false,
"1.11.0-alpha.2": false,
"1.11.0-beta.1": false,
"1.11.0-beta.2": false,
"1.11.0-rc.1": false,
"1.11.0-rc.2": false,
"1.11.0-rc.3": false,
"1.11.0": false,
"1.11.1": false,
"1.11.2": false,
"1.11.3": false,
"1.11.4": false,
"1.11.5": true,
"1.11.6": true,
"1.12.0-alpha.1": false,
"1.12.0-beta.0": false,
"1.12.0-beta.1": false,
"1.12.0-rc.1": false,
"1.12.0-rc.2": false,
"1.12.0": false,
"1.12.1": false,
"1.12.2": true,
"1.12.4": true,
"1.13.0-alpha.1": false,
"1.13.0-alpha.2": false,
"1.13.1": true,
}
// GetDefaultKubernetesVersion returns the default Kubernetes version, that is the latest patch of the default release
func GetDefaultKubernetesVersion(hasWindows bool) string {
defaultRelease := KubernetesDefaultRelease
if hasWindows {
defaultRelease = KubernetesDefaultReleaseWindows
}
return GetLatestPatchVersion(defaultRelease, GetAllSupportedKubernetesVersions(false, hasWindows))
}
// GetSupportedKubernetesVersion verifies that a passed-in version string is supported, or returns a default version string if not
func GetSupportedKubernetesVersion(version string, hasWindows bool) string {
k8sVersion := GetDefaultKubernetesVersion(hasWindows)
if hasWindows {
if AllKubernetesWindowsSupportedVersions[version] {
k8sVersion = version
}
} else {
if AllKubernetesSupportedVersions[version] {
k8sVersion = version
}
}
return k8sVersion
}
// GetAllSupportedKubernetesVersions returns a slice of all supported Kubernetes versions
func GetAllSupportedKubernetesVersions(isUpdate, hasWindows bool) []string {
var versions []string
allSupportedVersions := AllKubernetesSupportedVersions
if hasWindows {
allSupportedVersions = AllKubernetesWindowsSupportedVersions
}
for ver, supported := range allSupportedVersions {
if isUpdate || supported {
versions = append(versions, ver)
}
}
sort.Slice(versions, func(i, j int) bool {
return IsKubernetesVersionGe(versions[j], versions[i])
})
return versions
}
// GetVersionsGt returns a list of versions greater than a semver string given a list of versions
// inclusive=true means that we test for equality as well
// preReleases=true means that we include pre-release versions in the list
func GetVersionsGt(versions []string, version string, inclusive, preReleases bool) []string {
// Try to get latest version matching the release
var ret []string
minVersion, _ := semver.Make(version)
for _, v := range versions {
sv, _ := semver.Make(v)
if !preReleases && len(sv.Pre) != 0 {
continue
}
if (inclusive && sv.GTE(minVersion)) || (!inclusive && sv.GT(minVersion)) {
ret = append(ret, v)
}
}
return ret
}
// GetVersionsLt returns a list of versions less than than a semver string given a list of versions
// inclusive=true means that we test for equality as well
// preReleases=true means that we include pre-release versions in the list
func GetVersionsLt(versions []string, version string, inclusive, preReleases bool) []string {
// Try to get latest version matching the release
var ret []string
minVersion, _ := semver.Make(version)
for _, v := range versions {
sv, _ := semver.Make(v)
if !preReleases && len(sv.Pre) != 0 {
continue
}
if (inclusive && sv.LTE(minVersion)) || (!inclusive && sv.LT(minVersion)) {
ret = append(ret, v)
}
}
return ret
}
// GetVersionsBetween returns a list of versions between a min and max
// inclusive=true means that we test for equality on both bounds
// preReleases=true means that we include pre-release versions in the list
func GetVersionsBetween(versions []string, versionMin, versionMax string, inclusive, preReleases bool) []string {
var ret []string
if minV, _ := semver.Make(versionMin); len(minV.Pre) != 0 {
preReleases = true
}
greaterThan := GetVersionsGt(versions, versionMin, inclusive, preReleases)
lessThan := GetVersionsLt(versions, versionMax, inclusive, preReleases)
for _, lv := range lessThan {
for _, gv := range greaterThan {
if lv == gv {
ret = append(ret, lv)
}
}
}
return ret
}
// GetMinVersion gets the lowest semver version
// preRelease=true means accept a pre-release version as a min value
func GetMinVersion(versions []string, preRelease bool) string {
if len(versions) < 1 {
return ""
}
semverVersions := getSortedSemverVersions(versions, preRelease)
return semverVersions[0].String()
}
// GetMaxVersion gets the highest semver version
// preRelease=true means accept a pre-release version as a max value
func GetMaxVersion(versions []string, preRelease bool) string {
if len(versions) < 1 {
return ""
}
semverVersions := getSortedSemverVersions(versions, preRelease)
return semverVersions[len(semverVersions)-1].String()
}
func getSortedSemverVersions(versions []string, preRelease bool) []semver.Version {
var semverVersions []semver.Version
for _, v := range versions {
sv, _ := semver.Make(v)
if len(sv.Pre) == 0 || preRelease {
semverVersions = append(semverVersions, sv)
}
}
semver.Sort(semverVersions)
return semverVersions
}
// AllKubernetesWindowsSupportedVersions maintain a set of available k8s Windows versions in aks-engine
var AllKubernetesWindowsSupportedVersions = getAllKubernetesWindowsSupportedVersionsMap()
func getAllKubernetesWindowsSupportedVersionsMap() map[string]bool {
ret := make(map[string]bool)
for k, v := range AllKubernetesSupportedVersions {
ret[k] = v
}
for _, version := range []string{
"1.6.6",
"1.6.9",
"1.6.11",
"1.6.12",
"1.6.13",
"1.7.0",
"1.7.1",
"1.8.13",
"1.8.14",
"1.8.15",
"1.10.0-beta.2",
"1.10.0-beta.4",
"1.10.0-rc.1",
"1.11.0-alpha.1",
"1.11.0-alpha.2"} {
delete(ret, version)
}
// 1.8.12 is the latest supported patch for Windows
ret["1.8.12"] = true
return ret
}
// GetSupportedVersions get supported version list for a certain orchestrator
func GetSupportedVersions(orchType string, isUpdate, hasWindows bool) (versions []string, defaultVersion string) {
switch orchType {
case Kubernetes:
return GetAllSupportedKubernetesVersions(isUpdate, hasWindows), GetDefaultKubernetesVersion(hasWindows)
case DCOS:
return AllDCOSSupportedVersions, DCOSDefaultVersion
default:
return nil, ""
}
}
//GetValidPatchVersion gets the current valid patch version for the minor version of the passed in version
func GetValidPatchVersion(orchType, orchVer string, isUpdate, hasWindows bool) string {
if orchVer == "" {
return RationalizeReleaseAndVersion(
orchType,
"",
"",
isUpdate,
hasWindows)
}
// check if the current version is valid, this allows us to have multiple supported patch versions in the future if we need it
version := RationalizeReleaseAndVersion(
orchType,
"",
orchVer,
isUpdate,
hasWindows)
if version == "" {
sv, err := semver.Make(orchVer)
if err != nil {
return ""
}
sr := fmt.Sprintf("%d.%d", sv.Major, sv.Minor)
version = RationalizeReleaseAndVersion(
orchType,
sr,
"",
isUpdate,
hasWindows)
}
return version
}
// RationalizeReleaseAndVersion return a version when it can be rationalized from the input, otherwise ""
func RationalizeReleaseAndVersion(orchType, orchRel, orchVer string, isUpdate, hasWindows bool) (version string) {
// ignore "v" prefix in orchestrator version and release: "v1.8.0" is equivalent to "1.8.0", "v1.9" is equivalent to "1.9"
orchVer = strings.TrimPrefix(orchVer, "v")
orchRel = strings.TrimPrefix(orchRel, "v")
supportedVersions, defaultVersion := GetSupportedVersions(orchType, isUpdate, hasWindows)
if supportedVersions == nil {
return ""
}
if orchRel == "" && orchVer == "" {
return defaultVersion
}
if orchVer == "" {
// Try to get latest version matching the release
version = GetLatestPatchVersion(orchRel, supportedVersions)
return version
} else if orchRel == "" {
// Try to get version the same with orchVer
version = ""
for _, ver := range supportedVersions {
if ver == orchVer {
version = ver
break
}
}
return version
}
// Try to get latest version matching the release
version = ""
for _, ver := range supportedVersions {
sv, _ := semver.Make(ver)
sr := fmt.Sprintf("%d.%d", sv.Major, sv.Minor)
if sr == orchRel && ver == orchVer {
version = ver
break
}
}
return version
}
// IsKubernetesVersionGe returns true if actualVersion is greater than or equal to version
func IsKubernetesVersionGe(actualVersion, version string) bool {
v1, _ := semver.Make(actualVersion)
v2, _ := semver.Make(version)
return v1.GE(v2)
}
// GetLatestPatchVersion gets the most recent patch version from a list of semver versions given a major.minor string
func GetLatestPatchVersion(majorMinor string, versionsList []string) (version string) {
// Try to get latest version matching the release
version = ""
for _, ver := range versionsList {
sv, err := semver.Make(ver)
if err != nil {
return
}
sr := fmt.Sprintf("%d.%d", sv.Major, sv.Minor)
if sr == majorMinor {
if version == "" {
version = ver
} else {
current, _ := semver.Make(version)
if sv.GT(current) {
version = ver
}
}
}
}
return version
}
// IsSupportedKubernetesVersion return true if the provided Kubernetes version is supported
func IsSupportedKubernetesVersion(version string, isUpdate, hasWindows bool) bool {
for _, ver := range GetAllSupportedKubernetesVersions(isUpdate, hasWindows) {
if ver == version {
return true
}
}
return false
}