Skip to content

Commit 2e38855

Browse files
authored
Merge pull request #80 from fluxcd/docs-webhook-receivers
Add webhook receivers guide to docs
2 parents c61bf76 + 97592a1 commit 2e38855

File tree

2 files changed

+141
-2
lines changed

2 files changed

+141
-2
lines changed

docs/guides/webhook-receivers.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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

mkdocs.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ nav:
4141
- Get Started: get-started/index.md
4242
- Guides:
4343
- Setup Notifications: guides/notifications.md
44+
- Setup Webhook Receivers: guides/webhook-receivers.md
4445
- Toolkit Components:
4546
- Source Controller:
4647
- Overview: components/source/controller.md
@@ -53,10 +54,10 @@ nav:
5354
- Kustomize API Reference: components/kustomize/api.md
5455
- Notification Controller:
5556
- Overview: components/notification/controller.md
57+
- Event: components/notification/event.md
5658
- Provider CRD: components/notification/provider.md
5759
- Alert CRD: components/notification/alert.md
58-
- Event: components/notification/event.md
59-
- Webhook Receiver: components/notification/receiver.md
60+
- Receiver CRD: components/notification/receiver.md
6061
- Notification API Reference: components/notification/api.md
6162
- Toolkit CLI:
6263
- Overview: cmd/tk.md

0 commit comments

Comments
 (0)