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 support for binary_data field in config_map_v1_data #2616

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2616.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Added support for the `binary_data` field in the `kubernetes_config_map_v1_data` resource.
```
5 changes: 4 additions & 1 deletion docs/resources/config_map_v1_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This resource allows Terraform to manage data within a pre-existing ConfigMap. T
- `metadata` (Block List, Min: 1, Max: 1) (see [below for nested schema](#nestedblock--metadata))

### Optional

- `binary_data` (Map of String) BinaryData contains the binary data. Each key must consist of alphanumeric characters, '-', '_' or '.'. BinaryData can contain byte sequences that are not in the UTF-8 range. The keys stored in BinaryData must not overlap with the ones in the Data field, this is enforced during validation process. Using this field will require 1.10+ apiserver and kubelet. This field only accepts base64-encoded payloads that will be decoded/encoded before being sent/received to/from the apiserver.
- `field_manager` (String) Set the name of the field manager for the specified labels.
- `force` (Boolean) Force overwriting data that is managed outside of Terraform.

Expand Down Expand Up @@ -50,6 +50,9 @@ resource "kubernetes_config_map_v1_data" "example" {
data = {
"owner" = "myteam"
}
binary_data = {
"logo.png" = "HxShiC0rp..."
}
}
```

Expand Down
19 changes: 15 additions & 4 deletions kubernetes/resource_kubernetes_config_map_v1_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,17 @@ func resourceKubernetesConfigMapV1Data() *schema.Resource {
},
},
"data": {
Type: schema.TypeMap,
Description: "The data we want to add to the ConfigMap.",
Required: true,
Type: schema.TypeMap,
Description: "The data we want to add to the ConfigMap.",
AtLeastOneOf: []string{"data", "binary_data"},
Optional: true,
},
"binary_data": {
Type: schema.TypeMap,
Description: "BinaryData contains the binary data. Each key must consist of alphanumeric characters, '-', '_' or '.'. BinaryData can contain byte sequences that are not in the UTF-8 range. The keys stored in BinaryData must not overlap with the ones in the Data field, this is enforced during validation process. Using this field will require 1.10+ apiserver and kubelet. This field only accepts base64-encoded payloads that will be decoded/encoded before being sent/received to/from the apiserver.",
Optional: true,
AtLeastOneOf: []string{"data", "binary_data"},
ValidateFunc: validateBase64EncodedMap,
},
"force": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -120,6 +128,7 @@ func resourceKubernetesConfigMapV1DataRead(ctx context.Context, d *schema.Resour
}

d.Set("data", data)
d.Set("binary_data", flattenByteMapToBase64Map(res.BinaryData))
return nil
}

Expand Down Expand Up @@ -169,6 +178,7 @@ func resourceKubernetesConfigMapV1DataUpdate(ctx context.Context, d *schema.Reso

// craft the patch to update the data
data := d.Get("data")
binaryData := expandBase64MapToByteMap(d.Get("binary_data").(map[string]interface{}))
if d.Id() == "" {
// if we're deleting then just we just patch
// with an empty data map
Expand All @@ -181,7 +191,8 @@ func resourceKubernetesConfigMapV1DataUpdate(ctx context.Context, d *schema.Reso
"name": name,
"namespace": namespace,
},
"data": data,
"data": data,
"binaryData": binaryData,
}
patch := unstructured.Unstructured{}
patch.Object = patchobj
Expand Down
89 changes: 89 additions & 0 deletions kubernetes/resource_kubernetes_config_map_v1_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package kubernetes

import (
"fmt"
"os"
"path/filepath"
"regexp"
"testing"

Expand Down Expand Up @@ -88,6 +90,53 @@ func TestAccKubernetesConfigMapV1Data_validation(t *testing.T) {
})
}

func TestAccKubernetesConfigMapV1Data_binaryData(t *testing.T) {
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
resourceName := "kubernetes_config_map_v1_data.test"
baseDir := "."
cwd, _ := os.Getwd()
if filepath.Base(cwd) != "kubernetes" { // running from test binary
baseDir = "kubernetes"
}
jrhouston marked this conversation as resolved.
Show resolved Hide resolved

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
createConfigMap(name, "default")
},
IDRefreshName: resourceName,
IDRefreshIgnore: []string{"metadata.0.resource_version"},
ProviderFactories: testAccProviderFactories,
CheckDestroy: func(s *terraform.State) error {
return destroyConfigMap(name, "default")
},
Steps: []resource.TestStep{

{
Config: testAccKubernetesConfigMapV1Data_binaryData(name, baseDir),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "data.%", "1"),
resource.TestCheckResourceAttr(resourceName, "data.text", "initial data"),
resource.TestCheckResourceAttr(resourceName, "binary_data.%", "1"),
resource.TestCheckResourceAttrSet(resourceName, "binary_data.binary1"),
),
},

{
Config: testAccKubernetesConfigMapV1Data_binaryDataUpdated(name, baseDir),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "data.%", "1"),
resource.TestCheckResourceAttr(resourceName, "data.text", "updated data"),
resource.TestCheckResourceAttr(resourceName, "binary_data.%", "3"),
resource.TestCheckResourceAttrSet(resourceName, "binary_data.binary1"),
resource.TestCheckResourceAttrSet(resourceName, "binary_data.binary2"),
resource.TestCheckResourceAttr(resourceName, "binary_data.inline_binary", "UmF3IGlubGluZSBkYXRh"),
),
},
},
})
}

func testAccKubernetesConfigMapV1Data_empty(name string) string {
return fmt.Sprintf(`resource "kubernetes_config_map_v1_data" "test" {
metadata {
Expand Down Expand Up @@ -126,3 +175,43 @@ func testAccKubernetesConfigMapV1Data_modified(name string) string {
}
`, name)
}

func testAccKubernetesConfigMapV1Data_binaryData(name string, baseDir string) string {
return fmt.Sprintf(`resource "kubernetes_config_map_v1_data" "test" {
metadata {
name = %q
}

data = {
"text" = "initial data"
}

binary_data = {
"binary1" = "${filebase64("%s/test-fixtures/binary.data")}"
}

field_manager = "tftest"
}
`, name, baseDir)
}

func testAccKubernetesConfigMapV1Data_binaryDataUpdated(name string, baseDir string) string {
return fmt.Sprintf(`resource "kubernetes_config_map_v1_data" "test" {
metadata {
name = %q
}

data = {
"text" = "updated data"
}

binary_data = {
"binary1" = "${filebase64("%s/test-fixtures/binary.data")}"
"binary2" = "${filebase64("%s/test-fixtures/binary2.data")}"
"inline_binary" = "${base64encode("Raw inline data")}"
}

field_manager = "tftest"
}
`, name, baseDir, baseDir)
}
Loading