2017-03-02 12:26:06 +01:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-05-26 08:50:26 +02:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
pkgApi "k8s.io/apimachinery/pkg/types"
|
2017-03-02 12:26:06 +01:00
|
|
|
api "k8s.io/kubernetes/pkg/api/v1"
|
2017-05-26 08:50:26 +02:00
|
|
|
kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
2017-03-02 12:26:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2017-04-02 08:08:16 +02:00
|
|
|
"metadata": metadataSchema("namespace", true),
|
2017-03-02 12:26:06 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-05-26 08:50:26 +02:00
|
|
|
namespace, err := conn.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
|
2017-03-02 12:26:06 +01:00
|
|
|
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)
|
|
|
|
|
2017-04-10 16:01:40 +02:00
|
|
|
ops := patchMetadata("metadata.0.", "/metadata/", d)
|
|
|
|
data, err := ops.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to marshal update operations: %s", err)
|
2017-03-02 12:26:06 +01:00
|
|
|
}
|
2017-04-10 16:01:40 +02:00
|
|
|
|
|
|
|
log.Printf("[INFO] Updating namespace: %s", ops)
|
|
|
|
out, err := conn.CoreV1().Namespaces().Patch(d.Id(), pkgApi.JSONPatchType, data)
|
2017-03-02 12:26:06 +01:00
|
|
|
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)
|
2017-05-26 08:50:26 +02:00
|
|
|
err := conn.CoreV1().Namespaces().Delete(name, &meta_v1.DeleteOptions{})
|
2017-03-02 12:26:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Target: []string{},
|
|
|
|
Pending: []string{"Terminating"},
|
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
2017-05-26 08:50:26 +02:00
|
|
|
out, err := conn.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
|
2017-03-02 12:26:06 +01:00
|
|
|
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)
|
2017-05-26 08:50:26 +02:00
|
|
|
_, err := conn.CoreV1().Namespaces().Get(name, meta_v1.GetOptions{})
|
2017-03-02 12:26:06 +01:00
|
|
|
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
|
|
|
|
}
|