You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
10 KiB

  1. #!/bin/bash
  2. set -o errexit
  3. set -o pipefail
  4. if [[ ${DEBUG:-false} == "true" ]]; then
  5. set -o xtrace
  6. fi
  7. checksums_file="$(git rev-parse --show-toplevel)/roles/download/defaults/main/checksums.yml"
  8. downloads_folder=/tmp/kubespray_binaries
  9. function get_versions {
  10. local type="$1"
  11. local name="$2"
  12. # NOTE: Limit in the number of versions to be register in the checksums file
  13. local limit="${3:-7}"
  14. local python_app="${4:-"import sys,re;tags=[tag.rstrip() for tag in sys.stdin if re.match(\'^v?(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$\',tag)];print(\' \'.join(tags[:$limit]))"}"
  15. local version=""
  16. local attempt_counter=0
  17. readonly max_attempts=5
  18. until [ "$version" ]; do
  19. version=$("_get_$type" "$name" "$python_app")
  20. if [ "$version" ]; then
  21. break
  22. elif [ ${attempt_counter} -eq ${max_attempts} ]; then
  23. echo "Max attempts reached"
  24. exit 1
  25. fi
  26. attempt_counter=$((attempt_counter + 1))
  27. sleep $((attempt_counter * 2))
  28. done
  29. echo "${version}"
  30. }
  31. function _get_github_tags {
  32. local repo="$1"
  33. local python_app="$2"
  34. # The number of results per page (max 100).
  35. tags="$(curl -s "https://api.github.com/repos/$repo/tags?per_page=100")"
  36. if [ "$tags" ]; then
  37. echo "$tags" | grep -Po '"name":.*?[^\\]",' | awk -F '"' '{print $4}' | python -c "$python_app"
  38. fi
  39. }
  40. function _vercmp {
  41. local v1=$1
  42. local op=$2
  43. local v2=$3
  44. local result
  45. # sort the two numbers with sort's "-V" argument. Based on if v2
  46. # swapped places with v1, we can determine ordering.
  47. result=$(echo -e "$v1\n$v2" | sort -V | head -1)
  48. case $op in
  49. "==")
  50. [ "$v1" = "$v2" ]
  51. return
  52. ;;
  53. ">")
  54. [ "$v1" != "$v2" ] && [ "$result" = "$v2" ]
  55. return
  56. ;;
  57. "<")
  58. [ "$v1" != "$v2" ] && [ "$result" = "$v1" ]
  59. return
  60. ;;
  61. ">=")
  62. [ "$result" = "$v2" ]
  63. return
  64. ;;
  65. "<=")
  66. [ "$result" = "$v1" ]
  67. return
  68. ;;
  69. *)
  70. echo "unrecognised op: $op"
  71. exit 1
  72. ;;
  73. esac
  74. }
  75. function get_checksums {
  76. local binary="$1"
  77. local version_exceptions="cri_dockerd_archive nerdctl_archive containerd_archive"
  78. declare -A skip_archs=(
  79. ["crio_archive"]="arm ppc64le"
  80. ["calicoctl_binary"]="arm"
  81. ["ciliumcli_binary"]="ppc64le"
  82. ["etcd_binary"]="arm"
  83. ["cri_dockerd_archive"]="arm ppc64le"
  84. ["runc"]="arm"
  85. ["crun"]="arm ppc64le"
  86. ["youki"]="arm arm64 ppc64le"
  87. ["kata_containers_binary"]="arm arm64 ppc64le"
  88. ["gvisor_runsc_binary"]="arm ppc64le"
  89. ["gvisor_containerd_shim_binary"]="arm ppc64le"
  90. ["containerd_archive"]="arm"
  91. ["skopeo_binary"]="arm ppc64le"
  92. )
  93. echo "${binary}_checksums:" | tee --append "$checksums_file"
  94. for arch in arm arm64 amd64 ppc64le; do
  95. echo " $arch:" | tee --append "$checksums_file"
  96. for version in "${@:2}"; do
  97. checksum=0
  98. [[ "${skip_archs[$binary]}" == *"$arch"* ]] || checksum=$(_get_checksum "$binary" "$version" "$arch")
  99. [[ "$version_exceptions" != *"$binary"* ]] || version=${version#v}
  100. echo " $version: $checksum" | tee --append "$checksums_file"
  101. done
  102. done
  103. }
  104. function get_krew_archive_checksums {
  105. declare -A archs=(
  106. ["linux"]="arm arm64 amd64"
  107. ["darwin"]="arm64 amd64"
  108. ["windows"]="amd64"
  109. )
  110. echo "krew_archive_checksums:" | tee --append "$checksums_file"
  111. for os in "${!archs[@]}"; do
  112. echo " $os:" | tee --append "$checksums_file"
  113. for arch in arm arm64 amd64 ppc64le; do
  114. echo " $arch:" | tee --append "$checksums_file"
  115. for version in "$@"; do
  116. checksum=0
  117. [[ " ${archs[$os]} " != *" $arch "* ]] || checksum=$(_get_checksum "krew_archive" "$version" "$arch" "$os")
  118. echo " $version: $checksum" | tee --append "$checksums_file"
  119. done
  120. done
  121. done
  122. }
  123. function get_calico_crds_archive_checksums {
  124. echo "calico_crds_archive_checksums:" | tee --append "$checksums_file"
  125. for version in "$@"; do
  126. echo " $version: $(_get_checksum "calico_crds_archive" "$version")" | tee --append "$checksums_file"
  127. done
  128. }
  129. function get_containerd_archive_checksums {
  130. declare -A support_version_history=(
  131. ["arm"]="2"
  132. ["arm64"]="1.6.0"
  133. ["amd64"]="1.5.5"
  134. ["ppc64le"]="1.6.7"
  135. )
  136. echo "containerd_archive_checksums:" | tee --append "$checksums_file"
  137. for arch in arm arm64 amd64 ppc64le; do
  138. echo " $arch:" | tee --append "$checksums_file"
  139. for version in "${@}"; do
  140. _vercmp "${version#v}" '>=' "${support_version_history[$arch]}" && checksum=$(_get_checksum "containerd_archive" "$version" "$arch") || checksum=0
  141. echo " ${version#v}: $checksum" | tee --append "$checksums_file"
  142. done
  143. done
  144. }
  145. function _get_checksum {
  146. local binary="$1"
  147. local version="$2"
  148. local arch="${3:-amd64}"
  149. local os="${4:-linux}"
  150. local target="$downloads_folder/$binary/$version-$os-$arch"
  151. readonly github_url="https://github.com"
  152. readonly github_releases_url="$github_url/%s/releases/download/$version/%s"
  153. readonly github_archive_url="$github_url/%s/archive/%s"
  154. readonly google_url="https://storage.googleapis.com"
  155. readonly k8s_url="$google_url/kubernetes-release/release/$version/bin/$os/$arch/%s"
  156. # Download URLs
  157. declare -A urls=(
  158. ["crictl"]="$(printf "$github_releases_url" "kubernetes-sigs/cri-tools" "crictl-$version-$os-$arch.tar.gz")"
  159. ["crio_archive"]="$(printf "$github_releases_url" "cri-o/cri-o" "cri-o.$arch.$version.tar.gz")"
  160. ["kubelet"]="$(printf "$k8s_url" "kubelet")"
  161. ["kubectl"]="$(printf "$k8s_url" "kubectl")"
  162. ["kubeadm"]="$(printf "$k8s_url" "kubeadm")"
  163. ["etcd_binary"]="$(printf "$github_releases_url" "etcd-io/etcd" "etcd-$version-$os-$arch.tar.gz")"
  164. ["cni_binary"]="$(printf "$github_releases_url" "containernetworking/plugins" "cni-plugins-$os-$arch-$version.tgz")"
  165. ["calicoctl_binary"]="$(printf "$github_releases_url" "projectcalico/calico" "calicoctl-$os-$arch")"
  166. ["ciliumcli_binary"]="$(printf "$github_releases_url" "cilium/cilium-cli" "cilium-$os-$arch.tar.gz")"
  167. ["calico_crds_archive"]="$(printf "$github_archive_url" "projectcalico/calico" "$version.tar.gz")"
  168. ["krew_archive"]="$(printf "$github_releases_url" "kubernetes-sigs/krew" "krew-${os}_$arch.tar.gz")"
  169. ["helm_archive"]="https://get.helm.sh/helm-$version-$os-$arch.tar.gz"
  170. ["cri_dockerd_archive"]="$(printf "$github_releases_url" "Mirantis/cri-dockerd" "cri-dockerd-${version#v}.$arch.tgz")"
  171. ["runc"]="$(printf "$github_releases_url" "opencontainers/runc" "runc.$arch")"
  172. ["crun"]="$(printf "$github_releases_url" "containers/crun" "crun-$version-$os-$arch")"
  173. ["youki"]="$(printf "$github_releases_url" "containers/youki" "youki_$([ $version == "v0.0.1" ] && echo "v0_0_1" || echo "${version#v}" | sed 's|\.|_|g')_$os.tar.gz")"
  174. ["kata_containers_binary"]="$(printf "$github_releases_url" "kata-containers/kata-containers" "kata-static-$version-${arch//amd64/x86_64}.tar.xz")"
  175. ["gvisor_runsc_binary"]="$(printf "$google_url/gvisor/releases/release/$version/%s/runsc" "$(echo "$arch" | sed -e 's/amd64/x86_64/' -e 's/arm64/aarch64/')")"
  176. ["gvisor_containerd_shim_binary"]="$(printf "$google_url/gvisor/releases/release/$version/%s/containerd-shim-runsc-v1" "$(echo "$arch" | sed -e 's/amd64/x86_64/' -e 's/arm64/aarch64/')")"
  177. ["nerdctl_archive"]="$(printf "$github_releases_url" "containerd/nerdctl" "nerdctl-${version#v}-$os-$([ "$arch" == "arm" ] && echo "arm-v7" || echo "$arch" ).tar.gz")"
  178. ["containerd_archive"]="$(printf "$github_releases_url" "containerd/containerd" "containerd-${version#v}-$os-$arch.tar.gz")"
  179. ["skopeo_binary"]="$(printf "$github_releases_url" "lework/skopeo-binary" "skopeo-$os-$arch")"
  180. ["yq"]="$(printf "$github_releases_url" "mikefarah/yq" "yq_${os}_$arch")"
  181. )
  182. mkdir -p "$(dirname $target)"
  183. [ -f "$target" ] || curl -LfSs -o "${target}" "${urls[$binary]}"
  184. sha256sum ${target} | awk '{print $1}'
  185. }
  186. function main {
  187. mkdir -p "$(dirname "$checksums_file")"
  188. echo "---" | tee "$checksums_file"
  189. get_checksums crictl $(get_versions github_tags kubernetes-sigs/cri-tools)
  190. get_checksums crio_archive $(get_versions github_tags cri-o/cri-o)
  191. kubernetes_versions=$(get_versions github_tags kubernetes/kubernetes 20)
  192. echo "# Checksum" | tee --append "$checksums_file"
  193. echo "# Kubernetes versions above Kubespray's current target version are untested and should be used with caution." | tee --append "$checksums_file"
  194. get_checksums kubelet $kubernetes_versions
  195. get_checksums kubectl $kubernetes_versions
  196. get_checksums kubeadm $kubernetes_versions
  197. get_checksums etcd_binary $(get_versions github_tags etcd-io/etcd)
  198. get_checksums cni_binary $(get_versions github_tags containernetworking/plugins)
  199. calico_versions=$(get_versions github_tags projectcalico/calico 20)
  200. get_checksums calicoctl_binary $calico_versions
  201. get_checksums ciliumcli_binary $(get_versions github_tags cilium/cilium-cli 10)
  202. get_calico_crds_archive_checksums $calico_versions
  203. get_krew_archive_checksums $(get_versions github_tags kubernetes-sigs/krew 2)
  204. get_checksums helm_archive $(get_versions github_tags helm/helm)
  205. get_checksums cri_dockerd_archive $(get_versions github_tags Mirantis/cri-dockerd)
  206. get_checksums runc $(get_versions github_tags opencontainers/runc 5)
  207. get_checksums crun $(get_versions github_tags containers/crun)
  208. get_checksums youki $(get_versions github_tags containers/youki)
  209. get_checksums kata_containers_binary $(get_versions github_tags kata-containers/kata-containers 10)
  210. gvisor_versions=$(get_versions github_tags google/gvisor 0 "import sys,re;tags=[tag[8:16] for tag in sys.stdin if re.match('^release-?(0|[1-9]\d*)\.(0|[1-9]\d*)$',tag)];print(' '.join(tags[:9]))")
  211. get_checksums gvisor_runsc_binary $gvisor_versions
  212. get_checksums gvisor_containerd_shim_binary $gvisor_versions
  213. get_checksums nerdctl_archive $(get_versions github_tags containerd/nerdctl)
  214. get_containerd_archive_checksums $(get_versions github_tags containerd/containerd 30)
  215. get_checksums skopeo_binary $(get_versions github_tags lework/skopeo-binary)
  216. get_checksums yq $(get_versions github_tags mikefarah/yq)
  217. }
  218. if [[ ${__name__:-"__main__"} == "__main__" ]]; then
  219. main
  220. fi