Skip to content

Commit 36200a6

Browse files
authored
Implement cleanup of temporary files after execution (#62)
* 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
1 parent c2d877d commit 36200a6

File tree

2 files changed

+26
-61
lines changed

2 files changed

+26
-61
lines changed

pkg/helmfile/release_set.go

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ import (
1919
)
2020

2121
type ReleaseSet struct {
22-
Bin string
23-
Values []interface{}
24-
ValuesFiles []interface{}
25-
HelmBin string
26-
Path string
27-
Content string
28-
DiffOutput string
29-
ApplyOutput string
30-
Environment string
22+
Bin string
23+
Values []interface{}
24+
ValuesFiles []interface{}
25+
HelmBin string
26+
Content string
27+
DiffOutput string
28+
ApplyOutput string
29+
Environment string
30+
TmpHelmFilePath string
3131

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

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

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

124-
if f.Path != "" {
125-
if info, err := os.Stat(f.Path); err != nil {
126-
if !os.IsNotExist(err) {
127-
return nil, fmt.Errorf("verifying working_directory %q: %w", f.Path, err)
128-
}
129-
} else if info != nil && info.IsDir() {
130-
f.WorkingDirectory = f.Path
131-
} else {
132-
f.WorkingDirectory = filepath.Dir(f.Path)
133-
}
134-
}
135-
136-
logf("Printing computed working directory for %q: %s", d.Id(), f.WorkingDirectory)
137-
138118
if environmentVariables := d.Get(KeyEnvironmentVariables); environmentVariables != nil {
139119
f.EnvironmentVariables = environmentVariables.(map[string]interface{})
140120
}
@@ -146,31 +126,22 @@ func NewReleaseSet(d ResourceRead) (*ReleaseSet, error) {
146126
}
147127

148128
func NewCommandWithKubeconfig(fs *ReleaseSet, args ...string) (*exec.Cmd, error) {
149-
if fs.Content != "" && fs.Path != "" && fs.Path != HelmfileDefaultPath {
150-
return nil, fmt.Errorf("content and path can't be specified together: content=%q, path=%q", fs.Content, fs.Path)
151-
}
152-
153129
if fs.WorkingDirectory != "" {
154130
if err := os.MkdirAll(fs.WorkingDirectory, 0755); err != nil {
155131
return nil, fmt.Errorf("creating working directory %q: %w", fs.WorkingDirectory, err)
156132
}
157133
}
158134

159-
var path string
160-
if fs.Content != "" {
161-
bs := []byte(fs.Content)
162-
first := sha256.New()
163-
first.Write(bs)
164-
path = fmt.Sprintf("helmfile-%x.yaml", first.Sum(nil))
165-
if err := ioutil.WriteFile(filepath.Join(fs.WorkingDirectory, path), bs, 0700); err != nil {
166-
return nil, err
167-
}
168-
} else {
169-
path = fs.Path
135+
bs := []byte(fs.Content)
136+
first := sha256.New()
137+
first.Write(bs)
138+
fs.TmpHelmFilePath = fmt.Sprintf("helmfile-%x.yaml", first.Sum(nil))
139+
if err := ioutil.WriteFile(filepath.Join(fs.WorkingDirectory, fs.TmpHelmFilePath), bs, 0700); err != nil {
140+
return nil, err
170141
}
171142

172143
flags := []string{
173-
"--file", path,
144+
"--file", fs.TmpHelmFilePath,
174145
"--no-color",
175146
}
176147

@@ -307,6 +278,8 @@ func CreateReleaseSet(ctx *sdk.Context, fs *ReleaseSet, d ResourceReadWrite) err
307278
if err != nil {
308279
return err
309280
}
281+
defer os.Remove(fs.TmpHelmFilePath)
282+
310283
//obtain exclusive lock
311284
mutexKV.Lock(fs.WorkingDirectory)
312285
defer mutexKV.Unlock(fs.WorkingDirectory)
@@ -372,6 +345,7 @@ func runBuild(ctx *sdk.Context, fs *ReleaseSet, flags ...string) (*State, error)
372345
if err != nil {
373346
return nil, err
374347
}
348+
defer os.Remove(fs.TmpHelmFilePath)
375349

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

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

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

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

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

651-
if fs.Path != "" {
652-
_, err := os.Stat(fs.Path)
653-
if err != nil {
654-
return "", fmt.Errorf("verifying path %q: %w", fs.Path, err)
655-
}
656-
}
657-
658629
diff, err := readDiffFile(ctx, fs)
659630
if err != nil {
660631
state, err := runDiff(ctx, fs, diffConf)
@@ -854,6 +825,7 @@ func UpdateReleaseSet(ctx *sdk.Context, fs *ReleaseSet, d ResourceReadWrite) err
854825
if err != nil {
855826
return err
856827
}
828+
defer os.Remove(fs.TmpHelmFilePath)
857829

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

880853
//obtain exclusive lock
881854
mutexKV.Lock(fs.WorkingDirectory)

pkg/helmfile/resource_release.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package helmfile
22

33
import (
4-
"crypto/sha256"
54
"encoding/json"
65
"fmt"
76
"github.com/mumoshu/terraform-provider-eksctl/pkg/sdk/tfsdk"
87
"github.com/rs/xid"
98
"golang.org/x/xerrors"
10-
"io/ioutil"
119
"runtime/debug"
1210

1311
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -299,17 +297,11 @@ func NewReleaseSetWithSingleRelease(d ResourceRead) (*ReleaseSet, error) {
299297
if err != nil {
300298
return nil, err
301299
}
302-
first := sha256.New()
303-
first.Write(bs)
304-
path := fmt.Sprintf("helmfile-%x.yaml", first.Sum(nil))
305-
if err := ioutil.WriteFile(path, bs, 0755); err != nil {
306-
return nil, err
307-
}
308300

309301
rs := &ReleaseSet{
310302
Bin: r.Bin,
311303
HelmBin: r.HelmBin,
312-
Path: path,
304+
Content: string(bs),
313305
Environment: "default",
314306
WorkingDirectory: r.WorkingDirectory,
315307
Kubeconfig: r.Kubeconfig,

0 commit comments

Comments
 (0)