Skip to content

Commit

Permalink
Fix missing file errors when working_directory is set to non-current …
Browse files Browse the repository at this point in the history
…one (#53)

* Fix missing file errors when working_directory is set to non-current one

Ref #50
  • Loading branch information
mumoshu authored Feb 2, 2021
1 parent fe22f12 commit 05b820f
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 35 deletions.
12 changes: 6 additions & 6 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/testout
/helmfile-*.yaml
/*.log
/USE
/temp.values-*.yaml
/*.tf
/helmfile.yaml
/kubeconfig
/values.yaml
/test.tf
temp.values-*.yaml
/terraform.tfstate*
/helmfile-*.yaml
2 changes: 2 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ TF_LOG ?= TRACE
dev:
rm tf.log tf.*.log || true
rm -rf ./tf.*.pprof || true
kind get kubeconfig > kubeconfig
make testdata/01-bootstrap init plan apply
make testdata/02-more-resources plan apply
make testdata/03-add-prom plan apply
make testdata/04-issue50 plan apply
make destroy

.PHONY: init
Expand Down
9 changes: 9 additions & 0 deletions examples/issue50/scenario1/platform/helmfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repositories:
- name: flagger
url: https://flagger.app

releases:
- name: issue50-scenario1
chart: flagger/podinfo
values:
- foo: bar
9 changes: 9 additions & 0 deletions examples/issue50/scenario2/platform/helmfile.d/helmfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repositories:
- name: flagger
url: https://flagger.app

releases:
- name: issue50-scenario2
chart: flagger/podinfo
values:
- foo: bar
4 changes: 2 additions & 2 deletions examples/testdata/01-bootstrap/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ resource "helmfile_release_set" "mystack" {
FOO = "foo"
}

kubeconfig = "kubeconfig"

values = [
<<EOF
{"name": "myapp"}
Expand All @@ -26,6 +24,8 @@ EOF
selector = {
labelkey1 = "value1"
}

kubeconfig = "kubeconfig"
}

output "mystack_diff" {
Expand Down
1 change: 1 addition & 0 deletions examples/testdata/01-bootstrap/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
12 changes: 6 additions & 6 deletions examples/testdata/02-more-resources/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ resource "helmfile_embedding_example" "emb1" {
FOO = "emb1"
}

kubeconfig = "kubeconfig"

values = [
<<EOF
{"name": "myapp"}
Expand All @@ -27,6 +25,8 @@ EOF
selector = {
labelkey1 = "value1"
}

kubeconfig = "kubeconfig"
}
}

Expand All @@ -45,8 +45,6 @@ resource "helmfile_release_set" "mystack" {
FOO = "foo"
}

kubeconfig = "kubeconfig"

values = [
<<EOF
{"name": "myapp"}
Expand All @@ -56,6 +54,8 @@ EOF
selector = {
labelkey1 = "value1"
}

kubeconfig = "kubeconfig"
}

resource "helmfile_release_set" "mystack2" {
Expand Down Expand Up @@ -89,8 +89,6 @@ EOF
FOO = "foo"
}

kubeconfig = "kubeconfig"

values = [
<<EOF
{"name": "myapp"}
Expand All @@ -104,6 +102,8 @@ EOF
depends_on = [
helmfile_release_set.mystack,
]

kubeconfig = "kubeconfig"
}

output "mystack_diff" {
Expand Down
1 change: 1 addition & 0 deletions examples/testdata/02-more-resources/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
12 changes: 8 additions & 4 deletions examples/testdata/03-add-prom/test.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ resource "helmfile_embedding_example" "emb1" {

helm_binary = "helm3"



working_directory = path.module

environment = "default"
Expand All @@ -25,6 +23,8 @@ EOF
selector = {
labelkey1 = "value1"
}

kubeconfig = "kubeconfig"
}
}

Expand All @@ -33,8 +33,6 @@ resource "helmfile_release_set" "mystack" {

helm_binary = "helm3"



working_directory = path.module

environment = "default"
Expand All @@ -52,6 +50,8 @@ EOF
selector = {
labelkey1 = "value1"
}

kubeconfig = "kubeconfig"
}

resource "helmfile_release_set" "mystack2" {
Expand Down Expand Up @@ -92,6 +92,8 @@ EOF
selector = {
labelkey1 = "value1"
}

kubeconfig = "kubeconfig"
}

output "mystack_diff" {
Expand Down Expand Up @@ -123,6 +125,8 @@ resource "helmfile_release" "myapp" {
{ "image": {"tag": "3.1455" } }
EOF
]

kubeconfig = "kubeconfig"
}


Expand Down
16 changes: 16 additions & 0 deletions examples/testdata/04-issue50/helmfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repositories:
- name: flagger
url: https://flagger.app

releases:
- name: {{ .Values.name }}-{{ requiredEnv "FOO" }}
chart: flagger/podinfo
values:
- values.yaml
labels:
labelkey1: value1
labelkey2: value2

- name: prom-operator
chart: stable/prometheus-operator
disableValidation: true
23 changes: 23 additions & 0 deletions examples/testdata/04-issue50/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
provider "helmfile" {}

resource "helmfile_release_set" "issue50_scenario1" {
working_directory = "../examples/issue50/scenario1/platform"
binary = "helmfile-0.137.0"
content = file("issue50/scenario1/platform/helmfile.yaml")
kubeconfig = "kubeconfig"
values = [
<<-EOF
case: issue50_scenario1
EOF
]
}

resource "helmfile_release_set" "issue50_scenario2" {
working_directory = "../examples/issue50/scenario2/platform"
kubeconfig = "kubeconfig"
values = [
<<-EOF
case: issue50_scenario2
EOF
]
}
7 changes: 1 addition & 6 deletions examples/values.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
image:
tag: foobar2356

foo:
bar: BAR
baz: BAZ
foo: bar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
case: issue50_scenario1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
case: issue50_scenario2
39 changes: 28 additions & 11 deletions pkg/helmfile/release_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,25 @@ func NewCommandWithKubeconfig(fs *ReleaseSet, args ...string) (*exec.Cmd, error)
}
for _, vs := range fs.Values {
js := []byte(fmt.Sprintf("%s", vs))

first := sha256.New()
first.Write(js)
tmpf := fmt.Sprintf("temp.values-%x.yaml", first.Sum(nil))
if err := ioutil.WriteFile(filepath.Join(fs.WorkingDirectory, tmpf), js, 0700); err != nil {

relpath := filepath.Join(
fs.WorkingDirectory,
fmt.Sprintf("temp.values-%x.yaml", first.Sum(nil)),
)

abspath, err := filepath.Abs(relpath)
if err != nil {
return nil, xerrors.Errorf("getting absolute path to %s: %w", abspath, err)
}

if err := ioutil.WriteFile(abspath, js, 0700); err != nil {
return nil, err
}
flags = append(flags, "--state-values-file", tmpf)

flags = append(flags, "--state-values-file", abspath)
}

flags = append(flags, args...)
Expand Down Expand Up @@ -465,20 +477,25 @@ func runDiff(ctx *sdk.Context, fs *ReleaseSet, conf DiffConfig) (*State, error)
return nil, xerrors.Errorf("computing hash of object: %w", err)
}

tempDir := filepath.Join(".terraform", "helmfile", fmt.Sprintf("temp-%s", hash))
relpath := filepath.Join(".terraform", "helmfile", fmt.Sprintf("temp-%s", hash))

abspath, err := filepath.Abs(relpath)
if err != nil {
return nil, xerrors.Errorf("getting absolute path to %s: %w", relpath)
}

if info, _ := os.Stat(tempDir); info != nil {
if err := os.RemoveAll(tempDir); err != nil {
return nil, xerrors.Errorf("removing stable temp directory %s: %w", tempDir, err)
if info, _ := os.Stat(abspath); info != nil {
if err := os.RemoveAll(abspath); err != nil {
return nil, xerrors.Errorf("removing stable temp directory %s: %w", abspath, err)
}
}

if err := os.MkdirAll(tempDir, 0755); err != nil {
return nil, xerrors.Errorf("creating temp directory for helmfile and chartify %s: %w", tempDir, err)
if err := os.MkdirAll(abspath, 0755); err != nil {
return nil, xerrors.Errorf("creating temp directory for helmfile and chartify %s: %w", abspath, err)
}

cmd.Env = append(cmd.Env, "HELMFILE_TEMPDIR="+tempDir)
cmd.Env = append(cmd.Env, "CHARTIFY_TEMPDIR="+tempDir)
cmd.Env = append(cmd.Env, "HELMFILE_TEMPDIR="+abspath)
cmd.Env = append(cmd.Env, "CHARTIFY_TEMPDIR="+abspath)

if conf.Kubeconfig != "" {
cmd.Env = append(cmd.Env, "KUBECONFIG="+conf.Kubeconfig)
Expand Down

0 comments on commit 05b820f

Please sign in to comment.