From 5f1b81a8755163a27b23497795f4d15820629327 Mon Sep 17 00:00:00 2001 From: Alessandro Zanatta Date: Fri, 10 Jan 2025 09:33:00 +0100 Subject: [PATCH] Reading from filesystem only once when updating a Kustomization on Git. Changed variable names to clarify usage. Signed-off-by: Alessandro Zanatta --- pkg/argocd/git.go | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/pkg/argocd/git.go b/pkg/argocd/git.go index e4844bbc..d4f9d975 100644 --- a/pkg/argocd/git.go +++ b/pkg/argocd/git.go @@ -350,72 +350,74 @@ func writeKustomization(app *v1alpha1.Application, wbc *WriteBackConfig, gitC gi // to the file. This is the same behavior as kyaml.UpdateFile, but it preserves the original order of YAML fields // and indentation of YAML sequences to minimize git diffs. func updateKustomizeFile(filter kyaml.Filter, path string) (error, bool) { - // Read the yaml - y, err := kyaml.ReadFile(path) + // Open the input file for read + yRaw, err := os.ReadFile(path) if err != nil { return err, false } - originalData, err := y.String() + // Read the yaml document from bytes + originalYSlice, err := kio.FromBytes(yRaw) if err != nil { return err, false } - // Open the input file for read - in, err := os.Open(path) + // Check that we are dealing with a single document + if len(originalYSlice) != 1 { + return errors.New("target parameter file should contain a single YAML document"), false + } + originalY := originalYSlice[0] + + // Get the (parsed) original document + originalData, err := originalY.String() if err != nil { return err, false } - defer in.Close() // Create a reader, preserving indentation of sequences var out bytes.Buffer rw := &kio.ByteReadWriter{ - Reader: in, + Reader: bytes.NewBuffer(yRaw), Writer: &out, PreserveSeqIndent: true, } // Read input file - yCpySlice, err := rw.Read() + newYSlice, err := rw.Read() if err != nil { return err, false } - - // We expect a single yaml - if len(yCpySlice) != 1 { - return errors.New("target parameter file should contain a single YAML document"), false - } - yCpy := yCpySlice[0] + // We can safely assume we have a single document from the previous check + newY := newYSlice[0] // Update the yaml - if err := yCpy.PipeE(filter); err != nil { + if err := newY.PipeE(filter); err != nil { return err, false } // Preserve the original order of fields - if err := order.SyncOrder(y, yCpy); err != nil { + if err := order.SyncOrder(originalY, newY); err != nil { return err, false } // Write the yaml document to the output buffer - if err = rw.Write([]*kyaml.RNode{yCpy}); err != nil { + if err = rw.Write([]*kyaml.RNode{newY}); err != nil { return err, false } // yCpy contains metadata used by kio to preserve sequence indentation, // hence we need to parse the output buffer instead - parsed, err := kyaml.Parse(out.String()) + newParsedY, err := kyaml.Parse(out.String()) if err != nil { return err, false } - override, err := parsed.String() + newData, err := newParsedY.String() if err != nil { return err, false } // Diff the updated document with the original - if originalData == override { + if originalData == newData { log.Debugf("target parameter file and marshaled data are the same, skipping commit.") return nil, true }