-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwms_webhook.go
More file actions
104 lines (84 loc) · 4.19 KB
/
wms_webhook.go
File metadata and controls
104 lines (84 loc) · 4.19 KB
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
/*
MIT License
Copyright (c) 2024 Publieke Dienstverlening op de Kaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//nolint:dupl
package v3
import (
"context"
"fmt"
"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"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
pdoknlv3 "github.com/pdok/mapserver-operator/api/v3"
)
// log is for logging in this package.
var wmsLog = logf.Log.WithName("wms-resource")
// SetupWMSWebhookWithManager registers the webhook for WMS in the manager.
func SetupWMSWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).For(&pdoknlv3.WMS{}).
WithValidator(&WMSCustomValidator{}).
Complete()
}
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// NOTE: The 'path' attribute must follow a specific pattern and should not be modified directly here.
// Modifying the path for an invalid path can cause API server errors; failing to locate the webhook.
// +kubebuilder:webhook:path=/validate-pdok-nl-v3-wms,mutating=false,failurePolicy=fail,sideEffects=None,groups=pdok.nl,resources=wms,verbs=create;update,versions=v3,name=vwms-v3.kb.io,admissionReviewVersions=v1
// WMSCustomValidator struct is responsible for validating the WMS resource
// when it is created, updated, or deleted.
//
// NOTE: The +kubebuilder:object:generate=false marker prevents controller-gen from generating DeepCopy methods,
// as this struct is used only for temporary operations and does not need to be deeply copied.
type WMSCustomValidator struct {
// TODO(user): Add more fields as needed for validation
}
var _ webhook.CustomValidator = &WMSCustomValidator{}
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type WMS.
func (v *WMSCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
wms, ok := obj.(*pdoknlv3.WMS)
if !ok {
return nil, fmt.Errorf("expected a WMS object but got %T", obj)
}
wmsLog.Info("Validation for WMS upon creation", "name", wms.GetName())
return wms.ValidateCreate()
}
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type WMS.
func (v *WMSCustomValidator) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
wms, ok := newObj.(*pdoknlv3.WMS)
if !ok {
return nil, fmt.Errorf("expected a WMS object for the newObj but got %T", newObj)
}
wmsOld, ok := oldObj.(*pdoknlv3.WMS)
if !ok {
return nil, fmt.Errorf("expected a WMS object for the oldObj but got %T", newObj)
}
wmsLog.Info("Validation for WMS upon update", "name", wms.GetName())
return wms.ValidateUpdate(wmsOld)
}
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type WMS.
func (v *WMSCustomValidator) ValidateDelete(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
wms, ok := obj.(*pdoknlv3.WMS)
if !ok {
return nil, fmt.Errorf("expected a WMS object but got %T", obj)
}
wmsLog.Info("Validation for WMS upon deletion", "name", wms.GetName())
// TODO(user): fill in your validation logic upon object deletion.
return nil, nil
}