forked from kubernetes/kops
/
instancegroup.go
187 lines (154 loc) · 5.71 KB
/
instancegroup.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
/*
Copyright 2017 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 validation
import (
"fmt"
"regexp"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/util"
"k8s.io/kops/upup/pkg/fi"
)
// ValidateInstanceGroup is responsible for validating the configuration of a instancegroup
func ValidateInstanceGroup(g *kops.InstanceGroup) error {
if g.ObjectMeta.Name == "" {
return field.Required(field.NewPath("Name"), "")
}
switch g.Spec.Role {
case "":
return field.Required(field.NewPath("Role"), "Role must be set")
case kops.InstanceGroupRoleMaster:
case kops.InstanceGroupRoleNode:
case kops.InstanceGroupRoleBastion:
default:
return field.Invalid(field.NewPath("Role"), g.Spec.Role, "Unknown role")
}
if g.Spec.Tenancy != "" {
if g.Spec.Tenancy != "default" && g.Spec.Tenancy != "dedicated" && g.Spec.Tenancy != "host" {
return field.Invalid(field.NewPath("Tenancy"), g.Spec.Tenancy, "Unknown tenancy. Must be Default, Dedicated or Host.")
}
}
if g.Spec.MaxSize != nil && g.Spec.MinSize != nil {
if *g.Spec.MaxSize < *g.Spec.MinSize {
return field.Invalid(field.NewPath("MaxSize"), *g.Spec.MaxSize, "maxSize must be greater than or equal to minSize.")
}
}
if fi.Int32Value(g.Spec.RootVolumeIops) < 0 {
return field.Invalid(field.NewPath("RootVolumeIops"), g.Spec.RootVolumeIops, "RootVolumeIops must be greater than 0")
}
// @check all the hooks are valid in this instancegroup
for i := range g.Spec.Hooks {
if errs := validateHookSpec(&g.Spec.Hooks[i], field.NewPath("hooks").Index(i)); len(errs) > 0 {
return errs.ToAggregate()
}
}
// @check the fileAssets for this instancegroup are valid
for i := range g.Spec.FileAssets {
if errs := validateFileAssetSpec(&g.Spec.FileAssets[i], field.NewPath("fileAssets").Index(i)); len(errs) > 0 {
return errs.ToAggregate()
}
}
if g.IsMaster() {
if len(g.Spec.Subnets) == 0 {
return fmt.Errorf("Master InstanceGroup %s did not specify any Subnets", g.ObjectMeta.Name)
}
}
if len(g.Spec.AdditionalUserData) > 0 {
for _, UserDataInfo := range g.Spec.AdditionalUserData {
err := validateExtraUserData(&UserDataInfo)
if err != nil {
return err
}
}
}
if err := validateInstanceProfile(g.Spec.IAM, field.NewPath("iam")); err != nil {
return err
}
return nil
}
// CrossValidateInstanceGroup performs validation of the instance group, including that it is consistent with the Cluster
// It calls ValidateInstanceGroup, so all that validation is included.
func CrossValidateInstanceGroup(g *kops.InstanceGroup, cluster *kops.Cluster, strict bool) error {
err := ValidateInstanceGroup(g)
if err != nil {
return err
}
// Check that instance groups are defined in subnets that are defined in the cluster
{
clusterSubnets := make(map[string]*kops.ClusterSubnetSpec)
for i := range cluster.Spec.Subnets {
s := &cluster.Spec.Subnets[i]
if clusterSubnets[s.Name] != nil {
return fmt.Errorf("Subnets contained a duplicate value: %v", s.Name)
}
clusterSubnets[s.Name] = s
}
for _, z := range g.Spec.Subnets {
if clusterSubnets[z] == nil {
return fmt.Errorf("InstanceGroup %q is configured in %q, but this is not configured as a Subnet in the cluster", g.ObjectMeta.Name, z)
}
}
}
k8sVersion, err := util.ParseKubernetesVersion(cluster.Spec.KubernetesVersion)
if err != nil {
return fmt.Errorf("Unable to determine kubernetes version from %q", cluster.Spec.KubernetesVersion)
}
allErrs := field.ErrorList{}
fieldPath := field.NewPath("InstanceGroup")
if k8sVersion.Major == 1 && k8sVersion.Minor <= 5 {
if len(g.Spec.Taints) > 0 {
if !(g.IsMaster() && g.Spec.Taints[0] == kops.TaintNoScheduleMaster15 && len(g.Spec.Taints) == 1) {
allErrs = append(allErrs, field.Invalid(fieldPath.Child("Spec").Child("Taints"), g.Spec.Taints, "User-specified taints are not supported before kubernetes version 1.6.0"))
}
}
}
if len(allErrs) != 0 {
return allErrs[0]
}
return nil
}
func validateExtraUserData(userData *kops.UserData) error {
fieldPath := field.NewPath("AdditionalUserData")
if userData.Name == "" {
return field.Required(fieldPath.Child("Name"), "field must be set")
}
if userData.Content == "" {
return field.Required(fieldPath.Child("Content"), "field must be set")
}
switch userData.Type {
case "text/x-include-once-url":
case "text/x-include-url":
case "text/cloud-config-archive":
case "text/upstart-job":
case "text/cloud-config":
case "text/part-handler":
case "text/x-shellscript":
case "text/cloud-boothook":
default:
return field.Invalid(fieldPath.Child("Type"), userData.Type, "Invalid user-data content type")
}
return nil
}
// format is arn:aws:iam::123456789012:instance-profile/S3Access
var validARN = regexp.MustCompile(`^arn:aws:iam::\d+:instance-profile\/\S+$`)
// validateInstanceProfile checks the String values for the AuthProfile
func validateInstanceProfile(v *kops.IAMProfileSpec, fldPath *field.Path) *field.Error {
if v != nil && v.Profile != nil {
arn := *v.Profile
if !validARN.MatchString(arn) {
return field.Invalid(fldPath.Child("Profile"), arn,
"Instance Group IAM Instance Profile must be a valid aws arn such as arn:aws:iam::123456789012:instance-profile/KopsExampleRole")
}
}
return nil
}