@@ -17,6 +17,7 @@ limitations under the License.
17
17
package v1beta1
18
18
19
19
import (
20
+ "context"
20
21
"fmt"
21
22
22
23
"github.com/coreos/butane/config/common"
@@ -37,20 +38,43 @@ var (
37
38
rke2configlog = logf .Log .WithName ("rke2config-resource" )
38
39
)
39
40
40
- // SetupWebhookWithManager sets up and registers the webhook with the manager.
41
- func (r * RKE2Config ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
41
+ // RKE2ConfigCustomDefaulter struct is responsible for setting default values on the custom resource of the
42
+ // Kind RKE2Config when those are created or updated.
43
+ // NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods,
44
+ // as it is used only for temporary operations and does not need to be deeply copied.
45
+ // +kubebuilder:object:generate=false
46
+ type RKE2ConfigCustomDefaulter struct {}
47
+
48
+ // RKE2ConfigCustomValidator struct is responsible for validating the RKE2Config resource
49
+ // when it is created, updated, or deleted.
50
+ // NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods,
51
+ // as it is used only for temporary operations and does not need to be deeply copied.
52
+ // +kubebuilder:object:generate=false
53
+ type RKE2ConfigCustomValidator struct {}
54
+
55
+ // SetupRKE2ConfigWebhookWithManager sets up the Controller Manager for the Webhook for the RKE2ControlPlaneTemplate resource.
56
+ func SetupRKE2ConfigWebhookWithManager (mgr ctrl.Manager ) error {
42
57
return ctrl .NewWebhookManagedBy (mgr ).
43
- For (r ).
58
+ For (& RKE2Config {}).
59
+ WithValidator (& RKE2ConfigCustomValidator {}).
60
+ WithDefaulter (& RKE2ConfigCustomDefaulter {}).
44
61
Complete ()
45
62
}
46
63
47
64
//+kubebuilder:webhook:path=/mutate-bootstrap-cluster-x-k8s-io-v1beta1-rke2config,mutating=true,failurePolicy=fail,sideEffects=None,groups=bootstrap.cluster.x-k8s.io,resources=rke2configs,verbs=create;update,versions=v1beta1,name=mrke2config.kb.io,admissionReviewVersions=v1
48
65
49
- var _ webhook.Defaulter = & RKE2Config {}
66
+ var _ webhook.CustomDefaulter = & RKE2ConfigCustomDefaulter {}
50
67
51
68
// Default implements webhook.Defaulter so a webhook will be registered for the type.
52
- func (r * RKE2Config ) Default () {
53
- DefaultRKE2ConfigSpec (& r .Spec )
69
+ func (r * RKE2ConfigCustomDefaulter ) Default (_ context.Context , obj runtime.Object ) error {
70
+ rc , ok := obj .(* RKE2Config )
71
+ if ! ok {
72
+ return apierrors .NewBadRequest (fmt .Sprintf ("expected a RKE2Config but got a %T" , obj ))
73
+ }
74
+
75
+ DefaultRKE2ConfigSpec (& rc .Spec )
76
+
77
+ return nil
54
78
}
55
79
56
80
// DefaultRKE2ConfigSpec defaults the RKE2ConfigSpec.
@@ -62,40 +86,50 @@ func DefaultRKE2ConfigSpec(spec *RKE2ConfigSpec) {
62
86
63
87
//+kubebuilder:webhook:path=/validate-bootstrap-cluster-x-k8s-io-v1beta1-rke2config,mutating=false,failurePolicy=fail,sideEffects=None,groups=bootstrap.cluster.x-k8s.io,resources=rke2configs,verbs=create;update,versions=v1beta1,name=vrke2config.kb.io,admissionReviewVersions=v1
64
88
65
- var _ webhook.Validator = & RKE2Config {}
89
+ var _ webhook.CustomValidator = & RKE2ConfigCustomValidator {}
66
90
67
91
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
68
- func (r * RKE2Config ) ValidateCreate () (admission.Warnings , error ) {
69
- rke2configlog .Info ("RKE2Config validate create" , "rke2config" , klog .KObj (r ))
92
+ func (r * RKE2ConfigCustomValidator ) ValidateCreate (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
93
+ rc , ok := obj .(* RKE2Config )
94
+ if ! ok {
95
+ return nil , fmt .Errorf ("expected a RKE2Config object but got %T" , obj )
96
+ }
97
+
98
+ rke2configlog .Info ("RKE2Config validate create" , "rke2config" , klog .KObj (rc ))
70
99
71
100
var allErrs field.ErrorList
72
101
73
- allErrs = append (allErrs , ValidateRKE2ConfigSpec (r .Name , & r .Spec )... )
102
+ allErrs = append (allErrs , ValidateRKE2ConfigSpec (rc .Name , & rc .Spec )... )
74
103
75
104
if len (allErrs ) == 0 {
76
105
return nil , nil
77
106
}
78
107
79
- return nil , apierrors .NewInvalid (GroupVersion .WithKind ("RKE2Config" ).GroupKind (), r .Name , allErrs )
108
+ return nil , apierrors .NewInvalid (GroupVersion .WithKind ("RKE2Config" ).GroupKind (), rc .Name , allErrs )
80
109
}
81
110
82
111
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
83
- func (r * RKE2Config ) ValidateUpdate (_ runtime.Object ) (admission.Warnings , error ) {
84
- rke2configlog .Info ("RKE2Config validate update" , "rke2config" , klog .KObj (r ))
112
+ func (r * RKE2ConfigCustomValidator ) ValidateUpdate (_ context.Context , _ , newObj runtime.Object ) (admission.Warnings , error ) {
113
+ newrc , ok := newObj .(* RKE2Config )
114
+ if ! ok {
115
+ return nil , fmt .Errorf ("expected a RKE2Config object but got %T" , newObj )
116
+ }
117
+
118
+ rke2configlog .Info ("RKE2Config validate update" , "rke2config" , klog .KObj (newrc ))
85
119
86
120
var allErrs field.ErrorList
87
121
88
- allErrs = append (allErrs , ValidateRKE2ConfigSpec (r .Name , & r .Spec )... )
122
+ allErrs = append (allErrs , ValidateRKE2ConfigSpec (newrc .Name , & newrc .Spec )... )
89
123
90
124
if len (allErrs ) == 0 {
91
125
return nil , nil
92
126
}
93
127
94
- return nil , apierrors .NewInvalid (GroupVersion .WithKind ("RKE2Config" ).GroupKind (), r .Name , allErrs )
128
+ return nil , apierrors .NewInvalid (GroupVersion .WithKind ("RKE2Config" ).GroupKind (), newrc .Name , allErrs )
95
129
}
96
130
97
131
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
98
- func (r * RKE2Config ) ValidateDelete () (admission.Warnings , error ) {
132
+ func (r * RKE2ConfigCustomValidator ) ValidateDelete (_ context. Context , _ runtime. Object ) (admission.Warnings , error ) {
99
133
return nil , nil
100
134
}
101
135
0 commit comments