Skip to content

Commit

Permalink
Merge pull request #13 from fluxcd/controller-runtime-v0.7.0
Browse files Browse the repository at this point in the history
Update controller-runtime to v0.7.0
  • Loading branch information
stefanprodan authored Jan 15, 2021
2 parents 325d1f9 + a94ce62 commit e6957d9
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 103 deletions.
22 changes: 7 additions & 15 deletions controllers/gitrepository_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,34 @@ import (
)

// GitRepositoryRevisionChangePredicate triggers an update event
// when a Git source revision changes
// when a GitRepository revision changes.
type GitRepositoryRevisionChangePredicate struct {
predicate.Funcs
}

func (GitRepositoryRevisionChangePredicate) Update(e event.UpdateEvent) bool {
if e.MetaOld == nil || e.MetaNew == nil {
if e.ObjectOld == nil || e.ObjectNew == nil {
return false
}

oldRepo, ok := e.ObjectOld.(*sourcev1.GitRepository)
oldSource, ok := e.ObjectOld.(sourcev1.Source)
if !ok {
return false
}

newRepo, ok := e.ObjectNew.(*sourcev1.GitRepository)
newSource, ok := e.ObjectNew.(sourcev1.Source)
if !ok {
return false
}

if oldRepo.GetArtifact() == nil && newRepo.GetArtifact() != nil {
if oldSource.GetArtifact() == nil && newSource.GetArtifact() != nil {
return true
}

if oldRepo.GetArtifact() != nil && newRepo.GetArtifact() != nil &&
oldRepo.GetArtifact().Revision != newRepo.GetArtifact().Revision {
if oldSource.GetArtifact() != nil && newSource.GetArtifact() != nil &&
oldSource.GetArtifact().Revision != newSource.GetArtifact().Revision {
return true
}

return false
}

func (GitRepositoryRevisionChangePredicate) Create(e event.CreateEvent) bool {
return false
}

func (GitRepositoryRevisionChangePredicate) Delete(e event.DeleteEvent) bool {
return false
}
15 changes: 5 additions & 10 deletions controllers/gitrepository_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import (
"io/ioutil"
"net/http"
"os"
"strings"
"time"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/fluxcd/pkg/untar"
Expand All @@ -44,18 +43,15 @@ type GitRepositoryWatcher struct {
// +kubebuilder:rbac:groups=source.fluxcd.io,resources=gitrepositories,verbs=get;list;watch
// +kubebuilder:rbac:groups=source.fluxcd.io,resources=gitrepositories/status,verbs=get

func (r *GitRepositoryWatcher) Reconcile(req ctrl.Request) (ctrl.Result, error) {
// set timeout for the reconciliation
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
func (r *GitRepositoryWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := logr.FromContext(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 := r.Log.WithValues(strings.ToLower(repository.Kind), req.NamespacedName)
log.Info("New revision detected", "revision", repository.Status.Artifact.Revision)

// create tmp dir
Expand Down Expand Up @@ -89,8 +85,7 @@ func (r *GitRepositoryWatcher) Reconcile(req ctrl.Request) (ctrl.Result, error)

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

Expand All @@ -102,7 +97,7 @@ func (r *GitRepositoryWatcher) fetchArtifact(ctx context.Context, repository sou
url := repository.Status.Artifact.URL

// for local run:
// kubectl -n gitops-system port-forward svc/source-controller 8080:80
// kubectl -n flux-system port-forward svc/source-controller 8080:80
// export SOURCE_HOST=localhost:8080
if hostname := os.Getenv("SOURCE_HOST"); hostname != "" {
url = fmt.Sprintf("http://%s/gitrepository/%s/%s/latest.tar.gz", hostname, repository.Namespace, repository.Name)
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/fluxcd/source-watcher
go 1.15

require (
github.com/fluxcd/pkg/runtime v0.4.0
github.com/fluxcd/pkg/runtime v0.6.2
github.com/fluxcd/pkg/untar v0.0.5
github.com/fluxcd/source-controller/api v0.5.6
github.com/go-logr/logr v0.2.1
github.com/fluxcd/source-controller/api v0.6.1
github.com/go-logr/logr v0.3.0
github.com/spf13/pflag v1.0.5
k8s.io/apimachinery v0.19.4
k8s.io/client-go v0.19.4
sigs.k8s.io/controller-runtime v0.6.4
sigs.k8s.io/controller-runtime v0.7.0
)
Loading

0 comments on commit e6957d9

Please sign in to comment.