-
Notifications
You must be signed in to change notification settings - Fork 76
/
shoot.go
154 lines (130 loc) · 5.38 KB
/
shoot.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package validation
import (
"encoding/json"
"github.com/gardener/gardener/pkg/apis/core"
validationutils "github.com/gardener/gardener/pkg/utils/validation"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
api "github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure"
"github.com/gardener/gardener-extension-provider-azure/pkg/apis/azure/helper"
)
const maxDataVolumeCount = 64
// ValidateNetworking validates the network settings of a Shoot.
func ValidateNetworking(networking *core.Networking, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if networking == nil {
allErrs = append(allErrs, field.Required(fldPath, "networking field can't be empty for Azure shoots"))
return allErrs
}
if networking.Nodes == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("nodes"), "a nodes CIDR must be provided for Azure shoots"))
}
if networking.Type != nil && *networking.Type == "calico" && networking.ProviderConfig != nil {
networkConfig, err := decodeNetworkConfig(networking.ProviderConfig)
if err != nil {
allErrs = append(allErrs, field.InternalError(fldPath.Child("providerConfig"), err))
} else if overlay, ok := networkConfig["overlay"].(map[string]interface{}); ok {
if overlay["enabled"].(bool) {
allErrs = append(allErrs, field.Forbidden(fldPath.Child("providerConfig").Child("overlay").Child("enabled"), "Calico overlay network is not supported on azure"))
}
}
}
return allErrs
}
func decodeNetworkConfig(network *runtime.RawExtension) (map[string]interface{}, error) {
var networkConfig map[string]interface{}
if network == nil || network.Raw == nil {
return map[string]interface{}{}, nil
}
if err := json.Unmarshal(network.Raw, &networkConfig); err != nil {
return nil, err
}
return networkConfig, nil
}
// ValidateWorkers validates the workers of a Shoot.
func ValidateWorkers(workers []core.Worker, infra *api.InfrastructureConfig, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
for i, worker := range workers {
path := fldPath.Index(i)
if worker.Volume == nil {
allErrs = append(allErrs, field.Required(path.Child("volume"), "must not be nil"))
} else {
allErrs = append(allErrs, validateVolume(worker.Volume, path.Child("volume"))...)
}
if length := len(worker.DataVolumes); length > maxDataVolumeCount {
allErrs = append(allErrs, field.TooMany(path.Child("dataVolumes"), length, maxDataVolumeCount))
}
for j, volume := range worker.DataVolumes {
dataVolPath := path.Child("dataVolumes").Index(j)
allErrs = append(allErrs, validateDataVolume(&volume, dataVolPath)...)
}
// Zones validation
if infra.Zoned && len(worker.Zones) == 0 {
allErrs = append(allErrs, field.Required(path.Child("zones"), "at least one zone must be configured for zoned clusters"))
continue
}
if !infra.Zoned && len(worker.Zones) > 0 {
allErrs = append(allErrs, field.Required(path.Child("zones"), "zones must not be specified for non zoned clusters"))
continue
}
zones := sets.New[string]()
for j, zone := range worker.Zones {
if zones.Has(zone) {
allErrs = append(allErrs, field.Invalid(path.Child("zones").Index(j), zone, "must only be specified once per worker group"))
continue
}
zones.Insert(zone)
}
if !helper.IsUsingSingleSubnetLayout(infra) {
infraZones := sets.Set[string]{}
for _, zone := range infra.Networks.Zones {
infraZones.Insert(helper.InfrastructureZoneToString(zone.Name))
}
for zoneIndex, workerZone := range worker.Zones {
if !infraZones.Has(workerZone) {
allErrs = append(allErrs, field.Invalid(path.Child("zones").Index(zoneIndex), workerZone, "zone configuration must be specified in \"infrastructureConfig.networks.zones\""))
}
}
}
}
return allErrs
}
// ValidateWorkersUpdate validates updates on `workers`.
func ValidateWorkersUpdate(oldWorkers, newWorkers []core.Worker, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
for i, newWorker := range newWorkers {
for _, oldWorker := range oldWorkers {
if newWorker.Name == oldWorker.Name {
if validationutils.ShouldEnforceImmutability(newWorker.Zones, oldWorker.Zones) {
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newWorker.Zones, oldWorker.Zones, fldPath.Index(i).Child("zones"))...)
}
break
}
}
}
return allErrs
}
func validateVolume(vol *core.Volume, fldPath *field.Path) field.ErrorList {
return validateVolumeFunc(vol.Type, vol.VolumeSize, vol.Encrypted, fldPath)
}
func validateDataVolume(vol *core.DataVolume, fldPath *field.Path) field.ErrorList {
return validateVolumeFunc(vol.Type, vol.VolumeSize, vol.Encrypted, fldPath)
}
func validateVolumeFunc(volumeType *string, volumeSize string, encrypted *bool, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if volumeType == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("type"), "must not be empty"))
}
if volumeSize == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("size"), "must not be empty"))
}
if encrypted != nil {
allErrs = append(allErrs, field.NotSupported(fldPath.Child("encrypted"), *encrypted, []string{}))
}
return allErrs
}