How to use mutating webhook for the operator with operator-sdk

  • Prerequisites
  • Prepare your workstation
  • Create the operator. Follow all the step below this section. You can either choose to finish the creation and configuration of webhook, because that’s also necessary for the defaulting webhook, or skip the command to create the conversion webhook.
cd $GOPATH/src/github.com/example/memcached-operator
operator-sdk create webhook --version v1beta1 --kind Memcached --group cache --defaulting --force
--force 
//+kubebuilder:webhook:path=/mutate-cache-example-com-v1beta1-memcached,mutating=true,failurePolicy=fail,sideEffects=None,groups=cache.example.com,resources=memcacheds,verbs=create;update,versions=v1beta1,name=mmemcached.kb.io,admissionReviewVersions={v1alpha1,v1beta1}

var _ webhook.Defaulter = &Memcached{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *Memcached) Default() {
memcachedlog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
}
func (r *Memcached) Default() {
memcachedlog.Info("default", "name", r.Name)
if r.Spec.ReplicaSize == 0 {
r.Spec.ReplicaSize = 2
}
}
make generate
make manifests
#- patches/webhook_in_memcacheds.yaml
#- patches/cainjection_in_memcacheds.yaml
#- ../webhook
#- ../certmanager
#- manager_webhook_patch.yaml
#- webhookcainjection_patch.yaml
#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
# fieldref:
# fieldpath: metadata.namespace
#- name: CERTIFICATE_NAME
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
#- name: SERVICE_NAMESPACE # namespace of the service
# objref:
# kind: Service
# version: v1
# name: webhook-service
# fieldref:
# fieldpath: metadata.namespace
#- name: SERVICE_NAME
# objref:
# kind: Service
# version: v1
# name: webhook-service
export USER=<name>
make docker-build docker-push IMG=docker.io/$USER/memcached-operator:v0.0.2
make deploy IMG=docker.io/$USER/memcached-operator:v0.0.2
apiVersion: v1
kind: Namespace
metadata:
name: memcached-sample
---
apiVersion: cache.example.com/v1beta1
kind: Memcached
metadata:
name: memcached-sample
namespace: memcached-sample
kubectl logs -f deploy/memcached-operator-controller-manager -n memcached-operator-system -c manager

--

--

--

A Chinese software engineer, used to study in Belgium and currently working in US, as Knative & Tekton Operator Lead and Istio Operator Contributor.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Key features to look at from a system integrator in 2022

Reading Annotations at run-time using the Java Reflections API

Why You Should be Developing On the Command-Line

Beautify PowerShell on Windows 10

TAG Patch Notes: v0.1.0 → v0.1.7

Here’s how you can share your WhatsApp status on Facebook : Gadget Game News

Typing “ls -l” In the Shell

Musings For The Aspiring Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Hou

Vincent Hou

A Chinese software engineer, used to study in Belgium and currently working in US, as Knative & Tekton Operator Lead and Istio Operator Contributor.

More from Medium

How I overcome “writer’s block” — The Daily PPILL #89

Non-Disruptive Optimized Planning Reduces Milk-Run Costs

Banksters? Low-Income Customers are being Hammered with Overdraft Fees.

Too many Business Leaders Abdicate rather than Delegate