Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #573 from amit-handda/sync-before-helm-reconcile
Browse files Browse the repository at this point in the history
Sync chart mirror on chart spec change to prevent incorrect reconciliation
  • Loading branch information
hiddeco authored Dec 10, 2020
2 parents 90f4946 + b88c03e commit 797fe26
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/helm-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func main() {
// _before_ starting it or else the cache sync seems to hang at
// random
opr := operator.New(log.With(logger, "component", "operator"),
*logReleaseDiffs, kubeClient, hrInformer, queue, rel)
*logReleaseDiffs, kubeClient, hrInformer, queue, rel, gitChartSync)
go ifInformerFactory.Start(shutdown)

// wait for the caches to be synced before starting _any_ workers
Expand Down
15 changes: 15 additions & 0 deletions pkg/chartsync/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ func (c *GitChartSync) Delete(hr *v1.HelmRelease) bool {
return ok
}

// SyncMirror instructs the helmrelease's git mirror to sync from its upstream
func (c *GitChartSync) SyncMirror(hr *v1.HelmRelease) error {
mirror := mirrorName(hr)
c.logger.Log("info", "starting sync of git mirror", "mirror", mirror)
repo, ok := c.mirrors.Get(mirror)
if !ok {
return ChartNotReadyError{ErrNoMirror}
}
ctx, cancel := context.WithTimeout(context.Background(), c.config.GitTimeout)
repo.Refresh(ctx)
cancel()
c.logger.Log("info", "finished syncing git mirror", "mirror", mirror)
return nil
}

// SyncMirrors instructs all git mirrors to sync from their respective
// upstreams.
func (c *GitChartSync) SyncMirrors() {
Expand Down
16 changes: 14 additions & 2 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package operator

import (
"fmt"
"github.com/fluxcd/helm-operator/pkg/chartsync"
"os"
"path"
"sync"
Expand Down Expand Up @@ -43,7 +44,8 @@ type Controller struct {
hrLister iflister.HelmReleaseLister
hrSynced cache.InformerSynced

release *release.Release
release *release.Release
gitChartSync *chartsync.GitChartSync

// workqueue is a rate limited work queue. This is used to queue work to be
// processed instead of performing it as soon as a change happens. This
Expand All @@ -64,7 +66,8 @@ func New(
kubeclientset kubernetes.Interface,
hrInformer hrv1.HelmReleaseInformer,
releaseWorkqueue workqueue.RateLimitingInterface,
release *release.Release) *Controller {
release *release.Release,
gitChartSync *chartsync.GitChartSync) *Controller {

// Add helm-operator types to the default Kubernetes Scheme so Events can be
// logged for helm-operator types.
Expand All @@ -81,6 +84,7 @@ func New(
releaseWorkqueue: releaseWorkqueue,
recorder: recorder,
release: release,
gitChartSync: gitChartSync,
}

controller.logger.Log("info", "setting up event handlers")
Expand Down Expand Up @@ -275,6 +279,14 @@ func (c *Controller) enqueueUpdateJob(old, new interface{}) {
return
}

// if there is a change in the chartsource (ref change, eg),
// its possible that the mirror's ref-sha be obsolete w.r.t. upstream
// repo's ref-sha. To avoid spurious deploy, mirror is synced before
// doing helm reconciliation
if csDiff := cmp.Diff(oldHr.Spec.ChartSource, newHr.Spec.ChartSource); csDiff != "" {
c.gitChartSync.SyncMirror(&newHr)
}

c.enqueueJob(new)
}

Expand Down

0 comments on commit 797fe26

Please sign in to comment.