-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kubernetes: Add config_map resource (#12753)
- Loading branch information
1 parent
77e585f
commit 5f70ffb
Showing
7 changed files
with
515 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
builtin/providers/kubernetes/resource_kubernetes_config_map.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.