How to create validating webhook 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 validating 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 --programmatic-validation
FATA[0000] failed to create webhook with "go.kubebuilder.io/v3": webhook resource already exists
--force
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:path=/validate-cache-example-com-v1beta1-memcached,mutating=false,failurePolicy=fail,sideEffects=None,groups=cache.example.com,resources=memcacheds,verbs=create;update,versions=v1beta1,name=vmemcached.kb.io,admissionReviewVersions={v1alpha1,v1beta1}

var _ webhook.Validator = &Memcached{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Memcached) ValidateCreate() error {
memcachedlog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Memcached) ValidateUpdate(old runtime.Object) error {
memcachedlog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Memcached) ValidateDelete() error {
memcachedlog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
}
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
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: mutating-webhook-configuration
annotations:
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
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
spec:
replicaSize: "3.2"
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

Unity Development — Collectables

A quick overview of C# variables

Why pure-play news platform may not establish stickiness of user

The Top Azure Data Platform Announcements from MS Ignite

How CSS works? A look behind the scenes

Cascade

How to Install Mega Cloud Drive on Ubuntu — Free 20GB Storage

Deconstructing The Singleton

Computer Networks .. Part 2.. The Post Offices of Networks

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

Data Proof (excel file explanation)

5 Step-by-step guide to know about these awesome decentralized exchanges

10X Potential: Digital Turbine Inc (Ticker: APPS) — Part 4

The top 5 financial risk management software companies in 2022

financial risk management software