-
Notifications
You must be signed in to change notification settings - Fork 37
/
cinder_webhook.go
123 lines (95 loc) · 4.16 KB
/
cinder_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
/*
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 cinder --version v1beta1 --kind Cinder --programmatic-validation --defaulting
//
package v1beta1
import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// CinderDefaults -
type CinderDefaults struct {
APIContainerImageURL string
BackupContainerImageURL string
SchedulerContainerImageURL string
VolumeContainerImageURL string
}
var cinderDefaults CinderDefaults
// log is for logging in this package.
var cinderlog = logf.Log.WithName("cinder-resource")
// SetupDefaults - FIXME: remove in followup PR to satisfy CI for this PR
func (spec *CinderSpec) SetupDefaults(defaults CinderDefaults) {
SetupCinderDefaults(defaults)
}
// SetupCinderDefaults - initialize Cinder spec defaults for use with either internal or external webhooks
func SetupCinderDefaults(defaults CinderDefaults) {
cinderDefaults = defaults
cinderlog.Info("Cinder defaults initialized", "defaults", defaults)
}
// SetupWebhookWithManager sets up the webhook with the Manager
func (r *Cinder) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
//+kubebuilder:webhook:path=/mutate-cinder-openstack-org-v1beta1-cinder,mutating=true,failurePolicy=fail,sideEffects=None,groups=cinder.openstack.org,resources=cinders,verbs=create;update,versions=v1beta1,name=mcinder.kb.io,admissionReviewVersions=v1
var _ webhook.Defaulter = &Cinder{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Cinder) Default() {
cinderlog.Info("default", "name", r.Name)
r.Spec.Default()
}
// Default - set defaults for this Cinder spec
func (spec *CinderSpec) Default() {
if spec.CinderAPI.ContainerImage == "" {
spec.CinderAPI.ContainerImage = cinderDefaults.APIContainerImageURL
}
if spec.CinderBackup.ContainerImage == "" {
spec.CinderBackup.ContainerImage = cinderDefaults.BackupContainerImageURL
}
if spec.CinderScheduler.ContainerImage == "" {
spec.CinderScheduler.ContainerImage = cinderDefaults.SchedulerContainerImageURL
}
for index, cinderVolume := range spec.CinderVolumes {
if cinderVolume.ContainerImage == "" {
cinderVolume.ContainerImage = cinderDefaults.VolumeContainerImageURL
}
// This is required, as the loop variable is a by-value copy
spec.CinderVolumes[index] = cinderVolume
}
}
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
//+kubebuilder:webhook:path=/validate-cinder-openstack-org-v1beta1-cinder,mutating=false,failurePolicy=fail,sideEffects=None,groups=cinder.openstack.org,resources=cinders,verbs=create;update,versions=v1beta1,name=vcinder.kb.io,admissionReviewVersions=v1
var _ webhook.Validator = &Cinder{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Cinder) ValidateCreate() error {
cinderlog.Info("validate create", "name", r.Name)
// TODO(user): fill in your validation logic upon object creation.
return nil
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Cinder) ValidateUpdate(old runtime.Object) error {
cinderlog.Info("validate update", "name", r.Name)
// TODO(user): fill in your validation logic upon object update.
return nil
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Cinder) ValidateDelete() error {
cinderlog.Info("validate delete", "name", r.Name)
// TODO(user): fill in your validation logic upon object deletion.
return nil
}