Last active
March 30, 2021 11:32
-
-
Save davidlukac/7a70d5a9fe64f989d9193827f44a6eff to your computer and use it in GitHub Desktop.
Get Kubernetes cluster version from the cluster using kubectl CLI or kubernetes Terraform provider
This file contains hidden or 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
#!/usr/bin/env bash | |
# Prerequsites: | |
# - have kubectl installed | |
# - be logged in via kubectl | |
# - have enough permissions to access kubeadm-config Config Map | |
# - have jq installed (https://stedolan.github.io/jq/) | |
# - have yq installed (https://mikefarah.gitbook.io/yq/) | |
kubectl get configmap kubeadm-config -n kube-system -o json | \ | |
jq .data.ClusterConfiguration | \ | |
tail -c +2 | \ | |
head -c -2 | \ | |
sed 's/\\n/\n/g' | \ | |
sed 's/\\"/"/g' | \ | |
yq .kubernetesVersion |
This file contains hidden or 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
# Prerequisites: | |
# - have kubernetes provider installed and initiqlized | |
# - have kubernetes provider logged into K8s cluster with enough credentials to access the kubeadm-config Config Map | |
data "kubernetes_config_map" "kubeadm-config-kube-system" { | |
metadata { | |
name = "kubeadm-config" | |
namespace = "kube-system" | |
} | |
} | |
locals { | |
k8s-version-raw = data.kubernetes_config_map.kubeadm-config-kube-system.data["ClusterConfiguration"] | |
k8s-version-str = yamldecode(local.k8s-version-raw)["kubernetesVersion"] | |
k8s-version-trimmed = trimprefix(local.k8s-version-str, "v") | |
k8s-version-parts = split(".", local.k8s-version-trimmed) | |
k8s-version-major = local.k8s-version-parts[0] | |
k8s-version-minor = local.k8s-version-parts[1] | |
k8s-version-patch = local.k8s-version-parts[2] | |
} | |
output "k8s-version" { | |
value = { | |
text = local.k8s-version-str | |
full = "${local.k8s-version-major}.${local.k8s-version-minor}.${local.k8s-version-patch}" | |
short = "${local.k8s-version-major}.${local.k8s-version-minor}" | |
parts = local.k8s-version-parts | |
major = local.k8s-version-major | |
minor = local.k8s-version-minor | |
patch = local.k8s-version-patch | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment