Skip to content

Commit

Permalink
fixup! Add test for k8s version recency (#288)
Browse files Browse the repository at this point in the history
Signed-off-by: Hannes Baum <[email protected]>
  • Loading branch information
cah-hbaum committed Jun 21, 2023
1 parent b2ee5a7 commit 5945194
Showing 1 changed file with 60 additions and 31 deletions.
91 changes: 60 additions & 31 deletions Tests/kaas/k8s-version-recency/k8s-version-recency-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,27 @@ def __init__(self):
self.patch = 0

self.date = None
self.tag = None

def extract_version(self, string, separator="."):
v_dict = string.strip().split(separator)
self.major = v_dict[0]
self.minor = v_dict[1]
self.patch = v_dict[2]
self.major = int(v_dict[0])
self.minor = int(v_dict[1])
self.patch = int(v_dict[2])

def print(self):
print(f"{self.major}.{self.minor}.{self.patch}")


def diff_months(date1, date2):
return (date1.year - date2.year) * 12 + date1.month - date2.month
return abs((date1.year - date2.year) * 12 + date1.month - date2.month)


def diff_weeks(date1, date2):
day1 = (date1 - datetime.timedelta(days=date1.weekday()))
day2 = (date2 - datetime.timedelta(days=date2.weekday()))
diff = day2 - day1
return (diff.days / 7) + math.ceil(diff.seconds / 86400)
return abs((diff.days / 7) + math.ceil(diff.seconds / 86400))


async def get_k8s_cluster_version(kubeconfig):
Expand All @@ -69,30 +70,63 @@ async def get_k8s_cluster_version(kubeconfig):
ret = await vapi.get_code()

version = K8sVersionInfo()
version.major = ret.major
version.minor = ret.minor
version.patch = ret.git_version.split(".")[-1]
version.major = int(ret.major)
version.minor = int(ret.minor)
version.patch = int(ret.git_version.split(".")[-1])
version.date = datetime.datetime.strptime(ret.build_date, '%Y-%m-%dT%H:%M:%SZ')

version.print()
return version


def get_k8s_version(token):
def get_k8s_versions(token):

k8s_versions = []
msmr = 0

headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28"
}
resp = requests.get("https://api.github.com/repos/kubernetes/kubernetes/releases", headers=headers).json()

version = K8sVersionInfo()
version.extract_version(resp[0]['tag_name'].strip("v"))
version.date = datetime.datetime.strptime(resp[0]['published_at'], '%Y-%m-%dT%H:%M:%SZ')

version.print()
return version
resp = requests.get("https://api.github.com/repos/kubernetes/kubernetes/releases/latest", headers=headers).json()

lv = K8sVersionInfo()
lv.extract_version(resp['tag_name'].strip("v"))
lv.tag = "latest"
lv.date = datetime.datetime.strptime(resp['published_at'], '%Y-%m-%dT%H:%M:%SZ')

resp = requests.get("https://api.github.com/repos/kubernetes/kubernetes/releases?per_page=100", headers=headers).json()

for i in range(0, len(resp)):
v = K8sVersionInfo()
v.extract_version(resp[i]['tag_name'].split("-")[0].strip("v"))
v.date = datetime.datetime.strptime(resp[i]['published_at'], '%Y-%m-%dT%H:%M:%SZ')
if resp[i]['draft'] is False and resp[i]['prerelease'] is False:
k8s_versions.append(v)
if v.major == lv.major and v.minor == lv.minor and v.patch == "0":
msmr = diff_months(v.date, datetime.datetime.now())
break

for i in range(0, len(k8s_versions)):
v = k8s_versions[i]
if msmr >= 4 and v.minor != lv.minor:
v = None
else:
dm = diff_months(v.date, datetime.datetime.now())
if dm >= 4:
v = None
else:
for kv in k8s_versions:
if kv is not None:
dw = diff_weeks(datetime.datetime.now(), kv.date)
if kv is not None and v.major == kv.major and v.minor == kv.minor and v.patch < kv.patch and \
dw >= 1:
v = None
break
k8s_versions[i] = v

return [v for v in k8s_versions if v is not None]


async def main(argv):
Expand All @@ -115,21 +149,16 @@ async def main(argv):
if opt[0] == "-k" or opt[0] == "--kubeconfig":
kubeconfig = opt[1]

k8s_versions = get_k8s_versions(token)
cluster_version = await get_k8s_cluster_version(kubeconfig)
k8s_version = get_k8s_version(token)
if cluster_version.major != k8s_version.major:
return_code = 1
return
if cluster_version.minor != k8s_version.minor:
dm = diff_months(k8s_version.date, datetime.datetime.now())
if dm > MINOR_VERSION_CADENCE:
return_code = 1
return
if cluster_version.patch != k8s_version.patch:
dw = diff_weeks(k8s_version.date, datetime.datetime.now())
if dw > PATCH_VERSION_CADENCE:
return_code = 1
return

for k8sv in k8s_versions:
if k8sv.major == cluster_version.major and \
k8sv.minor == cluster_version.minor and \
k8sv.patch == cluster_version.patch:
return 0
return 1


if __name__ == "__main__":
loop = asyncio.get_event_loop()
Expand Down

0 comments on commit 5945194

Please sign in to comment.