Skip to content

Commit 6712918

Browse files
committed
Add watcher code snippet to docs
1 parent 87abddc commit 6712918

File tree

1 file changed

+69
-6
lines changed

1 file changed

+69
-6
lines changed

README.md

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,81 @@ Example consumer of the GitOps Toolkit Source APIs.
88

99
## Watch Git repositories
1010

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:
11+
The [GitRepositoryWatcher](controllers/gitrepository_watcher.go) controller does the following:
1712

1813
* subscribes to `GitRepository` events
1914
* detects when the Git revision changes
2015
* downloads and extracts the source artifact
2116
* write to stdout the extracted file names
2217

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+
2386
### Prerequisites
2487

2588
* go >= 1.13

0 commit comments

Comments
 (0)