Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment #101

Merged
merged 1 commit into from Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions kubernetes/provider.go
Expand Up @@ -110,6 +110,7 @@ func Provider() terraform.ResourceProvider {

ResourcesMap: map[string]*schema.Resource{
"kubernetes_config_map": resourceKubernetesConfigMap(),
"kubernetes_deployment": resourceKubernetesDeployment(),
"kubernetes_horizontal_pod_autoscaler": resourceKubernetesHorizontalPodAutoscaler(),
"kubernetes_limit_range": resourceKubernetesLimitRange(),
"kubernetes_namespace": resourceKubernetesNamespace(),
Expand Down
382 changes: 382 additions & 0 deletions kubernetes/resource_kubernetes_deployment.go
@@ -0,0 +1,382 @@
package kubernetes

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)

const (
// https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/deployment/util/deployment_util.go#L93
TimedOutReason = "ProgressDeadlineExceeded"
)

func resourceKubernetesDeployment() *schema.Resource {
return &schema.Resource{
Create: resourceKubernetesDeploymentCreate,
Read: resourceKubernetesDeploymentRead,
Exists: resourceKubernetesDeploymentExists,
Update: resourceKubernetesDeploymentUpdate,
Delete: resourceKubernetesDeploymentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"metadata": namespacedMetadataSchema("deployment", true),
"spec": {
Type: schema.TypeList,
Description: "Spec defines the specification of the desired behavior of the deployment. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/#deployment-v1-apps",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"min_ready_seconds": {
Type: schema.TypeInt,
Description: "Minimum number of seconds for which a newly created pod should be ready without any of its container crashing, for it to be considered available. Defaults to 0 (pod will be considered available as soon as it is ready)",
Optional: true,
Default: 0,
},
"paused": {
Type: schema.TypeBool,
Description: "Indicates that the deployment is paused.",
Optional: true,
Default: false,
},
"progress_deadline_seconds": {
Type: schema.TypeInt,
Description: "The maximum time in seconds for a deployment to make progress before it is considered to be failed. The deployment controller will continue to process failed deployments and a condition with a ProgressDeadlineExceeded reason will be surfaced in the deployment status. Note that progress will not be estimated during the time a deployment is paused. Defaults to 600s.",
Optional: true,
Default: 0,
},
"replicas": {
Type: schema.TypeInt,
Description: "The number of desired replicas. Defaults to 1. More info: http://kubernetes.io/docs/user-guide/replication-controller#what-is-a-replication-controller",
Optional: true,
Default: 1,
},
"revision_history_limit": {
Type: schema.TypeInt,
Description: "The number of old ReplicaSets to retain to allow rollback. This is a pointer to distinguish between explicit zero and not specified. Defaults to 10.",
Optional: true,
Default: 10,
},
"selector": {
Type: schema.TypeList,
Description: "A label query over volumes to consider for binding.",
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"match_expressions": {
Type: schema.TypeList,
Description: "A list of label selector requirements. The requirements are ANDed.",
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Description: "The label key that the selector applies to.",
Optional: true,
ForceNew: true,
},
"operator": {
Type: schema.TypeString,
Description: "A key's relationship to a set of values. Valid operators ard `In`, `NotIn`, `Exists` and `DoesNotExist`.",
Optional: true,
ForceNew: true,
},
"values": {
Type: schema.TypeSet,
Description: "An array of string values. If the operator is `In` or `NotIn`, the values array must be non-empty. If the operator is `Exists` or `DoesNotExist`, the values array must be empty. This array is replaced during a strategic merge patch.",
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
},
},
"match_labels": {
Type: schema.TypeMap,
Description: "A map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of `match_expressions`, whose key field is \"key\", the operator is \"In\", and the values array contains only \"value\". The requirements are ANDed.",
Optional: true,
ForceNew: true,
},
},
},
},
"strategy": {
Type: schema.TypeList,
Description: "The deployment strategy to use to replace existing pods with new ones.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Description: "Type of deployment. Can be 'Recreate' or 'RollingUpdate'. Default is RollingUpdate.",
Optional: true,
Default: "RollingUpdate",
},
"rolling_update": {
Type: schema.TypeList,
Description: "Rolling update config params. Present only if DeploymentStrategyType = RollingUpdate.",
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_surge": {
Type: schema.TypeString,
Description: "The maximum number of pods that can be scheduled above the desired number of pods. Value can be an absolute number (ex: 5) or a percentage of desired pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number is calculated from percentage by rounding up. Defaults to 25%. Example: when this is set to 30%, the new RC can be scaled up immediately when the rolling update starts, such that the total number of old and new pods do not exceed 130% of desired pods. Once old pods have been killed, new RC can be scaled up further, ensuring that total number of pods running at any time during the update is atmost 130% of desired pods.",
Optional: true,
},
"max_unavailable": {
Type: schema.TypeString,
Description: "The maximum number of pods that can be unavailable during the update. Value can be an absolute number (ex: 5) or a percentage of desired pods (ex: 10%). Absolute number is calculated from percentage by rounding down. This can not be 0 if MaxSurge is 0. Defaults to 25%. Example: when this is set to 30%, the old RC can be scaled down to 70% of desired pods immediately when the rolling update starts. Once new pods are ready, old RC can be scaled down further, followed by scaling up the new RC, ensuring that the total number of pods available at all times during the update is at least 70% of desired pods.",
Optional: true,
},
},
},
},
},
},
},
"template": {
Type: schema.TypeList,
Description: "Template describes the pods that will be created.",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"metadata": namespacedMetadataSchema("pod", true),
"spec": {
Type: schema.TypeList,
Description: "Spec defines the specification of the desired behavior of the deployment. More info: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.9/#deployment-v1-apps",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: podSpecFields(true),
},
},
},
},
},
},
},
},
},
}
}

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

