/
cache_webhook.go
176 lines (151 loc) · 5.78 KB
/
cache_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package v2alpha1
import (
"context"
"errors"
"fmt"
"net/http"
admissionv1 "k8s.io/api/admission/v1"
v1 "k8s.io/api/admission/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
var log = ctrl.Log.WithName("webhook").WithName("Cache")
func (c *Cache) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(c).
Complete()
}
// +kubebuilder:webhook:path=/mutate-infinispan-org-v2alpha1-cache,mutating=true,failurePolicy=fail,sideEffects=None,groups=infinispan.org,resources=caches,verbs=create;update,versions=v2alpha1,name=mcache.kb.io,admissionReviewVersions={v1,v1beta1}
var _ webhook.Defaulter = &Cache{}
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (c *Cache) Default() {
if c.Spec.AdminAuth != nil {
log.Info("Ignoring and removing 'spec.AdminAuth' field. The operator's admin credentials are now used to perform cache operations")
c.Spec.AdminAuth = nil
}
if c.Spec.Updates == nil {
c.Spec.Updates = &CacheUpdateSpec{
Strategy: CacheUpdateRetain,
}
}
if c.Spec.Updates.Strategy == "" {
c.Spec.Updates.Strategy = CacheUpdateRetain
}
}
// +kubebuilder:webhook:path=/validate-infinispan-org-v2alpha1-cache,mutating=false,failurePolicy=fail,sideEffects=None,groups=infinispan.org,resources=caches,verbs=create;update,versions=v2alpha1,name=vcache.kb.io,admissionReviewVersions={v1,v1beta1}
// RegisterCacheValidatingWebhook explicitly adds the validating webhook to the Webhook Server
// This is necessary as we need to implement admission.Handler interface directly so that the request context can be
// used by the runtime client
func RegisterCacheValidatingWebhook(mgr ctrl.Manager) {
hookServer := mgr.GetWebhookServer()
hookServer.Register("/validate-infinispan-org-v2alpha1-cache", &webhook.Admission{
Handler: &cacheValidator{},
})
}
type cacheValidator struct {
client runtimeClient.Client
decoder *admission.Decoder
}
var _ inject.Client = &cacheValidator{}
var _ admission.Handler = &cacheValidator{}
func (cv *cacheValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
// Get the object in the request
cache := &Cache{}
if req.Operation == v1.Create {
err := cv.decoder.Decode(req, cache)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
err = cv.Create(ctx, cache)
if err != nil {
var apiStatus apierrors.APIStatus
if errors.As(err, &apiStatus) {
return validationResponseFromStatus(false, apiStatus.Status())
}
return admission.Denied(err.Error())
}
}
if req.Operation == v1.Update {
oldCache := &Cache{}
err := cv.decoder.DecodeRaw(req.Object, cache)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
err = cv.decoder.DecodeRaw(req.OldObject, oldCache)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
err = cv.Update(cache, oldCache)
if err != nil {
var apiStatus apierrors.APIStatus
if errors.As(err, &apiStatus) {
return validationResponseFromStatus(false, apiStatus.Status())
}
return admission.Denied(err.Error())
}
}
return admission.Allowed("")
}
// InjectClient injects the client.
func (cv *cacheValidator) InjectClient(c runtimeClient.Client) error {
cv.client = c
return nil
}
// InjectDecoder injects the decoder.
func (cv *cacheValidator) InjectDecoder(d *admission.Decoder) error {
cv.decoder = d
return nil
}
func (cv *cacheValidator) Create(ctx context.Context, c *Cache) error {
var allErrs field.ErrorList
if c.Spec.ClusterName == "" {
allErrs = append(allErrs, field.Required(field.NewPath("spec").Child("clusterName"), "'spec.clusterName' must be configured"))
}
// Ensure that a Cache CR does not already exist in this namespace with the same spec.Name
list := &CacheList{}
if err := cv.client.List(ctx, list, &runtimeClient.ListOptions{Namespace: c.Namespace}); err != nil {
allErrs = append(allErrs, field.InternalError(field.NewPath("spec").Child("name"), err))
} else {
for _, cache := range list.Items {
if c.Spec.Name == cache.Spec.Name && c.Spec.ClusterName == cache.Spec.ClusterName {
msg := fmt.Sprintf("Cache CR already exists for cluster '%s' with spec.Name '%s'", c.Spec.ClusterName, c.Spec.Name)
allErrs = append(allErrs, field.Duplicate(field.NewPath("spec").Child("name"), msg))
}
}
}
return StatusError(c, allErrs)
}
func (cv *cacheValidator) Update(c *Cache, oldCache *Cache) error {
var allErrs field.ErrorList
if oldCache.Spec.ClusterName != c.Spec.ClusterName {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("clusterName"), "Cache clusterName is immutable and cannot be updated after initial Cache creation"))
}
if oldCache.Spec.Name != c.Spec.Name {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec").Child("name"), "Cache name is immutable and cannot be updated after initial Cache creation"))
}
return StatusError(c, allErrs)
}
func StatusError(c *Cache, allErrs field.ErrorList) error {
if len(allErrs) != 0 {
return apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: "Cache"},
c.Name, allErrs)
}
return nil
}
// validationResponseFromStatus returns a response for admitting a request with provided Status object.
func validationResponseFromStatus(allowed bool, status metav1.Status) admission.Response {
return admission.Response{
AdmissionResponse: admissionv1.AdmissionResponse{
Allowed: allowed,
Result: &status,
},
}
}