Skip to content

Commit cb60600

Browse files
authored
Merge pull request #801 from dak1n1/configmap
Add data source for reading config maps
2 parents 0f8d313 + c34cc42 commit cb60600

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package kubernetes
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
func dataSourceKubernetesConfigMap() *schema.Resource {
9+
return &schema.Resource{
10+
Read: dataSourceKubernetesConfigMapRead,
11+
12+
Schema: map[string]*schema.Schema{
13+
"metadata": namespacedMetadataSchema("config_map", false),
14+
"data": {
15+
Type: schema.TypeMap,
16+
Description: "A map of the config map data.",
17+
Computed: true,
18+
},
19+
"binary_data": {
20+
Type: schema.TypeMap,
21+
Description: "A map of the config map binary data.",
22+
Computed: true,
23+
},
24+
},
25+
}
26+
}
27+
28+
func dataSourceKubernetesConfigMapRead(d *schema.ResourceData, meta interface{}) error {
29+
om := meta_v1.ObjectMeta{
30+
Namespace: d.Get("metadata.0.namespace").(string),
31+
Name: d.Get("metadata.0.name").(string),
32+
}
33+
d.SetId(buildId(om))
34+
35+
return resourceKubernetesConfigMapRead(d, meta)
36+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package kubernetes
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
// TestAccKubernetesDataSourceConfigMap_basic tests that the data source is able to read
12+
// plaintext data, binary data, annotation, label, and name of the config map resource.
13+
func TestAccKubernetesDataSourceConfigMap_basic(t *testing.T) {
14+
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccKubernetesDataSourceConfigMapConfig_basic(name),
22+
Check: resource.ComposeAggregateTestCheckFunc(
23+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.name", name),
24+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.annotations.TestAnnotationOne", "one"),
25+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.labels.TestLabelOne", "one"),
26+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "data.one", "first"),
27+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "binary_data.raw", "UmF3IGRhdGEgc2hvdWxkIGNvbWUgYmFjayBhcyBpcyBpbiB0aGUgcG9k"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
// testAccKubernetesDataSourceConfigMapConfig_basic provides the terraform config
35+
// used to test basic functionality of the config_map data source.
36+
func testAccKubernetesDataSourceConfigMapConfig_basic(name string) string {
37+
return fmt.Sprintf(`
38+
resource "kubernetes_config_map" "test" {
39+
metadata {
40+
annotations = {
41+
TestAnnotationOne = "one"
42+
}
43+
44+
labels = {
45+
TestLabelOne = "one"
46+
}
47+
48+
name = "%s"
49+
}
50+
51+
data = {
52+
one = "first"
53+
}
54+
55+
binary_data = {
56+
raw = "${base64encode("Raw data should come back as is in the pod")}"
57+
}
58+
}
59+
60+
data "kubernetes_config_map" "test" {
61+
metadata {
62+
name = "${kubernetes_config_map.test.metadata.0.name}"
63+
}
64+
}`, name)
65+
}

kubernetes/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func Provider() terraform.ResourceProvider {
137137
},
138138

139139
DataSourcesMap: map[string]*schema.Resource{
140+
"kubernetes_config_map": dataSourceKubernetesConfigMap(),
140141
"kubernetes_secret": dataSourceKubernetesSecret(),
141142
"kubernetes_service": dataSourceKubernetesService(),
142143
"kubernetes_storage_class": dataSourceKubernetesStorageClass(),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
layout: "kubernetes"
3+
page_title: "Kubernetes: kubernetes_config map"
4+
sidebar_current: "docs-kubernetes-resource-config map"
5+
description: |-
6+
This data source reads configuration data from a config map.
7+
---
8+
9+
# kubernetes_config map
10+
11+
Config Maps are key-value pairs containing configuration data. The Config Map data source provides a mechanism for extracting these key-value pairs.
12+
13+
~> **Note:** All arguments including the config map data will be stored in the raw state as plain-text. [Read more about sensitive data in state](/docs/state/sensitive-data.html).
14+
15+
## Example Usage
16+
17+
```hcl
18+
data "kubernetes_config map" "example" {
19+
metadata {
20+
name = "my-config"
21+
}
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
The following arguments are supported:
28+
29+
* `metadata` - (Required) Standard config map's metadata. For more info see [Kubernetes reference](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#metadata)
30+
31+
## Nested Blocks
32+
33+
### `metadata`
34+
35+
#### Arguments
36+
37+
* `name` - (Required) Name of the config map, must be unique. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/identifiers#names)
38+
* `namespace` - (Optional) Namespace defines the space within which name of the config map must be unique.
39+
40+
#### Attributes
41+
42+
* `generation` - A sequence number representing a specific generation of the desired state.
43+
* `resource_version` - An opaque value that represents the internal version of this config map that can be used by clients to determine when config map has changed. For more info see [Kubernetes reference](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#concurrency-control-and-consistency)
44+
* `self_link` - A URL representing this config map.
45+
* `uid` - The unique in time and space value for this config map. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/identifiers#uids)
46+
47+
## Attribute Reference
48+
49+
* `data` - A map of the config map data.
50+
* `binary_data` - A map of preserved non-UTF8 data. For more info see [Kubernetes API reference](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#configmap-v1-core).

website/kubernetes.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<li<%= sidebar_current("docs-kubernetes-data-source") %>>
1919
<a href="#">Data Sources</a>
2020
<ul class="nav nav-visible">
21+
<li<%= sidebar_current("docs-kubernetes-data-source-config-map") %>>
22+
<a href="/docs/providers/kubernetes/d/config_map.html">kubernetes_config_map</a>
23+
</li>
2124
<li<%= sidebar_current("docs-kubernetes-data-source-secret") %>>
2225
<a href="/docs/providers/kubernetes/d/secret.html">kubernetes_secret</a>
2326
</li>

0 commit comments

Comments
 (0)