/
workers.go
79 lines (66 loc) · 2.9 KB
/
workers.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
/*
Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved.
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 contains functions to validate controller specifications
package validation
import (
"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/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/23technologies/gardener-extension-provider-hcloud/pkg/hcloud/apis/transcoder"
)
// ValidateWorkers validates the workers of a Shoot.
func ValidateWorkers(workers []core.Worker, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
for i, worker := range workers {
workerFldPath := fldPath.Index(i)
if len(worker.Zones) == 0 {
allErrs = append(allErrs, field.Required(workerFldPath.Child("zones"), "at least one zone must be configured"))
continue
}
if worker.Maximum != 0 && worker.Minimum == 0 {
allErrs = append(allErrs, field.Forbidden(workerFldPath.Child("minimum"), "minimum value must be >= 1 if maximum value > 0 (auto scaling to 0 is not supported)"))
}
zones := sets.NewString()
for j, zone := range worker.Zones {
if zones.Has(zone) {
allErrs = append(allErrs, field.Invalid(workerFldPath.Child("zones").Index(j), zone, "must only be specified once per worker group"))
continue
}
zones.Insert(zone)
}
providerConfig, _ := transcoder.DecodeWorkerConfigFromRawExtension(worker.ProviderConfig)
if providerConfig.PlacementGroupType == "spread" {
if worker.Maximum+worker.MaxSurge.IntVal > 10 {
allErrs = append(allErrs, field.Forbidden(workerFldPath.Child("maximum"), "When the workers of this pool should be placed in a placmentgroup, the pool must not be lager than 10 - MaxSurge"))
}
}
}
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
}