From 56c7ca99b40a8ef30172f1f71bd8046ed34b9845 Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Wed, 9 Nov 2022 14:33:38 +0000 Subject: [PATCH] feat: add tool to load image repository --- hack/stack.sh | 4 ++ magnum_cluster_api/cmd/image_loader.py | 72 ++++++++++++++++++++++++++ magnum_cluster_api/utils.py | 30 +++++++++++ pyproject.toml | 1 + 4 files changed, 107 insertions(+) create mode 100644 magnum_cluster_api/cmd/image_loader.py diff --git a/hack/stack.sh b/hack/stack.sh index ce955117..28146f7e 100755 --- a/hack/stack.sh +++ b/hack/stack.sh @@ -81,6 +81,10 @@ clusterctl init --infrastructure openstack # TODO: find a way to run this from `clusterctl` ? kubectl apply -f https://storage.googleapis.com/artifacts.k8s-staging-capi-openstack.appspot.com/components/nightly_main_20221108/infrastructure-components.yaml +# Install Skopeo +sudo curl -Lo /usr/local/bin/skopeo https://github.com/lework/skopeo-binary/releases/download/v1.10.0/skopeo-linux-amd64 +sudo chmod +x /usr/local/bin/skopeo + # Install `magnum-cluster-api` pip install -U setuptools pip $HOME/.local/bin/pip3 install -e . diff --git a/magnum_cluster_api/cmd/image_loader.py b/magnum_cluster_api/cmd/image_loader.py new file mode 100644 index 00000000..2b4f2074 --- /dev/null +++ b/magnum_cluster_api/cmd/image_loader.py @@ -0,0 +1,72 @@ +import subprocess + +import click + +from magnum_cluster_api import utils + +IMAGES = [ + "docker.io/calico/cni:v3.24.2", + "docker.io/calico/kube-controllers:v3.24.2", + "docker.io/calico/node:v3.24.2", + "docker.io/k8scloudprovider/cinder-csi-plugin:v1.25.3", + "docker.io/k8scloudprovider/openstack-cloud-controller-manager:v1.25.3", + "k8s.gcr.io/coredns/coredns:v1.8.6", + "k8s.gcr.io/coredns/coredns:v1.9.3", + "k8s.gcr.io/etcd:3.5.1-0", + "k8s.gcr.io/etcd:3.5.3-0", + "k8s.gcr.io/etcd:3.5.3-0", + "k8s.gcr.io/etcd:3.5.4-0", + "k8s.gcr.io/kube-apiserver:v1.23.13", + "k8s.gcr.io/kube-apiserver:v1.24.7", + "k8s.gcr.io/kube-apiserver:v1.24.7", + "k8s.gcr.io/kube-apiserver:v1.25.3", + "k8s.gcr.io/kube-controller-manager:v1.23.13", + "k8s.gcr.io/kube-controller-manager:v1.24.7", + "k8s.gcr.io/kube-controller-manager:v1.24.7", + "k8s.gcr.io/kube-controller-manager:v1.25.3", + "k8s.gcr.io/kube-proxy:v1.23.13", + "k8s.gcr.io/kube-proxy:v1.24.7", + "k8s.gcr.io/kube-proxy:v1.24.7", + "k8s.gcr.io/kube-proxy:v1.25.3", + "k8s.gcr.io/kube-scheduler:v1.23.13", + "k8s.gcr.io/kube-scheduler:v1.24.7", + "k8s.gcr.io/kube-scheduler:v1.24.7", + "k8s.gcr.io/kube-scheduler:v1.25.3", + "k8s.gcr.io/pause:3.6", + "k8s.gcr.io/pause:3.7", + "k8s.gcr.io/pause:3.8", + "k8s.gcr.io/sig-storage/csi-attacher:v3.4.0", + "k8s.gcr.io/sig-storage/csi-node-driver-registrar:v2.5.1", + "k8s.gcr.io/sig-storage/csi-provisioner:v3.1.0", + "k8s.gcr.io/sig-storage/csi-resizer:v1.4.0", + "k8s.gcr.io/sig-storage/csi-snapshotter:v6.0.1", + "k8s.gcr.io/sig-storage/livenessprobe:v2.7.0", +] + + +@click.command() +@click.option( + "--repository", + show_default=True, + default="quay.io/vexxhost", + help="Target image repository", +) +def main(repository): + """ + Load images into a remote registry for `container_infra_prefix` usage. + """ + + for image in IMAGES: + skoepo( + "copy", + "--multi-arch", + "all", + f"docker://{image}", + f"docker://{utils.get_image(image, repository)}", + ) + + +def skoepo(*args): + cmd = ["skopeo", "--insecure-policy"] + list(args) + click.echo(" ".join(cmd)) + subprocess.run(cmd, check=True) diff --git a/magnum_cluster_api/utils.py b/magnum_cluster_api/utils.py index 32f65957..52f9a507 100644 --- a/magnum_cluster_api/utils.py +++ b/magnum_cluster_api/utils.py @@ -114,3 +114,33 @@ def get_cluster_label_as_bool( ) -> bool: value = get_cluster_label(cluster, key, default) return strutils.bool_from_string(value, strict=True) + + +def get_image(name: str, repository: str = None): + """ + Get the image name from the target registry given a full image name. + """ + + if repository is None: + return repository + + new_image_name = name + if name.startswith("docker.io/calico"): + new_image_name = name.replace("docker.io/calico/", f"{repository}/calico-") + if name.startswith("docker.io/k8scloudprovider"): + new_image_name = name.replace("docker.io/k8scloudprovider", repository) + if name.startswith("k8s.gcr.io/sig-storage"): + new_image_name = name.replace("k8s.gcr.io/sig-storage", repository) + if new_image_name.startswith(f"{repository}/livenessprobe"): + return new_image_name.replace("livenessprobe", "csi-livenessprobe") + if new_image_name.startswith("k8s.gcr.io/coredns"): + return new_image_name.replace("k8s.gcr.io/coredns", repository) + if ( + new_image_name.startswith("k8s.gcr.io/etcd") + or new_image_name.startswith("k8s.gcr.io/kube-") + or new_image_name.startswith("k8s.gcr.io/pause") + ): + return new_image_name.replace("k8s.gcr.io", repository) + + assert new_image_name.startswith(repository) is True + return new_image_name diff --git a/pyproject.toml b/pyproject.toml index 891bf888..b1da4a0b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry.scripts] magnum-cluster-api-image-builder = "magnum_cluster_api.cmd.image_builder:main" +magnum-cluster-api-image-loader = "magnum_cluster_api.cmd.image_loader:main" [tool.poetry.plugins."magnum.drivers"] "k8s_cluster_api_ubuntu_focal" = "magnum_cluster_api.driver:UbuntuFocalDriver"