diff --git a/.github/workflows/_helm.yml b/.github/workflows/_helm.yml new file mode 100644 index 0000000..b99517e --- /dev/null +++ b/.github/workflows/_helm.yml @@ -0,0 +1,59 @@ +on: + workflow_call: + inputs: + publish: + type: boolean + description: If true, pushes the chart to the OCI registry (tags only) + +env: + HELM_VERSION_TO_INSTALL: 3.17.1 + +jobs: + chart: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Install helm + uses: Azure/setup-helm@v4 + with: + version: ${{ env.HELM_VERSION_TO_INSTALL }} + + # Chart versions must be semver. An early check saves waiting for the + # whole build before finding a problem. + - name: Check helm version tag + if: github.ref_type == 'tag' + env: + VERSION: ${{ github.ref_name }} + run: | + if [[ "${VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+((-alpha|-beta|-rc)\.[0-9]+)?$ ]]; then + echo "Valid version format: '${VERSION}'" + else + echo "Invalid version: '${VERSION}'. Expected: 'X.Y.Z' or 'X.Y.Z-beta.1'" + exit 1 + fi + + - name: Lint and render chart + run: | + helm lint Charts/tiled + helm template tiled Charts/tiled > /dev/null + + # The chart version and appVersion are both the git tag. appVersion is the + # default image tag, so a chart release deploys the image of the same tag. + - name: Package helm chart + env: + VERSION: ${{ github.ref_type == 'tag' && github.ref_name || '0.0.0' }} + run: | + helm package --version "${VERSION}" --app-version "${VERSION}" Charts/tiled + + - name: Push tagged helm chart to registry + if: inputs.publish && github.ref_type == 'tag' + env: + OWNER: ${{ github.repository_owner }} + TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + echo "${TOKEN}" | helm registry login ghcr.io --username "${OWNER}" --password-stdin + REGISTRY="oci://ghcr.io/${OWNER}/charts" + helm push tiled-*.tgz "${REGISTRY,,}" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1cb96e0..d0747ec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,11 +37,21 @@ jobs: contents: read packages: write + helm: + needs: test + if: always() + uses: ./.github/workflows/_helm.yml + with: + publish: ${{ needs.test.result == 'success' }} + permissions: + contents: read + packages: write + dist: uses: ./.github/workflows/_dist.yml release: - needs: [dist, test] + needs: [dist, test, container, helm] if: github.ref_type == 'tag' uses: ./.github/workflows/_release.yml permissions: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 10d7415..249388b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,6 +5,7 @@ repos: - id: check-added-large-files exclude: ^uv.lock - id: check-yaml + exclude: ^Charts/.*/templates/ - id: check-merge-conflict - id: end-of-file-fixer diff --git a/Charts/tiled/.helmignore b/Charts/tiled/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/Charts/tiled/.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/Charts/tiled/Chart.yaml b/Charts/tiled/Chart.yaml new file mode 100644 index 0000000..612a076 --- /dev/null +++ b/Charts/tiled/Chart.yaml @@ -0,0 +1,20 @@ +apiVersion: v2 +name: tiled +description: Tiled server (the dls-tiled image, upstream tiled plus Diamond Light Source plugins). + +# 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 + +# Chart version, stamped from the git tag by CI (helm package --version) +version: 0.0.0 + +# Container version, stamped from the git tag by CI (helm package --app-version). +# It is the default image tag, so a chart release deploys the image of the same tag. +appVersion: 0.0.0 diff --git a/Charts/tiled/templates/NOTES.txt b/Charts/tiled/templates/NOTES.txt new file mode 100644 index 0000000..2560662 --- /dev/null +++ b/Charts/tiled/templates/NOTES.txt @@ -0,0 +1,22 @@ +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 "tiled.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 "tiled.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "tiled.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 "tiled.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 }} diff --git a/Charts/tiled/templates/_helpers.tpl b/Charts/tiled/templates/_helpers.tpl new file mode 100644 index 0000000..2312e52 --- /dev/null +++ b/Charts/tiled/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "tiled.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 "tiled.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 "tiled.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "tiled.labels" -}} +helm.sh/chart: {{ include "tiled.chart" . }} +{{ include "tiled.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "tiled.selectorLabels" -}} +app.kubernetes.io/name: {{ include "tiled.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "tiled.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "tiled.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/Charts/tiled/templates/configmap.yaml b/Charts/tiled/templates/configmap.yaml new file mode 100644 index 0000000..aefbd0f --- /dev/null +++ b/Charts/tiled/templates/configmap.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "tiled.fullname" . }}-config +data: + config.yaml: |- + {{- toYaml .Values.config | nindent 4 }} diff --git a/Charts/tiled/templates/deployment.yaml b/Charts/tiled/templates/deployment.yaml new file mode 100644 index 0000000..b340bde --- /dev/null +++ b/Charts/tiled/templates/deployment.yaml @@ -0,0 +1,85 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "tiled.fullname" . }} + labels: + {{- include "tiled.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "tiled.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "tiled.labels" . | nindent 8 }} + {{- with .Values.podLabels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "tiled.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 }} + {{- with .Values.command }} + command: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.args }} + args: + {{- toYaml . | nindent 12 }} + {{- end }} + 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 }} + volumeMounts: + {{- with .Values.volumeMounts }} + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with .Values.extraEnvVars }} + env: + {{- toYaml . | nindent 12 }} + {{- end }} + volumes: + - name: config + projected: + sources: + - configMap: + name: {{ include "tiled.fullname" . }}-config + {{- with .Values.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 }} diff --git a/Charts/tiled/templates/hpa.yaml b/Charts/tiled/templates/hpa.yaml new file mode 100644 index 0000000..26e05d5 --- /dev/null +++ b/Charts/tiled/templates/hpa.yaml @@ -0,0 +1,32 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "tiled.fullname" . }} + labels: + {{- include "tiled.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "tiled.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 }} diff --git a/Charts/tiled/templates/ingress.yaml b/Charts/tiled/templates/ingress.yaml new file mode 100644 index 0000000..64cc215 --- /dev/null +++ b/Charts/tiled/templates/ingress.yaml @@ -0,0 +1,43 @@ +{{- if .Values.ingress.enabled -}} +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: {{ include "tiled.fullname" . }} + labels: + {{- include "tiled.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 "tiled.fullname" $ }} + port: + number: {{ $.Values.service.port }} + {{- end }} + {{- end }} +{{- end }} diff --git a/Charts/tiled/templates/service.yaml b/Charts/tiled/templates/service.yaml new file mode 100644 index 0000000..5feb1af --- /dev/null +++ b/Charts/tiled/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "tiled.fullname" . }} + labels: + {{- include "tiled.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "tiled.selectorLabels" . | nindent 4 }} diff --git a/Charts/tiled/templates/serviceaccount.yaml b/Charts/tiled/templates/serviceaccount.yaml new file mode 100644 index 0000000..3a7c569 --- /dev/null +++ b/Charts/tiled/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "tiled.serviceAccountName" . }} + labels: + {{- include "tiled.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }} diff --git a/Charts/tiled/templates/tests/test-connection.yaml b/Charts/tiled/templates/tests/test-connection.yaml new file mode 100644 index 0000000..54d3daa --- /dev/null +++ b/Charts/tiled/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "tiled.fullname" . }}-test-connection" + labels: + {{- include "tiled.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "tiled.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/Charts/tiled/values.yaml b/Charts/tiled/values.yaml new file mode 100644 index 0000000..7a89b08 --- /dev/null +++ b/Charts/tiled/values.yaml @@ -0,0 +1,153 @@ +# Default values for tiled. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +# The tiled server config, rendered into a ConfigMap mounted at /deploy/config. +config: + # Opt in to the dls-tiled plugins by listing their routers, e.g. the ViSR binning + # endpoint: + # routers: + # - dls_tiled.visr:visr_router + authentication: + allow_anonymous_access: false + trees: + - path: / + tree: catalog + args: + uri: "sqlite:////storage/catalog.db" + writable_storage: "/storage/data" + init_if_not_exists: true + +# 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/diamondlightsource/dls-tiled + # This sets the pull policy for images. + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +# Override the container command. If not set, the image's CMD is used. +command: [] + +# Override the container args. If not set, the image's ENTRYPOINT args are used. +args: [] + +# 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: false + # 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: 8000 + +# 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: /healthz + port: http +readinessProbe: + httpGet: + path: /healthz + 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: [] + + +# Additional volumeMounts on the output Deployment definition. +volumeMounts: + - name: config + mountPath: "/deploy/config" + readOnly: true + +nodeSelector: {} + +tolerations: [] + +affinity: {} + + +# Additional envVars to mount to the pod. The API key secret is optional so the +# chart installs without it; create a secret named tiled-secrets to set the key. +extraEnvVars: +- name: TILED_SINGLE_USER_API_KEY + valueFrom: + secretKeyRef: + name: tiled-secrets + key: TILED_SINGLE_USER_API_KEY + optional: true diff --git a/README.md b/README.md index 4e6f0d2..828956e 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,20 @@ routers: Ported from Hiran Wijesinghe's [visr-tiled](https://github.com/hyperrealist/visr-tiled). +## Helm chart + +`Charts/tiled` is a generic Helm chart for the image, published on each tag to +`oci://ghcr.io/diamondlightsource/charts/tiled`. The chart version and appVersion +are both the git tag, and appVersion is the default image tag. + +```bash +helm install tiled oci://ghcr.io/diamondlightsource/charts/tiled --version X.Y.Z +``` + +Plugins are off by default; see the `config.routers` comment in +`Charts/tiled/values.yaml`. The `TILED_SINGLE_USER_API_KEY` environment variable +is read from the optional secret `tiled-secrets`. + ## Logging tiled's default logging config has no handler for the plugin loggers, so their