diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b5d4d7fa..809c5442 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,6 +4,12 @@ repos: hooks: - id: check-added-large-files - id: check-yaml + # These yaml files output valid yaml only after templating + 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 diff --git a/copier.yml b/copier.yml index 046b88b2..5a128908 100644 --- a/copier.yml +++ b/copier.yml @@ -103,6 +103,26 @@ 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'}} and 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 + +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/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. 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..1ddd1b10 --- /dev/null +++ b/docs/how-to/deploy-cluster.md @@ -0,0 +1,116 @@ +# Deploy with Helm + +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/ + ├── Chart.yaml # Definition of the resource + ├── values.yaml # Defaults for templating + ├── charts/ # [Optionally] other charts to deploy with + └── templates/ # Templated Kubernetes resources +``` + +`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 + +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/{{ organisation }}/{{ repo_name }} + pullPolicy: Always + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +# 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 +``` + +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. + + +```yaml + 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" +``` + +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: + +```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 c5ca109b..d8524bd0 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"] +# Allows for modifying the ENTRYPOINT for debugging, e.g. +#ENTRYPOINT ["python", "-m", "debugpy"] +CMD ["-m", "{{ repo_name }}", "--version"]{% endif %} 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 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..8f2e8327 --- /dev/null +++ "b/template/{% if git_platform==\"github.com\" %}.github{% endif %}/workflows/{% if helm %}_helm.yml{% endif %}" @@ -0,0 +1,45 @@ +{% raw -%}on: + workflow_call: + +jobs: + build: + 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 tag for publishing chart + id: meta + uses: docker/metadata-action@v5 + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=semver,pattern={{version}} + + - name: Install Helm + uses: Azure/setup-helm@v4 + id: install + + - name: Lint Helm chart + 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 ${{ env.CHART_REPO }} --username ${{ github.repository_owner }} --password-stdin + + - 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://${{ 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..ecf3412e --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/templates/deployment.yaml @@ -0,0 +1,82 @@ +{% 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 }}" + 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 + 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..e1ad93fa --- /dev/null +++ b/template/{% if helm %}helm{% endif %}/{{ chart_name }}/values.yaml @@ -0,0 +1,136 @@ +# Default values for {{ chart_name }}. +# 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 + +# This sets the container image more information can be found here: https://kubernetes.io/docs/concepts/containers/images/ +image: + 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. + 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: {}