Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit dbfc0e2

Browse files
committed
Added gcip.addon.container.job.crane to allow copying container images between registries.
1 parent 2bf2ee6 commit dbfc0e2

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
* Improved `conftest.check()` function. Now AssertionError is handled, the user will get receive how to update comparison files.
2020
* Added new class `PredefinedImages` in `gcip.addons.container`. Allows access to container images, that are widley used.
2121
* Added new `class` which handels docker client config and renders it to a json string.
22+
* Added `gcip.addon.container.job.crane` to allow copying container images between registries.
2223

2324
### Changed
2425

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import Union, Optional
2+
3+
from gcip.core.job import Job
4+
from gcip.core.image import Image
5+
from gcip.addons.container.config import DockerClientConfig
6+
from gcip.addons.container.images import PredefinedImages
7+
8+
9+
def copy(
10+
src: str,
11+
dst: str,
12+
*,
13+
docker_client_config: Optional[DockerClientConfig] = None,
14+
crane_image: Optional[Union[Image, str]] = None,
15+
) -> Job:
16+
"""
17+
Creates a job to copy container images with `crane`.
18+
See [`crane`](https://github.com/google/go-containerregistry/tree/main/cmd/crane)
19+
20+
Copying an image is usfull, if you want to have container images as close as possible
21+
to your cluster or servers.
22+
23+
Args:
24+
src (str): Registry URL to copy container image from.
25+
dst (str): Registry URL to copy container image to.
26+
docker_client_config (Optional[DockerClientConfig], optional): Creates the Docker configuration file base on objects settings,
27+
used by crane to authenticate against given registries. Defaults to None.
28+
crane_image (Optional[Union[Image, str]], optional): Container image which contains `crane` command.
29+
Defaults to PredefindedImages.CRANE.
30+
31+
Returns:
32+
Job: Returns a `gcip.Job`, with neccessary configuration to copy a container image between images.
33+
"""
34+
if not crane_image:
35+
crane_image = PredefinedImages.CRANE
36+
37+
job = Job(
38+
script=[
39+
"date",
40+
f"crane validate --remote {src}",
41+
f"crane copy {src} {dst}",
42+
],
43+
namespace="push_container_image",
44+
)
45+
job.set_image(crane_image)
46+
47+
if docker_client_config:
48+
job.prepend_scripts(*docker_client_config.get_shell_command())
49+
50+
return job
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
stages:
2+
- push_container_image
3+
push-container-image-with-authentication:
4+
image:
5+
name: gcr.io/go-containerregistry/crane:latest
6+
stage: push_container_image
7+
script:
8+
- mkdir -p $HOME/.docker/
9+
- 'echo "{\"auths\": {\"index.docker.io\": {\"username\": \"$REGISTRY_USERNAME\",
10+
\"password\": \"$REGISTRY_PASSWORD\"}}, \"credHelpers\": {\"0132456789.dkr.eu-central-1.amazonaws.com\":
11+
\"ecr-login\"}}" > $HOME/.docker/config.json'
12+
- date
13+
- crane validate --remote index.docker.io/alpine:3
14+
- crane copy index.docker.io/alpine:3 0132456789.dkr.eu-central-1.amazonaws.com/namespace/alpine:3
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
stages:
2+
- push_container_image
3+
push-container-image-default:
4+
image:
5+
name: gcr.io/go-containerregistry/crane:latest
6+
stage: push_container_image
7+
script:
8+
- date
9+
- crane validate --remote index.docker.io/alpine:3
10+
- crane copy index.docker.io/alpine:3 index.docker.io/user/alpine:3
11+
push-container-image-custom-image:
12+
image:
13+
name: index.docker.io/user/crane:latest
14+
stage: push_container_image
15+
script:
16+
- date
17+
- crane validate --remote quay.io/wagoodman/dive:0.10.0
18+
- crane copy quay.io/wagoodman/dive:0.10.0 index.docker.io/user/dive:latest
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from gcip import Pipeline
2+
from tests import conftest
3+
from gcip.core.image import Image
4+
from gcip.addons.container.jobs import crane
5+
from gcip.addons.container.config import DockerClientConfig
6+
7+
8+
def test_default_crane_job():
9+
pipeline = Pipeline()
10+
11+
pipeline.add_children(
12+
crane.copy("index.docker.io/alpine:3", "index.docker.io/user/alpine:3"),
13+
name="default",
14+
)
15+
pipeline.add_children(
16+
crane.copy(
17+
"quay.io/wagoodman/dive:0.10.0",
18+
"index.docker.io/user/dive:latest",
19+
crane_image=Image("index.docker.io/user/crane:latest"),
20+
),
21+
name="custom_image",
22+
)
23+
conftest.check(pipeline.render())
24+
25+
26+
def test_advanced_crane_job():
27+
dcc = DockerClientConfig()
28+
dcc.add_auth(registry="index.docker.io")
29+
dcc.add_cred_helper("0132456789.dkr.eu-central-1.amazonaws.com", "ecr-login")
30+
31+
pipeline = Pipeline()
32+
pipeline.add_children(
33+
crane.copy(
34+
"index.docker.io/alpine:3",
35+
"0132456789.dkr.eu-central-1.amazonaws.com/namespace/alpine:3",
36+
docker_client_config=dcc,
37+
),
38+
name="with_authentication",
39+
)
40+
conftest.check(pipeline.render())

0 commit comments

Comments
 (0)