Skip to content

Instantly share code, notes, and snippets.

@mossad-zika
Created January 21, 2024 16:39
Show Gist options
  • Save mossad-zika/3bbcf649d44ffd55733259a9e5dd4f0d to your computer and use it in GitHub Desktop.
Save mossad-zika/3bbcf649d44ffd55733259a9e5dd4f0d to your computer and use it in GitHub Desktop.
Return a warning if version of kubectl is not compatible with the version of k8s server.
import subprocess
import yaml
def check_kubectl_compatibility_yaml():
"""Checks compatibility between kubectl client and server versions using YAML output."""
version_output = subprocess.check_output(["kubectl", "version", "--output=yaml"]).decode("utf-8")
version_data = yaml.safe_load(version_output)
kubectl_client_version_raw = version_data["clientVersion"]["gitVersion"]
server_version_raw = version_data["serverVersion"]["gitVersion"]
def convert_version_to_matrix_format(version):
parts = version.lstrip('v').split('.')
return '.'.join(parts[:2]) + '.x' # Returns format like '1.27.x'
kubectl_client_version_converted = convert_version_to_matrix_format(kubectl_client_version_raw)
server_version_converted = convert_version_to_matrix_format(server_version_raw)
# Compatibility matrix (adjust based on official documentation)
compatibility_matrix = {
"1.27.x": ["1.26.x", "1.27.x", "1.28.x"],
"1.26.x": ["1.25.x", "1.26.x", "1.27.x"],
"1.25.x": ["1.24.x", "1.25.x", "1.26.x"],
# ... Add more entries as needed
}
compatible = kubectl_client_version_converted in compatibility_matrix.get(server_version_converted, [])
if not compatible:
print("WARNING: Kubectl version", kubectl_client_version_raw, "is not fully compatible with server version",
server_version_raw)
print("Recommended client versions for server",
server_version_raw + ":", compatibility_matrix.get(server_version_converted))
if __name__ == "__main__":
check_kubectl_compatibility_yaml()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment