-
Notifications
You must be signed in to change notification settings - Fork 0
/
shoot.go
171 lines (141 loc) · 6.55 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2022 OnMetal 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 validator
import (
"context"
"fmt"
"reflect"
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
"github.com/gardener/gardener/pkg/apis/core"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/onmetal/gardener-extension-provider-onmetal/pkg/admission"
apisonmetal "github.com/onmetal/gardener-extension-provider-onmetal/pkg/apis/onmetal"
onmetalvalidation "github.com/onmetal/gardener-extension-provider-onmetal/pkg/apis/onmetal/validation"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
type shoot struct {
client client.Client
decoder runtime.Decoder
lenientDecoder runtime.Decoder
}
// NewShootValidator returns a new instance of a shoot validator.
func NewShootValidator(mgr manager.Manager) extensionswebhook.Validator {
return &shoot{
decoder: serializer.NewCodecFactory(mgr.GetScheme(), serializer.EnableStrict).UniversalDecoder(),
}
}
// Validate validates the given shoot objects.
func (s *shoot) Validate(ctx context.Context, new, old client.Object) error {
shoot, ok := new.(*core.Shoot)
if !ok {
return fmt.Errorf("wrong object type %T", new)
}
if old != nil {
oldShoot, ok := old.(*core.Shoot)
if !ok {
return fmt.Errorf("wrong object type %T for old object", old)
}
return s.validateUpdate(ctx, oldShoot, shoot)
}
return s.validateCreate(ctx, shoot)
}
var (
specPath = field.NewPath("spec")
networkPath = specPath.Child("networking")
providerPath = specPath.Child("provider")
infrastructureConfigPath = providerPath.Child("infrastructureConfig")
controlPlaneConfigPath = providerPath.Child("controlPlaneConfig")
workersPath = providerPath.Child("workers")
)
type validationContext struct {
shoot *core.Shoot
infrastructureConfig *apisonmetal.InfrastructureConfig
controlPlaneConfig *apisonmetal.ControlPlaneConfig
cloudProfile *gardencorev1beta1.CloudProfile
}
func (s *shoot) validateContext(valContext *validationContext) field.ErrorList {
var (
allErrors = field.ErrorList{}
)
allErrors = append(allErrors, onmetalvalidation.ValidateNetworking(valContext.shoot.Spec.Networking, networkPath)...)
allErrors = append(allErrors, onmetalvalidation.ValidateInfrastructureConfig(valContext.infrastructureConfig, valContext.shoot.Spec.Networking.Nodes, valContext.shoot.Spec.Networking.Pods, valContext.shoot.Spec.Networking.Services, infrastructureConfigPath)...)
allErrors = append(allErrors, onmetalvalidation.ValidateWorkers(valContext.shoot.Spec.Provider.Workers, workersPath)...)
allErrors = append(allErrors, onmetalvalidation.ValidateControlPlaneConfig(valContext.controlPlaneConfig, valContext.shoot.Spec.Kubernetes.Version, controlPlaneConfigPath)...)
return allErrors
}
func (s *shoot) validateCreate(ctx context.Context, shoot *core.Shoot) error {
validationContext, err := newValidationContext(ctx, s.decoder, s.client, shoot)
if err != nil {
return err
}
return s.validateContext(validationContext).ToAggregate()
}
func (s *shoot) validateUpdate(ctx context.Context, oldShoot, currentShoot *core.Shoot) error {
oldValContext, err := newValidationContext(ctx, s.lenientDecoder, s.client, oldShoot)
if err != nil {
return err
}
currentValContext, err := newValidationContext(ctx, s.decoder, s.client, currentShoot)
if err != nil {
return err
}
var (
oldInfrastructureConfig, currentInfrastructureConfig = oldValContext.infrastructureConfig, currentValContext.infrastructureConfig
oldControlPlaneConfig, currentControlPlaneConfig = oldValContext.controlPlaneConfig, currentValContext.controlPlaneConfig
allErrors = field.ErrorList{}
)
if !reflect.DeepEqual(oldInfrastructureConfig, currentInfrastructureConfig) {
allErrors = append(allErrors, onmetalvalidation.ValidateInfrastructureConfigUpdate(oldInfrastructureConfig, currentInfrastructureConfig, infrastructureConfigPath)...)
}
if !reflect.DeepEqual(oldControlPlaneConfig, currentControlPlaneConfig) {
allErrors = append(allErrors, onmetalvalidation.ValidateControlPlaneConfigUpdate(oldControlPlaneConfig, currentControlPlaneConfig, controlPlaneConfigPath)...)
}
allErrors = append(allErrors, onmetalvalidation.ValidateWorkersUpdate(oldValContext.shoot.Spec.Provider.Workers, currentValContext.shoot.Spec.Provider.Workers, workersPath)...)
allErrors = append(allErrors, s.validateContext(currentValContext)...)
return allErrors.ToAggregate()
}
func newValidationContext(ctx context.Context, decoder runtime.Decoder, c client.Client, shoot *core.Shoot) (*validationContext, error) {
if shoot.Spec.Provider.InfrastructureConfig == nil {
return nil, field.Required(infrastructureConfigPath, "infrastructureConfig must be set for onmetal shoots")
}
infrastructureConfig, err := admission.DecodeInfrastructureConfig(decoder, shoot.Spec.Provider.InfrastructureConfig)
if err != nil {
return nil, fmt.Errorf("error decoding infrastructureConfig: %v", err)
}
if shoot.Spec.Provider.ControlPlaneConfig == nil {
return nil, field.Required(controlPlaneConfigPath, "controlPlaneConfig must be set for onmetal shoots")
}
controlPlaneConfig, err := admission.DecodeControlPlaneConfig(decoder, shoot.Spec.Provider.ControlPlaneConfig)
if err != nil {
return nil, fmt.Errorf("error decoding controlPlaneConfig: %v", err)
}
cloudProfile := &gardencorev1beta1.CloudProfile{}
if err := c.Get(ctx, kutil.Key(shoot.Spec.CloudProfileName), cloudProfile); err != nil {
return nil, err
}
if cloudProfile.Spec.ProviderConfig == nil {
return nil, fmt.Errorf("providerConfig is not given for cloud profile %q", cloudProfile.Name)
}
return &validationContext{
shoot: shoot,
infrastructureConfig: infrastructureConfig,
controlPlaneConfig: controlPlaneConfig,
cloudProfile: cloudProfile,
}, nil
}