@@ -6,168 +6,6 @@ Example consumer of the GitOps Toolkit Source APIs.
66
77![ ] ( https://raw.githubusercontent.com/fluxcd/toolkit/master/docs/_files/source-controller.png )
88
9- ## Watch Git repositories
9+ ## Guides
1010
11- The [ GitRepositoryWatcher] ( controllers/gitrepository_watcher.go ) controller does the following:
12-
13- * subscribes to ` GitRepository ` events
14- * detects when the Git revision changes
15- * downloads and extracts the source artifact
16- * write to stdout the extracted file names
17-
18- ``` go
19- // GitRepositoryWatcher watches GitRepository objects for revision changes
20- type GitRepositoryWatcher struct {
21- client.Client
22- Log logr.Logger
23- Scheme *runtime.Scheme
24- }
25-
26- // +kubebuilder:rbac:groups=source.fluxcd.io,resources=gitrepositories,verbs=get;list;watch
27- // +kubebuilder:rbac:groups=source.fluxcd.io,resources=gitrepositories/status,verbs=get
28-
29- func (r *GitRepositoryWatcher ) Reconcile (req ctrl .Request ) (ctrl .Result , error ) {
30- // set timeout for the reconciliation
31- ctx , cancel := context.WithTimeout (context.Background (), 15 *time.Second )
32- defer cancel ()
33-
34- // get source object
35- var repository sourcev1.GitRepository
36- if err := r.Get (ctx, req.NamespacedName , &repository); err != nil {
37- return ctrl.Result {}, client.IgnoreNotFound (err)
38- }
39-
40- log := r.Log .WithValues (strings.ToLower (repository.Kind ), req.NamespacedName )
41- log.Info (" New revision detected" , " revision" , repository.Status .Artifact .Revision )
42-
43- // create tmp dir
44- tmpDir , err := ioutil.TempDir (" " , repository.Name )
45- if err != nil {
46- return ctrl.Result {}, fmt.Errorf (" failed to create temp dir, error: % w" , err)
47- }
48- defer os.RemoveAll (tmpDir)
49-
50- // download and extract artifact
51- summary , err := r.fetchArtifact (ctx, repository, tmpDir)
52- if err != nil {
53- log.Error (err, " unable to fetch artifact" )
54- return ctrl.Result {}, err
55- }
56- log.Info (summary)
57-
58- // list artifact content
59- files , err := ioutil.ReadDir (tmpDir)
60- if err != nil {
61- return ctrl.Result {}, fmt.Errorf (" faild to list files, error: % w" , err)
62- }
63-
64- // do something with the artifact content
65- for _ , f := range files {
66- log.Info (" Processing " + f.Name ())
67- }
68-
69- return ctrl.Result {}, nil
70- }
71-
72- func (r *GitRepositoryWatcher ) SetupWithManager (mgr ctrl .Manager ) error {
73- return ctrl.NewControllerManagedBy (mgr).
74- For (&sourcev1.GitRepository {}).
75- WithEventFilter (GitRepositoryRevisionChangePredicate{}).
76- Complete (r)
77- }
78- ```
79-
80- Source code:
81-
82- * controller [ gitrepository_watcher.go] ( controllers/gitrepository_watcher.go )
83- * predicate [ gitrepository_predicate.go] ( controllers/gitrepository_predicate.go )
84- * initialisation [ main.go] ( main.go )
85-
86- ### Prerequisites
87-
88- * go >= 1.13
89- * kubebuilder >= 2.3
90- * kind >= 0.8
91-
92- Clone source watcher repo:
93-
94- ``` sh
95- git clone https://github.com/stefanprodan/source-watcher
96- cd source-watcher
97- ```
98-
99- Build the controller:
100-
101- ``` sh
102- make
103- ```
104-
105- ### Install the GitOps toolkit
106-
107- Create a cluster for testing:
108-
109- ``` sh
110- kind create cluster --name testing
111- ```
112-
113- Install the toolkit CLI:
114-
115- ``` sh
116- curl -s https://toolkit.fluxcd.io/install.sh | sudo bash
117- ```
118-
119- Install the toolkit controllers:
120-
121- ``` sh
122- tk install
123- ```
124-
125- ### Run the controller
126-
127- Port forward to source controller artifacts server:
128-
129- ``` sh
130- kubectl -n gitops-system port-forward svc/source-controller 8080:80
131- ```
132-
133- Export the local address as ` SOURCE_HOST ` :
134-
135- ``` sh
136- export SOURCE_HOST=localhost:8080
137- ```
138-
139- Run the controller:
140-
141- ``` sh
142- make run
143- ```
144-
145- Create a Git source:
146-
147- ``` sh
148- tk create source git test \
149- --url=https://github.com/stefanprodan/podinfo \
150- --tag=4.0.0
151- ```
152-
153- The source watcher should log the revision:
154-
155- ``` console
156- New revision detected {"gitrepository": "gitops-system/test", "revision": "4.0.0/ab953493ee14c3c9800bda0251e0c507f9741408"}
157- Extracted tarball into /var/folders/77/3y6x_p2j2g9fspdkzjbm5_s40000gn/T/test292235827: 123 files, 29 dirs (32.603415ms)
158- Processing files...
159- ```
160-
161- Change the git tag:
162-
163- ``` sh
164- tk create source git test \
165- --url=https://github.com/stefanprodan/podinfo \
166- --tag=4.0.1
167- ```
168-
169- The source watcher should log the new revision:
170-
171- ``` console
172- New revision detected {"gitrepository": "gitops-system/test", "revision": "4.0.1/113360052b3153e439a0cf8de76b8e3d2a7bdf27"}
173- ```
11+ * [ Watching for source changes] ( https://toolkit.fluxcd.io/dev-guides/source-watcher/ )
0 commit comments