This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 522
/
validate.go
295 lines (247 loc) · 7.87 KB
/
validate.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package v20180331
import (
"net"
"regexp"
"strings"
"github.com/Azure/go-autorest/autorest/to"
"github.com/Azure/aks-engine/pkg/api/common"
"github.com/pkg/errors"
validator "gopkg.in/go-playground/validator.v9"
)
const (
// KubernetesMinMaxPods is the minimum valid value for MaxPods, necessary for running kube-system pods
KubernetesMinMaxPods = 5
)
var validate *validator.Validate
func init() {
validate = validator.New()
}
// Validate implements APIObject
func (a *AgentPoolProfile) Validate() error {
// Don't need to call validate.Struct(a)
// It is handled by Properties.Validate()
return validatePoolName(a.Name)
}
// Validate implements APIObject
func (l *LinuxProfile) Validate() error {
// Don't need to call validate.Struct(l)
// It is handled by Properties.Validate()
if e := validate.Var(l.SSH.PublicKeys[0].KeyData, "required"); e != nil {
return errors.New("KeyData in LinuxProfile.SSH.PublicKeys cannot be empty string")
}
return nil
}
// Validate implements APIObject
func (a *AADProfile) Validate(rbacEnabled *bool) error {
if !to.Bool(rbacEnabled) {
return ErrorRBACNotEnabledForAAD
}
if e := validate.Var(a.ServerAppID, "required"); e != nil {
return ErrorAADServerAppIDNotSet
}
// Don't need to call validate.Struct(l)
// It is handled by Properties.Validate()
if e := validate.Var(a.ServerAppSecret, "required"); e != nil {
return ErrorAADServerAppSecretNotSet
}
if e := validate.Var(a.ClientAppID, "required"); e != nil {
return ErrorAADClientAppIDNotSet
}
if e := validate.Var(a.TenantID, "required"); e != nil {
return ErrorAADTenantIDNotSet
}
return nil
}
func handleValidationErrors(e validator.ValidationErrors) error {
err := e[0]
ns := err.Namespace()
switch ns {
// TODO: Add more validation here
case "Properties.ServicePrincipalProfile.ClientID",
"Properties.ServicePrincipalProfile.Secret", "Properties.WindowsProfile.AdminUsername",
"Properties.WindowsProfile.AdminPassword":
return errors.Errorf("missing %s", ns)
default:
if strings.HasPrefix(ns, "Properties.AgentPoolProfiles") {
switch {
case strings.HasSuffix(ns, ".Name") || strings.HasSuffix(ns, "VMSize"):
return errors.Errorf("missing %s", ns)
case strings.HasSuffix(ns, ".Count"):
return errors.Errorf("AgentPoolProfile count needs to be in the range [%d,%d]", MinAgentCount, MaxAgentCount)
case strings.HasSuffix(ns, ".OSDiskSizeGB"):
return errors.Errorf("Invalid os disk size of %d specified. The range of valid values are [%d, %d]", err.Value().(int), MinDiskSizeGB, MaxDiskSizeGB)
case strings.HasSuffix(ns, ".StorageProfile"):
return errors.Errorf("Unknown storageProfile '%s'. Must specify %s", err.Value().(string), ManagedDisks)
default:
break
}
}
}
return errors.Errorf("Namespace %s is not caught, %+v", ns, e)
}
// Validate implements APIObject
func (a *Properties) Validate() error {
if e := validate.Struct(a); e != nil {
return handleValidationErrors(e.(validator.ValidationErrors))
}
// Don't need to call validate.Struct(m)
// It is handled by Properties.Validate()
if e := common.ValidateDNSPrefix(a.DNSPrefix); e != nil {
return e
}
if e := validateUniqueProfileNames(a.AgentPoolProfiles); e != nil {
return e
}
for _, agentPoolProfile := range a.AgentPoolProfiles {
if e := agentPoolProfile.Validate(); e != nil {
return e
}
}
if a.LinuxProfile != nil {
if e := a.LinuxProfile.Validate(); e != nil {
return e
}
}
if e := validateVNET(a); e != nil {
return e
}
if a.AADProfile != nil {
if e := a.AADProfile.Validate(a.EnableRBAC); e != nil {
return e
}
}
return nil
}
func validatePoolName(poolName string) error {
// we will cap at length of 12 and all lowercase letters since this makes up the VMName
poolNameRegex := `^([a-z][a-z0-9]{0,11})$`
re, err := regexp.Compile(poolNameRegex)
if err != nil {
return err
}
submatches := re.FindStringSubmatch(poolName)
if len(submatches) != 2 {
return errors.Errorf("pool name '%s' is invalid. A pool name must start with a lowercase letter, have max length of 12, and only have characters a-z0-9", poolName)
}
return nil
}
func validateUniqueProfileNames(profiles []*AgentPoolProfile) error {
profileNames := make(map[string]bool)
for _, profile := range profiles {
if _, ok := profileNames[profile.Name]; ok {
return errors.Errorf("profile name '%s' already exists, profile names must be unique across pools", profile.Name)
}
profileNames[profile.Name] = true
}
return nil
}
// validateVNET validate network profile and custom VNET logic
func validateVNET(a *Properties) error {
n := a.NetworkProfile
// validate network profile settings
if n != nil {
switch n.NetworkPlugin {
case Azure, Kubenet:
if n.ServiceCidr != "" && n.DNSServiceIP != "" && n.DockerBridgeCidr != "" {
// validate ServiceCidr
_, serviceCidr, err := net.ParseCIDR(n.ServiceCidr)
if err != nil {
return ErrorInvalidServiceCidr
}
// validate ServiceCidr not too large
var ones, bits = serviceCidr.Mask.Size()
if bits-ones > 20 {
return ErrorServiceCidrTooLarge
}
// validate DNSServiceIP
dnsServiceIP := net.ParseIP(n.DNSServiceIP)
if dnsServiceIP == nil {
return ErrorInvalidDNSServiceIP
}
// validate DockerBridgeCidr
_, _, err = net.ParseCIDR(n.DockerBridgeCidr)
if err != nil {
return ErrorInvalidDockerBridgeCidr
}
// validate DNSServiceIP is within ServiceCidr
if !serviceCidr.Contains(dnsServiceIP) {
return ErrorDNSServiceIPNotInServiceCidr
}
// validate DNSServiceIP is not the first IP in ServiceCidr. The first IP is reserved for redirect svc.
kubernetesServiceIP, err := common.CidrStringFirstIP(n.ServiceCidr)
if err != nil {
return ErrorInvalidServiceCidr
}
if dnsServiceIP.String() == kubernetesServiceIP.String() {
return ErrorDNSServiceIPAlreadyUsed
}
} else if n.ServiceCidr == "" && n.DNSServiceIP == "" && n.DockerBridgeCidr == "" {
// this is a valid case, and no validation needed.
} else {
return ErrorInvalidNetworkProfile
}
// PodCidr should not be set for Azure CNI
if n.NetworkPlugin == Azure && n.PodCidr != "" {
return ErrorPodCidrNotSetableInAzureCNI
}
default:
return ErrorInvalidNetworkPlugin
}
}
// validate agent pool custom VNET settings
if a.AgentPoolProfiles != nil {
if e := validateAgentPoolVNET(a.AgentPoolProfiles); e != nil {
return e
}
}
return nil
}
func validateAgentPoolVNET(a []*AgentPoolProfile) error {
// validate custom VNET logic at agent pool level
if isCustomVNET(a) {
var subscription string
var resourceGroup string
var vnet string
for _, agentPool := range a {
// validate each agent pool has a subnet
if !agentPool.IsCustomVNET() {
return ErrorAtLeastAgentPoolNoSubnet
}
if agentPool.MaxPods != nil && *agentPool.MaxPods < KubernetesMinMaxPods {
return ErrorInvalidMaxPods
}
// validate subscription, resource group and vnet are the same among subnets
subnetSubscription, subnetResourceGroup, subnetVnet, _, err := common.GetVNETSubnetIDComponents(agentPool.VnetSubnetID)
if err != nil {
return ErrorParsingSubnetID
}
if subscription == "" {
subscription = subnetSubscription
} else if subscription != subnetSubscription {
return ErrorSubscriptionNotMatch
}
if resourceGroup == "" {
resourceGroup = subnetResourceGroup
} else if resourceGroup != subnetResourceGroup {
return ErrorResourceGroupNotMatch
}
if vnet == "" {
vnet = subnetVnet
} else if vnet != subnetVnet {
return ErrorVnetNotMatch
}
}
}
return nil
}
// check agent pool subnet, return true as long as one agent pool has a subnet defined.
func isCustomVNET(a []*AgentPoolProfile) bool {
for _, agentPool := range a {
if agentPool.IsCustomVNET() {
return true
}
}
return false
}