Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_list attribute in helm_release #1071

Merged
merged 15 commits into from
Mar 20, 2023
3 changes: 3 additions & 0 deletions .changelog/1071.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
`helm/resource_release.go`: Add `set_list` attribute
```
25 changes: 25 additions & 0 deletions helm/data_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ func dataTemplate() *schema.Resource {
},
},
},
"set_list": {
Type: schema.TypeList,
Optional: true,
Description: "Custom sensitive values to be merged with the values.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"auto", "string",
}, false),
BBBmau marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
},
"set_sensitive": {
Type: schema.TypeSet,
Optional: true,
Expand Down
61 changes: 61 additions & 0 deletions helm/resource_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,31 @@ func resourceRelease() *schema.Resource {
},
},
},
"set_list": {
Type: schema.TypeList,
Optional: true,
Description: "Custom sensitive values to be merged with the values.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"value": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
"auto", "string",
}, false),
},
},
},
},
"set_sensitive": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -1176,6 +1201,13 @@ func getValues(d resourceGetter) (map[string]interface{}, error) {
}
}

for _, raw := range d.Get("set_list").([]interface{}) {
set_list := raw.(map[string]interface{})
if err := getListValue(base, set_list); err != nil {
return nil, err
}
}

for _, raw := range d.Get("set_sensitive").(*schema.Set).List() {
set := raw.(map[string]interface{})
if err := getValue(base, set); err != nil {
Expand All @@ -1186,6 +1218,34 @@ func getValues(d resourceGetter) (map[string]interface{}, error) {
return base, logValues(base, d)
}

func getListValue(base, set map[string]interface{}) error {
name := set["name"].(string)
listValue := set["value"].([]interface{}) // this is going to be a list
valueType := set["type"].(string)

listStringArray := make([]string, len(listValue))

for i, s := range listValue {
listStringArray[i] = s.(string)
}
listString := strings.Join(listStringArray, ",")

switch valueType {
case "auto", "":
if err := strvals.ParseInto(fmt.Sprintf("%s={%s}", name, listString), base); err != nil {
return fmt.Errorf("failed parsing key %q with value %s, %s", name, listString, err)
}
case "string":
if err := strvals.ParseIntoString(fmt.Sprintf("%s={%s}", name, listString), base); err != nil {
return fmt.Errorf("failed parsing key %q with value %s, %s", name, listString, err)
}
default:
return fmt.Errorf("unexpected type: %s", valueType)
}

return nil
BBBmau marked this conversation as resolved.
Show resolved Hide resolved
}

func getValue(base, set map[string]interface{}) error {
name := set["name"].(string)
value := set["value"].(string)
Expand Down Expand Up @@ -1426,6 +1486,7 @@ func valuesKnown(d *schema.ResourceDiff) bool {
"values",
"set",
"set_sensitive",
"set_list",
}
for _, attr := range checkAttributes {
if !rawPlan.GetAttr(attr).IsWhollyKnown() {
Expand Down
107 changes: 107 additions & 0 deletions helm/resource_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,27 @@ func TestGetValuesString(t *testing.T) {
}
}

func TestGetListValues(t *testing.T) {
d := resourceRelease().Data(nil)
err := d.Set("set_list", []interface{}{
map[string]interface{}{"name": "foo", "value": []string{"1", "2", "3"}, "type": "string"},
})
if err != nil {
t.Fatalf("error setting values: %s", err)
return
}

values, err := getValues(d)
if err != nil {
t.Fatalf("error getValues: %s", err)
return
}

if len(values["foo"].([]interface{})) != 3 {
BBBmau marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("error merging values, expected length of 3, got %v", len(values["foo"].([]interface{})))
}
}

func TestCloakSetValues(t *testing.T) {
d := resourceRelease().Data(nil)
err := d.Set("set_sensitive", []interface{}{
Expand Down Expand Up @@ -1340,6 +1361,62 @@ func TestAccResourceRelease_manifestUnknownValues(t *testing.T) {
})
}

func TestAccResourceRelease_set_list_chart(t *testing.T) {
name := randName("helm-setlist-chart")
namespace := createRandomNamespace(t)
defer deleteNamespace(t, namespace)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckHelmReleaseDestroy(namespace),
Steps: []resource.TestStep{
{
Config: testAccHelmReleaseSetListValues(testResourceName, namespace, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("helm_release.test", "metadata.0.chart", "test-chart"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.0", "1"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.1", "2"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.2", "3"),
),
},
},
})
}

func TestAccResourceRelease_update_set_list_chart(t *testing.T) {
name := randName("helm-setlist-chart")
namespace := createRandomNamespace(t)
defer deleteNamespace(t, namespace)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckHelmReleaseDestroy(namespace),
Steps: []resource.TestStep{
{
Config: testAccHelmReleaseSetListValues(testResourceName, namespace, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("helm_release.test", "metadata.0.chart", "test-chart"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.0", "1"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.1", "2"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.2", "3"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.#", "3"),
),
},
{
Config: testAccHelmReleaseUpdateSetListValues(testResourceName, namespace, name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("helm_release.test", "metadata.0.chart", "test-chart"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.0", "2"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.1", "1"),
resource.TestCheckResourceAttr("helm_release.test", "set_list.0.value.#", "2"),
),
},
},
})
}

func setupOCIRegistry(t *testing.T, usepassword bool) (string, func()) {
dockerPath, err := exec.LookPath("docker")
if err != nil {
Expand Down Expand Up @@ -1796,3 +1873,33 @@ func TestResourceExampleInstanceStateUpgradeV0(t *testing.T) {
}
}
}

func testAccHelmReleaseSetListValues(resource, ns, name string) string {
return fmt.Sprintf(`
resource "helm_release" "%s" {
name = %q
namespace = %q
chart = "./testdata/charts/test-chart-v2"

set_list {
name = "set_list_test"
value = [1, 2, 3]
}
}
`, resource, name, ns)
}

func testAccHelmReleaseUpdateSetListValues(resource, ns, name string) string {
return fmt.Sprintf(`
resource "helm_release" "%s" {
name = %q
namespace = %q
chart = "./testdata/charts/test-chart-v2"

set_list {
name = "set_list_test"
value = [2, 1]
}
}
`, resource, name, ns)
}
BBBmau marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions helm/testdata/charts/test-chart-v2/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ nodeSelector: {}
tolerations: []

affinity: {}

set_list_test: []
1 change: 1 addition & 0 deletions website/docs/d/template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ The following arguments are supported:
* `wait` - (Optional) Will wait until all resources are in a ready state before marking the release as successful. It will wait for as long as `timeout`. Defaults to `true`.
* `values` - (Optional) List of values in raw yaml to pass to helm. Values will be merged, in order, as Helm does with multiple `-f` options.
* `set` - (Optional) Value block with custom values to be merged with the values yaml.
* `set_list` - (Optional) Value block with list of custom values to be merged with the values yaml.
* `set_sensitive` - (Optional) Value block with custom sensitive values to be merged with the values yaml that won't be exposed in the plan's diff.
* `set_string` - (Optional) Value block with custom STRING values to be merged with the values yaml.
* `dependency_update` - (Optional) Runs helm dependency update before installing the chart. Defaults to `false`.
Expand Down
14 changes: 11 additions & 3 deletions website/docs/r/release.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ The following arguments are supported:

* `values` - (Optional) List of values in raw yaml to pass to helm. Values will be merged, in order, as Helm does with multiple `-f` options.
* `set` - (Optional) Value block with custom values to be merged with the values yaml.
* `set_list` - (Optional) Value block with list of custom values to be merged with the values yaml.
* `set_sensitive` - (Optional) Value block with custom sensitive values to be merged with the values yaml that won't be exposed in the plan's diff.
* `dependency_update` - (Optional) Runs helm dependency update before installing the chart. Defaults to `false`.
* `replace` - (Optional) Re-use the given name, only if that name is a deleted release which remains in the history. This is unsafe in production. Defaults to `false`.
Expand All @@ -212,7 +213,7 @@ The following arguments are supported:
* `lint` - (Optional) Run the helm chart linter during the plan. Defaults to `false`.
* `create_namespace` - (Optional) Create the namespace if it does not yet exist. Defaults to `false`.

The `set` and `set_sensitive` blocks support:
The `set`, `set_list`, and `set_sensitive` blocks support:

* `name` - (Required) full name of the variable to be set.
* `value` - (Required) value of the variable to be set.
Expand All @@ -222,8 +223,15 @@ Since Terraform Utilizes HCL as well as Helm using the Helm Template Language, i

```hcl
set {
name = "grafana.ingress.annotations\\.alb\\.ingress\\.kubernetes\\.io/group\\.name"
value = "shared-ingress"
name = "grafana.ingress.annotations\\.alb\\.ingress\\.kubernetes\\.io/group\\.name"
value = "shared-ingress"
}
```

```hcl
set_list {
name = "hashicorp"
value = ["terraform", "nomad", "vault"]
}
```

Expand Down