Skip to content

Commit

Permalink
kubernetes: Add config_map resource (#12753)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Mar 16, 2017
1 parent 77e585f commit 5f70ffb
Show file tree
Hide file tree
Showing 7 changed files with 515 additions and 1 deletion.
3 changes: 2 additions & 1 deletion builtin/providers/kubernetes/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"kubernetes_namespace": resourceKubernetesNamespace(),
"kubernetes_config_map": resourceKubernetesConfigMap(),
"kubernetes_namespace": resourceKubernetesNamespace(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
125 changes: 125 additions & 0 deletions builtin/providers/kubernetes/resource_kubernetes_config_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package kubernetes

import (
"log"

"github.com/hashicorp/terraform/helper/schema"
"k8s.io/kubernetes/pkg/api/errors"
api "k8s.io/kubernetes/pkg/api/v1"
kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5"
)

func resourceKubernetesConfigMap() *schema.Resource {
return &schema.Resource{
Create: resourceKubernetesConfigMapCreate,
Read: resourceKubernetesConfigMapRead,
Exists: resourceKubernetesConfigMapExists,
Update: resourceKubernetesConfigMapUpdate,
Delete: resourceKubernetesConfigMapDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"metadata": namespacedMetadataSchema("config map", true),
"data": {
Type: schema.TypeMap,
Description: "A map of the configuration data.",
Optional: true,
},
},
}
}

func resourceKubernetesConfigMapCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

metadata := expandMetadata(d.Get("metadata").([]interface{}))
cfgMap := api.ConfigMap{
ObjectMeta: metadata,
Data: expandStringMap(d.Get("data").(map[string]interface{})),
}
log.Printf("[INFO] Creating new config map: %#v", cfgMap)
out, err := conn.CoreV1().ConfigMaps(metadata.Namespace).Create(&cfgMap)
if err != nil {
return err
}
log.Printf("[INFO] Submitted new config map: %#v", out)
d.SetId(buildId(out.ObjectMeta))

return resourceKubernetesConfigMapRead(d, meta)
}

func resourceKubernetesConfigMapRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
log.Printf("[INFO] Reading config map %s", name)
cfgMap, err := conn.CoreV1().ConfigMaps(namespace).Get(name)
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return err
}
log.Printf("[INFO] Received config map: %#v", cfgMap)
err = d.Set("metadata", flattenMetadata(cfgMap.ObjectMeta))
if err != nil {
return err
}
d.Set("data", cfgMap.Data)

return nil
}

func resourceKubernetesConfigMapUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

metadata := expandMetadata(d.Get("metadata").([]interface{}))
namespace, name := idParts(d.Id())
// This is necessary in case the name is generated
metadata.Name = name

cfgMap := api.ConfigMap{
ObjectMeta: metadata,
Data: expandStringMap(d.Get("data").(map[string]interface{})),
}
log.Printf("[INFO] Updating config map: %#v", cfgMap)
out, err := conn.CoreV1().ConfigMaps(namespace).Update(&cfgMap)
if err != nil {
return err
}
log.Printf("[INFO] Submitted updated config map: %#v", out)
d.SetId(buildId(out.ObjectMeta))

return resourceKubernetesConfigMapRead(d, meta)
}

func resourceKubernetesConfigMapDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
log.Printf("[INFO] Deleting config map: %#v", name)
err := conn.CoreV1().ConfigMaps(namespace).Delete(name, &api.DeleteOptions{})
if err != nil {
return err
}

log.Printf("[INFO] Config map %s deleted", name)

d.SetId("")
return nil
}

func resourceKubernetesConfigMapExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)

namespace, name := idParts(d.Id())
log.Printf("[INFO] Checking config map %s", name)
_, err := conn.CoreV1().ConfigMaps(namespace).Get(name)
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
}
log.Printf("[DEBUG] Received error: %#v", err)
}
return true, err
}
Loading

0 comments on commit 5f70ffb

Please sign in to comment.