terraform/builtin/providers/kubernetes/resource_kubernetes_namespa...

145 lines
4.1 KiB
Go
Raw Normal View History

package kubernetes
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
api "k8s.io/kubernetes/pkg/api/v1"
kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
)
func resourceKubernetesNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceKubernetesNamespaceCreate,
Read: resourceKubernetesNamespaceRead,
Exists: resourceKubernetesNamespaceExists,
Update: resourceKubernetesNamespaceUpdate,
Delete: resourceKubernetesNamespaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"metadata": metadataSchema("namespace", true),
},
}
}
func resourceKubernetesNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
metadata := expandMetadata(d.Get("metadata").([]interface{}))
namespace := api.Namespace{
ObjectMeta: metadata,
}
log.Printf("[INFO] Creating new namespace: %#v", namespace)
out, err := conn.CoreV1().Namespaces().Create(&namespace)
if err != nil {
return err
}
log.Printf("[INFO] Submitted new namespace: %#v", out)
d.SetId(out.Name)
return resourceKubernetesNamespaceRead(d, meta)
}
func resourceKubernetesNamespaceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
name := d.Id()
log.Printf("[INFO] Reading namespace %s", name)
namespace, err := conn.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return err
}
log.Printf("[INFO] Received namespace: %#v", namespace)
err = d.Set("metadata", flattenMetadata(namespace.ObjectMeta))
if err != nil {
return err
}
return nil
}
func resourceKubernetesNamespaceUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
ops := patchMetadata("metadata.0.", "/metadata/", d)
data, err := ops.MarshalJSON()
if err != nil {
return fmt.Errorf("Failed to marshal update operations: %s", err)
}
log.Printf("[INFO] Updating namespace: %s", ops)
out, err := conn.CoreV1().Namespaces().Patch(d.Id(), pkgApi.JSONPatchType, data)
if err != nil {
return err
}
log.Printf("[INFO] Submitted updated namespace: %#v", out)
d.SetId(out.Name)
return resourceKubernetesNamespaceRead(d, meta)
}
func resourceKubernetesNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*kubernetes.Clientset)
name := d.Id()
log.Printf("[INFO] Deleting namespace: %#v", name)
err := conn.CoreV1().Namespaces().Delete(name, &meta_v1.DeleteOptions{})
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Target: []string{},
Pending: []string{"Terminating"},
Timeout: 5 * time.Minute,
Refresh: func() (interface{}, string, error) {
out, err := conn.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return nil, "", nil
}
log.Printf("[ERROR] Received error: %#v", err)
return out, "Error", err
}
statusPhase := fmt.Sprintf("%v", out.Status.Phase)
log.Printf("[DEBUG] Namespace %s status received: %#v", out.Name, statusPhase)
return out, statusPhase, nil
},
}
_, err = stateConf.WaitForState()
if err != nil {
return err
}
log.Printf("[INFO] Namespace %s deleted", name)
d.SetId("")
return nil
}
func resourceKubernetesNamespaceExists(d *schema.ResourceData, meta interface{}) (bool, error) {
conn := meta.(*kubernetes.Clientset)
name := d.Id()
log.Printf("[INFO] Checking namespace %s", name)
_, err := conn.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
if err != nil {
if statusErr, ok := err.(*errors.StatusError); ok && statusErr.ErrStatus.Code == 404 {
return false, nil
}
log.Printf("[DEBUG] Received error: %#v", err)
}
log.Printf("[INFO] Namespace %s exists", name)
return true, err
}