-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from whole-tale/k8s
Add support for K8s deployments
- Loading branch information
Showing
29 changed files
with
2,353 additions
and
971 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
gwvolman.egg-info/ | ||
*.pyc | ||
*venv* | ||
.coverage | ||
Testing | ||
build/ | ||
dist/ | ||
pip-wheel-metadata/ | ||
.vscode/ | ||
gwvolman.code-workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
from .docker import DockerImageBuilder # noqa | ||
from .kaniko import KanikoImageBuilder # noqa | ||
|
||
if os.environ.get("DEPLOYMENT", "docker") == "k8s": | ||
ImageBuilder = KanikoImageBuilder | ||
else: | ||
ImageBuilder = DockerImageBuilder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import hashlib | ||
import logging | ||
import os | ||
import tempfile | ||
|
||
import docker | ||
|
||
from ..utils import ( | ||
DEPLOYMENT, | ||
DummyTask, | ||
stop_container, | ||
) | ||
from .builder import ImageBuilderBase | ||
|
||
|
||
class DockerHelper: | ||
def __init__(self, auth=True): | ||
username = os.environ.get("REGISTRY_USER", "fido") | ||
password = os.environ.get("REGISTRY_PASS") | ||
self.cli = docker.from_env(version="1.28") | ||
self.apicli = docker.APIClient(base_url="unix://var/run/docker.sock") | ||
if auth: | ||
self.cli.login( | ||
username=username, password=password, registry=DEPLOYMENT.registry_url | ||
) | ||
self.apicli.login( | ||
username=username, password=password, registry=DEPLOYMENT.registry_url | ||
) | ||
|
||
|
||
class DockerImageBuilder(ImageBuilderBase): | ||
def __init__(self, gc, imageId=None, tale=None, auth=True): | ||
super().__init__(gc, imageId=imageId, tale=tale, auth=auth) | ||
self.dh = DockerHelper(auth) | ||
|
||
def pull_r2d(self): | ||
try: | ||
self.dh.cli.images.pull(self.container_config.repo2docker_version) | ||
except docker.errors.NotFound: | ||
raise ValueError( | ||
f"Requested r2d image '{self.container_config.repo2docker_version}' not found." | ||
) | ||
|
||
def push_image(self, image): | ||
"""Push image to the registry""" | ||
repository, tag = image.split(":", 1) | ||
for line in self.dh.apicli.push(repository, tag=tag, stream=True, decode=True): | ||
print(line) | ||
|
||
def run_r2d(self, tag, dry_run=False, task=None): | ||
""" | ||
Run repo2docker on the workspace using a shared temp directory. Note that | ||
this uses the "local" provider. Use the same default user-id and | ||
user-name as BinderHub | ||
""" | ||
|
||
task = task or DummyTask | ||
|
||
# Extra arguments for r2d | ||
r2d_cmd = self.r2d_command(tag, dry_run=dry_run) | ||
r2d_context_dir = os.path.relpath(self.build_context, tempfile.gettempdir()) | ||
host_r2d_context_dir = os.path.join(DEPLOYMENT.tmpdir_mount, r2d_context_dir) | ||
|
||
logging.info("Calling %s", r2d_cmd) | ||
|
||
volumes = { | ||
"/var/run/docker.sock": {"bind": "/var/run/docker.sock", "mode": "rw"}, | ||
host_r2d_context_dir: {"bind": self.build_context, "mode": "ro"}, | ||
} | ||
|
||
print(f"Using repo2docker {self.container_config.repo2docker_version}") | ||
container = self.dh.cli.containers.run( | ||
image=self.container_config.repo2docker_version, | ||
command=r2d_cmd, | ||
environment=["DOCKER_HOST=unix:///var/run/docker.sock"], | ||
privileged=True, | ||
detach=True, | ||
remove=False, | ||
volumes=volumes, | ||
) | ||
|
||
# Job output must come from stdout/stderr | ||
h = hashlib.md5("R2D output".encode()) | ||
for line in container.logs(stream=True): | ||
if task.canceled: | ||
task.request.chain = None | ||
stop_container(container) | ||
break | ||
output = line.decode("utf-8").strip() | ||
if not output.startswith("Using local repo"): # contains variable path | ||
h.update(output.encode("utf-8")) | ||
if not dry_run: # We don't want to see it. | ||
print(output) | ||
|
||
try: | ||
ret = container.wait() | ||
except docker.errors.NotFound: | ||
ret = {"StatusCode": -123} | ||
container.remove() | ||
|
||
if ret["StatusCode"] != 0: | ||
logging.error("Error building image") | ||
# Since detach=True, then we need to explicitly check for the | ||
# container exit code | ||
return ret, h.hexdigest() |
Oops, something went wrong.