Skip to content

Commit

Permalink
Bootstrap admission webhook for bindings.rabbitmq.com
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Mar 17, 2021
1 parent ee547a3 commit b39c02d
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 20 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,9 @@ generate-manifests:
mkdir -p releases
kustomize build config/installation/ > releases/messaging-topology-operator.yaml

CERT_MANAGER_VERSION ?=v1.2.0
cert-manager:
kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/$(CERT_MANAGER_VERSION)/cert-manager.yaml

destroy-cert-manager:
kubectl delete -f https://github.com/jetstack/cert-manager/releases/download/$(CERT_MANAGER_VERSION)/cert-manager.yaml
41 changes: 41 additions & 0 deletions api/v1alpha1/binding_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

var logger = logf.Log.WithName("binding-webhook")

func (r *Binding) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-rabbitmq-com-v1alpha1-binding,mutating=false,failurePolicy=fail,groups=rabbitmq.com,resources=bindings,versions=v1alpha1,name=vbinding.kb.io,sideEffects=none,admissionReviewVersions=v1

var _ webhook.Validator = &Binding{}

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

return nil
}

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

return nil
}

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

return nil
}
8 changes: 2 additions & 6 deletions config/certmanager/certificate.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# The following manifests contain a self-signed issuer CR and a certificate CR.
# More document can be found at https://docs.cert-manager.io
# WARNING: Targets CertManager 0.11 check https://docs.cert-manager.io/en/latest/tasks/upgrading/index.html for
# breaking changes
apiVersion: cert-manager.io/v1alpha2
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: selfsigned-issuer
namespace: system
spec:
selfSigned: {}
---
apiVersion: cert-manager.io/v1alpha2
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
Expand Down
7 changes: 7 additions & 0 deletions config/crd/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ resources:
- bases/rabbitmq.com_policies.yaml
# +kubebuilder:scaffold:crdkustomizeresource

patchesStrategicMerge:
- patches/webhook_in_bindings.yaml
# +kubebuilder:scaffold:crdkustomizewebhookpatch

- patches/cainjection_in_bindings.yaml
# +kubebuilder:scaffold:crdkustomizecainjectionpatch

configurations:
- kustomizeconfig.yaml
2 changes: 1 addition & 1 deletion config/crd/patches/cainjection_in_bindings.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The following patch adds a directive for certmanager to inject CA into the CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1alpha1
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
Expand Down
18 changes: 9 additions & 9 deletions config/crd/patches/webhook_in_bindings.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# The following patch enables conversion webhook for CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1alpha1
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: bindings.rabbitmq.com
spec:
conversion:
strategy: Webhook
webhookClientConfig:
# this is "\n" used as a placeholder, otherwise it will be rejected by the apiserver for being blank,
# but we're going to set it later using the cert-manager (or potentially a patch if not using cert-manager)
caBundle: Cg==
service:
namespace: system
name: webhook-service
path: /convert
webhook:
conversionReviewVersions: ["v1", "v1beta1"]
clientConfig:
caBundle: Cg==
service:
namespace: system
name: webhook-service
path: /convert
34 changes: 34 additions & 0 deletions config/default/base/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,40 @@ namespace: rabbitmq-system
resources:
- ../../crd
- ../../manager
- ../../webhook
- ../../certmanager

patches:
- manager_webhook_patch.yaml
- webhookcainjection_patch.yaml

vars:
- 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

images:
- name: controller
Expand Down
23 changes: 23 additions & 0 deletions config/default/base/manager_webhook_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: operator
namespace: system
spec:
template:
spec:
containers:
- name: manager
ports:
- containerPort: 9443
name: webhook-server
protocol: TCP
volumeMounts:
- mountPath: /tmp/k8s-webhook-server/serving-certs
name: cert
readOnly: true
volumes:
- name: cert
secret:
defaultMode: 420
secretName: webhook-server-cert
7 changes: 7 additions & 0 deletions config/default/base/webhookcainjection_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: validating-webhook-configuration
annotations:
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
2 changes: 0 additions & 2 deletions config/webhook/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# the following config is for teaching kustomize where to look at when substituting vars.
# It requires kustomize v2.1.0 or newer to work properly.
nameReference:
- kind: Service
version: v1
Expand Down
28 changes: 28 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
creationTimestamp: null
name: validating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-rabbitmq-com-v1alpha1-binding
failurePolicy: Fail
name: vbinding.kb.io
rules:
- apiGroups:
- rabbitmq.com
apiVersions:
- v1alpha1
operations:
- CREATE
- UPDATE
resources:
- bindings
sideEffects: None
3 changes: 1 addition & 2 deletions config/webhook/service.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

apiVersion: v1
kind: Service
metadata:
Expand All @@ -9,4 +8,4 @@ spec:
- port: 443
targetPort: 9443
selector:
control-plane: controller-manager
app.kubernetes.io/name: messaging-topology-operator
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"

rabbitmqcomv1alpha1 "github.com/rabbitmq/messaging-topology-operator/api/v1alpha1"
topologyv1alpha1 "github.com/rabbitmq/messaging-topology-operator/api/v1alpha1"
"github.com/rabbitmq/messaging-topology-operator/controllers"
// +kubebuilder:scaffold:imports
Expand All @@ -43,6 +44,7 @@ func init() {
_ = rabbitmqv1beta1.AddToScheme(scheme)

_ = topologyv1alpha1.AddToScheme(scheme)
_ = rabbitmqcomv1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}

Expand Down Expand Up @@ -118,6 +120,10 @@ func main() {
log.Error(err, "unable to create controller", "controller", policyControllerName)
os.Exit(1)
}
if err = (&rabbitmqcomv1alpha1.Binding{}).SetupWebhookWithManager(mgr); err != nil {
log.Error(err, "unable to create webhook", "webhook", "Binding")
os.Exit(1)
}
// +kubebuilder:scaffold:builder

log.Info("starting manager")
Expand Down

0 comments on commit b39c02d

Please sign in to comment.