Skip to content

Commit

Permalink
Add helmfile_release_set.skip_diff_on_missing_files (#47)
Browse files Browse the repository at this point in the history
* Add helmfile_release_set.skip_diff_on_missing_files

Resolves #38
  • Loading branch information
mumoshu authored Nov 10, 2020
1 parent 5f0e6af commit 2c669a4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/helmfile/release_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ type ReleaseSet struct {
// HelmVersion is the version number or the semver version range for the helm version to use
HelmVersion string
HelmDiffVersion string

// SkipDiffOnMissingFiles is the list of local files. Any file contained in the list but missing on the file system
// result in the provider to skip running `helmfile-diff`. Use with Terraform's `depends_on`, so that
// you can let another dependent Terraform resource to created required files like kubeconfig or Helmfile values
// files to be used by the helmfile_release_set.
//
// See https://github.com/mumoshu/terraform-provider-helmfile/issues/38 for more information on expected use-cases.
SkipDiffOnMissingFiles []string
}

func NewReleaseSet(d ResourceRead) (*ReleaseSet, error) {
Expand Down Expand Up @@ -77,6 +85,16 @@ func NewReleaseSet(d ResourceRead) (*ReleaseSet, error) {
f.ValuesFiles = valuesFiles.([]interface{})
}

if vs := d.Get(KeySkipDiffOnMissingFiles); vs != nil {
var ss []string

for _, v := range vs.([]interface{}) {
ss = append(ss, v.(string))
}

f.SkipDiffOnMissingFiles = ss
}

f.Values = d.Get(KeyValues).([]interface{})
f.ReleasesValues = d.Get(KeyReleasesValues).(map[string]interface{})
f.Bin = d.Get(KeyBin).(string)
Expand Down
18 changes: 18 additions & 0 deletions pkg/helmfile/resource_release_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/rs/xid"
"golang.org/x/xerrors"
"log"
"os"
"runtime/debug"
Expand All @@ -26,6 +27,7 @@ const KeyApplyOutput = "apply_output"
const KeyDirty = "dirty"
const KeyConcurrency = "concurrency"
const KeyReleasesValues = "releases_values"
const KeySkipDiffOnMissingFiles = "skip_diff_on_missing_files"

const HelmfileDefaultPath = "helmfile.yaml"

Expand All @@ -46,6 +48,14 @@ var ReleaseSetSchema = map[string]*schema.Schema{
Type: schema.TypeString,
},
},
KeySkipDiffOnMissingFiles: {
Type: schema.TypeList,
Optional: true,
ForceNew: false,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
KeySelector: {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -233,6 +243,14 @@ func resourceReleaseSetDiff(d *schema.ResourceDiff, meta interface{}) (finalErr
return nil
}

if v, err := shouldDiff(fs); err != nil {
return xerrors.Errorf("checking skip_diff_on_missing_files to determine if the provider needs to run helmfile-diff: %w", err)
} else if !v {
logf("Skipping helmfile-diff due to that one or more files listed in skip_diff_on_missing_files were missing")

return nil
}

provider := meta.(*ProviderInstance)

diff, err := DiffReleaseSet(fs, resourceDiffToFields(d), WithDiffConfig(DiffConfig{
Expand Down
23 changes: 23 additions & 0 deletions pkg/helmfile/skip_diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helmfile

import (
"errors"
"golang.org/x/xerrors"
"os"
)

func shouldDiff(rs *ReleaseSet) (bool, error) {
for _, path := range rs.SkipDiffOnMissingFiles {
if _, err := os.Stat(path); err != nil {
if errors.Is(err, os.ErrNotExist) {
logf("detected missing file: %s", path)

return false, nil
}

return false, xerrors.Errorf("failed calling stat: %w", err)
}
}

return true, nil
}

0 comments on commit 2c669a4

Please sign in to comment.