-
Notifications
You must be signed in to change notification settings - Fork 458
/
seed.go
162 lines (133 loc) · 7.06 KB
/
seed.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
// Copyright (c) 2018 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 (
"github.com/gardener/gardener/pkg/apis/core"
cidrvalidation "github.com/gardener/gardener/pkg/utils/validation/cidr"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// ValidateSeed validates a Seed object.
func ValidateSeed(seed *core.Seed) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&seed.ObjectMeta, false, ValidateName, field.NewPath("metadata"))...)
allErrs = append(allErrs, ValidateSeedSpec(&seed.Spec, field.NewPath("spec"))...)
return allErrs
}
// ValidateSeedUpdate validates a Seed object before an update.
func ValidateSeedUpdate(newSeed, oldSeed *core.Seed) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateObjectMetaUpdate(&newSeed.ObjectMeta, &oldSeed.ObjectMeta, field.NewPath("metadata"))...)
allErrs = append(allErrs, ValidateSeedSpecUpdate(&newSeed.Spec, &oldSeed.Spec, field.NewPath("spec"))...)
allErrs = append(allErrs, ValidateSeed(newSeed)...)
return allErrs
}
// ValidateSeedSpec validates the specification of a Seed object.
func ValidateSeedSpec(seedSpec *core.SeedSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
providerPath := fldPath.Child("provider")
if len(seedSpec.Provider.Type) == 0 {
allErrs = append(allErrs, field.Required(providerPath.Child("type"), "must provide a provider type"))
}
if len(seedSpec.Provider.Region) == 0 {
allErrs = append(allErrs, field.Required(providerPath.Child("region"), "must provide a provider region"))
}
allErrs = append(allErrs, validateDNS1123Subdomain(seedSpec.DNS.IngressDomain, fldPath.Child("dns", "ingressDomain"))...)
if seedSpec.SecretRef != nil {
allErrs = append(allErrs, validateSecretReference(*seedSpec.SecretRef, fldPath.Child("secretRef"))...)
}
networksPath := fldPath.Child("networks")
networks := []cidrvalidation.CIDR{
cidrvalidation.NewCIDR(seedSpec.Networks.Pods, networksPath.Child("pods")),
cidrvalidation.NewCIDR(seedSpec.Networks.Services, networksPath.Child("services")),
}
if seedSpec.Networks.Nodes != nil {
networks = append(networks, cidrvalidation.NewCIDR(*seedSpec.Networks.Nodes, networksPath.Child("nodes")))
}
if shootDefaults := seedSpec.Networks.ShootDefaults; shootDefaults != nil {
if shootDefaults.Pods != nil {
networks = append(networks, cidrvalidation.NewCIDR(*shootDefaults.Pods, networksPath.Child("shootDefaults", "pods")))
}
if shootDefaults.Services != nil {
networks = append(networks, cidrvalidation.NewCIDR(*shootDefaults.Services, networksPath.Child("shootDefaults", "services")))
}
}
allErrs = append(allErrs, cidrvalidation.ValidateCIDRParse(networks...)...)
allErrs = append(allErrs, cidrvalidation.ValidateCIDROverlap(networks, networks, false)...)
if seedSpec.Backup != nil {
if len(seedSpec.Backup.Provider) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("backup", "provider"), "must provide a backup cloud provider name"))
}
if seedSpec.Provider.Type != seedSpec.Backup.Provider && (seedSpec.Backup.Region == nil || len(*seedSpec.Backup.Region) == 0) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("backup", "region"), "", "region must be specified for if backup provider is different from provider used in `spec.cloud`"))
}
allErrs = append(allErrs, validateSecretReference(seedSpec.Backup.SecretRef, fldPath.Child("backup", "secretRef"))...)
}
var foundTaintKeys = sets.NewString()
for i, taint := range seedSpec.Taints {
idxPath := fldPath.Child("taints").Index(i)
if len(taint.Key) == 0 {
allErrs = append(allErrs, field.Required(idxPath.Child("key"), "cannot be empty"))
}
if foundTaintKeys.Has(taint.Key) {
allErrs = append(allErrs, field.Duplicate(idxPath.Child("key"), taint.Key))
}
foundTaintKeys.Insert(taint.Key)
}
if seedSpec.Volume != nil {
if seedSpec.Volume.MinimumSize != nil {
allErrs = append(allErrs, validateResourceQuantityValue("minimumSize", *seedSpec.Volume.MinimumSize, fldPath.Child("volume", "minimumSize"))...)
}
volumeProviderPurposes := make(map[string]struct{}, len(seedSpec.Volume.Providers))
for i, provider := range seedSpec.Volume.Providers {
idxPath := fldPath.Child("volume", "providers").Index(i)
if len(provider.Purpose) == 0 {
allErrs = append(allErrs, field.Required(idxPath.Child("purpose"), "cannot be empty"))
}
if len(provider.Name) == 0 {
allErrs = append(allErrs, field.Required(idxPath.Child("name"), "cannot be empty"))
}
if _, ok := volumeProviderPurposes[provider.Purpose]; ok {
allErrs = append(allErrs, field.Duplicate(idxPath.Child("purpose"), provider.Purpose))
}
volumeProviderPurposes[provider.Purpose] = struct{}{}
}
}
return allErrs
}
// ValidateSeedSpecUpdate validates the specification updates of a Seed object.
func ValidateSeedSpecUpdate(newSeedSpec, oldSeedSpec *core.SeedSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSeedSpec.Networks.Pods, oldSeedSpec.Networks.Pods, fldPath.Child("networks", "pods"))...)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSeedSpec.Networks.Services, oldSeedSpec.Networks.Services, fldPath.Child("networks", "services"))...)
if oldSeedSpec.Networks.Nodes != nil {
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSeedSpec.Networks.Nodes, oldSeedSpec.Networks.Nodes, fldPath.Child("networks", "nodes"))...)
}
if oldSeedSpec.Backup != nil {
if newSeedSpec.Backup != nil {
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSeedSpec.Backup.Provider, oldSeedSpec.Backup.Provider, fldPath.Child("backup", "provider"))...)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSeedSpec.Backup.Region, oldSeedSpec.Backup.Region, fldPath.Child("backup", "region"))...)
} else {
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newSeedSpec.Backup, oldSeedSpec.Backup, fldPath.Child("backup"))...)
}
}
// If oldSeedSpec doesn't have backup configured, we allow to add it; but not the vice versa.
return allErrs
}
// ValidateSeedStatusUpdate validates the status field of a Seed object.
func ValidateSeedStatusUpdate(newSeed, oldSeed *core.Seed) field.ErrorList {
allErrs := field.ErrorList{}
return allErrs
}