metadata := expandMetadata(d.Get("metadata").([]interface{}))
spec, err := expandDeploymentSpec(d.Get("spec").([]interface{}))
if err != nil {
return err
}

spec.Template.Spec.AutomountServiceAccountToken = ptrToBool(false)

deployment := appsv1.Deployment{
ObjectMeta: metadata,
Spec: spec,
}

log.Printf("[INFO] Creating new deployment: %#v", deployment)
out, err := conn.AppsV1().Deployments(metadata.Namespace).Create(&deployment)
if err != nil {
return fmt.Errorf("Failed to create deployment: %s", err)
}

d.SetId(buildId(out.ObjectMeta))

log.Printf("[INFO] Submitted new deployment: %#v", out)

log.Printf("[DEBUG] Waiting for deployment %s to schedule %d replicas", d.Id(), *out.Spec.Replicas)

limiter := time.Tick(time.Second * 15)

for {
<-limiter

// Query the deployment to get a status update.
dply, err := conn.AppsV1().Deployments(metadata.Namespace).Get(deployment.Name, metav1.GetOptions{})
if err != nil {
return err
}

if dply.Generation <= dply.Status.ObservedGeneration {
cond := GetDeploymentCondition(dply.Status, appsv1.DeploymentProgressing)
if cond != nil && cond.Reason == TimedOutReason {
return fmt.Errorf("Deployment exceeded its progress deadline")
}

if dply.Status.UpdatedReplicas < *dply.Spec.Replicas {
log.Printf("[DEBUG] Waiting for rollout to finish: %d out of %d new replicas have been updated...", dply.Status.UpdatedReplicas, dply.Spec.Replicas)
continue
}

if dply.Status.Replicas > dply.Status.UpdatedReplicas {
log.Printf("[DEBUG] Waiting for rollout to finish: %d old replicas are pending termination...", dply.Status.Replicas-dply.Status.UpdatedReplicas)
continue
}

if dply.Status.AvailableReplicas < dply.Status.UpdatedReplicas {
log.Printf("[DEBUG] Waiting for rollout to finish: %d of %d updated replicas are available...", dply.Status.AvailableReplicas, dply.Status.UpdatedReplicas)
continue
}

break
}
}

log.Printf("[INFO] Deployment complete")

return resourceKubernetesDeploymentRead(d, meta)
}

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

namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

ops := patchMetadata("metadata.0.", "/metadata/", d)

if d.HasChange("spec") {
spec, err := expandDeploymentSpec(d.Get("spec").([]interface{}))
if err != nil {
return err
}

ops = append(ops, &ReplaceOperation{
Path: "/spec",
Value: spec,
})
}
data, err := ops.MarshalJSON()
if err != nil {
return fmt.Errorf("Failed to marshal update operations: %s", err)
}
log.Printf("[INFO] Updating deployment %q: %v", name, string(data))
out, err := conn.AppsV1().Deployments(namespace).Patch(name, pkgApi.JSONPatchType, data)
if err != nil {
return fmt.Errorf("Failed to update deployment: %s", err)
}
log.Printf("[INFO] Submitted updated deployment: %#v", out)

err = resource.Retry(d.Timeout(schema.TimeoutUpdate),
waitForDesiredReplicasFunc(conn, namespace, name))
if err != nil {
return err
}

return resourceKubernetesDeploymentRead(d, meta)
}

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

namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Reading deployment %s", name)
deployment, err := conn.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
log.Printf("[DEBUG] Received error: %#v", err)
return err
}
log.Printf("[INFO] Received deployment: %#v", deployment)

err = d.Set("metadata", flattenMetadata(deployment.ObjectMeta))
if err != nil {
return err
}

spec, err := flattenDeploymentSpec(deployment.Spec)
if err != nil {
return err
}

err = d.Set("spec", spec)
if err != nil {
return err
}

return nil
}

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

namespace, name, err := idParts(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Deleting deployment: %#v", name)

err = conn.AppsV1().Deployments(namespace).Delete(name, &metav1.DeleteOptions{})
if err != nil {
return err
}

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

d.SetId("")
return nil
}

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

namespace, name, err := idParts(d.Id())
if err != nil {
return false, err
}

log.Printf("[INFO] Checking deployment %s", name)
_, err = conn.AppsV1().Deployments(namespace).Get(name, metav1.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)
}
return true, err
}

// GetDeploymentConditionInternal returns the condition with the provided type.
// Borrowed from: https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/deployment/util/deployment_util.go#L135
func GetDeploymentCondition(status appsv1.DeploymentStatus, condType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_pod.go
Expand Up @@ -34,7 +34,7 @@ func resourceKubernetesPod() *schema.Resource {
"metadata": namespacedMetadataSchema("pod", true),
"spec": {
Type: schema.TypeList,
Description: "Spec of the pod owned by the cluster",
Description: "Specification of the desired behavior of the pod.",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Expand Down