Skip to content

Commit

Permalink
Implement cleanup of temporary files after execution (#62)
Browse files Browse the repository at this point in the history
* Move temporary helmfile creation logic for both helmfile_release and helmfile_release_set to NewCommandWithKubeconfig of helmfile_release_set. This is a function that actually executes helmfile binary.

* Added TmpHelfilePath field to ReleaseSet strucutre to store info about used temporarty helmfile path.
Handle tmpHelmfilPath removal in all places where `NewCommandWithKubeconfig` executed.
Unify path for temporart helmfile and other tmp provider resources.

* Reverting change making tmp helmfiles created in .terraform/helmfile, cause it causes issue that dir not exist.
Making provider delete tmp directory after run
  • Loading branch information
dm3ch authored Sep 5, 2021
1 parent c2d877d commit 36200a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 61 deletions.
77 changes: 25 additions & 52 deletions pkg/helmfile/release_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (
)

type ReleaseSet struct {
Bin string
Values []interface{}
ValuesFiles []interface{}
HelmBin string
Path string
Content string
DiffOutput string
ApplyOutput string
Environment string
Bin string
Values []interface{}
ValuesFiles []interface{}
HelmBin string
Content string
DiffOutput string
ApplyOutput string
Environment string
TmpHelmFilePath string

// Selector is a helmfile label selector that is a AND list of label key-value pairs
Selector map[string]interface{}
Expand Down Expand Up @@ -69,12 +69,6 @@ func NewReleaseSet(d ResourceRead) (*ReleaseSet, error) {
if env := d.Get(KeyEnvironment); env != nil {
f.Environment = env.(string)
}
// environment defaults to "" for helmfile_release_set but it's always nil for helmfile_release.
// This nil-check is required to handle the latter case. Otherwise it ends up with:
// panic: interface conversion: interface {} is nil, not string
if path := d.Get(KeyPath); path != nil {
f.Path = path.(string)
}

if content := d.Get(KeyContent); content != nil {
f.Content = content.(string)
Expand Down Expand Up @@ -121,20 +115,6 @@ func NewReleaseSet(d ResourceRead) (*ReleaseSet, error) {

logf("Printing raw working directory for %q: %s", d.Id(), f.WorkingDirectory)

if f.Path != "" {
if info, err := os.Stat(f.Path); err != nil {
if !os.IsNotExist(err) {
return nil, fmt.Errorf("verifying working_directory %q: %w", f.Path, err)
}
} else if info != nil && info.IsDir() {
f.WorkingDirectory = f.Path
} else {
f.WorkingDirectory = filepath.Dir(f.Path)
}
}

logf("Printing computed working directory for %q: %s", d.Id(), f.WorkingDirectory)

if environmentVariables := d.Get(KeyEnvironmentVariables); environmentVariables != nil {
f.EnvironmentVariables = environmentVariables.(map[string]interface{})
}
Expand All @@ -146,31 +126,22 @@ func NewReleaseSet(d ResourceRead) (*ReleaseSet, error) {
}

func NewCommandWithKubeconfig(fs *ReleaseSet, args ...string) (*exec.Cmd, error) {
if fs.Content != "" && fs.Path != "" && fs.Path != HelmfileDefaultPath {
return nil, fmt.Errorf("content and path can't be specified together: content=%q, path=%q", fs.Content, fs.Path)
}

if fs.WorkingDirectory != "" {
if err := os.MkdirAll(fs.WorkingDirectory, 0755); err != nil {
return nil, fmt.Errorf("creating working directory %q: %w", fs.WorkingDirectory, err)
}
}

var path string
if fs.Content != "" {
bs := []byte(fs.Content)
first := sha256.New()
first.Write(bs)
path = fmt.Sprintf("helmfile-%x.yaml", first.Sum(nil))
if err := ioutil.WriteFile(filepath.Join(fs.WorkingDirectory, path), bs, 0700); err != nil {
return nil, err
}
} else {
path = fs.Path
bs := []byte(fs.Content)
first := sha256.New()
first.Write(bs)
fs.TmpHelmFilePath = fmt.Sprintf("helmfile-%x.yaml", first.Sum(nil))
if err := ioutil.WriteFile(filepath.Join(fs.WorkingDirectory, fs.TmpHelmFilePath), bs, 0700); err != nil {
return nil, err
}

flags := []string{
"--file", path,
"--file", fs.TmpHelmFilePath,
"--no-color",
}

Expand Down Expand Up @@ -307,6 +278,8 @@ func CreateReleaseSet(ctx *sdk.Context, fs *ReleaseSet, d ResourceReadWrite) err
if err != nil {
return err
}
defer os.Remove(fs.TmpHelmFilePath)

//obtain exclusive lock
mutexKV.Lock(fs.WorkingDirectory)
defer mutexKV.Unlock(fs.WorkingDirectory)
Expand Down Expand Up @@ -372,6 +345,7 @@ func runBuild(ctx *sdk.Context, fs *ReleaseSet, flags ...string) (*State, error)
if err != nil {
return nil, err
}
defer os.Remove(fs.TmpHelmFilePath)

//obtain exclusive lock
mutexKV.Lock(fs.WorkingDirectory)
Expand All @@ -390,6 +364,7 @@ func getHelmfileVersion(ctx *sdk.Context, fs *ReleaseSet) (*semver.Version, erro
if err != nil {
return nil, fmt.Errorf("creating command: %w", err)
}
defer os.Remove(fs.TmpHelmFilePath)

//obtain exclusive lock
mutexKV.Lock(fs.WorkingDirectory)
Expand Down Expand Up @@ -423,6 +398,7 @@ func runTemplate(ctx *sdk.Context, fs *ReleaseSet) (*State, error) {
if err != nil {
return nil, err
}
defer os.Remove(fs.TmpHelmFilePath)

//obtain exclusive lock
mutexKV.Lock(fs.WorkingDirectory)
Expand Down Expand Up @@ -467,6 +443,7 @@ func runDiff(ctx *sdk.Context, fs *ReleaseSet, conf DiffConfig) (*State, error)
if err != nil {
return nil, err
}
defer os.Remove(fs.TmpHelmFilePath)

// Use the stable directory for storing temporary charts and values files
// so that helmfile-diff output becomes stables and terraform plan doesn't break.
Expand All @@ -493,6 +470,7 @@ func runDiff(ctx *sdk.Context, fs *ReleaseSet, conf DiffConfig) (*State, error)
if err := os.MkdirAll(abspath, 0755); err != nil {
return nil, xerrors.Errorf("creating temp directory for helmfile and chartify %s: %w", abspath, err)
}
defer os.Remove(abspath)

cmd.Env = append(cmd.Env, "HELMFILE_TEMPDIR="+abspath)
cmd.Env = append(cmd.Env, "CHARTIFY_TEMPDIR="+abspath)
Expand Down Expand Up @@ -648,13 +626,6 @@ func DiffReleaseSet(ctx *sdk.Context, fs *ReleaseSet, d ResourceReadWrite, opts
o(&diffConf)
}

if fs.Path != "" {
_, err := os.Stat(fs.Path)
if err != nil {
return "", fmt.Errorf("verifying path %q: %w", fs.Path, err)
}
}

diff, err := readDiffFile(ctx, fs)
if err != nil {
state, err := runDiff(ctx, fs, diffConf)
Expand Down Expand Up @@ -854,6 +825,7 @@ func UpdateReleaseSet(ctx *sdk.Context, fs *ReleaseSet, d ResourceReadWrite) err
if err != nil {
return err
}
defer os.Remove(fs.TmpHelmFilePath)

//obtain exclusive lock
mutexKV.Lock(fs.WorkingDirectory)
Expand All @@ -876,6 +848,7 @@ func DeleteReleaseSet(ctx *sdk.Context, fs *ReleaseSet, d ResourceReadWrite) err
if err != nil {
return err
}
defer os.Remove(fs.TmpHelmFilePath)

//obtain exclusive lock
mutexKV.Lock(fs.WorkingDirectory)
Expand Down
10 changes: 1 addition & 9 deletions pkg/helmfile/resource_release.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package helmfile

import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
"github.com/rs/xid"
"golang.org/x/xerrors"
"io/ioutil"
"runtime/debug"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -299,17 +297,11 @@ func NewReleaseSetWithSingleRelease(d ResourceRead) (*ReleaseSet, error) {
if err != nil {
return nil, err
}
first := sha256.New()
first.Write(bs)
path := fmt.Sprintf("helmfile-%x.yaml", first.Sum(nil))
if err := ioutil.WriteFile(path, bs, 0755); err != nil {
return nil, err
}

rs := &ReleaseSet{
Bin: r.Bin,
HelmBin: r.HelmBin,
Path: path,
Content: string(bs),
Environment: "default",
WorkingDirectory: r.WorkingDirectory,
Kubeconfig: r.Kubeconfig,
Expand Down

0 comments on commit 36200a6

Please sign in to comment.