Implement force sync via predicates

This commit is contained in:
stefanprodan 2020-04-06 15:41:57 +03:00
parent 9ad2be5b67
commit 3deb7caf9a
5 changed files with 56 additions and 9 deletions

View File

@ -50,16 +50,16 @@ spec:
description: GitRepositorySpec defines the desired state of GitRepository
properties:
branch:
description: The branch to checkout
description: The git branch to checkout, defaults to ('master').
type: string
interval:
description: The interval at which to check for repository updates
description: The interval at which to check for repository updates.
type: string
tag:
description: The tag to checkout
description: The git tag to checkout, takes precedence over branch.
type: string
url:
description: The repository address
description: The repository URL, can be a HTTP or SSH address.
pattern: ^(http|https|ssh)://
type: string
required:

View File

@ -2,6 +2,8 @@ apiVersion: sourcer.fluxcd.io/v1alpha1
kind: GitRepository
metadata:
name: podinfo
annotations:
sourcer.fluxcd.io/syncAt: "2020-04-06T15:39:52+03:00"
spec:
interval: 1m
url: https://github.com/stefanprodan/podinfo

View File

@ -19,6 +19,7 @@ package controllers
import (
"context"
"fmt"
"sigs.k8s.io/controller-runtime/pkg/event"
"time"
"github.com/go-git/go-git/v5"
@ -97,7 +98,13 @@ func (r *GitRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
func (r *GitRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&sourcerv1.GitRepository{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
WithEventFilter(predicate.Funcs{
DeleteFunc: func(e event.DeleteEvent) bool {
//TODO: cleanup
return false
},
}).
WithEventFilter(RepositoryChangePredicate{}).
Complete(r)
}
@ -116,6 +123,7 @@ func (r *GitRepositoryReconciler) sync(spec sourcerv1.GitRepositorySpec) (source
URL: spec.Url,
Depth: 2,
ReferenceName: refName,
SingleBranch: true,
})
if err != nil {
ex := fmt.Errorf("git clone error %w", err)

View File

@ -25,6 +25,7 @@ import (
"path"
"time"
sourcerv1 "github.com/fluxcd/sourcer/api/v1alpha1"
"github.com/go-logr/logr"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
@ -32,9 +33,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
sourcerv1 "github.com/fluxcd/sourcer/api/v1alpha1"
)
// HelmRepositoryReconciler reconciles a HelmRepository object
@ -107,7 +105,7 @@ func (r *HelmRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
func (r *HelmRepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&sourcerv1.HelmRepository{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
WithEventFilter(RepositoryChangePredicate{}).
Complete(r)
}

39
controllers/predicate.go Normal file
View File

@ -0,0 +1,39 @@
package controllers
import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
type RepositoryChangePredicate struct {
predicate.Funcs
}
// Update implements default UpdateEvent filter for validating repository change
func (RepositoryChangePredicate) Update(e event.UpdateEvent) bool {
if e.MetaOld == nil || e.MetaNew == nil {
// ignore objects without metadata
return false
}
if e.MetaNew.GetGeneration() != e.MetaOld.GetGeneration() {
// reconcile on spec changes
return true
}
// handle force sync
if val, ok := e.MetaNew.GetAnnotations()[ForceSyncAnnotation]; ok {
if valOld, okOld := e.MetaOld.GetAnnotations()[ForceSyncAnnotation]; okOld {
if val != valOld {
return true
}
} else {
return true
}
}
return false
}
const (
ForceSyncAnnotation string = "sourcer.fluxcd.io/syncAt"
)