Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
kubernetes rollout status external data source
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}"
}
}
output "status" {
value = "${data.external.ds.result["status"]}"
description = "Result of the rollout status check"
}
#!/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}'
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