From 02cdb19589f1e757c8f57c9e98b6f77fffe0f34e Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Tue, 18 Mar 2025 16:35:28 +0000 Subject: [PATCH 01/13] Add debugpy to Dockerfile for service containers --- template/Dockerfile.jinja | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/template/Dockerfile.jinja b/template/Dockerfile.jinja index c5ca109b..7dbeaa7b 100644 --- a/template/Dockerfile.jinja +++ b/template/Dockerfile.jinja @@ -17,13 +17,16 @@ FROM developer AS build COPY . /context WORKDIR /context RUN touch dev-requirements.txt && pip install -c dev-requirements.txt . +RUN pip install debugpy # The runtime stage copies the built venv into a slim runtime container FROM python:${PYTHON_VERSION}-slim AS runtime -# Add apt-get system dependecies for runtime here if needed +# Add apt-get system dependencies for runtime here if needed COPY --from=build /venv/ /venv/ ENV PATH=/venv/bin:$PATH -# change this entrypoint if it is not the same as the repo -ENTRYPOINT ["{{ repo_name }}"] -CMD ["--version"]{% endif %} +# change CMD if it is not the same as the repo +ENTRYPOINT ["python" "-m"] +# Allows for modifying the ENTRYPOINT for debugging, e.g. +#ENTRYPOINT ["python", "-m", "debugpy", "-m"] +CMD ["{{ repo_name }}", "--version"]{% endif %} From f9b1176aaf7501f49aedfe2e66a959fef57661c8 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Tue, 18 Mar 2025 16:57:23 +0000 Subject: [PATCH 02/13] Add ADR --- docs/explanations/decisions/0020-debugpy.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 docs/explanations/decisions/0020-debugpy.md diff --git a/docs/explanations/decisions/0020-debugpy.md b/docs/explanations/decisions/0020-debugpy.md new file mode 100644 index 00000000..27ec5a37 --- /dev/null +++ b/docs/explanations/decisions/0020-debugpy.md @@ -0,0 +1,19 @@ +# 20. Add debugpy to service Dockerfile + +Date: 2025-03-18 + +## Status + +Accepted + +## Context + +Developers require a way to debug containerized services in e.g. Kubernetes. + +## Decision + +Add debugpy to the container image that is produced for services. + +## Consequences + +Debugging containerized services has a clear and documented path. From f8ac5ef54b069efe67c84dba30fcde5fbf1d5222 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Wed, 19 Mar 2025 14:18:04 +0000 Subject: [PATCH 03/13] Add docs for creating Helm resources --- .github/workflows/_container.yml | 18 ++++ copier.yml | 8 ++ docs/how-to/coverage.md | 1 - docs/how-to/deploy-cluster.md | 142 +++++++++++++++++++++++++++++++ example-answers.yml | 1 + template/Dockerfile.jinja | 6 +- 6 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 docs/how-to/deploy-cluster.md diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml index da5e4936..7cb65197 100644 --- a/.github/workflows/_container.yml +++ b/.github/workflows/_container.yml @@ -58,3 +58,21 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + +{% if helm %} + - name: Install Helm + uses: Azure/setup-helm@v4 + id: install + + - name: Log in to GitHub Container Registry + if: github.ref_type == 'tag' + run: | + echo ${{ secrets.GITHUB_TOKEN }} | helm registry login ghcr.io/${{ github.repository }} --username ${{ github.repository_owner }} --password-stdin + + - name: package chart and push it + if: github.ref_type == 'tag' + run: | + helm dependencies update helm/{{ repo_name }} + helm package helm/{{ repo_name }} --version ${{ steps.meta.outputs.version }} --app-version ${{ steps.meta.outputs.version }} -d /tmp/ + helm push /tmp/{{ repo_name }}-${{ steps.meta.outputs.version }}.tgz oci://ghcr.io/${{ github.repository_owner }}/charts +{% end if %} diff --git a/copier.yml b/copier.yml index 046b88b2..5a637fcc 100644 --- a/copier.yml +++ b/copier.yml @@ -103,6 +103,14 @@ docker: Would you like to publish your project in a Docker container? You should select this if you are making a service. +helm: + type: bool + when: "{{ github_org == 'DiamondLightSource' && docker}}" + help: | + Would you like to publish a Helm chart? + You should select this if you are making a long-running service. + This requires that releases use Semantic Versioning + docs_type: type: str help: | diff --git a/docs/how-to/coverage.md b/docs/how-to/coverage.md index 161ffc2c..4f03519e 100644 --- a/docs/how-to/coverage.md +++ b/docs/how-to/coverage.md @@ -1,4 +1,3 @@ - # How to check code coverage Code coverage is reported to the command line and to a `cov.xml` file by the command `tox -e tests`. The file is uploaded to the Codecov service in CI. diff --git a/docs/how-to/deploy-cluster.md b/docs/how-to/deploy-cluster.md new file mode 100644 index 00000000..d964ff8b --- /dev/null +++ b/docs/how-to/deploy-cluster.md @@ -0,0 +1,142 @@ +# How to deploy containers to Kubernetes cluster + +If your project is a service with a `Dockerfile`, you may wish to deploy it in a Kubernetes cluster. + +## Creating a Helm chart + +Helm bundles multiple Kubernetes resources into a single top level resource, `Chart`, and templates resources to inject specified `values`. + +``` + service/ + ├── Chart.yaml # Definition of the resource + ├── values.yaml # Defaults for templating + ├── charts/ # [Optionally] other charts to deploy with + └── templates/ # Templated Kubernetes resources +``` + +`templates/` may include at least: +- `deployment.yaml`: creates a pod including your container image +- `service.yaml`: manages Kubernetes networking, potentially exposing your service +- `ingress.yaml`: optionally maps a DNS entry to the Kubernetes networking + +Using `helm create` ensures your service is using the latest standards, therefore Helm resources are not included in this template. +To avoid collisions and to maintain a neat repository, it is recommended to run `helm create ` inside a directory named `helm/` in the root of your repository. + +Assuming your container is published to the GitHub container registry, modify your `values.yaml` to deploy your built container. + +```yaml +image: + repository: ghcr.io// + pullPolicy: Always + # Overrides the image tag whose default is the chart appVersion. + tag: "" +``` + +The container will use the `ENTRYPOINT` and `CMD` defined in your `Dockerfile`. + +It is recommended to preserve all of the templates within `templates/`: resources you do not need can be disabled from `values.yaml` while maintaining the ability to deploy or extend the chart. + +## Enabling container debugging + +The generated `Dockerfile` installs debugpy and with a few modifications can enable remote debugging of a service deployed inside a cluster. + +Adding the following to your `values.yaml` gives a standard way of enabling/disabling debugging and documenting the configuration. + +```yaml +# Use `kubectl port forward` to access from your machine +debug: + # Whether the container should start in debug mode + enabled: false + # Whether to suspend the process until a debugger connects + suspend: false + # Port to listen for the debugger on + port: 5678 +``` + +The `ENTRYPOINT` and `CMD` concepts in the Dockerfile are analogous to Kubernetes' `command` and `args`. +If `command` is set, it overrides `ENTRYPOINT` and uses `args` if set, ignoring `CMD`. +If `args` is set, `ENTRYPOINT` remains and `CMD` is replaced. + +Assuming your `Dockerfile` contains the following, or analogous: + +```Dockerfile +ENTRYPOINT ["python"] +CMD ["-m", "service", "--version"] +``` + +Modifying `deployment.yaml` in the following way allows for your service to enable debugging via the configuration added to `values.yaml`. + +```yaml + containers: + - ... + args: + {{- if .Values.debug.enabled}} + - "-Xfrozen_modules=off" + - "-m" + - "debugpy" + {{- if .Values.debug.suspend }} + - "--wait-for-client" + {{- end }} + - "--listen" + - "0.0.0.0:{{ .Values.debug.port }}" + {{- end }} + - "-m" + - "service" + - "--version" +``` + +## Connecting to debug mode container + +`kubectl port forward` forwards your development machine's port 5678 to the container's: + +```sh +$ kubectl get pods +NAME READY STATUS RESTARTS AGE +service 1/1 Running 0 (1h ago) 1h +$ kubectl port forward pod/service 5678:5678 +Forwarding from 127.0.0.1:5678 -> 5678 +``` + +Check out the version of your service that was built into the container and configure your IDE to attach to a remote debugpy process: + +The following is a launch configuration from VSCode `launch.json`. +`"remoteRoot"` should match for the version of Python your `Dockerfile` is built from and use your service's name. +`"justMyCode": False` was found to be required for breakpoints to be active. +`"autoReload"` Configured hot swapping of code from your developer machine to the deployed instance. + +> ⚠️ **Changes made by autoReload are not preserved.** Code changes made while debugging or resolving an issue should be committed, pushed and built into a new container as soon as possible. + + +```json +{ + "name": "Python Debugger: Remote Attach", + "type": "debugpy", + "request": "attach", + "connect": { + "host": "localhost", + "port": 5678 + }, + "pathMappings": [ + { + "localRoot": "${workspaceFolder}/src", + "remoteRoot": "/venv/lib//site-packages/" + } + ], + "justMyCode": false, + "autoReload": { + "enable": true, + "exclude": [ + "**/.git/**", + "**/__pycache__/**", + "**/node_modules/**", + "**/.metadata/**", + "**/site-packages/**" + ], + "include": [ + "**/*.py", + "**/*.pyw" + ] + } +} +``` + diff --git a/example-answers.yml b/example-answers.yml index 0ff28f7f..d20cf643 100644 --- a/example-answers.yml +++ b/example-answers.yml @@ -6,6 +6,7 @@ component_lifecycle: experimental description: An expanded https://github.com/DiamondLightSource/python-copier-template to illustrate how it looks with all the options enabled. distribution_name: dls-python-copier-template-example docker: true +helm: false docs_type: sphinx git_platform: github.com github_org: DiamondLightSource diff --git a/template/Dockerfile.jinja b/template/Dockerfile.jinja index 7dbeaa7b..d8524bd0 100644 --- a/template/Dockerfile.jinja +++ b/template/Dockerfile.jinja @@ -26,7 +26,7 @@ COPY --from=build /venv/ /venv/ ENV PATH=/venv/bin:$PATH # change CMD if it is not the same as the repo -ENTRYPOINT ["python" "-m"] +ENTRYPOINT ["python"] # Allows for modifying the ENTRYPOINT for debugging, e.g. -#ENTRYPOINT ["python", "-m", "debugpy", "-m"] -CMD ["{{ repo_name }}", "--version"]{% endif %} +#ENTRYPOINT ["python", "-m", "debugpy"] +CMD ["-m", "{{ repo_name }}", "--version"]{% endif %} From 3e9133cf78e0271de4f0a4f34ba52330aff87bf1 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Wed, 19 Mar 2025 15:23:08 +0000 Subject: [PATCH 04/13] Move Helm chart --- .github/workflows/_container.yml | 18 ---------- .../{% if helm %}_helm.yml{% endif %}" | 33 +++++++++++++++++++ 2 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 "template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" diff --git a/.github/workflows/_container.yml b/.github/workflows/_container.yml index 7cb65197..da5e4936 100644 --- a/.github/workflows/_container.yml +++ b/.github/workflows/_container.yml @@ -58,21 +58,3 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - -{% if helm %} - - name: Install Helm - uses: Azure/setup-helm@v4 - id: install - - - name: Log in to GitHub Container Registry - if: github.ref_type == 'tag' - run: | - echo ${{ secrets.GITHUB_TOKEN }} | helm registry login ghcr.io/${{ github.repository }} --username ${{ github.repository_owner }} --password-stdin - - - name: package chart and push it - if: github.ref_type == 'tag' - run: | - helm dependencies update helm/{{ repo_name }} - helm package helm/{{ repo_name }} --version ${{ steps.meta.outputs.version }} --app-version ${{ steps.meta.outputs.version }} -d /tmp/ - helm push /tmp/{{ repo_name }}-${{ steps.meta.outputs.version }}.tgz oci://ghcr.io/${{ github.repository_owner }}/charts -{% end if %} diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" new file mode 100644 index 00000000..2848695f --- /dev/null +++ "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" @@ -0,0 +1,33 @@ +on: + workflow_call: + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Create tags for publishing image + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=semver,pattern={{version}} + type=ref,event=tag + type=raw,value=latest + + - name: Install Helm + uses: Azure/setup-helm@v4 + id: install + + - name: Log in to GitHub Container Registry + if: github.ref_type == 'tag' + run: | + echo ${{ secrets.GITHUB_TOKEN }} | helm registry login ghcr.io/${{ github.repository }} --username ${{ github.repository_owner }} --password-stdin + + - name: package chart and push it + if: github.ref_type == 'tag' + run: | + helm dependencies update helm/{{ repo_name }} + helm package helm/{{ repo_name }} {% raw %}--version ${{ steps.meta.outputs.version }} --app-version ${{ steps.meta.outputs.version }}{% endraw %} -d /tmp/ + helm push /tmp/{{ repo_name }}-{% raw %}${{ steps.meta.outputs.version }}.tgz oci://ghcr.io/${{ github.repository_owner }}{% endraw %}/charts From 8490124515c00e9f7a6b4293053acdfed2d544a1 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Wed, 19 Mar 2025 15:24:34 +0000 Subject: [PATCH 05/13] Run Helm job when tagged --- .../workflows/ci.yml.jinja" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/ci.yml.jinja" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/ci.yml.jinja" index c6ed106f..790c584d 100644 --- "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/ci.yml.jinja" +++ "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/ci.yml.jinja" @@ -41,6 +41,13 @@ jobs: permissions: contents: read packages: write +{% endif %}{% if helm %} + helm: + needs: container + uses: ./.github/workflows/_helm.yml + permissions: + contents: read + packages: write {% endif %}{% if sphinx %} docs: needs: check From d06a87a46c2bf22a500bafaba136c95dd87c9b92 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Wed, 19 Mar 2025 15:35:59 +0000 Subject: [PATCH 06/13] Linting and test fixes --- copier.yml | 2 +- docs/how-to/deploy-cluster.md | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/copier.yml b/copier.yml index 5a637fcc..a2d13378 100644 --- a/copier.yml +++ b/copier.yml @@ -105,7 +105,7 @@ docker: helm: type: bool - when: "{{ github_org == 'DiamondLightSource' && docker}}" + when: "{{ github_org == 'DiamondLightSource'}} and docker" help: | Would you like to publish a Helm chart? You should select this if you are making a long-running service. diff --git a/docs/how-to/deploy-cluster.md b/docs/how-to/deploy-cluster.md index d964ff8b..9c6dba27 100644 --- a/docs/how-to/deploy-cluster.md +++ b/docs/how-to/deploy-cluster.md @@ -139,4 +139,3 @@ The following is a launch configuration from VSCode `launch.json`. } } ``` - From 842fd630c1b7053561e43ca043d9294a202f1519 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Wed, 19 Mar 2025 17:02:42 +0000 Subject: [PATCH 07/13] Do not try and lint yaml files in Helm templates --- .pre-commit-config.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b5d4d7fa..c6148bd1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,6 +4,8 @@ repos: hooks: - id: check-added-large-files - id: check-yaml + # These yaml files output valid yaml only after templating + exclude: ^helm/{{ repo_name }}/templates/ - id: check-merge-conflict - id: end-of-file-fixer From b86e1a16ca05bce1a3a8c807575c0b0d7fe827d2 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Wed, 19 Mar 2025 17:16:46 +0000 Subject: [PATCH 08/13] Fix Helm action --- .../workflows/{% if helm %}_helm.yml{% endif %}" | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" index 2848695f..77e93895 100644 --- "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" +++ "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" @@ -1,3 +1,7 @@ +env: + CHART_NAME: {{ repo_name }} +{% raw %} + on: workflow_call: @@ -20,6 +24,9 @@ jobs: uses: Azure/setup-helm@v4 id: install + - name: Lint Helm chart + run: helm lint helm/{{ repo_name }} + - name: Log in to GitHub Container Registry if: github.ref_type == 'tag' run: | @@ -28,6 +35,7 @@ jobs: - name: package chart and push it if: github.ref_type == 'tag' run: | - helm dependencies update helm/{{ repo_name }} - helm package helm/{{ repo_name }} {% raw %}--version ${{ steps.meta.outputs.version }} --app-version ${{ steps.meta.outputs.version }}{% endraw %} -d /tmp/ - helm push /tmp/{{ repo_name }}-{% raw %}${{ steps.meta.outputs.version }}.tgz oci://ghcr.io/${{ github.repository_owner }}{% endraw %}/charts + helm dependencies update helm/${{ env.CHART_NAME }} + helm package helm/${{ env.CHART_NAME }} --version ${{ steps.meta.outputs.version }} --app-version ${{ steps.meta.outputs.version }} -d /tmp/ + helm push /tmp/${{ env.CHART_NAME }}-${{ steps.meta.outputs.version }}.tgz oci://ghcr.io/${{ github.repository_owner }}/charts +{% endraw %} From 75fb3075c3aa17c81dc582632788791d8e1007e8 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Wed, 19 Mar 2025 17:21:27 +0000 Subject: [PATCH 09/13] Checkout chart before linting it --- .../workflows/{% if helm %}_helm.yml{% endif %}" | 3 +++ 1 file changed, 3 insertions(+) diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" index 77e93895..ae3b6277 100644 --- "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" +++ "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" @@ -10,6 +10,9 @@ jobs: runs-on: ubuntu-latest steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Create tags for publishing image id: meta uses: docker/metadata-action@v5 From 96cdd92494ddf5f84d63cc32b7030f17e7bd3acf Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Thu, 20 Mar 2025 13:29:14 +0000 Subject: [PATCH 10/13] Add Helm chart --- copier.yml | 12 ++ .../{% if helm %}_helm.yml{% endif %}" | 27 ++-- .../{{ chart_name }}/.helmignore | 23 ++++ .../{{ chart_name }}/Chart.yaml | 24 ++++ .../{{ chart_name }}/templates/NOTES.txt | 22 +++ .../{{ chart_name }}/templates/_helpers.tpl | 62 +++++++++ .../templates/deployment.yaml | 68 ++++++++++ .../{{ chart_name }}/templates/hpa.yaml | 32 +++++ .../{{ chart_name }}/templates/ingress.yaml | 43 ++++++ .../{{ chart_name }}/templates/service.yaml | 15 +++ .../templates/serviceaccount.yaml | 13 ++ .../templates/tests/test-connection.yaml | 15 +++ .../{{ chart_name }}/values.yaml | 127 ++++++++++++++++++ 13 files changed, 470 insertions(+), 13 deletions(-) create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/.helmignore create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/Chart.yaml create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/NOTES.txt create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/_helpers.tpl create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/hpa.yaml create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/ingress.yaml create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/service.yaml create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/serviceaccount.yaml create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/tests/test-connection.yaml create mode 100644 template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml diff --git a/copier.yml b/copier.yml index a2d13378..5a128908 100644 --- a/copier.yml +++ b/copier.yml @@ -111,6 +111,18 @@ helm: You should select this if you are making a long-running service. This requires that releases use Semantic Versioning +chart_name: + type: str + when: helm + default: "{{ package_name | replace('_', '') | lower() }}" + help: | + Chart names must be lower case letters and numbers. Words may be separated with dashes (-) + But dashes may cause issues with templating. + validator: >- + {% if not (package_name | regex_search('^[a-z0-9]+(?:-?[a-z0-9])+$')) %} + {{chart_name}} is not a valid Helm chart name + {% endif %} + docs_type: type: str help: | diff --git "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" index ae3b6277..8f2e8327 100644 --- "a/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" +++ "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" @@ -1,8 +1,4 @@ -env: - CHART_NAME: {{ repo_name }} -{% raw %} - -on: +{% raw -%}on: workflow_call: jobs: @@ -10,35 +6,40 @@ jobs: runs-on: ubuntu-latest steps: + - name: Assign environment variables + # This is required because oci requires the repository is lowercase + run: | + echo "CHART_REPO=ghcr.io/${OWNER@L}" >>${GITHUB_ENV} + echo "CHART_NAME=${CHART@L}" >>${GITHUB_ENV} + env: + OWNER: "${{ github.repository_owner }}" + CHART: {% endraw %}{{ chart_name }}{% raw %} - name: Checkout uses: actions/checkout@v4 - - name: Create tags for publishing image + - name: Create tag for publishing chart id: meta uses: docker/metadata-action@v5 with: images: ghcr.io/${{ github.repository }} tags: | type=semver,pattern={{version}} - type=ref,event=tag - type=raw,value=latest - name: Install Helm uses: Azure/setup-helm@v4 id: install - name: Lint Helm chart - run: helm lint helm/{{ repo_name }} + run: helm lint helm/${{ env.CHART_NAME }} - name: Log in to GitHub Container Registry if: github.ref_type == 'tag' run: | - echo ${{ secrets.GITHUB_TOKEN }} | helm registry login ghcr.io/${{ github.repository }} --username ${{ github.repository_owner }} --password-stdin + echo ${{ secrets.GITHUB_TOKEN }} | helm registry login ${{ env.CHART_REPO }} --username ${{ github.repository_owner }} --password-stdin - - name: package chart and push it + - name: Publish Helm chart to container registry if: github.ref_type == 'tag' run: | helm dependencies update helm/${{ env.CHART_NAME }} helm package helm/${{ env.CHART_NAME }} --version ${{ steps.meta.outputs.version }} --app-version ${{ steps.meta.outputs.version }} -d /tmp/ - helm push /tmp/${{ env.CHART_NAME }}-${{ steps.meta.outputs.version }}.tgz oci://ghcr.io/${{ github.repository_owner }}/charts -{% endraw %} + helm push /tmp/${{ env.CHART_NAME }}-${{ steps.meta.outputs.version }}.tgz oci://${{ env.CHART_REPO }}/charts{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/.helmignore b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/Chart.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/Chart.yaml new file mode 100644 index 00000000..42407c02 --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: {{ chart_name }} +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/NOTES.txt b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/NOTES.txt new file mode 100644 index 00000000..e7dc5e5a --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/NOTES.txt @@ -0,0 +1,22 @@ +{% raw -%}1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch its status by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "{% endraw %}{{ chart_name }}{% raw %}.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + export CONTAINER_PORT=$(kubectl get pod --namespace {{ .Release.Namespace }} $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:$CONTAINER_PORT +{{- end }}{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/_helpers.tpl b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/_helpers.tpl new file mode 100644 index 00000000..5ea6006a --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{% raw -%}{{/* +Expand the name of the chart. +*/}} +{{- define "{% endraw %}{{ chart_name }}{% raw %}.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "{% endraw %}{{ chart_name }}{% raw %}.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "{% endraw %}{{ chart_name }}{% raw %}.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "{% endraw %}{{ chart_name }}{% raw %}.labels" -}} +helm.sh/chart: {{ include "{% endraw %}{{ chart_name }}{% raw %}.chart" . }} +{{ include "{% endraw %}{{ chart_name }}{% raw %}.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "{% endraw %}{{ chart_name }}{% raw %}.selectorLabels" -}} +app.kubernetes.io/name: {{ include "{% endraw %}{{ chart_name }}{% raw %}.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "{% endraw %}{{ chart_name }}{% raw %}.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "{% endraw %}{{ chart_name }}{% raw %}.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }}{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml new file mode 100644 index 00000000..95b40873 --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml @@ -0,0 +1,68 @@ +{% raw -%}apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }} + labels: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.labels" . | nindent 8 }} + {{- with .Values.podLabels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "{% endraw %}{{ chart_name }}{% raw %}.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: {{ .Values.service.port }} + protocol: TCP + livenessProbe: + {{- toYaml .Values.livenessProbe | nindent 12 }} + readinessProbe: + {{- toYaml .Values.readinessProbe | nindent 12 }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.volumeMounts }} + volumeMounts: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.volumes }} + volumes: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }}{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/hpa.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/hpa.yaml new file mode 100644 index 00000000..0523c1c3 --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/hpa.yaml @@ -0,0 +1,32 @@ +{% raw %}{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }} + labels: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + target: + type: Utilization + averageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }}{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/ingress.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/ingress.yaml new file mode 100644 index 00000000..4e7c6565 --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/ingress.yaml @@ -0,0 +1,43 @@ +{% raw %}{{- if .Values.ingress.enabled -}} +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }} + labels: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- with .Values.ingress.className }} + ingressClassName: {{ . }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- with .pathType }} + pathType: {{ . }} + {{- end }} + backend: + service: + name: {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" $ }} + port: + number: {{ $.Values.service.port }} + {{- end }} + {{- end }} +{{- end }}{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/service.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/service.yaml new file mode 100644 index 00000000..ce3c5ce6 --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/service.yaml @@ -0,0 +1,15 @@ +{% raw -%}apiVersion: v1 +kind: Service +metadata: + name: {{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }} + labels: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.selectorLabels" . | nindent 4 }}{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/serviceaccount.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/serviceaccount.yaml new file mode 100644 index 00000000..20a9f0c1 --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{% raw -%}{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "{% endraw %}{{ chart_name }}{% raw %}.serviceAccountName" . }} + labels: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }}{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/tests/test-connection.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/tests/test-connection.yaml new file mode 100644 index 00000000..dc49f36e --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +{% raw -%}apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }}-test-connection" + labels: + {{- include "{% endraw %}{{ chart_name }}{% raw %}.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "{% endraw %}{{ chart_name }}{% raw %}.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never{% endraw %} diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml new file mode 100644 index 00000000..9fd73160 --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml @@ -0,0 +1,127 @@ +# Default values for {{ chart_name }}. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +# This will set the replicaset count more information can be found here: https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/ +replicaCount: 1 + +# This sets the container image more information can be found here: https://kubernetes.io/docs/concepts/containers/images/ +image: + repository: nginx + # This sets the pull policy for images. + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +# This is for the secretes for pulling an image from a private repository more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/ +imagePullSecrets: [] +# This is to override the chart name. +nameOverride: "" +fullnameOverride: "" + +#This section builds out the service account more information can be found here: https://kubernetes.io/docs/concepts/security/service-accounts/ +serviceAccount: + # Specifies whether a service account should be created + create: true + # Automatically mount a ServiceAccount's API credentials? + automount: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +# This is for setting Kubernetes Annotations to a Pod. +# For more information checkout: https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/ +podAnnotations: {} +# This is for setting Kubernetes Labels to a Pod. +# For more information checkout: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ +podLabels: {} + +podSecurityContext: + {} + # fsGroup: 2000 + +securityContext: + {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +# This is for setting up a service more information can be found here: https://kubernetes.io/docs/concepts/services-networking/service/ +service: + # This sets the service type more information can be found here: https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types + type: ClusterIP + # This sets the ports more information can be found here: https://kubernetes.io/docs/concepts/services-networking/service/#field-spec-ports + port: 80 + +# This block is for setting up the ingress for more information can be found here: https://kubernetes.io/docs/concepts/services-networking/ingress/ +ingress: + enabled: false + className: "" + annotations: + {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: + {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +# This is to setup the liveness and readiness probes more information can be found here: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/ +livenessProbe: + httpGet: + path: / + port: http +readinessProbe: + httpGet: + path: / + port: http + +#This section is for setting up autoscaling more information can be found here: https://kubernetes.io/docs/concepts/workloads/autoscaling/ +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +# Additional volumes on the output Deployment definition. +volumes: [] +# - name: foo +# secret: +# secretName: mysecret +# optional: false + +# Additional volumeMounts on the output Deployment definition. +volumeMounts: [] +# - name: foo +# mountPath: "/etc/foo" +# readOnly: true + +nodeSelector: {} + +tolerations: [] + +affinity: {} From 41d715421de073d3d3e22eda24a2f9ec97755a44 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Thu, 20 Mar 2025 14:01:19 +0000 Subject: [PATCH 11/13] Add debugging to the pre-generated Helm chart --- .../{{ chart_name }}/templates/deployment.yaml | 14 ++++++++++++++ .../{{ chart_name }}/values.yaml | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml index 95b40873..ecf3412e 100644 --- a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml @@ -35,6 +35,20 @@ spec: securityContext: {{- toYaml .Values.securityContext | nindent 12 }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + args: + {{- if .Values.debug.enabled}} + - "-Xfrozen_modules=off" + - "-m" + - "debugpy" + {{- if .Values.debug.suspend }} + - "--wait-for-client" + {{- end }} + - "--listen" + - "0.0.0.0:{{ .Values.debug.port }}" + {{- end }} + - "-m" + - {% endraw %}"{{ repo_name }}"{% raw %} + - "--version" imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml index 9fd73160..e7f9b3aa 100644 --- a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml @@ -2,6 +2,15 @@ # This is a YAML-formatted file. # Declare variables to be passed into your templates. +# Use `kubectl port forward` to access from your machine +debug: + # Whether the container should start in debug mode + enabled: false + # Whether to suspend the process until a debugger connects + suspend: false + # Port to listen for the debugger on + port: 5678 + # This will set the replicaset count more information can be found here: https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/ replicaCount: 1 From c1dac1ddbb75091ab6e86dd33209039a5971ec07 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Thu, 20 Mar 2025 14:41:34 +0000 Subject: [PATCH 12/13] Finish Helm? --- docs/how-to/deploy-cluster.md | 73 ++++++------------- .../{{ chart_name }}/values.yaml | 2 +- 2 files changed, 25 insertions(+), 50 deletions(-) diff --git a/docs/how-to/deploy-cluster.md b/docs/how-to/deploy-cluster.md index 9c6dba27..1ddd1b10 100644 --- a/docs/how-to/deploy-cluster.md +++ b/docs/how-to/deploy-cluster.md @@ -1,10 +1,7 @@ -# How to deploy containers to Kubernetes cluster +# Deploy with Helm -If your project is a service with a `Dockerfile`, you may wish to deploy it in a Kubernetes cluster. - -## Creating a Helm chart - -Helm bundles multiple Kubernetes resources into a single top level resource, `Chart`, and templates resources to inject specified `values`. +If your project is a service, you may wish to deploy it into a Kubernetes cluster using Helm. +If enabled, a `helm/` directory is created, which bundles Kubernetes resources into a top level resource, `Chart`, and templates resources to inject specified `values`. ``` service/ @@ -14,35 +11,20 @@ Helm bundles multiple Kubernetes resources into a single top level resource, `Ch └── templates/ # Templated Kubernetes resources ``` -`templates/` may include at least: +`templates/` includes among others: - `deployment.yaml`: creates a pod including your container image - `service.yaml`: manages Kubernetes networking, potentially exposing your service - `ingress.yaml`: optionally maps a DNS entry to the Kubernetes networking -Using `helm create` ensures your service is using the latest standards, therefore Helm resources are not included in this template. -To avoid collisions and to maintain a neat repository, it is recommended to run `helm create ` inside a directory named `helm/` in the root of your repository. - -Assuming your container is published to the GitHub container registry, modify your `values.yaml` to deploy your built container. +Assuming your container is published to the GitHub container registry, `values.yaml` will be pre-configured to use your built container and enable debugging. ```yaml image: - repository: ghcr.io// + repository: ghcr.io/{{ organisation }}/{{ repo_name }} pullPolicy: Always # Overrides the image tag whose default is the chart appVersion. tag: "" -``` - -The container will use the `ENTRYPOINT` and `CMD` defined in your `Dockerfile`. - -It is recommended to preserve all of the templates within `templates/`: resources you do not need can be disabled from `values.yaml` while maintaining the ability to deploy or extend the chart. - -## Enabling container debugging -The generated `Dockerfile` installs debugpy and with a few modifications can enable remote debugging of a service deployed inside a cluster. - -Adding the following to your `values.yaml` gives a standard way of enabling/disabling debugging and documenting the configuration. - -```yaml # Use `kubectl port forward` to access from your machine debug: # Whether the container should start in debug mode @@ -53,39 +35,32 @@ debug: port: 5678 ``` +To enable debugging, the CMD arguments of the Dockerfile have been overwritten by the analogous `args` from Kubernetes. The `ENTRYPOINT` and `CMD` concepts in the Dockerfile are analogous to Kubernetes' `command` and `args`. If `command` is set, it overrides `ENTRYPOINT` and uses `args` if set, ignoring `CMD`. If `args` is set, `ENTRYPOINT` remains and `CMD` is replaced. -Assuming your `Dockerfile` contains the following, or analogous: - -```Dockerfile -ENTRYPOINT ["python"] -CMD ["-m", "service", "--version"] -``` - -Modifying `deployment.yaml` in the following way allows for your service to enable debugging via the configuration added to `values.yaml`. ```yaml - containers: - - ... - args: - {{- if .Values.debug.enabled}} - - "-Xfrozen_modules=off" - - "-m" - - "debugpy" - {{- if .Values.debug.suspend }} - - "--wait-for-client" - {{- end }} - - "--listen" - - "0.0.0.0:{{ .Values.debug.port }}" - {{- end }} - - "-m" - - "service" - - "--version" + args: + {{- if .Values.debug.enabled}} + - "-Xfrozen_modules=off" + - "-m" + - "debugpy" + {{- if .Values.debug.suspend }} + - "--wait-for-client" + {{- end }} + - "--listen" + - "0.0.0.0:{{ .Values.debug.port }}" + {{- end }} + - "-m" + - "service" + - "--version" ``` -## Connecting to debug mode container +It is recommended to preserve all of the templates within `templates/`: resources you do not need can be disabled from `values.yaml` while maintaining the ability to deploy or extend the chart. + +## Connecting to a container in debug mode `kubectl port forward` forwards your development machine's port 5678 to the container's: diff --git a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml index e7f9b3aa..e1ad93fa 100644 --- a/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml @@ -16,7 +16,7 @@ replicaCount: 1 # This sets the container image more information can be found here: https://kubernetes.io/docs/concepts/containers/images/ image: - repository: nginx + repository: ghcr.io/{{ organisation }}/{{ repo_name }} # This sets the pull policy for images. pullPolicy: IfNotPresent # Overrides the image tag whose default is the chart appVersion. From e5f94814b0d3a8bcd1acf2ab3514deb9b795b1f4 Mon Sep 17 00:00:00 2001 From: "Ware, Joseph (DLSLtd,RAL,LSCI)" Date: Thu, 20 Mar 2025 15:10:46 +0000 Subject: [PATCH 13/13] Configure pre-commit to ignore Helm templates --- .pre-commit-config.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c6148bd1..809c5442 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,11 @@ repos: - id: check-added-large-files - id: check-yaml # These yaml files output valid yaml only after templating - exclude: ^helm/{{ repo_name }}/templates/ + ignore: + - "template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/" + - "template/{% if helm %}helm{% endif %}/{{ chart_name }}/Chart.yaml" + - "{% if helm %}helm{% endif %}/{{ chart_name }}/templates/" + - "{% if helm %}helm{% endif %}/{{ chart_name }}/Chart.yaml" - id: check-merge-conflict - id: end-of-file-fixer