Created
January 11, 2019 18:54
-
-
Save ukayani/87a8b6f81a4feae7db9647bad0395ab1 to your computer and use it in GitHub Desktop.
Kubernetes Rollout Status - Terraform External Data Source
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
resource "local_file" "kube_config" { | |
content = "${var.kube_config}" | |
filename = "${path.module}/kubeconfig" | |
} | |
# Hacky way to force a dependency on some external resource (since modules dont support a depends_on) | |
data "template_file" "deps" { | |
template = "${join(",", var.depends_on)}" | |
} | |
data "external" "ds" { | |
depends_on = ["local_file.kube_config", "data.template_file.deps"] | |
program = ["bash", "${path.module}/rollout-status.sh"] | |
query { | |
name = "${var.name}" | |
type = "${var.type}" | |
timeout = "${var.timeout}" | |
kubeconfig = "${path.module}/kubeconfig" | |
namespace = "${var.namespace}" | |
} | |
} |
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
output "status" { | |
value = "${data.external.ds.result["status"]}" | |
description = "Result of the rollout status check" | |
} |
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
#!/bin/bash | |
# Exit if any of the intermediate steps fail | |
set -e | |
# Extract args to script | |
eval "$(jq -r '@sh "NAMESPACE=\(.namespace) TYPE=\(.type) NAME=\(.name) TIMEOUT=\(.timeout) KUBE_CONFIG=\(.kubeconfig)"')" | |
kubectl rollout status ${TYPE} ${NAME} -n ${NAMESPACE} --timeout=${TIMEOUT} --kubeconfig ${KUBE_CONFIG} &> /dev/null | |
# Safely produce a JSON object containing the result value. | |
# jq will ensure that the value is properly quoted | |
# and escaped to produce a valid JSON string. | |
jq -n --arg status "true" '{"status":$status}' |
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
variable "namespace" { | |
description = "Kubernetes namespace" | |
default = "default" | |
} | |
variable "type" { | |
description = "The type of the resource" | |
default = "deployment" | |
} | |
variable "timeout" { | |
description = "The amount of time to wait for the completion, before timing out." | |
default = "1m" | |
} | |
variable "kube_config" { | |
description = "The path of the kubeconfig" | |
} | |
variable "name" { | |
description = "Name of the resource" | |
} | |
variable "depends_on" { | |
description = "List of dependencies. To create explicit dependency on external resources." | |
default = [] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment