|
| 1 | +# Copyright 2023 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +import datetime |
| 17 | +import re |
| 18 | +from collections import defaultdict |
| 19 | +import subprocess |
| 20 | +import shutil |
| 21 | +from dateutil.relativedelta import relativedelta |
| 22 | + |
| 23 | +def check_gh_command(): |
| 24 | + """ |
| 25 | + Pretty much everything is processed from `gh` |
| 26 | + Check that the `gh` command is in the path before anything else |
| 27 | + """ |
| 28 | + if not shutil.which('gh'): |
| 29 | + print("Error: The `gh` command is not available in the PATH.") |
| 30 | + print("Please install the GitHub CLI (https://cli.github.com/) and try again.") |
| 31 | + exit(1) |
| 32 | + |
| 33 | +def duration_ago(dt): |
| 34 | + """ |
| 35 | + Humanize duration ouputs |
| 36 | + """ |
| 37 | + delta = relativedelta(datetime.datetime.now(), dt) |
| 38 | + if delta.years > 0: |
| 39 | + return f"{delta.years} year{'s' if delta.years > 1 else ''} ago" |
| 40 | + elif delta.months > 0: |
| 41 | + return f"{delta.months} month{'s' if delta.months > 1 else ''} ago" |
| 42 | + elif delta.days > 0: |
| 43 | + return f"{delta.days} day{'s' if delta.days > 1 else ''} ago" |
| 44 | + elif delta.hours > 0: |
| 45 | + return f"{delta.hours} hour{'s' if delta.hours > 1 else ''} ago" |
| 46 | + elif delta.minutes > 0: |
| 47 | + return f"{delta.minutes} minute{'s' if delta.minutes > 1 else ''} ago" |
| 48 | + else: |
| 49 | + return "just now" |
| 50 | + |
| 51 | +def parse_version(version): |
| 52 | + """ |
| 53 | + Parse version assuming it is in the form of v1.2.3 |
| 54 | + """ |
| 55 | + pattern = r"v(\d+)\.(\d+)\.(\d+)" |
| 56 | + match = re.match(pattern, version) |
| 57 | + if match: |
| 58 | + major, minor, patch = map(int, match.groups()) |
| 59 | + return (major, minor, patch) |
| 60 | + |
| 61 | +def end_of_life_grouped_versions(versions): |
| 62 | + """ |
| 63 | + Calculate the end of life date for a minor release version according to : https://kubernetes-csi.github.io/docs/project-policies.html#support |
| 64 | +
|
| 65 | + The input is an array of tuples of: |
| 66 | + * grouped versions (e.g. 1.0, 1.1) |
| 67 | + * array of that contains all versions and their release date (e.g. 1.0.0, 01-01-2013) |
| 68 | +
|
| 69 | + versions structure example : |
| 70 | + [((3, 5), [('v3.5.0', datetime.datetime(2023, 4, 27, 22, 28, 6))]), |
| 71 | + ((3, 4), |
| 72 | + [('v3.4.1', datetime.datetime(2023, 4, 5, 17, 41, 15)), |
| 73 | + ('v3.4.0', datetime.datetime(2022, 12, 27, 23, 43, 41))])] |
| 74 | + """ |
| 75 | + supported_versions = [] |
| 76 | + # Prepare dates for later calculation |
| 77 | + now = datetime.datetime.now() |
| 78 | + one_year = datetime.timedelta(days=365) |
| 79 | + three_months = datetime.timedelta(days=90) |
| 80 | + |
| 81 | + # get the newer versions on top |
| 82 | + sorted_versions_list = sorted(versions.items(), key=lambda x: x[0], reverse=True) |
| 83 | + |
| 84 | + # the latest version is always supported no matter the release date |
| 85 | + latest = sorted_versions_list.pop(0) |
| 86 | + supported_versions.append(latest[1][-1]) |
| 87 | + |
| 88 | + for v in sorted_versions_list: |
| 89 | + first_release = v[1][-1] |
| 90 | + last_release = v[1][0] |
| 91 | + # if the release is less than a year old we support the latest patch version |
| 92 | + if now - first_release[1] < one_year: |
| 93 | + supported_versions.append(last_release) |
| 94 | + # if the main release is older than a year and has a recent path, this is supported |
| 95 | + elif now - last_release[1] < three_months: |
| 96 | + supported_versions.append(last_release) |
| 97 | + return supported_versions |
| 98 | + |
| 99 | +def get_release_docker_image(repo, version): |
| 100 | + """ |
| 101 | + Extract docker image name from the relase page documentation |
| 102 | + """ |
| 103 | + output = subprocess.check_output(['gh', 'release', '-R', repo, 'view', version], text=True) |
| 104 | + #Extract matching image name excluding ` |
| 105 | + match = re.search(r"docker pull ([\.\/\-\:\w\d]*)", output) |
| 106 | + docker_image = match.group(1) if match else '' |
| 107 | + return((version, docker_image)) |
| 108 | + |
| 109 | +def get_versions_from_releases(repo): |
| 110 | + """ |
| 111 | + Using `gh` cli get the github releases page details then |
| 112 | + create a list of grouped version on major.minor |
| 113 | + and for each give all major.minor.patch with release dates |
| 114 | + """ |
| 115 | + # Run the `gh release` command to get the release list |
| 116 | + output = subprocess.check_output(['gh', 'release', '-R', repo, 'list'], text=True) |
| 117 | + # Parse the output and group by major and minor version numbers |
| 118 | + versions = defaultdict(lambda: []) |
| 119 | + for line in output.strip().split('\n'): |
| 120 | + parts = line.split('\t') |
| 121 | + # pprint.pprint(parts) |
| 122 | + version = parts[0] |
| 123 | + parsed_version = parse_version(version) |
| 124 | + if parsed_version is None: |
| 125 | + continue |
| 126 | + major, minor, patch = parsed_version |
| 127 | + |
| 128 | + published = datetime.datetime.strptime(parts[3], '%Y-%m-%dT%H:%M:%SZ') |
| 129 | + versions[(major, minor)].append((version, published)) |
| 130 | + return(versions) |
| 131 | + |
| 132 | + |
| 133 | +def main(): |
| 134 | + manual = """ |
| 135 | + This script lists the supported versions Github releases according to https://kubernetes-csi.github.io/docs/project-policies.html#support |
| 136 | + It has been designed to help to update the tables from : https://kubernetes-csi.github.io/docs/sidecar-containers.html\n\n |
| 137 | + It can take multiple repos as argument, for all CSI sidecars details you can run: |
| 138 | + ./get_supported_version_csi-sidecar.py -R kubernetes-csi/external-attacher -R kubernetes-csi/external-provisioner -R kubernetes-csi/external-resizer -R kubernetes-csi/external-snapshotter -R kubernetes-csi/livenessprobe -R kubernetes-csi/node-driver-registrar -R kubernetes-csi/external-health-monitor\n |
| 139 | + With the output you can then update the documentation manually. |
| 140 | + """ |
| 141 | + parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=manual) |
| 142 | + parser.add_argument('--repo', '-R', required=True, action='append', dest='repos', help='The name of the repository in the format owner/repo.') |
| 143 | + parser.add_argument('--display', '-d', action='store_true', help='(default) Display EOL versions with their dates', default=True) |
| 144 | + parser.add_argument('--doc', '-D', action='store_true', help='Helper to https://kubernetes-csi.github.io/docs/ that prints Docker image for each EOL version') |
| 145 | + |
| 146 | + args = parser.parse_args() |
| 147 | + |
| 148 | + # Verify pre-reqs |
| 149 | + check_gh_command() |
| 150 | + |
| 151 | + # Process all repos |
| 152 | + for repo in args.repos: |
| 153 | + versions = get_versions_from_releases(repo) |
| 154 | + eol_versions = end_of_life_grouped_versions(versions) |
| 155 | + |
| 156 | + if args.display: |
| 157 | + print(f"Supported versions with release date and age of `{repo}`:\n") |
| 158 | + for version in eol_versions: |
| 159 | + print(f"{version[0]}\t{version[1].strftime('%Y-%m-%d')}\t{duration_ago(version[1])}") |
| 160 | + |
| 161 | + # TODO : generate proper doc output for the tables of: https://kubernetes-csi.github.io/docs/sidecar-containers.html |
| 162 | + if args.doc: |
| 163 | + print("\nSupported Versions with docker images for each end of life version:\n") |
| 164 | + for version in eol_versions: |
| 165 | + _, image = get_release_docker_image(repo, version[0]) |
| 166 | + print(f"{version[0]}\t{image}") |
| 167 | + print() |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + main() |
0 commit comments