-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmanila_webhook.go
264 lines (206 loc) · 8.84 KB
/
manila_webhook.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Copyright 2022.
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.
*/
//
// Generated by:
//
// operator-sdk create webhook --group manila --version v1beta1 --kind Manila --programmatic-validation --defaulting
//
package v1beta1
import (
"fmt"
"github.com/openstack-k8s-operators/lib-common/modules/common/service"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// ManilaDefaults -
type ManilaDefaults struct {
APIContainerImageURL string
SchedulerContainerImageURL string
ShareContainerImageURL string
DBPurgeAge int
DBPurgeSchedule string
APITimeout int
}
var manilaDefaults ManilaDefaults
// log is for logging in this package.
var manilalog = logf.Log.WithName("manila-resource")
// SetupDefaults - initializes any CRD field defaults based on environment variables
// (the defaulting mechanism itself is implemented via webhooks)
func SetupDefaults() {
// Acquire environmental defaults and initialize Manila defaults with them
manilaDefaults = ManilaDefaults{
APIContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_API_IMAGE_URL_DEFAULT", ManilaAPIContainerImage),
SchedulerContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_SCHEDULER_IMAGE_URL_DEFAULT", ManilaSchedulerContainerImage),
ShareContainerImageURL: util.GetEnvVar("RELATED_IMAGE_MANILA_SHARE_IMAGE_URL_DEFAULT", ManilaShareContainerImage),
DBPurgeAge: DBPurgeDefaultAge,
DBPurgeSchedule: DBPurgeDefaultSchedule,
APITimeout: APIDefaultTimeout,
}
manilalog.Info("Manila defaults initialized", "defaults", manilaDefaults)
}
// SetupWebhookWithManager sets up the webhook with the Manager
func (r *Manila) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
//+kubebuilder:webhook:path=/mutate-manila-openstack-org-v1beta1-manila,mutating=true,failurePolicy=fail,sideEffects=None,groups=manila.openstack.org,resources=manilas,verbs=create;update,versions=v1beta1,name=mmanila.kb.io,admissionReviewVersions=v1
var _ webhook.Defaulter = &Manila{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Manila) Default() {
manilalog.Info("default", "name", r.Name)
r.Spec.Default()
}
// Default - set defaults for this Manila spec
func (spec *ManilaSpec) Default() {
// only put container image validations here
if spec.ManilaAPI.ContainerImage == "" {
spec.ManilaAPI.ContainerImage = manilaDefaults.APIContainerImageURL
}
if spec.ManilaScheduler.ContainerImage == "" {
spec.ManilaScheduler.ContainerImage = manilaDefaults.SchedulerContainerImageURL
}
for key, manilaShare := range spec.ManilaShares {
if manilaShare.ContainerImage == "" {
manilaShare.ContainerImage = manilaDefaults.ShareContainerImageURL
// This is required, as the loop variable is a by-value copy
spec.ManilaShares[key] = manilaShare
}
}
spec.ManilaSpecBase.Default()
}
// Default - set defaults for this Manila spec base
func (spec *ManilaSpecBase) Default() {
if spec.APITimeout == 0 {
spec.APITimeout = manilaDefaults.APITimeout
}
if spec.DBPurge.Age == 0 {
spec.DBPurge.Age = manilaDefaults.DBPurgeAge
}
if spec.DBPurge.Schedule == "" {
spec.DBPurge.Schedule = manilaDefaults.DBPurgeSchedule
}
}
// Default - set defaults for this Manila spec core. This version is used by OpenStackControlplane
func (spec *ManilaSpecCore) Default() {
spec.ManilaSpecBase.Default()
}
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-manila-openstack-org-v1beta1-manila,mutating=false,failurePolicy=fail,sideEffects=None,groups=manila.openstack.org,resources=manilas,verbs=create;update,versions=v1beta1,name=vmanila.kb.io,admissionReviewVersions=v1
var _ webhook.Validator = &Manila{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Manila) ValidateCreate() (admission.Warnings, error) {
manilalog.Info("validate create", "name", r.Name)
var allErrs field.ErrorList
basePath := field.NewPath("spec")
if err := r.Spec.ValidateCreate(basePath); err != nil {
allErrs = append(allErrs, err...)
}
if len(allErrs) != 0 {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: "manila.openstack.org", Kind: "Manila"},
r.Name, allErrs)
}
return nil, nil
}
// ValidateCreate - Exported function wrapping non-exported validate functions,
// this function can be called externally to validate an manila spec.
func (spec *ManilaSpec) ValidateCreate(basePath *field.Path) field.ErrorList {
var allErrs field.ErrorList
// validate the service override key is valid
allErrs = append(allErrs, service.ValidateRoutedOverrides(
basePath.Child("manilaAPI").Child("override").Child("service"),
spec.ManilaAPI.Override.Service)...)
return allErrs
}
// ValidateCreate -
func (spec *ManilaSpecCore) ValidateCreate(basePath *field.Path) field.ErrorList {
var allErrs field.ErrorList
// validate the service override key is valid
allErrs = append(allErrs, service.ValidateRoutedOverrides(
basePath.Child("manilaAPI").Child("override").Child("service"),
spec.ManilaAPI.Override.Service)...)
return allErrs
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Manila) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
manilalog.Info("validate update", "name", r.Name)
oldManila, ok := old.(*Manila)
if !ok || oldManila == nil {
return nil, apierrors.NewInternalError(fmt.Errorf("unable to convert existing object"))
}
var allErrs field.ErrorList
basePath := field.NewPath("spec")
if err := r.Spec.ValidateUpdate(oldManila.Spec, basePath); err != nil {
allErrs = append(allErrs, err...)
}
if len(allErrs) != 0 {
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: "manila.openstack.org", Kind: "Manila"},
r.Name, allErrs)
}
return nil, nil
}
// ValidateUpdate - Exported function wrapping non-exported validate functions,
// this function can be called externally to validate an manila spec.
func (spec *ManilaSpec) ValidateUpdate(old ManilaSpec, basePath *field.Path) field.ErrorList {
var allErrs field.ErrorList
// validate the service override key is valid
allErrs = append(allErrs, service.ValidateRoutedOverrides(
basePath.Child("manilaAPI").Child("override").Child("service"),
spec.ManilaAPI.Override.Service)...)
return allErrs
}
// ValidateUpdate -
func (spec *ManilaSpecCore) ValidateUpdate(old ManilaSpecCore, basePath *field.Path) field.ErrorList {
var allErrs field.ErrorList
// validate the service override key is valid
allErrs = append(allErrs, service.ValidateRoutedOverrides(
basePath.Child("manilaAPI").Child("override").Child("service"),
spec.ManilaAPI.Override.Service)...)
return allErrs
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Manila) ValidateDelete() (admission.Warnings, error) {
manilalog.Info("validate delete", "name", r.Name)
// TODO(user): fill in your validation logic upon object deletion.
return nil, nil
}
// SetDefaultRouteAnnotations sets HAProxy timeout values of the route
func (spec *ManilaSpecCore) SetDefaultRouteAnnotations(annotations map[string]string) {
const haProxyAnno = "haproxy.router.openshift.io/timeout"
// Use a custom annotation to flag when the operator has set the default HAProxy timeout
// With the annotation func determines when to overwrite existing HAProxy timeout with the APITimeout
const manilaAnno = "api.manila.openstack.org/timeout"
valManila, okManila := annotations[manilaAnno]
valHAProxy, okHAProxy := annotations[haProxyAnno]
// Human operator set the HAProxy timeout manually
if !okManila && okHAProxy {
return
}
// Human operator modified the HAProxy timeout manually without removing the Manila flag
if okManila && okHAProxy && valManila != valHAProxy {
delete(annotations, manilaAnno)
return
}
timeout := fmt.Sprintf("%ds", spec.APITimeout)
annotations[manilaAnno] = timeout
annotations[haProxyAnno] = timeout
}