Skip to content

Commit

Permalink
feat: add tool to load image repository
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaser committed Nov 9, 2022
1 parent b2f0d9e commit 56c7ca9
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hack/stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
72 changes: 72 additions & 0 deletions magnum_cluster_api/cmd/image_loader.py
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions magnum_cluster_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 56c7ca9

Please sign in to comment.