Skip to content
This repository was archived by the owner on Nov 18, 2020. It is now read-only.

Commit 01a0e2d

Browse files
Added webhook example (#124)
* Added webhook example
1 parent 9bb42cd commit 01a0e2d

5 files changed

Lines changed: 167 additions & 31 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"errors"
21+
22+
"k8s.io/apimachinery/pkg/runtime"
23+
ctrl "sigs.k8s.io/controller-runtime"
24+
logf "sigs.k8s.io/controller-runtime/pkg/log"
25+
"sigs.k8s.io/controller-runtime/pkg/webhook"
26+
)
27+
28+
// log is for logging in this package.
29+
var memcachedlog = logf.Log.WithName("memcached-resource")
30+
31+
func (r *Memcached) SetupWebhookWithManager(mgr ctrl.Manager) error {
32+
return ctrl.NewWebhookManagedBy(mgr).
33+
For(r).
34+
Complete()
35+
}
36+
37+
// +kubebuilder:webhook:path=/mutate-cache-example-com-v1alpha1-memcached,mutating=true,failurePolicy=fail,groups=cache.example.com,resources=memcacheds,verbs=create;update,versions=v1alpha1,name=mmemcached.kb.io
38+
39+
var _ webhook.Defaulter = &Memcached{}
40+
41+
// Default implements webhook.Defaulter so a webhook will be registered for the type
42+
func (r *Memcached) Default() {
43+
memcachedlog.Info("default", "name", r.Name)
44+
45+
if r.Spec.Size == 0 {
46+
r.Spec.Size = 3
47+
}
48+
}
49+
50+
// +kubebuilder:webhook:verbs=create;update,path=/validate-cache-example-com-v1alpha1-memcached,mutating=false,failurePolicy=fail,groups=cache.example.com,resources=memcacheds,versions=v1alpha1,name=vmemcached.kb.io
51+
52+
var _ webhook.Validator = &Memcached{}
53+
54+
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
55+
func (r *Memcached) ValidateCreate() error {
56+
memcachedlog.Info("validate create", "name", r.Name)
57+
58+
return validateOdd(r.Spec.Size)
59+
}
60+
61+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
62+
func (r *Memcached) ValidateUpdate(old runtime.Object) error {
63+
memcachedlog.Info("validate update", "name", r.Name)
64+
65+
return validateOdd(r.Spec.Size)
66+
}
67+
68+
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
69+
func (r *Memcached) ValidateDelete() error {
70+
memcachedlog.Info("validate delete", "name", r.Name)
71+
72+
return nil
73+
}
74+
75+
func validateOdd(n int32) error {
76+
if n%2 == 0 {
77+
return errors.New("Cluster size must be an odd number")
78+
}
79+
return nil
80+
}

go/kubebuilder/memcached-operator/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/kubebuilder/memcached-operator/config/default/kustomization.yaml

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ bases:
1818
- ../manager
1919
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2020
# crd/kustomization.yaml
21-
#- ../webhook
21+
- ../webhook
2222
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
23-
#- ../certmanager
23+
- ../certmanager
2424
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
2525
#- ../prometheus
2626

@@ -32,39 +32,39 @@ patchesStrategicMerge:
3232

3333
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
3434
# crd/kustomization.yaml
35-
#- manager_webhook_patch.yaml
35+
- manager_webhook_patch.yaml
3636

3737
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
3838
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
3939
# 'CERTMANAGER' needs to be enabled to use ca injection
40-
#- webhookcainjection_patch.yaml
40+
- webhookcainjection_patch.yaml
4141

4242
# the following config is for teaching kustomize how to do var substitution
4343
vars:
4444
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
45-
#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
46-
# objref:
47-
# kind: Certificate
48-
# group: cert-manager.io
49-
# version: v1alpha2
50-
# name: serving-cert # this name should match the one in certificate.yaml
51-
# fieldref:
52-
# fieldpath: metadata.namespace
53-
#- name: CERTIFICATE_NAME
54-
# objref:
55-
# kind: Certificate
56-
# group: cert-manager.io
57-
# version: v1alpha2
58-
# name: serving-cert # this name should match the one in certificate.yaml
59-
#- name: SERVICE_NAMESPACE # namespace of the service
60-
# objref:
61-
# kind: Service
62-
# version: v1
63-
# name: webhook-service
64-
# fieldref:
65-
# fieldpath: metadata.namespace
66-
#- name: SERVICE_NAME
67-
# objref:
68-
# kind: Service
69-
# version: v1
70-
# name: webhook-service
45+
- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
46+
objref:
47+
kind: Certificate
48+
group: cert-manager.io
49+
version: v1alpha2
50+
name: serving-cert # this name should match the one in certificate.yaml
51+
fieldref:
52+
fieldpath: metadata.namespace
53+
- name: CERTIFICATE_NAME
54+
objref:
55+
kind: Certificate
56+
group: cert-manager.io
57+
version: v1alpha2
58+
name: serving-cert # this name should match the one in certificate.yaml
59+
- name: SERVICE_NAMESPACE # namespace of the service
60+
objref:
61+
kind: Service
62+
version: v1
63+
name: webhook-service
64+
fieldref:
65+
fieldpath: metadata.namespace
66+
- name: SERVICE_NAME
67+
objref:
68+
kind: Service
69+
version: v1
70+
name: webhook-service
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
---
3+
apiVersion: admissionregistration.k8s.io/v1beta1
4+
kind: MutatingWebhookConfiguration
5+
metadata:
6+
creationTimestamp: null
7+
name: mutating-webhook-configuration
8+
webhooks:
9+
- clientConfig:
10+
caBundle: Cg==
11+
service:
12+
name: webhook-service
13+
namespace: system
14+
path: /mutate-cache-example-com-v1alpha1-memcached
15+
failurePolicy: Fail
16+
name: mmemcached.kb.io
17+
rules:
18+
- apiGroups:
19+
- cache.example.com
20+
apiVersions:
21+
- v1alpha1
22+
operations:
23+
- CREATE
24+
- UPDATE
25+
resources:
26+
- memcacheds
27+
28+
---
29+
apiVersion: admissionregistration.k8s.io/v1beta1
30+
kind: ValidatingWebhookConfiguration
31+
metadata:
32+
creationTimestamp: null
33+
name: validating-webhook-configuration
34+
webhooks:
35+
- clientConfig:
36+
caBundle: Cg==
37+
service:
38+
name: webhook-service
39+
namespace: system
40+
path: /validate-cache-example-com-v1alpha1-memcached
41+
failurePolicy: Fail
42+
name: vmemcached.kb.io
43+
rules:
44+
- apiGroups:
45+
- cache.example.com
46+
apiVersions:
47+
- v1alpha1
48+
operations:
49+
- CREATE
50+
- UPDATE
51+
resources:
52+
- memcacheds

go/kubebuilder/memcached-operator/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func main() {
7474
setupLog.Error(err, "unable to create controller", "controller", "Memcached")
7575
os.Exit(1)
7676
}
77+
if err = (&cachev1alpha1.Memcached{}).SetupWebhookWithManager(mgr); err != nil {
78+
setupLog.Error(err, "unable to create webhook", "webhook", "Memcached")
79+
os.Exit(1)
80+
}
7781
// +kubebuilder:scaffold:builder
7882

7983
setupLog.Info("starting manager")

0 commit comments

Comments
 (0)