This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 561
/
helper.go
174 lines (159 loc) · 6.15 KB
/
helper.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
package common
import (
"fmt"
"strings"
"github.com/Masterminds/semver"
validator "gopkg.in/go-playground/validator.v9"
)
// HandleValidationErrors is the helper function to catch validator.ValidationError
// based on Namespace of the error, and return customized error message.
func HandleValidationErrors(e validator.ValidationErrors) error {
err := e[0]
ns := err.Namespace()
switch ns {
case "Properties.OrchestratorProfile", "Properties.OrchestratorProfile.OrchestratorType",
"Properties.MasterProfile", "Properties.MasterProfile.DNSPrefix", "Properties.MasterProfile.VMSize",
"Properties.LinuxProfile", "Properties.ServicePrincipalProfile.ClientID",
"Properties.WindowsProfile.AdminUsername",
"Properties.WindowsProfile.AdminPassword":
return fmt.Errorf("missing %s", ns)
case "Properties.MasterProfile.Count":
return fmt.Errorf("MasterProfile count needs to be 1, 3, or 5")
case "Properties.MasterProfile.OSDiskSizeGB":
return fmt.Errorf("Invalid os disk size of %d specified. The range of valid values are [%d, %d]", err.Value().(int), MinDiskSizeGB, MaxDiskSizeGB)
case "Properties.MasterProfile.IPAddressCount":
return fmt.Errorf("MasterProfile.IPAddressCount needs to be in the range [%d,%d]", MinIPAddressCount, MaxIPAddressCount)
case "Properties.MasterProfile.StorageProfile":
return fmt.Errorf("Unknown storageProfile '%s'. Specify either %s or %s", err.Value().(string), StorageAccount, ManagedDisks)
default:
if strings.HasPrefix(ns, "Properties.AgentPoolProfiles") {
switch {
case strings.HasSuffix(ns, ".Name") || strings.HasSuffix(ns, "VMSize"):
return fmt.Errorf("missing %s", ns)
case strings.HasSuffix(ns, ".Count"):
return fmt.Errorf("AgentPoolProfile count needs to be in the range [%d,%d]", MinAgentCount, MaxAgentCount)
case strings.HasSuffix(ns, ".OSDiskSizeGB"):
return fmt.Errorf("Invalid os disk size of %d specified. The range of valid values are [%d, %d]", err.Value().(int), MinDiskSizeGB, MaxDiskSizeGB)
case strings.Contains(ns, ".Ports"):
return fmt.Errorf("AgentPoolProfile Ports must be in the range[%d, %d]", MinPort, MaxPort)
case strings.HasSuffix(ns, ".StorageProfile"):
return fmt.Errorf("Unknown storageProfile '%s'. Specify either %s or %s", err.Value().(string), StorageAccount, ManagedDisks)
case strings.Contains(ns, ".DiskSizesGB"):
return fmt.Errorf("A maximum of %d disks may be specified, The range of valid disk size values are [%d, %d]", MaxDisks, MinDiskSizeGB, MaxDiskSizeGB)
case strings.HasSuffix(ns, ".IPAddressCount"):
return fmt.Errorf("AgentPoolProfile.IPAddressCount needs to be in the range [%d,%d]", MinIPAddressCount, MaxIPAddressCount)
default:
break
}
}
}
return fmt.Errorf("Namespace %s is not caught, %+v", ns, e)
}
// GetSupportedVersions get supported version list for a certain orchestrator
func GetSupportedVersions(orchType string, hasWindows bool) (versions []string, defaultVersion string) {
switch orchType {
case Kubernetes:
if hasWindows {
return GetAllSupportedKubernetesVersionsWindows(), string(KubernetesDefaultVersion)
}
return GetAllSupportedKubernetesVersions(), string(KubernetesDefaultVersion)
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) string {
if orchVer == "" {
return RationalizeReleaseAndVersion(
orchType,
"",
"",
false)
}
// 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,
false)
if version == "" {
sv, err := semver.NewVersion(orchVer)
if err != nil {
return ""
}
sr := fmt.Sprintf("%d.%d", sv.Major(), sv.Minor())
version = RationalizeReleaseAndVersion(
orchType,
sr,
"",
false)
}
return version
}
// RationalizeReleaseAndVersion return a version when it can be rationalized from the input, otherwise ""
func RationalizeReleaseAndVersion(orchType, orchRel, orchVer string, 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, 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.NewVersion(ver)
sr := fmt.Sprintf("%d.%d", sv.Major(), sv.Minor())
if sr == orchRel && ver == orchVer {
version = ver
break
}
}
return version
}
// IsKubernetesVersionGe returns if a semver string is >= to a compare-against semver string (suppport "-" suffixes)
func IsKubernetesVersionGe(actualVersion, version string) bool {
orchestratorVersion, _ := semver.NewVersion(strings.Split(actualVersion, "-")[0]) // to account for -alpha and -beta suffixes
constraint, _ := semver.NewConstraint(">=" + version)
return constraint.Check(orchestratorVersion)
}
// 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, _ := semver.NewVersion(ver)
sr := fmt.Sprintf("%d.%d", sv.Major(), sv.Minor())
if sr == majorMinor {
if version == "" {
version = ver
} else {
cons, _ := semver.NewConstraint(">" + version)
if cons.Check(sv) {
version = ver
}
}
}
}
return version
}