Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for image pull secrets for build pod #1806

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions binderhub/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ def _default_namespace(self):
def _default_builder_info(self):
return {"build_image": self.build_image}

image_pull_secrets = List(
[], help="Pull secrets for the builder image", config=True
)

docker_host = Unicode(
"/var/run/docker.sock",
allow_none=True,
Expand Down Expand Up @@ -455,6 +459,18 @@ def get_builder_volumes(self):

return volumes, volume_mounts

def get_image_pull_secrets(self):
"""
Get the list of image pull secrets to be used for the builder image
"""

image_pull_secrets = []

for secret in self.image_pull_secrets:
image_pull_secrets.append(client.V1LocalObjectReference(name=secret))

return image_pull_secrets

def submit(self):
"""
Submit a build pod to create the image for the repository.
Expand Down Expand Up @@ -526,6 +542,7 @@ def submit(self):
volumes=volumes,
restart_policy="Never",
affinity=self.get_affinity(),
image_pull_secrets=self.get_image_pull_secrets(),
),
)

Expand Down
38 changes: 38 additions & 0 deletions binderhub/tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,44 @@ class EnvBuild(KubernetesBuildExecutor):
assert env == extra_environments


def test_build_image_pull_secrets():
build_pull_secrets = ["build-image-secret", "build-image-secret-2"]

mock_k8s_api = _list_image_builder_pods_mock()

class PullBuild(KubernetesBuildExecutor):
q = mock.MagicMock()
api = mock_k8s_api
name = "test_build"
repo_url = "repo"
ref = "ref"
image_name = "name"
namespace = "build_namespace"
push_secret = ""
build_image = "image"
image_pull_secrets = build_pull_secrets
memory_limit = 0
docker_host = "http://mydockerregistry.local"
node_selector = {}

build = PullBuild()

with mock.patch.object(build.stop_event, "is_set", return_value=True):
build.submit()

call_args_list = mock_k8s_api.create_namespaced_pod.call_args_list
assert len(call_args_list) == 1

args = call_args_list[0][0]
pod = args[1]

assert len(pod.spec.containers) == 1

pull_secrets = [secret.name for secret in pod.spec.image_pull_secrets]

assert pull_secrets == build_pull_secrets


async def test_local_repo2docker_build():
q = Queue()
repo_url = "https://github.com/binderhub-ci-repos/cached-minimal-dockerfile"
Expand Down