@@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package v1beta1
17
+ package webhooks
18
18
19
19
import (
20
+ "context"
20
21
"fmt"
21
22
"reflect"
22
23
@@ -28,31 +29,42 @@ import (
28
29
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
29
30
30
31
"sigs.k8s.io/cluster-api/feature"
32
+ infrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1"
31
33
)
32
34
33
35
const dockerClusterTemplateImmutableMsg = "DockerClusterTemplate spec.template.spec field is immutable. Please create a new resource instead."
34
36
35
- func (r * DockerClusterTemplate ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
37
+ // DockerClusterTemplate implements a validating and defaulting webhook for DockerClusterTemplate.
38
+ type DockerClusterTemplate struct {}
39
+
40
+ func (webhook * DockerClusterTemplate ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
36
41
return ctrl .NewWebhookManagedBy (mgr ).
37
- For (r ).
42
+ For (& infrav1.DockerClusterTemplate {}).
43
+ WithDefaulter (webhook ).
44
+ WithValidator (webhook ).
38
45
Complete ()
39
46
}
40
47
41
48
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-dockerclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=dockerclustertemplates,versions=v1beta1,name=default.dockerclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
42
49
43
- var _ webhook.Defaulter = & DockerClusterTemplate {}
50
+ var _ webhook.CustomDefaulter = & DockerClusterTemplate {}
44
51
45
52
// Default implements webhook.Defaulter so a webhook will be registered for the type.
46
- func (r * DockerClusterTemplate ) Default () {
47
- defaultDockerClusterSpec (& r .Spec .Template .Spec )
53
+ func (webhook * DockerClusterTemplate ) Default (_ context.Context , obj runtime.Object ) error {
54
+ clusterTemplate , ok := obj .(* infrav1.DockerClusterTemplate )
55
+ if ! ok {
56
+ return apierrors .NewBadRequest (fmt .Sprintf ("expected a DockerClusterTemplate but got a %T" , obj ))
57
+ }
58
+ defaultDockerClusterSpec (& clusterTemplate .Spec .Template .Spec )
59
+ return nil
48
60
}
49
61
50
62
// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-dockerclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=dockerclustertemplates,versions=v1beta1,name=validation.dockerclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
51
63
52
- var _ webhook.Validator = & DockerClusterTemplate {}
64
+ var _ webhook.CustomValidator = & DockerClusterTemplate {}
53
65
54
66
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
55
- func (r * DockerClusterTemplate ) ValidateCreate () (admission.Warnings , error ) {
67
+ func (webhook * DockerClusterTemplate ) ValidateCreate (_ context. Context , obj runtime. Object ) (admission.Warnings , error ) {
56
68
// NOTE: DockerClusterTemplate is behind ClusterTopology feature gate flag; the web hook
57
69
// must prevent creating new objects in case the feature flag is disabled.
58
70
if ! feature .Gates .Enabled (feature .ClusterTopology ) {
@@ -62,34 +74,43 @@ func (r *DockerClusterTemplate) ValidateCreate() (admission.Warnings, error) {
62
74
)
63
75
}
64
76
65
- allErrs := validateDockerClusterSpec (r .Spec .Template .Spec )
77
+ clusterTemplate , ok := obj .(* infrav1.DockerClusterTemplate )
78
+ if ! ok {
79
+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a DockerClusterTemplate but got a %T" , obj ))
80
+ }
81
+
82
+ allErrs := validateDockerClusterSpec (clusterTemplate .Spec .Template .Spec )
66
83
67
84
// Validate the metadata of the template.
68
- allErrs = append (allErrs , r .Spec .Template .ObjectMeta .Validate (field .NewPath ("spec" , "template" , "metadata" ))... )
85
+ allErrs = append (allErrs , clusterTemplate .Spec .Template .ObjectMeta .Validate (field .NewPath ("spec" , "template" , "metadata" ))... )
69
86
70
87
if len (allErrs ) > 0 {
71
- return nil , apierrors .NewInvalid (GroupVersion .WithKind ("DockerClusterTemplate" ).GroupKind (), r .Name , allErrs )
88
+ return nil , apierrors .NewInvalid (infrav1 . GroupVersion .WithKind ("DockerClusterTemplate" ).GroupKind (), clusterTemplate .Name , allErrs )
72
89
}
73
90
return nil , nil
74
91
}
75
92
76
93
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
77
- func (r * DockerClusterTemplate ) ValidateUpdate (oldRaw runtime.Object ) (admission.Warnings , error ) {
94
+ func (webhook * DockerClusterTemplate ) ValidateUpdate (_ context. Context , oldRaw , newRaw runtime.Object ) (admission.Warnings , error ) {
78
95
var allErrs field.ErrorList
79
- old , ok := oldRaw .(* DockerClusterTemplate )
96
+ oldTemplate , ok := oldRaw .(* infrav1. DockerClusterTemplate )
80
97
if ! ok {
81
98
return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a DockerClusterTemplate but got a %T" , oldRaw ))
82
99
}
83
- if ! reflect .DeepEqual (r .Spec .Template .Spec , old .Spec .Template .Spec ) {
84
- allErrs = append (allErrs , field .Invalid (field .NewPath ("spec" , "template" , "spec" ), r , dockerClusterTemplateImmutableMsg ))
100
+ newTemplate , ok := newRaw .(* infrav1.DockerClusterTemplate )
101
+ if ! ok {
102
+ return nil , apierrors .NewBadRequest (fmt .Sprintf ("expected a DockerClusterTemplate but got a %T" , newRaw ))
103
+ }
104
+ if ! reflect .DeepEqual (newTemplate .Spec .Template .Spec , oldTemplate .Spec .Template .Spec ) {
105
+ allErrs = append (allErrs , field .Invalid (field .NewPath ("spec" , "template" , "spec" ), newTemplate , dockerClusterTemplateImmutableMsg ))
85
106
}
86
107
if len (allErrs ) == 0 {
87
108
return nil , nil
88
109
}
89
- return nil , apierrors .NewInvalid (GroupVersion .WithKind ("DockerClusterTemplate" ).GroupKind (), r .Name , allErrs )
110
+ return nil , apierrors .NewInvalid (infrav1 . GroupVersion .WithKind ("DockerClusterTemplate" ).GroupKind (), newTemplate .Name , allErrs )
90
111
}
91
112
92
113
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
93
- func (r * DockerClusterTemplate ) ValidateDelete () (admission.Warnings , error ) {
114
+ func (webhook * DockerClusterTemplate ) ValidateDelete (_ context. Context , _ runtime. Object ) (admission.Warnings , error ) {
94
115
return nil , nil
95
116
}
0 commit comments