Skip to content

Commit

Permalink
feat: Use crane for image loader instead of skopeo
Browse files Browse the repository at this point in the history
  • Loading branch information
okozachenko1203 authored and mnaser committed Jan 17, 2023
1 parent 838d231 commit 8567f90
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 44 deletions.
52 changes: 39 additions & 13 deletions magnum_cluster_api/cmd/image_loader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import subprocess
import shutil

import click
from oslo_concurrency import processutils
from oslo_config import cfg
from oslo_log import log as logging

from magnum_cluster_api import utils
from magnum_cluster_api import image_utils

CONF = cfg.CONF
LOG = logging.getLogger(__name__)

IMAGES = [
"docker.io/calico/cni:v3.24.2",
Expand Down Expand Up @@ -55,18 +61,38 @@ def main(repository):
"""
Load images into a remote registry for `container_infra_prefix` usage.
"""
crane_path = shutil.which("crane")

for image in IMAGES:
skoepo(
"copy",
"--multi-arch",
"all",
f"docker://{image}",
f"docker://{utils.get_image(image, repository)}",
if crane_path is None:
raise click.UsageError(
"Crane is not installed. Please install it before running this command."
)

seen = []
for image in IMAGES:
if IMAGES[image] in seen:
continue

src = image
dst = image_utils.get_image(image, repository)

LOG.debug(f"Starting to mirror {src} to {dst}")

try:
processutils.execute(
crane_path,
"copy",
src,
dst,
)
except processutils.ProcessExecutionError as e:
if "401 Unauthorized" in e.stderr:
LOG.error(
"Authentication failed. Please ensure you're logged in via Crane"
)
return

raise

def skoepo(*args):
cmd = ["skopeo", "--insecure-policy"] + list(args)
click.echo(" ".join(cmd))
subprocess.run(cmd, check=True)
seen.append(image)
LOG.info(f"Successfully mirrored {src} to {dst}")
29 changes: 29 additions & 0 deletions magnum_cluster_api/image_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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

33 changes: 2 additions & 31 deletions magnum_cluster_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from tenacity import retry, retry_if_exception_type

from magnum_cluster_api import clients, objects
from magnum_cluster_api import image_utils


def get_or_generate_cluster_api_cloud_config_secret_name(
Expand Down Expand Up @@ -137,36 +138,6 @@ def get_cluster_label_as_bool(
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


def update_manifest_images(
cluster: magnum_objects.Cluster, file, repository=None, replacements=[]
):
Expand All @@ -181,7 +152,7 @@ def update_manifest_images(
for src, dst in replacements:
container["image"] = container["image"].replace(src, dst)
if repository:
container["image"] = get_image(container["image"], repository)
container["image"] = image_utils.get_image(container["image"], repository)

# Fix CCM cluster-name
if (
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ click = "*"
requests = "*"
shortuuid = "*"
certifi = "*"
"oslo.log" = "^5.0.2"
"oslo.config" = "^9.0.0"
"oslo.concurrency" = "^5.0.1"

[build-system]
requires = ["setuptools", "poetry-core"]
Expand Down

0 comments on commit 8567f90

Please sign in to comment.