|
1 | 1 | # source-watcher |
2 | | -Example consumer of the GitOps Toolkit Source APIs |
| 2 | + |
| 3 | +[](https://github.com/stefanprodan/source-watcher/actions) |
| 4 | + |
| 5 | +Example consumer of the GitOps Toolkit Source APIs. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Watch Git repositories |
| 10 | + |
| 11 | +Source code: |
| 12 | + |
| 13 | +* controller [gitrepository_watcher.go](controllers/gitrepository_watcher.go) |
| 14 | +* predicate [gitrepository_predicate.go](controllers/gitrepository_predicate.go) |
| 15 | + |
| 16 | +The `GitRepositoryWatcher` controller does the following: |
| 17 | + |
| 18 | +* subscribes to `GitRepository` events |
| 19 | +* detects when the Git revision changes |
| 20 | +* downloads and extracts the source artifact |
| 21 | +* write to stdout the extracted file names |
| 22 | + |
| 23 | +### Prerequisites |
| 24 | + |
| 25 | +* go >= 1.13 |
| 26 | +* kubebuilder >= 2.3 |
| 27 | +* kind >= 0.8 |
| 28 | + |
| 29 | +Clone source watcher repo: |
| 30 | + |
| 31 | +```sh |
| 32 | +git clone https://github.com/stefanprodan/source-watcher |
| 33 | +cd source-watcher |
| 34 | +``` |
| 35 | + |
| 36 | +Build the controller: |
| 37 | + |
| 38 | +```sh |
| 39 | +make |
| 40 | +``` |
| 41 | + |
| 42 | +### Install the GitOps toolkit |
| 43 | + |
| 44 | +Create a cluster for testing: |
| 45 | + |
| 46 | +```sh |
| 47 | +kind create cluster --name testing |
| 48 | +``` |
| 49 | + |
| 50 | +Install the toolkit CLI: |
| 51 | + |
| 52 | +```sh |
| 53 | +curl -s https://toolkit.fluxcd.io/install.sh | sudo bash |
| 54 | +``` |
| 55 | + |
| 56 | +Install the toolkit controllers: |
| 57 | + |
| 58 | +```sh |
| 59 | +tk install |
| 60 | +``` |
| 61 | + |
| 62 | +### Run the controller |
| 63 | + |
| 64 | +Port forward to source controller artifacts server: |
| 65 | + |
| 66 | +```sh |
| 67 | +kubectl -n gitops-system port-forward svc/source-controller 8080:80 |
| 68 | +``` |
| 69 | + |
| 70 | +Export the local address as `SOURCE_HOST`: |
| 71 | + |
| 72 | +```sh |
| 73 | +export SOURCE_HOST=localhost:8080 |
| 74 | +``` |
| 75 | + |
| 76 | +Run the controller: |
| 77 | + |
| 78 | +```sh |
| 79 | +make run |
| 80 | +``` |
| 81 | + |
| 82 | +Create a Git source: |
| 83 | + |
| 84 | +```sh |
| 85 | +tk create source git test \ |
| 86 | +--url=https://github.com/stefanprodan/podinfo \ |
| 87 | +--tag=4.0.0 |
| 88 | +``` |
| 89 | + |
| 90 | +The source watcher should log the revision: |
| 91 | + |
| 92 | +```console |
| 93 | +New revision detected {"gitrepository": "gitops-system/test", "revision": "4.0.0/ab953493ee14c3c9800bda0251e0c507f9741408"} |
| 94 | +Extracted tarball into /var/folders/77/3y6x_p2j2g9fspdkzjbm5_s40000gn/T/test292235827: 123 files, 29 dirs (32.603415ms) |
| 95 | +Processing files... |
| 96 | +``` |
| 97 | + |
| 98 | +Change the git tag: |
| 99 | + |
| 100 | +```sh |
| 101 | +tk create source git test \ |
| 102 | +--url=https://github.com/stefanprodan/podinfo \ |
| 103 | +--tag=4.0.1 |
| 104 | +``` |
| 105 | + |
| 106 | +The source watcher should log the new revision: |
| 107 | + |
| 108 | +```console |
| 109 | +New revision detected {"gitrepository": "gitops-system/test", "revision": "4.0.1/113360052b3153e439a0cf8de76b8e3d2a7bdf27"} |
| 110 | +``` |
0 commit comments