From 9aa11a1395214165ea09961eedea6696f36d0c17 Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Thu, 20 Dec 2018 13:53:06 +0000 Subject: [PATCH] Use checksum per resource In using a checksum for a whole stack (which at present is everything in the repo), it makes the syncing very sensitive to changes. Since it would mostly be just changing annotations, I don't think this is a problem for correctness, but it will keep Kubernetes busy, updating annotations every time anything changes. It's not expensive to calculate the checksum for each resource, as it goes past. We don't actually use it for change detection -- it's only consulted when it comes to garbage collect items, to check if it was updated earlier (and if not, a warning is issued). --- cluster/kubernetes/kubernetes.go | 4 +--- cluster/kubernetes/sync.go | 8 ++++++-- cluster/sync.go | 1 - sync/sync.go | 23 +---------------------- 4 files changed, 8 insertions(+), 28 deletions(-) diff --git a/cluster/kubernetes/kubernetes.go b/cluster/kubernetes/kubernetes.go index cb717777c1..ce1ad4dbda 100644 --- a/cluster/kubernetes/kubernetes.go +++ b/cluster/kubernetes/kubernetes.go @@ -14,11 +14,9 @@ import ( k8sclientdynamic "k8s.io/client-go/dynamic" k8sclient "k8s.io/client-go/kubernetes" - kresource "github.com/weaveworks/flux/cluster/kubernetes/resource" - fhrclient "github.com/weaveworks/flux/integrations/client/clientset/versioned" - "github.com/weaveworks/flux" "github.com/weaveworks/flux/cluster" + fhrclient "github.com/weaveworks/flux/integrations/client/clientset/versioned" "github.com/weaveworks/flux/ssh" ) diff --git a/cluster/kubernetes/sync.go b/cluster/kubernetes/sync.go index 8cb38e7999..baf7472b84 100644 --- a/cluster/kubernetes/sync.go +++ b/cluster/kubernetes/sync.go @@ -2,6 +2,8 @@ package kubernetes import ( "bytes" + "crypto/sha1" + "encoding/hex" "fmt" "io" "os/exec" @@ -59,14 +61,16 @@ func (c *Cluster) Sync(spec cluster.SyncDef) error { id := res.ResourceID().String() // make a record of the checksum, whether we stage it to // be applied or not, so that we don't delete it later. - checksums[id] = checksum{stack.Name, stack.Checksum} + csum := sha1.Sum(res.Bytes()) + checkHex := hex.EncodeToString(csum[:]) + checksums[id] = checksum{stack.Name, checkHex} if res.Policy().Has(policy.Ignore) { continue } if cres, ok := clusterResources[id]; ok && cres.Policy().Has(policy.Ignore) { continue } - resBytes, err := applyMetadata(res, stack.Name, stack.Checksum) + resBytes, err := applyMetadata(res, stack.Name, checkHex) if err == nil { cs.stage("apply", res.ResourceID(), res.Source(), resBytes) } else { diff --git a/cluster/sync.go b/cluster/sync.go index bd4057e8fb..6588bf0fee 100644 --- a/cluster/sync.go +++ b/cluster/sync.go @@ -18,7 +18,6 @@ import ( // it involves examining each resource individually). type SyncStack struct { Name string - Checksum string Resources []resource.Resource } diff --git a/sync/sync.go b/sync/sync.go index 7d6f8ac515..08313adf1d 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -1,10 +1,6 @@ package sync import ( - "crypto/sha1" - "encoding/hex" - "sort" - "github.com/go-kit/kit/log" "github.com/weaveworks/flux/cluster" @@ -34,31 +30,14 @@ func Sync(logger log.Logger, repoResources map[string]resource.Resource, clus Sy func makeStack(name string, repoResources map[string]resource.Resource, logger log.Logger) cluster.SyncStack { stack := cluster.SyncStack{Name: name} var resources []resource.Resource - - // To get a stable checksum, we have to sort the resources. - var ids []string - for id, _ := range repoResources { - ids = append(ids, id) - } - sort.Strings(ids) - - checksum := sha1.New() - for _, id := range ids { - res := repoResources[id] + for _, res := range repoResources { resources = append(resources, res) if res.Policy().Has(policy.Ignore) { logger.Log("resource", res.ResourceID(), "ignore", "apply") continue } - // Ignored resources are not included in the checksum; this - // means if you mark something as ignored, the checksum will - // come out differently. But the alternative is that adding - // ignored resources changes the checksum even though they are - // not intended to be created. - checksum.Write(res.Bytes()) } stack.Resources = resources - stack.Checksum = hex.EncodeToString(checksum.Sum(nil)) return stack }