Skip to content

Commit 027abb2

Browse files
authored
feat: run workload and charm as unprivileged user (#284)
Signed-off-by: Dario Faccin <dario.faccin@canonical.com>
1 parent 8b38d82 commit 027abb2

File tree

11 files changed

+2056
-915
lines changed

11 files changed

+2056
-915
lines changed

.github/workflows/integrate.yaml

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
steps:
2222
- name: Check out code
2323
uses: actions/checkout@v4
24+
2425
- name: Install dependencies
2526
run: pipx install tox
2627

@@ -54,24 +55,35 @@ jobs:
5455

5556
integration-test:
5657
name: Integration
57-
runs-on: ubuntu-24.04
58+
runs-on: ["self-hosted-linux-amd64-noble-xlarge"]
5859
strategy:
5960
matrix:
6061
tox-environments:
6162
- integration
6263
- integration-with-profiles
6364
steps:
64-
- name: Maximise GH runner space
65-
uses: jlumbroso/free-disk-space@v1.3.1
66-
6765
- name: Check out code
6866
uses: actions/checkout@v4
67+
6968
- name: Install dependencies
70-
run: pipx install tox
69+
run: |
70+
sudo apt-get install -yqq pipx
71+
pipx ensurepath
72+
pipx install tox
7173
7274
- name: Setup environment
7375
run: |
74-
sudo apt-get remove -y docker-ce docker-ce-cli containerd.io
76+
# Avoiding dockerhub rate limits (see https://canonical-self-hosted-github-runner-docs.readthedocs-hosted.com/en/latest/usage/faq/how-to-avoid-dockerhub-rate-limits/ for more informations)
77+
if [ -n "$DOCKERHUB_MIRROR" ]; then
78+
MIRROR_CONFIG=/etc/containerd/hosts.d/docker.io
79+
sudo mkdir -p ${MIRROR_CONFIG}
80+
sudo chown $USER ${MIRROR_CONFIG}
81+
cat << EOF > ${MIRROR_CONFIG}/hosts.toml
82+
[host."$DOCKERHUB_MIRROR"]
83+
capabilities = ["pull", "resolve"]
84+
EOF
85+
fi
86+
sudo apt-get remove -y docker.io containerd
7587
sudo rm -rf /run/containerd
7688
sudo snap install concierge --classic
7789
sudo concierge prepare --trace
@@ -81,28 +93,47 @@ jobs:
8193
run: |
8294
# this step is required to prefetch heavy images to avoid false-positive failures in
8395
# integration tests due to timeouts unrelated to the charm itself
84-
RUNTIME_FILES=(
85-
"src/training_runtimes/deepspeed_distributed.yaml"
86-
)
87-
for RUNTIME_FILE in "${RUNTIME_FILES[@]}"; do
88-
IMAGE=$(yq '.spec.template.spec.replicatedJobs.[0].template.spec.template.spec.containers[0].image' "${RUNTIME_FILE}")
96+
RUNTIME_FOLDER=./src/training_runtimes/
97+
for TPL in "${RUNTIME_FOLDER}"*.yaml; do
98+
IMAGE=$(yq '.spec.template.spec.replicatedJobs.[0].template.spec.template.spec.containers[0].image' "${TPL}")
8999
# set internal field separator to ":" and tokenize the image string
90100
IFS=':' read -ra SPLIT_IMAGE <<< "${IMAGE}"
91-
HASH="latest"
92-
# handle case in which image already has an hash
93-
if [ ${#SPLIT_IMAGE[@]} = 2 ]; then
94-
HASH=${SPLIT_IMAGE[-1]}
101+
TAG="latest"
102+
# handle case in which image already has a tag
103+
if [ ${#SPLIT_IMAGE[@]} == 2 ]; then
104+
TAG=${SPLIT_IMAGE[-1]}
95105
fi
96-
FINAL_IMAGE="${SPLIT_IMAGE[0]}:${HASH}"
106+
# handle implicit registry
107+
IFS='/' read -ra SPLIT_IMAGE <<< "${SPLIT_IMAGE[0]}"
108+
REGISTRY="docker.io"
109+
if [ "${SPLIT_IMAGE[0]}" == "ghcr.io" ]; then
110+
REGISTRY=${SPLIT_IMAGE[0]}
111+
else
112+
SPLIT_IMAGE=("${REGISTRY}" "${SPLIT_IMAGE[@]}" )
113+
fi
114+
# set internal field separator to "/" and join the image string plus tag
115+
IFS="/" FINAL_IMAGE="${SPLIT_IMAGE[*]:0:${#SPLIT_IMAGE[@]}}:${TAG}"
97116
echo "Pulling ${FINAL_IMAGE}"
98117
# explanation of this command can be found in Canonical K8s image management doc
99118
# https://documentation.ubuntu.com/canonical-kubernetes/latest/snap/howto/image-management/
100119
sudo /snap/k8s/current/bin/ctr --namespace k8s.io image pull "${FINAL_IMAGE}" > /dev/null 2>&1
120+
sudo /snap/k8s/current/bin/ctr --namespace k8s.io image label "${FINAL_IMAGE}" io.cri-containerd.pinned=pinned
101121
done
102122
if: ${{ matrix.tox-environments == 'integration' }}
103123

124+
- name: Fetch charm
125+
uses: actions/download-artifact@v5
126+
with:
127+
name: built-charm
128+
path: built/
129+
130+
- name: Get charm path
131+
id: charm-path
132+
run: echo "charm_path=$(find built/ -name '*.charm' -type f -print)" >> $GITHUB_OUTPUT
133+
104134
- name: Run integration tests
105-
run: tox -e ${{ matrix.tox-environments }} -- --model testing
135+
run: |
136+
tox -e ${{ matrix.tox-environments }} -- --model testing --charm-path="${{ steps.charm-path.outputs.charm_path }}"
106137
107138
- name: Capture k8s resources on failure
108139
run: |

.github/workflows/on_pull_request.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,35 @@ on:
99

1010
jobs:
1111

12+
build-charm:
13+
name: Build charm
14+
runs-on: ubuntu-24.04
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Setup LXD
20+
uses: canonical/setup-lxd@main
21+
with:
22+
channel: 5.21/stable
23+
24+
- name: Install charmcraft
25+
run: sudo snap install charmcraft --classic
26+
27+
- name: Build charm under test
28+
run: charmcraft pack --verbose
29+
30+
- name: Archive charm
31+
uses: actions/upload-artifact@v4
32+
with:
33+
name: built-charm
34+
path: "*.charm"
35+
retention-days: 5
36+
1237
tests:
1338
name: Run Tests
39+
needs:
40+
- build-charm
1441
uses: ./.github/workflows/integrate.yaml
1542
secrets: inherit
1643

.github/workflows/on_push.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,35 @@ on:
1414
- track/2**
1515

1616
jobs:
17+
build-charm:
18+
name: Build charm
19+
runs-on: ubuntu-24.04
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
24+
- name: Setup LXD
25+
uses: canonical/setup-lxd@main
26+
with:
27+
channel: 5.21/stable
28+
29+
- name: Install charmcraft
30+
run: sudo snap install charmcraft --classic
31+
32+
- name: Build charm under test
33+
run: charmcraft pack --verbose
34+
35+
- name: Archive charm
36+
uses: actions/upload-artifact@v4
37+
with:
38+
name: built-charm
39+
path: "*.charm"
40+
retention-days: 5
1741

1842
tests:
1943
name: Run Tests
44+
needs:
45+
- build-charm
2046
uses: ./.github/workflows/integrate.yaml
2147
secrets: inherit
2248

metadata.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ provides:
1616
requires:
1717
dashboard-links:
1818
interface: kubeflow_dashboard_links
19+
charm-user: non-root

0 commit comments

Comments
 (0)