From ec39ec2880655baa07c5234f05b0cce7fcf11c3e Mon Sep 17 00:00:00 2001 From: Brad Davidson Date: Wed, 9 Feb 2022 14:51:58 -0800 Subject: [PATCH] Don't rewrite manifests when values don't change Signed-off-by: Brad Davidson --- pkg/bootstrap/bootstrap.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/pkg/bootstrap/bootstrap.go b/pkg/bootstrap/bootstrap.go index d3dcd21a163..69f1b299a44 100644 --- a/pkg/bootstrap/bootstrap.go +++ b/pkg/bootstrap/bootstrap.go @@ -1,9 +1,11 @@ package bootstrap import ( + "bytes" "crypto/sha256" "encoding/hex" "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -295,13 +297,13 @@ func setChartValues(manifestsDir string, nodeConfig *daemonconfig.Node, cfg cmds // If the file cannot be decoded as a HelmChart, it is silently skipped. Any other IO error is considered // a failure. func rewriteChart(fileName string, info os.FileInfo, chartValues map[string]string, serializer *json.Serializer) error { - bytes, err := ioutil.ReadFile(fileName) + b, err := ioutil.ReadFile(fileName) if err != nil { return errors.Wrapf(err, "Failed to read manifest %s", fileName) } // Ignore manifest if it cannot be decoded - obj, _, err := serializer.Decode(bytes, nil, nil) + obj, _, err := serializer.Decode(b, nil, nil) if err != nil { logrus.Debugf("Failed to decode manifest %s: %s", fileName, err) return nil @@ -321,21 +323,37 @@ func rewriteChart(fileName string, info os.FileInfo, chartValues map[string]stri chart.Spec.Set = map[string]intstr.IntOrString{} } + changed := false for k, v := range chartValues { - chart.Spec.Set[k] = intstr.FromString(v) + val := intstr.FromString(v) + if cur, ok := chart.Spec.Set[k]; ok { + curBytes, _ := cur.MarshalJSON() + newBytes, _ := val.MarshalJSON() + if bytes.Equal(curBytes, newBytes) { + continue + } + } + changed = true + chart.Spec.Set[k] = val } - f, err := os.OpenFile(fileName, os.O_RDWR|os.O_TRUNC, info.Mode()) - if err != nil { - return errors.Wrapf(err, "Unable to open HelmChart %s", fileName) + if !changed { + logrus.Infof("No cluster configuration value changes necessary for HelmChart %s", fileName) + return nil } - if err := serializer.Encode(chart, f); err != nil { - _ = f.Close() + var buf bytes.Buffer + if err := serializer.Encode(chart, &buf); err != nil { return errors.Wrapf(err, "Failed to serialize modified HelmChart %s", fileName) } - if err := f.Close(); err != nil { + f, err := os.OpenFile(fileName, os.O_RDWR|os.O_TRUNC, info.Mode()) + if err != nil { + return errors.Wrapf(err, "Unable to open HelmChart %s", fileName) + } + defer f.Close() + + if _, err := io.Copy(f, &buf); err != nil { return errors.Wrapf(err, "Failed to write modified HelmChart %s", fileName) }