2020-05-27 21:52:40 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# After a new version of Kubernetes has been released,
|
2023-12-12 20:47:27 +08:00
|
|
|
# run this script to update roles/kubespray-defaults/defaults/main/download.yml
|
2020-05-27 21:52:40 +08:00
|
|
|
# with new hashes.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2024-01-30 10:06:10 +08:00
|
|
|
from itertools import count
|
|
|
|
from collections import defaultdict
|
2020-05-27 21:52:40 +08:00
|
|
|
import requests
|
|
|
|
from ruamel.yaml import YAML
|
2024-01-30 10:06:10 +08:00
|
|
|
from packaging.version import Version
|
2020-05-27 21:52:40 +08:00
|
|
|
|
2024-01-23 23:41:20 +08:00
|
|
|
CHECKSUMS_YML = "../roles/kubespray-defaults/defaults/main/checksums.yml"
|
2020-05-27 21:52:40 +08:00
|
|
|
|
2024-01-23 23:41:20 +08:00
|
|
|
def open_checksums_yaml():
|
2020-05-27 21:52:40 +08:00
|
|
|
yaml = YAML()
|
|
|
|
yaml.explicit_start = True
|
|
|
|
yaml.preserve_quotes = True
|
|
|
|
yaml.width = 4096
|
|
|
|
|
2024-01-23 23:41:20 +08:00
|
|
|
with open(CHECKSUMS_YML, "r") as checksums_yml:
|
|
|
|
data = yaml.load(checksums_yml)
|
2020-05-27 21:52:40 +08:00
|
|
|
|
|
|
|
return data, yaml
|
|
|
|
|
|
|
|
|
2024-01-30 10:06:10 +08:00
|
|
|
def download_hash(minors):
|
2022-02-04 16:14:00 +08:00
|
|
|
architectures = ["arm", "arm64", "amd64", "ppc64le"]
|
2020-05-27 21:52:40 +08:00
|
|
|
downloads = ["kubelet", "kubectl", "kubeadm"]
|
|
|
|
|
2024-01-23 23:41:20 +08:00
|
|
|
data, yaml = open_checksums_yaml()
|
2024-01-30 10:06:10 +08:00
|
|
|
if not minors:
|
|
|
|
minors = {'.'.join(minor.split('.')[:-1]) for minor in data["kubelet_checksums"]["amd64"].keys()}
|
2020-05-27 21:52:40 +08:00
|
|
|
|
|
|
|
for download in downloads:
|
|
|
|
checksum_name = f"{download}_checksums"
|
2024-01-30 10:06:10 +08:00
|
|
|
data[checksum_name] = defaultdict(dict, data[checksum_name])
|
2020-05-27 21:52:40 +08:00
|
|
|
for arch in architectures:
|
2024-01-30 10:06:10 +08:00
|
|
|
for minor in minors:
|
|
|
|
if not minor.startswith("v"):
|
|
|
|
minor = f"v{minor}"
|
|
|
|
for release in (f"{minor}.{patch}" for patch in count(start=0, step=1)):
|
|
|
|
if release in data[checksum_name][arch]:
|
|
|
|
continue
|
|
|
|
hash_file = requests.get(f"https://dl.k8s.io/release/{release}/bin/linux/{arch}/{download}.sha256", allow_redirects=True)
|
|
|
|
if hash_file.status_code == 404:
|
|
|
|
print(f"Unable to find {download} hash file for release {release} (arch: {arch})")
|
|
|
|
break
|
|
|
|
hash_file.raise_for_status()
|
|
|
|
sha256sum = hash_file.content.decode().strip()
|
|
|
|
if len(sha256sum) != 64:
|
|
|
|
raise Exception(f"Checksum has an unexpected length: {len(sha256sum)} (binary: {download}, arch: {arch}, release: 1.{minor}.{patch})")
|
|
|
|
data[checksum_name][arch][release] = sha256sum
|
|
|
|
data[checksum_name] = {arch : {r : releases[r] for r in sorted(releases.keys(),
|
|
|
|
key=lambda v : Version(v[1:]),
|
|
|
|
reverse=True)}
|
|
|
|
for arch, releases in data[checksum_name].items()}
|
2020-05-27 21:52:40 +08:00
|
|
|
|
2024-01-23 23:41:20 +08:00
|
|
|
with open(CHECKSUMS_YML, "w") as checksums_yml:
|
|
|
|
yaml.dump(data, checksums_yml)
|
|
|
|
print(f"\n\nUpdated {CHECKSUMS_YML}\n")
|
2020-05-27 21:52:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
def usage():
|
|
|
|
print(f"USAGE:\n {sys.argv[0]} [k8s_version1] [[k8s_version2]....[k8s_versionN]]")
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv=None):
|
2024-01-30 10:06:10 +08:00
|
|
|
download_hash(sys.argv[1:])
|
2021-08-19 21:51:24 +08:00
|
|
|
return 0
|
2020-05-27 21:52:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|