From 3e84799644ced2b2e074de29fc7211c085b55a58 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Tue, 9 Jul 2019 08:52:31 +0300 Subject: [PATCH] Detect changes in pod template metadata Use the pod template spec hash to track changes (breaking) --- go.mod | 1 + pkg/canary/deployer.go | 21 ++++++--------------- pkg/canary/status.go | 15 +++++++-------- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 4755ac4f..7100be67 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/mattn/go-isatty v0.0.7 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-testing-interface v1.0.0 // indirect + github.com/mitchellh/hashstructure v1.0.0 github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829 github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect github.com/prometheus/common v0.3.0 // indirect diff --git a/pkg/canary/deployer.go b/pkg/canary/deployer.go index 98485d2a..aa83eab3 100644 --- a/pkg/canary/deployer.go +++ b/pkg/canary/deployer.go @@ -2,20 +2,18 @@ package canary import ( "crypto/rand" - "encoding/base64" - "encoding/json" "fmt" + "io" + "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" + "github.com/mitchellh/hashstructure" flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1alpha3" clientset "github.com/weaveworks/flagger/pkg/client/clientset/versioned" "go.uber.org/zap" - "io" appsv1 "k8s.io/api/apps/v1" hpav1 "k8s.io/api/autoscaling/v2beta1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" @@ -146,19 +144,12 @@ func (c *Deployer) HasDeploymentChanged(cd *flaggerv1.Canary) (bool, error) { return true, nil } - newSpec := &canary.Spec.Template.Spec - oldSpecJson, err := base64.StdEncoding.DecodeString(cd.Status.LastAppliedSpec) + newHash, err := hashstructure.Hash(canary.Spec.Template, nil) if err != nil { - return false, fmt.Errorf("%s.%s decode error %v", cd.Name, cd.Namespace, err) - } - oldSpec := &corev1.PodSpec{} - err = json.Unmarshal(oldSpecJson, oldSpec) - if err != nil { - return false, fmt.Errorf("%s.%s unmarshal error %v", cd.Name, cd.Namespace, err) + return false, fmt.Errorf("hash error %v", err) } - if diff := cmp.Diff(*newSpec, *oldSpec, cmpopts.IgnoreUnexported(resource.Quantity{})); diff != "" { - //fmt.Println(diff) + if cd.Status.LastAppliedSpec != fmt.Sprintf("%d", newHash) { return true, nil } diff --git a/pkg/canary/status.go b/pkg/canary/status.go index e613e570..7c2cb317 100644 --- a/pkg/canary/status.go +++ b/pkg/canary/status.go @@ -1,10 +1,9 @@ package canary import ( - "encoding/base64" - "encoding/json" "fmt" + "github.com/mitchellh/hashstructure" flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1alpha3" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -20,22 +19,22 @@ func (c *Deployer) SyncStatus(cd *flaggerv1.Canary, status flaggerv1.CanaryStatu return fmt.Errorf("deployment %s.%s query error %v", cd.Spec.TargetRef.Name, cd.Namespace, err) } - specJson, err := json.Marshal(dep.Spec.Template.Spec) - if err != nil { - return fmt.Errorf("deployment %s.%s marshal error %v", cd.Spec.TargetRef.Name, cd.Namespace, err) - } - configs, err := c.ConfigTracker.GetConfigRefs(cd) if err != nil { return fmt.Errorf("configs query error %v", err) } + hash, err := hashstructure.Hash(dep.Spec.Template, nil) + if err != nil { + return fmt.Errorf("hash error %v", err) + } + cdCopy := cd.DeepCopy() cdCopy.Status.Phase = status.Phase cdCopy.Status.CanaryWeight = status.CanaryWeight cdCopy.Status.FailedChecks = status.FailedChecks cdCopy.Status.Iterations = status.Iterations - cdCopy.Status.LastAppliedSpec = base64.StdEncoding.EncodeToString(specJson) + cdCopy.Status.LastAppliedSpec = fmt.Sprintf("%d", hash) cdCopy.Status.LastTransitionTime = metav1.Now() cdCopy.Status.TrackedConfigs = configs