Skip to content

Commit

Permalink
Only write applied plan contents if the plan actually changes (#75)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Kim <[email protected]>
  • Loading branch information
Oats87 authored Mar 3, 2022
1 parent b5710ef commit ad6e3be
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions pkg/applyinator/applyinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -148,21 +149,13 @@ func (a *Applyinator) Apply(ctx context.Context, input ApplyInput) (ApplyOutput,
now := time.Now()
nowUnixTimeString := now.Format(time.UnixDate)
nowString := now.Format(applyinatorDateCodeLayout)
appliedPlanFile := nowString + appliedPlanFileSuffix
executionDir := filepath.Join(a.workDir, nowString)
logrus.Tracef("[Applyinator] Applying calculated node plan contents %v", input.CalculatedPlan.Checksum)
logrus.Tracef("[Applyinator] Using %s as execution directory", executionDir)
if a.appliedPlanDir != "" {
logrus.Debugf("[Applyinator] Writing applied calculated plan contents to historical plan directory %s", a.appliedPlanDir)
if err := os.MkdirAll(a.appliedPlanDir, 0700); err != nil {
return output, err
}
anpString, err := json.Marshal(input.CalculatedPlan)
if err != nil {
return output, err
}
if err := writeContentToFile(filepath.Join(a.appliedPlanDir, appliedPlanFile), os.Getuid(), os.Getgid(), 0600, anpString); err != nil {
return output, err
if err := a.writePlanToDisk(now, &input.CalculatedPlan); err != nil {
logrus.Errorf("error writing applied plan to disk: %v", err)
}
if err := a.appliedPlanRetentionPolicy(planRetentionPolicyCount); err != nil {
logrus.Errorf("error while applying plan retention policy: %v", err)
Expand Down Expand Up @@ -339,18 +332,11 @@ func generateByteBufferFromBytes(input []byte) (*bytes.Buffer, error) {
}

func (a *Applyinator) appliedPlanRetentionPolicy(retention int) error {
var planFiles []os.DirEntry
dirListedPlanFiles, err := os.ReadDir(a.appliedPlanDir)
planFiles, err := a.getAppliedPlanFiles()
if err != nil {
return err
}

for _, f := range dirListedPlanFiles {
if strings.HasSuffix(f.Name(), appliedPlanFileSuffix) && !f.IsDir() {
planFiles = append(planFiles, f)
}
}

if len(planFiles) <= retention {
return nil
}
Expand All @@ -370,6 +356,53 @@ func (a *Applyinator) appliedPlanRetentionPolicy(retention int) error {
return nil
}

func (a *Applyinator) getAppliedPlanFiles() ([]os.DirEntry, error) {
var planFiles []os.DirEntry
dirListedPlanFiles, err := os.ReadDir(a.appliedPlanDir)
if err != nil {
return nil, err
}

for _, f := range dirListedPlanFiles {
if strings.HasSuffix(f.Name(), appliedPlanFileSuffix) && !f.IsDir() {
planFiles = append(planFiles, f)
}
}
return planFiles, nil
}

func (a *Applyinator) writePlanToDisk(now time.Time, plan *CalculatedPlan) error {
planFiles, err := a.getAppliedPlanFiles()
if err != nil {
return err
}

file := now.Format(applyinatorDateCodeLayout) + appliedPlanFileSuffix
if err := os.MkdirAll(a.appliedPlanDir, 0700); err != nil {
return err
}
anpString, err := json.Marshal(plan)
if err != nil {
return err
}

if len(planFiles) != 0 {
sort.Slice(planFiles, func(i, j int) bool {
return planFiles[i].Name() > planFiles[j].Name()
})
existingFileContent, err := ioutil.ReadFile(filepath.Join(a.appliedPlanDir, planFiles[0].Name()))
if err != nil {
return err
}
if bytes.Equal(existingFileContent, anpString) {
logrus.Debugf("[Applyinator] Not writing applied plan to file %s as the last file written (%s) had identical contents", file, planFiles[0].Name())
return nil
}
}

return writeContentToFile(filepath.Join(a.appliedPlanDir, file), os.Getuid(), os.Getgid(), 0600, anpString)
}

func (a *Applyinator) execute(ctx context.Context, prefix, executionDir string, instruction CommonInstruction, combinedOutput bool) ([]byte, []byte, int, error) {
if instruction.Image == "" {
logrus.Infof("[Applyinator] No image provided, creating empty working directory %s", executionDir)
Expand Down

0 comments on commit ad6e3be

Please sign in to comment.