|
| 1 | +# Setup Webhook Receivers |
| 2 | + |
| 3 | +The GitOps toolkit controllers are by design **pull-based**. |
| 4 | +In order to notify the controllers about changes in Git or Helm repositories, |
| 5 | +you can setup webhooks and trigger a cluster reconciliation |
| 6 | +every time a source changes. Using webhook receivers, you can build **push-based** |
| 7 | +GitOps pipelines that react to external events. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +To follow this guide you'll need a Kubernetes cluster with the GitOps |
| 12 | +toolkit controllers installed on it. |
| 13 | +Please see the [get started guide](../get-started/index.md) |
| 14 | +or the [install command docs](../cmd/tk_install.md). |
| 15 | + |
| 16 | +The [notification controller](../components/notification/controller.md) |
| 17 | +can handle events coming from external systems |
| 18 | +(GitHub, GitLab, Bitbucket, Harbour, Jenkins, etc) |
| 19 | +and notify the GitOps toolkit controllers about source changes. |
| 20 | +The notification controller is part of the default toolkit installation. |
| 21 | + |
| 22 | +## Expose the webhook receiver |
| 23 | + |
| 24 | +In order to receive Git push or Helm chart upload events, you'll have to |
| 25 | +expose the webhook receiver endpoint outside of your Kubernetes cluster on |
| 26 | +a public address. |
| 27 | + |
| 28 | +The notification controller handles webhook requests on port `9292`. |
| 29 | +This port can be used to create a Kubernetes LoadBalancer Service or Ingress. |
| 30 | + |
| 31 | +Create a `LoadBalancer` service: |
| 32 | + |
| 33 | +```yaml |
| 34 | +apiVersion: v1 |
| 35 | +kind: Service |
| 36 | +metadata: |
| 37 | + name: receiver |
| 38 | + namespace: gitops-system |
| 39 | +spec: |
| 40 | + type: LoadBalancer |
| 41 | + selector: |
| 42 | + app: notification-controller |
| 43 | + ports: |
| 44 | + - name: http |
| 45 | + port: 80 |
| 46 | + protocol: TCP |
| 47 | + targetPort: 9292 |
| 48 | +``` |
| 49 | +
|
| 50 | +Wait for Kubernetes to assign a public address with: |
| 51 | +
|
| 52 | +```sh |
| 53 | +watch kubectl -n gitops-system get svc/receiver |
| 54 | +``` |
| 55 | + |
| 56 | +## Define a Git repository |
| 57 | + |
| 58 | +Create a Git source pointing to a GitHub repository that you have control over: |
| 59 | + |
| 60 | +```yaml |
| 61 | +apiVersion: source.fluxcd.io/v1alpha1 |
| 62 | +kind: GitRepository |
| 63 | +metadata: |
| 64 | + name: webapp |
| 65 | + namespace: gitops-system |
| 66 | +spec: |
| 67 | + interval: 60m |
| 68 | + url: https://github.com/<GH-ORG>/<GH-REPO> |
| 69 | + ref: |
| 70 | + branch: master |
| 71 | +``` |
| 72 | +
|
| 73 | +!!! hint "Authentication" |
| 74 | + SSH or token based authentication can be configured for private repositories. |
| 75 | + See the [GitRepository CRD docs](../components/source/gitrepositories.md) for more details. |
| 76 | +
|
| 77 | +## Define a Git repository receiver |
| 78 | +
|
| 79 | +First generate a random string and create a secret with a `token` field: |
| 80 | + |
| 81 | +```sh |
| 82 | +TOKEN=$(head -c 12 /dev/urandom | shasum | cut -d ' ' -f1) |
| 83 | +echo $TOKEN |
| 84 | +
|
| 85 | +kubectl -n gitops-system create secret generic webhook-token \ |
| 86 | +--from-literal=token=$TOKEN |
| 87 | +``` |
| 88 | + |
| 89 | +Create a receiver for GitHub and specify the `GitRepository` object: |
| 90 | + |
| 91 | +```yaml |
| 92 | +apiVersion: notification.fluxcd.io/v1alpha1 |
| 93 | +kind: Receiver |
| 94 | +metadata: |
| 95 | + name: webapp |
| 96 | + namespace: gitops-system |
| 97 | +spec: |
| 98 | + type: github |
| 99 | + events: |
| 100 | + - "ping" |
| 101 | + - "push" |
| 102 | + secretRef: |
| 103 | + name: webhook-token |
| 104 | + resources: |
| 105 | + - kind: GitRepository |
| 106 | + name: webapp |
| 107 | +``` |
| 108 | + |
| 109 | +!!! hint "Note" |
| 110 | + Besides GitHub, you can define receivers for **GitLab**, **Bitbucket**, **Harbour** |
| 111 | + and any other system that supports webhooks e.g. Jenkins, CircleCI, etc. |
| 112 | + See the [Receiver CRD docs](../components/notification/receiver.md) for more details. |
| 113 | + |
| 114 | +The notification controller generates a unique URL using the provided token and the receiver name/namespace. |
| 115 | + |
| 116 | +Find the URL with: |
| 117 | + |
| 118 | +```console |
| 119 | +$ kubectl -n gitops-system get receiver/webapp |
| 120 | +
|
| 121 | +NAME READY STATUS |
| 122 | +webapp True Receiver initialised with URL: /hook/bed6d00b5555b1603e1f59b94d7fdbca58089cb5663633fb83f2815dc626d92b |
| 123 | +``` |
| 124 | + |
| 125 | +On GitHub, navigate to your repository and click on the "Add webhook" button under "Settings/Webhooks". |
| 126 | +Fill the form with: |
| 127 | + |
| 128 | +* **Payload URL**: compose the address using the receiver LB and the generated URL `http://<LoadBalancerAddress>/<ReceiverURL>` |
| 129 | +* **Secret**: use the `token` string |
| 130 | + |
| 131 | +With the above settings, when you push a commit to the repository, the following happens: |
| 132 | + |
| 133 | +* GitHub sends the Git push event to the receiver address |
| 134 | +* Notification controller validates the authenticity of the payload using HMAC |
| 135 | +* Source controller is notified about the changes |
| 136 | +* Source controller pulls the changes into the cluster and updates the `GitRepository` revision |
| 137 | +* Kustomize controller is notified about the revision change |
| 138 | +* Kustomize controller reconciles all the `Kustomizations` that reference the `GitRepository` object |
0 commit comments