forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
400 lines (350 loc) · 12.2 KB
/
validation.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
396
397
398
399
400
package api
import (
"fmt"
"github.com/blang/semver"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubernetes/pkg/util/validation"
"k8s.io/kubernetes/pkg/util/validation/field"
"net"
"net/url"
"strings"
)
func (c *Cluster) Validate(strict bool) error {
specField := field.NewPath("Spec")
var err error
specPath := field.NewPath("Cluster").Child("Spec")
if c.Name == "" {
return field.Required(field.NewPath("Name"), "Cluster Name is required (e.g. --name=mycluster.myzone.com)")
}
{
// Must be a dns name
errs := validation.IsDNS1123Subdomain(c.Name)
if len(errs) != 0 {
return fmt.Errorf("Cluster Name must be a valid DNS name (e.g. --name=mycluster.myzone.com) errors: %s", strings.Join(errs, ", "))
}
if !strings.Contains(c.Name, ".") {
return fmt.Errorf("Cluster Name must be a fully-qualified DNS name (e.g. --name=mycluster.myzone.com)")
}
}
if len(c.Spec.Zones) == 0 {
// TODO: Auto choose zones from region?
return fmt.Errorf("must configure at least one Zone (use --zones)")
}
if strict && c.Spec.Kubelet == nil {
return fmt.Errorf("Kubelet not configured")
}
if strict && c.Spec.MasterKubelet == nil {
return fmt.Errorf("MasterKubelet not configured")
}
if strict && c.Spec.KubeControllerManager == nil {
return fmt.Errorf("KubeControllerManager not configured")
}
if strict && c.Spec.KubeDNS == nil {
return fmt.Errorf("KubeDNS not configured")
}
if strict && c.Spec.Kubelet == nil {
return fmt.Errorf("Kubelet not configured")
}
if strict && c.Spec.KubeAPIServer == nil {
return fmt.Errorf("KubeAPIServer not configured")
}
if strict && c.Spec.KubeProxy == nil {
return fmt.Errorf("KubeProxy not configured")
}
if strict && c.Spec.Docker == nil {
return fmt.Errorf("Docker not configured")
}
// Check NetworkCIDR
var networkCIDR *net.IPNet
{
networkCIDRString := c.Spec.NetworkCIDR
if networkCIDRString == "" {
return field.Required(specField.Child("NetworkCIDR"), "Cluster did not have NetworkCIDR set")
}
_, networkCIDR, err = net.ParseCIDR(networkCIDRString)
if err != nil {
return field.Invalid(specField.Child("NetworkCIDR"), networkCIDRString, fmt.Sprintf("Cluster had an invalid NetworkCIDR"))
}
}
// Check NonMasqueradeCIDR
var nonMasqueradeCIDR *net.IPNet
{
nonMasqueradeCIDRString := c.Spec.NonMasqueradeCIDR
if nonMasqueradeCIDRString == "" {
return fmt.Errorf("Cluster did not have NonMasqueradeCIDR set")
}
_, nonMasqueradeCIDR, err = net.ParseCIDR(nonMasqueradeCIDRString)
if err != nil {
return fmt.Errorf("Cluster had an invalid NonMasqueradeCIDR: %q", nonMasqueradeCIDRString)
}
if subnetsOverlap(nonMasqueradeCIDR, networkCIDR) {
return fmt.Errorf("NonMasqueradeCIDR %q cannot overlap with NetworkCIDR %q", nonMasqueradeCIDRString, c.Spec.NetworkCIDR)
}
if c.Spec.Kubelet != nil && c.Spec.Kubelet.NonMasqueradeCIDR != nonMasqueradeCIDRString {
return fmt.Errorf("Kubelet NonMasqueradeCIDR did not match cluster NonMasqueradeCIDR")
}
if c.Spec.MasterKubelet != nil && c.Spec.MasterKubelet.NonMasqueradeCIDR != nonMasqueradeCIDRString {
return fmt.Errorf("MasterKubelet NonMasqueradeCIDR did not match cluster NonMasqueradeCIDR")
}
}
// Check ServiceClusterIPRange
var serviceClusterIPRange *net.IPNet
{
serviceClusterIPRangeString := c.Spec.ServiceClusterIPRange
if serviceClusterIPRangeString == "" {
if strict {
return fmt.Errorf("Cluster did not have ServiceClusterIPRange set")
}
} else {
_, serviceClusterIPRange, err = net.ParseCIDR(serviceClusterIPRangeString)
if err != nil {
return fmt.Errorf("Cluster had an invalid ServiceClusterIPRange: %q", serviceClusterIPRangeString)
}
if !isSubnet(nonMasqueradeCIDR, serviceClusterIPRange) {
return fmt.Errorf("ServiceClusterIPRange %q must be a subnet of NonMasqueradeCIDR %q", serviceClusterIPRangeString, c.Spec.NonMasqueradeCIDR)
}
if c.Spec.KubeAPIServer != nil && c.Spec.KubeAPIServer.ServiceClusterIPRange != serviceClusterIPRangeString {
return fmt.Errorf("KubeAPIServer ServiceClusterIPRange did not match cluster ServiceClusterIPRange")
}
}
}
// Check ClusterCIDR
if c.Spec.KubeControllerManager != nil {
var clusterCIDR *net.IPNet
clusterCIDRString := c.Spec.KubeControllerManager.ClusterCIDR
if clusterCIDRString != "" {
_, clusterCIDR, err = net.ParseCIDR(clusterCIDRString)
if err != nil {
return fmt.Errorf("Cluster had an invalid KubeControllerManager.ClusterCIDR: %q", clusterCIDRString)
}
if !isSubnet(nonMasqueradeCIDR, clusterCIDR) {
return fmt.Errorf("KubeControllerManager.ClusterCIDR %q must be a subnet of NonMasqueradeCIDR %q", clusterCIDRString, c.Spec.NonMasqueradeCIDR)
}
}
}
// Check KubeDNS.ServerIP
if c.Spec.KubeDNS != nil {
serverIPString := c.Spec.KubeDNS.ServerIP
if serverIPString == "" {
return fmt.Errorf("Cluster did not have KubeDNS.ServerIP set")
}
dnsServiceIP := net.ParseIP(serverIPString)
if dnsServiceIP == nil {
return fmt.Errorf("Cluster had an invalid KubeDNS.ServerIP: %q", serverIPString)
}
if !serviceClusterIPRange.Contains(dnsServiceIP) {
return fmt.Errorf("ServiceClusterIPRange %q must contain the DNS Server IP %q", c.Spec.ServiceClusterIPRange, serverIPString)
}
if c.Spec.Kubelet != nil && c.Spec.Kubelet.ClusterDNS != c.Spec.KubeDNS.ServerIP {
return fmt.Errorf("Kubelet ClusterDNS did not match cluster KubeDNS.ServerIP")
}
if c.Spec.MasterKubelet != nil && c.Spec.MasterKubelet.ClusterDNS != c.Spec.KubeDNS.ServerIP {
return fmt.Errorf("MasterKubelet ClusterDNS did not match cluster KubeDNS.ServerIP")
}
}
// Check CloudProvider
{
cloudProvider := c.Spec.CloudProvider
if cloudProvider == "" {
return field.Required(specPath.Child("CloudProvider"), "")
}
if c.Spec.Kubelet != nil && cloudProvider != c.Spec.Kubelet.CloudProvider {
return field.Invalid(specPath.Child("Kubelet", "CloudProvider"), c.Spec.Kubelet.CloudProvider, "Did not match cluster CloudProvider")
}
if c.Spec.MasterKubelet != nil && cloudProvider != c.Spec.Kubelet.CloudProvider {
return field.Invalid(specPath.Child("MasterKubelet", "CloudProvider"), c.Spec.Kubelet.CloudProvider, "Did not match cluster CloudProvider")
}
if c.Spec.KubeAPIServer != nil && cloudProvider != c.Spec.KubeAPIServer.CloudProvider {
return field.Invalid(specPath.Child("KubeAPIServer", "CloudProvider"), c.Spec.KubeAPIServer.CloudProvider, "Did not match cluster CloudProvider")
}
if c.Spec.KubeControllerManager != nil && cloudProvider != c.Spec.KubeControllerManager.CloudProvider {
return field.Invalid(specPath.Child("KubeControllerManager", "CloudProvider"), c.Spec.KubeControllerManager.CloudProvider, "Did not match cluster CloudProvider")
}
}
// Check that the zone CIDRs are all consistent
{
for _, z := range c.Spec.Zones {
if z.CIDR == "" {
if strict {
return fmt.Errorf("Zone %q did not have a CIDR set", z.Name)
}
} else {
_, zoneCIDR, err := net.ParseCIDR(z.CIDR)
if err != nil {
return fmt.Errorf("Zone %q had an invalid CIDR: %q", z.Name, z.CIDR)
}
if !isSubnet(networkCIDR, zoneCIDR) {
return fmt.Errorf("Zone %q had a CIDR %q that was not a subnet of the NetworkCIDR %q", z.Name, z.CIDR, c.Spec.NetworkCIDR)
}
}
}
}
// UpdatePolicy
if c.Spec.UpdatePolicy != nil {
switch *c.Spec.UpdatePolicy {
case UpdatePolicyExternal:
// Valid
default:
return fmt.Errorf("unrecognized value for UpdatePolicy: %v", *c.Spec.UpdatePolicy)
}
}
// AdminAccess
if strict && len(c.Spec.AdminAccess) == 0 {
return fmt.Errorf("AdminAccess not configured")
}
for _, adminAccess := range c.Spec.AdminAccess {
_, _, err := net.ParseCIDR(adminAccess)
if err != nil {
return fmt.Errorf("AdminAccess rule %q could not be parsed (invalid CIDR)", adminAccess)
}
}
// KubeProxy
if c.Spec.KubeProxy != nil {
kubeProxyPath := specPath.Child("KubeProxy")
master := c.Spec.KubeProxy.Master
if strict && master == "" {
return field.Required(kubeProxyPath.Child("Master"), "")
}
if master != "" && !isValidAPIServersURL(master) {
return field.Invalid(kubeProxyPath.Child("Master"), master, "Not a valid APIServer URL")
}
}
// Kubelet
if c.Spec.Kubelet != nil {
kubeletPath := specPath.Child("Kubelet")
if strict && c.Spec.Kubelet.APIServers == "" {
return field.Required(kubeletPath.Child("APIServers"), "")
}
if c.Spec.Kubelet.APIServers != "" && !isValidAPIServersURL(c.Spec.Kubelet.APIServers) {
return field.Invalid(kubeletPath.Child("APIServers"), c.Spec.Kubelet.APIServers, "Not a valid APIServer URL")
}
}
// MasterKubelet
if c.Spec.MasterKubelet != nil {
masterKubeletPath := specPath.Child("MasterKubelet")
if strict && c.Spec.MasterKubelet.APIServers == "" {
return field.Required(masterKubeletPath.Child("APIServers"), "")
}
if c.Spec.Kubelet.APIServers != "" && !isValidAPIServersURL(c.Spec.MasterKubelet.APIServers) {
return field.Invalid(masterKubeletPath.Child("APIServers"), c.Spec.MasterKubelet.APIServers, "Not a valid APIServer URL")
}
}
// Etcd
{
if len(c.Spec.EtcdClusters) == 0 {
return field.Required(specField.Child("EtcdClusters"), "")
}
for _, etcd := range c.Spec.EtcdClusters {
if etcd.Name == "" {
return fmt.Errorf("EtcdCluster did not have name")
}
if len(etcd.Members) == 0 {
return fmt.Errorf("No members defined in etcd cluster %q", etcd.Name)
}
if (len(etcd.Members) % 2) == 0 {
// Not technically a requirement, but doesn't really make sense to allow
return fmt.Errorf("There should be an odd number of master-zones, for etcd's quorum. Hint: Use --zones and --master-zones to declare node zones and master zones separately.")
}
for _, m := range etcd.Members {
if m.Name == "" {
return fmt.Errorf("EtcdMember did not have Name in cluster %q", etcd.Name)
}
if fi.StringValue(m.Zone) == "" {
return fmt.Errorf("EtcdMember did not have Zone in cluster %q", etcd.Name)
}
}
}
}
// KubernetesVersion
if c.Spec.KubernetesVersion == "" {
if strict {
return field.Required(specField.Child("KubernetesVersion"), "")
}
} else {
sv, err := ParseKubernetesVersion(c.Spec.KubernetesVersion)
if err != nil {
return field.Invalid(specField.Child("KubernetesVersion"), c.Spec.KubernetesVersion, "unable to determine kubernetes version")
}
if sv.GTE(semver.Version{Major: 1, Minor: 4}) {
if c.Spec.Networking != nil && c.Spec.Networking.Classic != nil {
return field.Invalid(specField.Child("Networking"), "classic", "classic networking is not supported with kubernetes versions 1.4 and later")
}
}
}
return nil
}
func isValidAPIServersURL(s string) bool {
u, err := url.Parse(s)
if err != nil {
return false
}
if u.Host == "" || u.Scheme == "" {
return false
}
return true
}
func DeepValidate(c *Cluster, groups []*InstanceGroup, strict bool) error {
err := c.Validate(strict)
if err != nil {
return err
}
if len(groups) == 0 {
return fmt.Errorf("must configure at least one InstanceGroup")
}
masterGroupCount := 0
nodeGroupCount := 0
for _, g := range groups {
if g.IsMaster() {
masterGroupCount++
} else {
nodeGroupCount++
}
}
if masterGroupCount == 0 {
return fmt.Errorf("must configure at least one Master InstanceGroup")
}
if nodeGroupCount == 0 {
return fmt.Errorf("must configure at least one Node InstanceGroup")
}
for _, g := range groups {
err := g.CrossValidate(c, strict)
if err != nil {
return err
}
}
return nil
}
// isSubnet checks if child is a subnet of parent
func isSubnet(parent *net.IPNet, child *net.IPNet) bool {
parentOnes, parentBits := parent.Mask.Size()
childOnes, childBits := child.Mask.Size()
if childBits != parentBits {
return false
}
if parentOnes > childOnes {
return false
}
childMasked := child.IP.Mask(parent.Mask)
parentMasked := parent.IP.Mask(parent.Mask)
return childMasked.Equal(parentMasked)
}
// subnetsOverlap checks if two subnets overlap
func subnetsOverlap(l *net.IPNet, r *net.IPNet) bool {
return l.Contains(r.IP) || r.Contains(l.IP)
}
func IsValidValue(fldPath *field.Path, v *string, validValues []string) field.ErrorList {
allErrs := field.ErrorList{}
if v != nil {
found := false
for _, validValue := range validValues {
if *v == validValue {
found = true
break
}
}
if !found {
allErrs = append(allErrs, field.NotSupported(fldPath, *v, validValues))
}
}
return allErrs
}