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

Commit 4abb36a

Browse files
committed
Added crane.pull() function to gcip.addons.container.crane.
1 parent f34a014 commit 4abb36a

6 files changed

Lines changed: 148 additions & 16 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* Added @properties to all public Job attributes.
2626
* New addons: *aws* to allow receiving AWS account id and region.
2727
* New `gcip.addons.container.registries.Registry.AWS()` allows getting an ECR URL to be used in pipeline.
28+
* Added `crane.pull()` function to `gcip.addons.container.crane`.
2829

2930
### Changed
3031
* Normalize config_file_path in `gcip.addons.container.config.DockerClientConfig`

gcip/addons/container/jobs/crane.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,67 @@ def push(
118118
job.prepend_scripts(*docker_client_config.get_shell_command())
119119

120120
return job
121+
122+
123+
def pull(
124+
src_registry: Union[Registry, str],
125+
*,
126+
tar_path: Optional[str] = None,
127+
image_name: Optional[str] = None,
128+
image_tag: Optional[str] = None,
129+
docker_client_config: Optional[DockerClientConfig] = None,
130+
crane_image: Optional[Union[Image, str]] = None,
131+
) -> Job:
132+
"""
133+
Creates a job to pull container image from remote container registry with `crane`.
134+
135+
Args:
136+
src_registry (str): Registry URL to pull container image from.
137+
image_name (str): Container image with namespace to pull from `src_registry`.
138+
If `None` it defaults internally to `PredefinedVariables.CI_PROJECT_NAME`.
139+
image_tag (str): Tag of the image which will be pulled. If `None` it defaults internally to `PredefinedVariables.CI_COMMIT_TAG`
140+
or `PredefinedVariables.CI_COMMIT_REF_SLUG` in order.
141+
tar_path (Optional[str], optional): Path where to save the container image tarball.
142+
If `None` it defaults internally to `PredefinedVariables.CI_PROJECT_DIR`. Defaults to None.
143+
docker_client_config (Optional[DockerClientConfig], optional): Creates the Docker configuration file base on objects settings,
144+
to authenticate against given registries. Defaults to a `DockerClientConfig` with login to the official Docker Hub
145+
and expecting credentials given as environment variables `REGISTRY_USER` and `REGISTRY_LOGIN`.
146+
crane_image (Optional[Union[Image, str]], optional): Container image which contains `crane` command.
147+
Defaults to PredefindedImages.CRANE.
148+
149+
Returns:
150+
Job: Returns a `gcip.Job`, with neccessary configuration to pull a container image from a remote registry and stores it in `tar_path`.
151+
Job runs in ```stage=pull```
152+
"""
153+
if not crane_image:
154+
crane_image = PredefinedImages.CRANE
155+
156+
if not tar_path:
157+
tar_path = PredefinedVariables.CI_PROJECT_DIR
158+
159+
if not image_name:
160+
image_name = PredefinedVariables.CI_PROJECT_NAME
161+
image_path = image_name.replace("/", "_")
162+
163+
if not docker_client_config:
164+
docker_client_config = DockerClientConfig()
165+
docker_client_config.add_auth(registry=Registry.DOCKER)
166+
167+
if not image_tag:
168+
if PredefinedVariables.CI_COMMIT_TAG:
169+
image_tag = PredefinedVariables.CI_COMMIT_TAG
170+
else:
171+
image_tag = PredefinedVariables.CI_COMMIT_REF_SLUG
172+
173+
job = Job(
174+
script=[
175+
f"crane pull {src_registry}/{image_name}:{image_tag} {tar_path}/{image_path}.tar",
176+
],
177+
stage="pull",
178+
)
179+
job.set_image(crane_image)
180+
181+
if docker_client_config:
182+
job.prepend_scripts(*docker_client_config.get_shell_command())
183+
184+
return job
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
stages:
2+
- pull
3+
pull:
4+
image:
5+
name: gcr.io/go-containerregistry/crane:debug
6+
entrypoint:
7+
- ''
8+
stage: pull
9+
script:
10+
- mkdir -p $HOME/.docker
11+
- 'echo "{\"auths\": {\"https://index.docker.io/v1/\": {\"username\": \"$REGISTRY_USERNAME\",
12+
\"password\": \"$REGISTRY_PASSWORD\"}}, \"credHelpers\": {\"0132456789.dkr.eu-central-1.amazonaws.com\":
13+
\"ecr-login\"}}" > $HOME/.docker/config.json'
14+
- crane pull gcr.io/thomass/gcip:main test/foo/bar/thomass_gcip.tar
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
stages:
2+
- pull
3+
pull:
4+
image:
5+
name: gcr.io/go-containerregistry/crane:debug
6+
entrypoint:
7+
- ''
8+
stage: pull
9+
script:
10+
- mkdir -p $HOME/.docker
11+
- 'echo "{\"auths\": {\"https://index.docker.io/v1/\": {\"username\": \"$REGISTRY_USERNAME\",
12+
\"password\": \"$REGISTRY_PASSWORD\"}}}" > $HOME/.docker/config.json'
13+
- crane pull gcr.io/gitlab-ci-project:11.22.33 /path/to/project/gitlab-ci-project.tar
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
stages:
2+
- pull
3+
pull:
4+
image:
5+
name: gcr.io/go-containerregistry/crane:debug
6+
entrypoint:
7+
- ''
8+
stage: pull
9+
script:
10+
- mkdir -p $HOME/.docker
11+
- 'echo "{\"auths\": {\"https://index.docker.io/v1/\": {\"username\": \"$REGISTRY_USERNAME\",
12+
\"password\": \"$REGISTRY_PASSWORD\"}}}" > $HOME/.docker/config.json'
13+
- crane pull gcr.io/gitlab-ci-project:my-awsome-feature-branch /path/to/project/gitlab-ci-project.tar

tests/unit/test_addons_container_crane.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from gcip import Pipeline
24
from tests import conftest
35
from gcip.core.image import Image
@@ -6,6 +8,14 @@
68
from gcip.addons.container.registries import Registry
79

810

11+
@pytest.fixture()
12+
def docker_client_config() -> DockerClientConfig:
13+
dcc = DockerClientConfig()
14+
dcc.add_auth(registry="index.docker.io")
15+
dcc.add_cred_helper("0132456789.dkr.eu-central-1.amazonaws.com", cred_helper="ecr-login")
16+
return dcc
17+
18+
919
def test_simple_crane_copy_job(gitlab_ci_environment_variables):
1020
pipeline = Pipeline()
1121

@@ -24,17 +34,13 @@ def test_simple_crane_copy_job(gitlab_ci_environment_variables):
2434
conftest.check(pipeline.render())
2535

2636

27-
def test_advanced_crane_copy_job(gitlab_ci_environment_variables):
28-
dcc = DockerClientConfig()
29-
dcc.add_auth(registry="index.docker.io")
30-
dcc.add_cred_helper("0132456789.dkr.eu-central-1.amazonaws.com", "ecr-login")
31-
37+
def test_advanced_crane_copy_job(gitlab_ci_environment_variables, docker_client_config):
3238
pipeline = Pipeline()
3339
pipeline.add_children(
3440
crane.copy(
3541
"index.docker.io/alpine:3",
3642
"0132456789.dkr.eu-central-1.amazonaws.com/namespace/alpine:3",
37-
docker_client_config=dcc,
43+
docker_client_config=docker_client_config,
3844
),
3945
name="with_authentication",
4046
)
@@ -47,35 +53,56 @@ def test_simple_crane_push_job(gitlab_ci_environment_variables):
4753
conftest.check(pipeline.render())
4854

4955

50-
def test_advanced_crane_push_job(gitlab_ci_environment_variables):
51-
dcc = DockerClientConfig()
52-
dcc.add_auth(registry="index.docker.io")
53-
dcc.add_cred_helper("0132456789.dkr.eu-central-1.amazonaws.com", cred_helper="ecr-login")
56+
def test_advanced_crane_push_job(gitlab_ci_environment_variables, docker_client_config):
5457
pipeline = Pipeline()
5558
pipeline.add_children(
5659
crane.push(
5760
dst_registry="index.docker.io",
5861
image_name="crane",
59-
docker_client_config=dcc,
62+
docker_client_config=docker_client_config,
6063
crane_image="crane_image:v1.1.2",
6164
),
6265
name="push_image",
6366
)
6467
conftest.check(pipeline.render())
6568

6669

67-
def test_addons_container_jobs_crane_push_registry(gitlab_ci_environment_variables):
68-
dcc = DockerClientConfig()
69-
dcc.add_auth(registry=Registry.DOCKER)
70-
dcc.add_cred_helper("0132456789.dkr.eu-central-1.amazonaws.com", cred_helper="ecr-login")
70+
def test_addons_container_jobs_crane_push_registry(gitlab_ci_environment_variables, docker_client_config):
7171
pipeline = Pipeline()
7272
pipeline.add_children(
7373
crane.push(
7474
dst_registry=Registry.DOCKER,
7575
image_name="crane",
76-
docker_client_config=dcc,
76+
docker_client_config=docker_client_config,
7777
crane_image="crane_image:v1.1.2",
7878
),
7979
name="push_image",
8080
)
8181
conftest.check(pipeline.render())
82+
83+
84+
def test_crane_simple_pull(gitlab_ci_environment_variables):
85+
pipeline = Pipeline()
86+
pipeline.add_children(crane.pull(Registry.GCR))
87+
conftest.check(pipeline.render())
88+
89+
90+
def test_crane_simple_pull_with_ref(monkeypatch, gitlab_ci_environment_variables):
91+
monkeypatch.delenv("CI_COMMIT_TAG")
92+
pipeline = Pipeline()
93+
pipeline.add_children(crane.pull(Registry.GCR))
94+
conftest.check(pipeline.render())
95+
96+
97+
def test_crane_advanced_pull(gitlab_ci_environment_variables, docker_client_config):
98+
pipeline = Pipeline()
99+
pipeline.add_children(
100+
crane.pull(
101+
Registry.GCR,
102+
docker_client_config=docker_client_config,
103+
image_name="thomass/gcip",
104+
image_tag="main",
105+
tar_path="test/foo/bar",
106+
)
107+
)
108+
conftest.check(pipeline.render())

0 commit comments

Comments
 (0)