Deploying Admission Webhooks

Kind Cluster

](https://book.kubebuilder.io/cronjob-tutorial/#kind-cluster) It is recommended to develop your webhook with akind cluster for faster iteration.Why?

  • You can bring up a multi-node cluster locally within 1 minute.
  • You can tear it down in seconds.
  • You don’t need to push your images to remote registry.

Cert Manager

You need follow this to install the cert manager bundle.

Build your image

Run the following command to build your image locally.

  1. make docker-build

You don’t need to push the image to a remote container registry if you are usinga kind cluster. You can directly load your local image to your kind cluster:

  1. kind load docker-image your-image-namge:your-tag

Deploy Webhooks

You need to enable the webhook and cert manager configuration through kustomize.config/default/kustomization.yaml should now look like the following:

  1. # Adds namespace to all resources.
  2. namespace: project-system
  3. # Value of this field is prepended to the
  4. # names of all resources, e.g. a deployment named
  5. # "wordpress" becomes "alices-wordpress".
  6. # Note that it should also match with the prefix (text before '-') of the namespace
  7. # field above.
  8. namePrefix: project-
  9. # Labels to add to all resources and selectors.
  10. #commonLabels:
  11. # someName: someValue
  12. bases:
  13. - ../crd
  14. - ../rbac
  15. - ../manager
  16. # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
  17. - ../webhook
  18. # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
  19. - ../certmanager
  20. patches:
  21. - manager_image_patch.yaml
  22. # Protect the /metrics endpoint by putting it behind auth.
  23. # Only one of manager_auth_proxy_patch.yaml and
  24. # manager_prometheus_metrics_patch.yaml should be enabled.
  25. - manager_auth_proxy_patch.yaml
  26. # If you want your controller-manager to expose the /metrics
  27. # endpoint w/o any authn/z, uncomment the following line and
  28. # comment manager_auth_proxy_patch.yaml.
  29. # Only one of manager_auth_proxy_patch.yaml and
  30. # manager_prometheus_metrics_patch.yaml should be enabled.
  31. #- manager_prometheus_metrics_patch.yaml
  32. # [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in crd/kustomization.yaml
  33. - manager_webhook_patch.yaml
  34. # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
  35. # Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
  36. # 'CERTMANAGER' needs to be enabled to use ca injection
  37. - webhookcainjection_patch.yaml
  38. # the following config is for teaching kustomize how to do var substitution
  39. vars:
  40. # [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
  41. - name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
  42. objref:
  43. kind: Certificate
  44. group: certmanager.k8s.io
  45. version: v1alpha1
  46. name: serving-cert # this name should match the one in certificate.yaml
  47. fieldref:
  48. fieldpath: metadata.namespace
  49. - name: CERTIFICATE_NAME
  50. objref:
  51. kind: Certificate
  52. group: certmanager.k8s.io
  53. version: v1alpha1
  54. name: serving-cert # this name should match the one in certificate.yaml
  55. - name: SERVICE_NAMESPACE # namespace of the service
  56. objref:
  57. kind: Service
  58. version: v1
  59. name: webhook-service
  60. fieldref:
  61. fieldpath: metadata.namespace
  62. - name: SERVICE_NAME
  63. objref:
  64. kind: Service
  65. version: v1
  66. name: webhook-service

Now you can deploy it to your cluster by

  1. make deploy

Wait a while til the webhook pod comes up and the certificates are provisioned.It usually completes within 1 minute.

Now you can create a valid CronJob to test your webhooks. The creation shouldsuccessfully go through.

  1. kubectl create -f config/samples/batch_v1_cronjob.yaml

You can also try to create an invalid CronJob (e.g. use an ill-formattedschedule field). You should see a creation failure with a validation error.

Note: If you are