@@ -17,7 +17,9 @@ limitations under the License.
17
17
package v1beta1
18
18
19
19
import (
20
+ "errors"
20
21
"k8s.io/apimachinery/pkg/runtime"
22
+ "regexp"
21
23
ctrl "sigs.k8s.io/controller-runtime"
22
24
logf "sigs.k8s.io/controller-runtime/pkg/log"
23
25
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -32,9 +34,6 @@ func (r *Module) SetupWebhookWithManager(mgr ctrl.Manager) error {
32
34
Complete ()
33
35
}
34
36
35
- // TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
36
-
37
- // TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
38
37
//+kubebuilder:webhook:path=/validate-kmm-sigs-x-k8s-io-v1beta1-module,mutating=false,failurePolicy=fail,sideEffects=None,groups=kmm.sigs.x-k8s.io,resources=modules,verbs=create;update,versions=v1beta1,name=vmodule.kb.io,admissionReviewVersions=v1
39
38
40
39
var _ webhook.Validator = & Module {}
@@ -43,22 +42,39 @@ var _ webhook.Validator = &Module{}
43
42
func (r * Module ) ValidateCreate () error {
44
43
modulelog .Info ("validate create" , "name" , r .Name )
45
44
46
- // TODO(user): fill in your validation logic upon object creation.
45
+ if err := r .validateKernelMappingRegex (); err != nil {
46
+ return err
47
+ }
48
+
47
49
return nil
48
50
}
49
51
50
52
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
51
53
func (r * Module ) ValidateUpdate (old runtime.Object ) error {
52
54
modulelog .Info ("validate update" , "name" , r .Name )
53
55
54
- // TODO(user): fill in your validation logic upon object update.
56
+ if err := r .validateKernelMappingRegex (); err != nil {
57
+ return err
58
+ }
59
+
55
60
return nil
56
61
}
57
62
58
63
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
59
64
func (r * Module ) ValidateDelete () error {
60
- modulelog .Info ("validate delete" , "name" , r .Name )
65
+ return nil
66
+ }
67
+
68
+ func (r * Module ) validateKernelMappingRegex () error {
69
+ if r .Spec .ModuleLoader .Container .KernelMappings == nil {
70
+ return nil
71
+ }
72
+
73
+ for _ , km := range r .Spec .ModuleLoader .Container .KernelMappings {
74
+ if _ , rErr := regexp .Compile (km .Regexp ); rErr != nil {
75
+ return errors .New ("invalid regex: " + rErr .Error ())
76
+ }
77
+ }
61
78
62
- // TODO(user): fill in your validation logic upon object deletion.
63
79
return nil
64
80
}
0 commit comments