Skip to content

Commit

Permalink
Fix prepull script (#98)
Browse files Browse the repository at this point in the history
* use desired_number_scheduled instead of hardcoded

* fix prepull daemon watch

* remove dev notebook

* use label
  • Loading branch information
avouacr authored Oct 11, 2023
1 parent 719cf46 commit 1db7068
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 9 deletions.
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,165 @@ utils/prepull-deployment-manifest.yaml
utils/prepull-daemon-manifest.yaml
**/nohup.out
**/prepull.log


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
32 changes: 25 additions & 7 deletions utils/prepull_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
from random import randint
import time

import yaml
import kubernetes
Expand Down Expand Up @@ -66,6 +67,7 @@ def prepull_deployment(namespace, images_to_prepull=None):
manifest = build_manifest(kind="Deployment",
images_to_prepull=images_to_prepull)
label_name = "prepull-deployment-" + str(randint(100000, 999999))
manifest["metadata"]["labels"]["name"] = label_name
manifest["spec"]["template"]["metadata"]["labels"]["name"] = label_name
manifest["spec"]["selector"]["matchLabels"]["name"] = label_name
kube_apps_api.create_namespaced_deployment(namespace=namespace,
Expand All @@ -75,7 +77,8 @@ def prepull_deployment(namespace, images_to_prepull=None):
w = kubernetes.watch.Watch()
for event in w.stream(kube_core_api.list_namespaced_pod,
namespace=namespace,
label_selector=f"name={label_name}"
label_selector=f"name={label_name}",
timeout_seconds=0
):
pod_state = event['object'].status.phase
if pod_state == "Running":
Expand All @@ -89,18 +92,33 @@ def prepull_daemon(namespace, images_to_prepull=None):
manifest = build_manifest(kind="DaemonSet",
images_to_prepull=images_to_prepull)
label_name = "prepull-daemonset-" + str(randint(100000, 999999))
manifest["metadata"]["labels"]["name"] = label_name
manifest["spec"]["template"]["metadata"]["labels"]["name"] = label_name
manifest["spec"]["selector"]["matchLabels"]["name"] = label_name
kube_apps_api.create_namespaced_daemon_set(namespace=namespace,
body=manifest)

# Wait for all daemons to be in Running state and remove the DaemonSet
# Get total number of daemons that will be launched
time.sleep(5) # Let the daemonset set itself up
daemon_info = kube_apps_api.list_namespaced_daemon_set(namespace=namespace,
label_selector=f"name={label_name}")
n_daemons_total = daemon_info.to_dict()["items"][0]["status"]["desired_number_scheduled"]

# Wait for all daemons to be in Running state
counter_n_daemons_ready = 0
w = kubernetes.watch.Watch()
for event in w.stream(kube_apps_api.list_namespaced_daemon_set,
namespace=namespace,
label_selector=f"name={label_name}"):
label_selector=f"name={label_name}",
timeout_seconds=0
):
n_daemons_ready = event['object'].status.number_ready
if n_daemons_ready == 11:

if n_daemons_ready > counter_n_daemons_ready:
logging.info(f'{n_daemons_ready}/{n_daemons_total} daemons done.')
counter_n_daemons_ready = n_daemons_ready

if n_daemons_ready == n_daemons_total:
w.stop()
break

Expand All @@ -109,12 +127,12 @@ def prepull_images(namespace, images_to_prepull=None):
"""Full prepull procedure."""
# 1st step : create a Deployment to pull the images in the global registry cache once
logging.info('1st step : Deployment')
prepull_deployment(namespace=NAMESPACE, images_to_prepull=images_to_prepull)
prepull_deployment(namespace=namespace, images_to_prepull=images_to_prepull)

# 2nd step : create a DaemonSet to pull the images in each worker's local cache
logging.info('2nd step : DaemonSet')
prepull_daemon(namespace=NAMESPACE, images_to_prepull=images_to_prepull)
prepull_daemon(namespace=namespace, images_to_prepull=images_to_prepull)

logging.info('Prepull job done')


Expand Down
3 changes: 2 additions & 1 deletion utils/prepull_images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ pip install kubernetes

KUBERNETES_NAMESPACE=`cat /var/run/secrets/kubernetes.io/serviceaccount/namespace`

# IMAGES_TO_PREPULL='inseefrlab/onyxia-vscode-python:py3.10.9'
#
# IMAGES_TO_PREPULL='inseefrlab/onyxia-jupyter-python:py3.11.4,inseefrlab/onyxia-jupyter-python:py3.10.12'
python prepull_images.py $KUBERNETES_NAMESPACE $IMAGES_TO_PREPULL

kubectl delete deployments.apps --ignore-not-found=true prepull
Expand Down
2 changes: 1 addition & 1 deletion utils/prepull_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ kind: ""
metadata:
name: prepull
labels:
name: prepull
name: ""
spec:
selector:
matchLabels:
Expand Down

0 comments on commit 1db7068

Please sign in to comment.