update/patch-scripts/kube-upgrade/KUBE.1.preapply
Bart Wensley 6d1cd07e39 Update scripts that build k8s upgrade patches - part 2
Updating scripts used to build k8s upgrade patches to handle
current kubernetes version (1.18.1). My previous commit
missed changes to the preapply/preremove scripts.

Change-Id: I70b2796f625ac79df1de08e4e802d70e3e33d36a
Story: 2006781
Task: 38479
Signed-off-by: Bart Wensley <barton.wensley@windriver.com>
2020-04-21 12:41:38 -05:00

57 lines
1.3 KiB
Python

#!/usr/bin/python
"""
Copyright (c) 2020 Wind River Systems, Inc.
SPDX-License-Identifier: Apache-2.0
"""
from kubernetes import client
from kubernetes import config
from distutils.version import LooseVersion
import sys
patch_states = {}
def fatal_error(message):
print(message)
sys.exit(1)
def get_kube_apiserver_versions():
config.load_kube_config('/etc/kubernetes/admin.conf')
versions = []
v1 = client.CoreV1Api()
ret = v1.list_pod_for_all_namespaces(watch=False)
for item in ret.items:
if item.metadata.labels.get('component') == 'kube-apiserver':
for c in item.spec.containers:
if c.name == 'kube-apiserver':
versions.append(c.image.split(':')[-1])
return versions
def main():
running_versions = get_kube_apiserver_versions()
if len(running_versions) == 0:
fatal_error("Could not determine kube-apiserver version")
# Running version must be at least v1.18.1
test_version = 'v1.18.1'
for running_version in running_versions:
if LooseVersion(running_version) < LooseVersion(test_version):
fatal_error("A kube-apiserver is running %s, but must be"
" at least %s" % (running_version, test_version))
# Success
sys.exit(0)
if __name__ == '__main__':
main()