Skip to content

Commit 6fd35b2

Browse files
committed
Add data source for config map
Add data source for reading config maps. Based on PR #90. Solves #76.
1 parent 4ac7c6b commit 6fd35b2

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
func TestAccKubernetesDataSourceConfigMap_basic(t *testing.T) {
12+
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
13+
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccKubernetesDataSourceConfigMapConfig_basic(name),
20+
Check: resource.ComposeAggregateTestCheckFunc(
21+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.name", name),
22+
resource.TestCheckResourceAttrSet("data.kubernetes_config_map.test", "metadata.0.generation"),
23+
resource.TestCheckResourceAttrSet("data.kubernetes_config_map.test", "metadata.0.resource_version"),
24+
resource.TestCheckResourceAttrSet("data.kubernetes_config_map.test", "metadata.0.self_link"),
25+
resource.TestCheckResourceAttrSet("data.kubernetes_config_map.test", "metadata.0.uid"),
26+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.annotations.%", "2"),
27+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.annotations.TestAnnotationOne", "one"),
28+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.annotations.TestAnnotationTwo", "two"),
29+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.labels.TestLabelOne", "one"),
30+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.labels.TestLabelTwo", "two"),
31+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "metadata.0.labels.TestLabelThree", "three"),
32+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "data.%", "2"),
33+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "data.one", "first"),
34+
resource.TestCheckResourceAttr("data.kubernetes_config_map.test", "data.two", "second"),
35+
),
36+
},
37+
},
38+
})
39+
}
40+
41+
func testAccKubernetesDataSourceConfigMapConfig_basic(name string) string {
42+
return testAccKubernetesConfigMapConfig_basic(name) + `
43+
data "kubernetes_config_map" "test" {
44+
metadata {
45+
name = "${kubernetes_config_map.test.metadata.0.name}"
46+
}
47+
}
48+
`
49+
}

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)