-
Notifications
You must be signed in to change notification settings - Fork 416
/
azureclustertemplate_validation.go
161 lines (129 loc) · 5.14 KB
/
azureclustertemplate_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
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta1
import (
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)
func (c *AzureClusterTemplate) validateClusterTemplate() error {
var allErrs field.ErrorList
allErrs = append(allErrs, c.validateClusterTemplateSpec()...)
if len(allErrs) == 0 {
return nil
}
return apierrors.NewInvalid(
schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "AzureClusterTemplate"},
c.Name, allErrs)
}
func (c *AzureClusterTemplate) validateClusterTemplateSpec() field.ErrorList {
var allErrs field.ErrorList
allErrs = append(allErrs, validateVnetCIDR(
c.Spec.Template.Spec.NetworkSpec.Vnet.CIDRBlocks,
field.NewPath("spec").Child("template").Child("spec").
Child("networkSpec").Child("vnet").Child("cidrBlocks"))...)
allErrs = append(allErrs, validateSubnetTemplates(
c.Spec.Template.Spec.NetworkSpec.Subnets,
c.Spec.Template.Spec.NetworkSpec.Vnet,
field.NewPath("spec").Child("template").Child("spec").Child("networkSpec").Child("subnets"),
)...)
allErrs = append(allErrs, c.validateAPIServerLB(
field.NewPath("spec").Child("template").Child("spec").Child("networkSpec").Child("apiServerLB"),
)...)
var oneSubnetWithoutNatGateway bool
networkSpec := c.Spec.Template.Spec.NetworkSpec
for _, subnet := range networkSpec.Subnets {
if subnet.Role == SubnetNode && !subnet.IsNatGatewayEnabled() {
oneSubnetWithoutNatGateway = true
break
}
}
if oneSubnetWithoutNatGateway {
allErrs = append(allErrs, c.validateNodeOutboundLB()...)
}
allErrs = append(allErrs, c.validateControlPlaneOutboundLB()...)
allErrs = append(allErrs, c.validatePrivateDNSZoneName()...)
return allErrs
}
func validateSubnetTemplates(subnets SubnetTemplatesSpec, vnet VnetTemplateSpec, fld *field.Path) field.ErrorList {
var allErrs field.ErrorList
subnetNames := make(map[string]bool, len(subnets))
requiredSubnetRoles := map[string]bool{
"control-plane": false,
"node": false,
}
for i, subnet := range subnets {
if err := validateSubnetName(subnet.Name, fld.Index(i).Child("name")); err != nil {
allErrs = append(allErrs, err)
}
if _, ok := subnetNames[subnet.Name]; ok {
allErrs = append(allErrs, field.Duplicate(fld, subnet.Name))
}
subnetNames[subnet.Name] = true
for role := range requiredSubnetRoles {
if role == string(subnet.Role) {
requiredSubnetRoles[role] = true
}
}
for j, rule := range subnet.SecurityGroup.SecurityRules {
if err := validateSecurityRule(
rule,
fld.Index(i).Child("securityGroup").Child("securityGroup").Child("securityRules").Index(j),
); err != nil {
allErrs = append(allErrs, err)
}
}
allErrs = append(allErrs, validateSubnetCIDR(subnet.CIDRBlocks, vnet.CIDRBlocks, fld.Index(i).Child("cidrBlocks"))...)
}
for k, v := range requiredSubnetRoles {
if !v {
allErrs = append(allErrs, field.Required(fld,
fmt.Sprintf("required role %s not included in provided subnets", k)))
}
}
return allErrs
}
func (c *AzureClusterTemplate) validateAPIServerLB(apiServerLBPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
lb := c.Spec.Template.Spec.NetworkSpec.APIServerLB
allErrs = append(allErrs, validateClassSpecForAPIServerLB(lb, nil, apiServerLBPath)...)
return allErrs
}
func (c *AzureClusterTemplate) validateNodeOutboundLB() field.ErrorList {
var allErrs field.ErrorList
fldPath := field.NewPath("spec").Child("template").Child("spec").Child("networkSpec").Child("nodeOutboundLB")
apiserverLB := c.Spec.Template.Spec.NetworkSpec.APIServerLB
lb := c.Spec.Template.Spec.NetworkSpec.NodeOutboundLB
allErrs = append(allErrs, validateClassSpecForNodeOutboundLB(lb, nil, apiserverLB, fldPath)...)
return allErrs
}
func (c *AzureClusterTemplate) validateControlPlaneOutboundLB() field.ErrorList {
var allErrs field.ErrorList
fldPath := field.NewPath("spec").Child("template").Child("spec").Child("networkSpec").Child("controlPlaneOutboundLB")
apiserverLB := c.Spec.Template.Spec.NetworkSpec.APIServerLB
lb := c.Spec.Template.Spec.NetworkSpec.ControlPlaneOutboundLB
allErrs = append(allErrs, validateClassSpecForControlPlaneOutboundLB(lb, apiserverLB, fldPath)...)
return allErrs
}
func (c *AzureClusterTemplate) validatePrivateDNSZoneName() field.ErrorList {
var allErrs field.ErrorList
fldPath := field.NewPath("spec").Child("template").Child("spec").Child("networkSpec").Child("privateDNSZoneName")
networkSpec := c.Spec.Template.Spec.NetworkSpec
allErrs = append(allErrs, validatePrivateDNSZoneName(
networkSpec.PrivateDNSZoneName,
networkSpec.APIServerLB.Type,
fldPath,
)...)
return allErrs
}