Watching for source changes

Develop a Kubernetes controller that reacts to source changes.

In this guide you’ll be developing a Kubernetes controller with Kubebuilder that subscribes to GitRepository events and reacts to revision changes by downloading the artifact produced by source-controller.


On your dev machine install the following tools:

  • go >= 1.18
  • kubebuilder >= 3.0
  • kind >= 0.8
  • kubectl >= 1.21

Install Flux

Create a cluster for testing:

kind create cluster --name dev

Install the Flux CLI:

curl -s | sudo bash

Verify that your dev machine satisfies the prerequisites with:

flux check --pre

Install source-controller on the dev cluster:

flux install \
--namespace=flux-system \
--network-policy=false \

Clone the sample controller

You’ll be using fluxcd/source-watcher as a template for developing your own controller. The source-watcher was scaffolded with kubebuilder init.

Clone the source-watcher repository:

git clone
cd source-watcher

Build the controller:


Run the controller

Port forward to source-controller artifacts server:

kubectl -n flux-system port-forward svc/source-controller 8181:80

Export the local address as SOURCE_HOST:

export SOURCE_HOST=localhost:8181

Run source-watcher locally:

make run

Create a Git source:

flux create source git test \
--url= \

The source-watcher should log the revision:

New revision detected   {"gitrepository": "flux-system/test", "revision": "4.0.0/ab953493ee14c3c9800bda0251e0c507f9741408"}
Extracted tarball into /var/folders/77/3y6x_p2j2g9fspdkzjbm5_s40000gn/T/test292235827: 123 files, 29 dirs (32.603415ms)
Processing files...

Change the Git tag:

flux create source git test \
--url= \

The source-watcher should log the new revision:

New revision detected   {"gitrepository": "flux-system/test", "revision": "4.0.1/113360052b3153e439a0cf8de76b8e3d2a7bdf27"}

The source-controller reports the revision under GitRepository.Status.Artifact.Revision in the format: <branch|tag>/<commit>.

How it works

The GitRepositoryWatcher controller does the following:

  • subscribes to GitRepository events
  • detects when the Git revision changes
  • downloads and extracts the source artifact
  • write to stdout the extracted file names
// GitRepositoryWatcher watches GitRepository objects for revision changes
type GitRepositoryWatcher struct {
	Scheme *runtime.Scheme


func (r *GitRepositoryWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
	log := ctrl.LoggerFrom(ctx)

	// get source object
	var repository sourcev1.GitRepository
	if err := r.Get(ctx, req.NamespacedName, &repository); err != nil {
		return ctrl.Result{}, client.IgnoreNotFound(err)

	log.Info("New revision detected", "revision", repository.Status.Artifact.Revision)

	// create tmp dir
	tmpDir, err := os.MkdirTemp("", repository.Name)
	if err != nil {
		return ctrl.Result{}, fmt.Errorf("failed to create temp dir, error: %w", err)
	defer os.RemoveAll(tmpDir)

	// download and extract artifact
	summary, err := r.fetchArtifact(ctx, repository, tmpDir)
	if err != nil {
		log.Error(err, "unable to fetch artifact")
		return ctrl.Result{}, err

	// list artifact content
	files, err := os.ReadDir(tmpDir)
	if err != nil {
		return ctrl.Result{}, fmt.Errorf("failed to list files, error: %w", err)

	// do something with the artifact content
	for _, f := range files {
		log.Info("Processing " + f.Name())

	return ctrl.Result{}, nil

func (r *GitRepositoryWatcher) SetupWithManager(mgr ctrl.Manager) error {
	return ctrl.NewControllerManagedBy(mgr).
		For(&sourcev1.GitRepository{}, builder.WithPredicates(GitRepositoryRevisionChangePredicate{})).

To add the watcher to an existing project, copy the controller and the revision change predicate to your controllers dir:

In your main.go init function, register the Source API schema:

import (
	utilruntime ""
	clientgoscheme ""

	sourcev1 ""

func init() {

	// +kubebuilder:scaffold:scheme

Start the controller in the main function:

func main()  {

	if err = (&controllers.GitRepositoryWatcher{
		Client: mgr.GetClient(),
		Scheme: mgr.GetScheme(),
	}).SetupWithManager(mgr); err != nil {
		setupLog.Error(err, "unable to create controller", "controller", "GitRepositoryWatcher")


Note that the watcher controller depends on Kubernetes client-go >= 1.21. Your go.mod should require controller-runtime v0.9 or newer:

require ( v0.25.0 v0.25.0 v0.12.0

That’s it! Happy hacking!