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

Commit

Permalink
Use checksum per resource
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
squaremo committed Dec 20, 2018
1 parent 6735b48 commit 1d629b0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 28 deletions.
4 changes: 1 addition & 3 deletions cluster/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
8 changes: 6 additions & 2 deletions cluster/kubernetes/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kubernetes

import (
"bytes"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"os/exec"
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion cluster/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
// it involves examining each resource individually).
type SyncStack struct {
Name string
Checksum string
Resources []resource.Resource
}

Expand Down
23 changes: 1 addition & 22 deletions sync/sync.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package sync

import (
"crypto/sha1"
"encoding/hex"
"sort"

"github.com/go-kit/kit/log"

"github.com/weaveworks/flux/cluster"
Expand Down Expand Up @@ -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
}

0 comments on commit 1d629b0

Please sign in to comment.