From 5a290383add900e076fb461ab74c11ae93515b2d Mon Sep 17 00:00:00 2001 From: Alex Manning Date: Fri, 3 Mar 2023 21:56:07 +0000 Subject: [PATCH] Add standalone chart for homeassistant. --- .woodpecker/homeassistant.yaml | 13 ++ charts/homeassistant/.helmignore | 23 ++++ charts/homeassistant/Chart.yaml | 24 ++++ charts/homeassistant/templates/_helpers.tpl | 62 +++++++++ .../homeassistant/templates/certificate.yaml | 22 ++++ .../templates/configmap-init.yaml | 18 +++ charts/homeassistant/templates/configmap.yaml | 30 +++++ .../homeassistant/templates/deployment.yaml | 122 ++++++++++++++++++ charts/homeassistant/templates/pvc.yaml | 35 +++++ charts/homeassistant/values.yaml | 69 ++++++++++ 10 files changed, 418 insertions(+) create mode 100644 .woodpecker/homeassistant.yaml create mode 100644 charts/homeassistant/.helmignore create mode 100644 charts/homeassistant/Chart.yaml create mode 100644 charts/homeassistant/templates/_helpers.tpl create mode 100644 charts/homeassistant/templates/certificate.yaml create mode 100644 charts/homeassistant/templates/configmap-init.yaml create mode 100644 charts/homeassistant/templates/configmap.yaml create mode 100644 charts/homeassistant/templates/deployment.yaml create mode 100644 charts/homeassistant/templates/pvc.yaml create mode 100644 charts/homeassistant/values.yaml diff --git a/.woodpecker/homeassistant.yaml b/.woodpecker/homeassistant.yaml new file mode 100644 index 0000000..b8ca560 --- /dev/null +++ b/.woodpecker/homeassistant.yaml @@ -0,0 +1,13 @@ +--- +when: + path: "charts/homeassistant/**" +branches: + - main +pipeline: + package: + image: "docker.io/alpine/helm:latest" + secrets: [helm_user, helm_password, helm_repo] + commands: + - helm plugin install https://github.com/chartmuseum/helm-push + - helm repo add therepo $HELM_REPO --username $HELM_USER --password $HELM_PASSWORD + - helm cm-push charts/homeassistant therepo diff --git a/charts/homeassistant/.helmignore b/charts/homeassistant/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/charts/homeassistant/.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/homeassistant/Chart.yaml b/charts/homeassistant/Chart.yaml new file mode 100644 index 0000000..a139a3f --- /dev/null +++ b/charts/homeassistant/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: homeassistant +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/charts/homeassistant/templates/_helpers.tpl b/charts/homeassistant/templates/_helpers.tpl new file mode 100644 index 0000000..f9ab598 --- /dev/null +++ b/charts/homeassistant/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "homeassistant.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 "homeassistant.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 "homeassistant.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "homeassistant.labels" -}} +helm.sh/chart: {{ include "homeassistant.chart" . }} +{{ include "homeassistant.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "homeassistant.selectorLabels" -}} +app.kubernetes.io/name: {{ include "homeassistant.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "homeassistant.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "homeassistant.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/charts/homeassistant/templates/certificate.yaml b/charts/homeassistant/templates/certificate.yaml new file mode 100644 index 0000000..89dd9ae --- /dev/null +++ b/charts/homeassistant/templates/certificate.yaml @@ -0,0 +1,22 @@ +{{- if .Values.cert.dnsNames }} +apiVersion: cert-manager.io/v1 +kind: Certificate +metadata: + name: {{ include "homeassistant.fullname" . }} + labels: + {{- include "homeassistant.labels" . | nindent 4 }} +spec: + secretName: {{ include "homeassistant.fullname" . }}-cert-secret + secretTemplate: + labels: + {{- include "homeassistant.labels" . | nindent 6 }} + duration: 2160h + renewBefore: 360h + dnsNames: + {{- range .Values.cert.dnsNames }} + - {{ . | quote}} + {{- end }} + issuerRef: + name: {{ .Values.cert.issuerName }} + kind: ClusterIssuer +{{- end }} diff --git a/charts/homeassistant/templates/configmap-init.yaml b/charts/homeassistant/templates/configmap-init.yaml new file mode 100644 index 0000000..26457ae --- /dev/null +++ b/charts/homeassistant/templates/configmap-init.yaml @@ -0,0 +1,18 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "homeassistant.fullname" . }}-initscript + labels: + {{- include "homeassistant.labels" . | nindent 4 }} +data: + init.py: | + import pathlib + # Symlink some paths to move them outside the config volume. + paths = {"/config/.storage": "/media/storage", "/config/configuration.yaml": "/media/configuration/configuration.yaml"} + for from_p, to in paths.items(): + try: + from_path = pathlib.Path(from_p) + from_path.symlink_to(to) + print(f'Created {from_p} -> {to}') + except FileExistsError: + print(f'{from_p} -> {to} link already existed.') diff --git a/charts/homeassistant/templates/configmap.yaml b/charts/homeassistant/templates/configmap.yaml new file mode 100644 index 0000000..5469e74 --- /dev/null +++ b/charts/homeassistant/templates/configmap.yaml @@ -0,0 +1,30 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "homeassistant.fullname" . }}-configuration + labels: + {{- include "homeassistant.labels" . | nindent 4 }} +data: + configuration.yaml: | + homeassistant: + {{- range $key, $value := .Values.config.homeassistant }} + {{ printf "%s: %s" $key ($value | quote) }} + {{- end }} + packages: !include_dir_named /media/packages + default_config: + automation: !include automations.yaml + script: !include scripts.yaml + scene: !include scenes.yaml + {{ .Values.mainConfig | toYaml | nindent 4 }} +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "homeassistant.fullname" . }}-packages + labels: + {{- include "homeassistant.labels" . | nindent 4 }} +data: + {{- range $key, $value := fromYaml .Values.config.packages }} + "{{ $key }}": | + {{- $value | toYaml | nindent 4 }} + {{- end }} diff --git a/charts/homeassistant/templates/deployment.yaml b/charts/homeassistant/templates/deployment.yaml new file mode 100644 index 0000000..926a3cf --- /dev/null +++ b/charts/homeassistant/templates/deployment.yaml @@ -0,0 +1,122 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "homeassistant.fullname" . }} + labels: + {{- include "homeassistant.labels" . | nindent 4 }} +spec: + replicas: 1 + strategy: + type: Recreate + selector: + matchLabels: + hass.kube.a09.uk/automationService: homeassistant + {{- include "homeassistant.selectorLabels" . | nindent 6 }} + template: + metadata: + annotations: + checksum/initConfig: {{ include (print $.Template.BasePath "/configmap-init.yaml") . | sha256sum }} + checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }} + {{- with .Values.podAnnotations }} + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + hass.kube.a09.uk/automationService: homeassistant + {{- include "homeassistant.selectorLabels" . | nindent 8 }} + spec: + hostNetwork: true + dnsPolicy: ClusterFirstWithHostNet + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + priorityClassName: {{ .Values.priorityClassName }} + containers: + - name: {{ .Chart.Name }}-homeassistant + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + args: + - 'python3' + - '-m' + - 'homeassistant' + - '--config' + - '/config' + - '--log-file' + - '/tmp/home-assistant.log' + ports: + - name: https + containerPort: 443 + protocol: TCP + #startupProbe: + # httpGet: + # path: / + # port: https + # failureThreshold: 30 + #livenessProbe: + # httpGet: + # path: / + # port: https + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: config + mountPath: /config + - name: storage + mountPath: /media/storage + - name: certificate-secret + mountPath: /media/certificates + - name: main-configmap + mountPath: /media/configuration + - name: packages-configmap + mountPath: /media/packages + securityContext: + privileged: true + initContainers: + - name: init + image: docker.io/python + command: ['python', '-u', '/init/init.py'] + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: config + mountPath: /config + - name: init + mountPath: /init + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} + volumes: + - name: config + persistentVolumeClaim: + claimName: {{ include "homeassistant.fullname" . }}-config + - name: storage + persistentVolumeClaim: + claimName: {{ include "homeassistant.fullname" . }}-storage + - name: init + configMap: + name: {{ include "homeassistant.fullname" . }}-initscript + - name: certificate-secret + secret: + secretName: {{ include "homeassistant.fullname" . }}-cert-secret + - name: main-configmap + configMap: + name: {{ include "homeassistant.fullname" . }}-configuration + - name: packages-configmap + configMap: + name: {{ include "homeassistant.fullname" . }}-packages diff --git a/charts/homeassistant/templates/pvc.yaml b/charts/homeassistant/templates/pvc.yaml new file mode 100644 index 0000000..9f00d4e --- /dev/null +++ b/charts/homeassistant/templates/pvc.yaml @@ -0,0 +1,35 @@ +{{- if not .Values.pvc.config.existingClaim -}} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ include "homeassistant.fullname" . }}-config + labels: + {{- include "homeassistant.labels" . | nindent 4 }} + annotations: + helm.sh/resource-policy: "keep" +spec: + accessModes: + - ReadWriteOnce + storageClassName: {{ .Values.pvc.config.storageClassName | quote }} + resources: + requests: + storage: {{ .Values.pvc.config.storage | quote }} +{{- end }} +--- +{{- if not .Values.pvc.storage.existingClaim -}} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ include "homeassistant.fullname" . }}-storage + labels: + {{- include "homeassistant.labels" . | nindent 4 }} + annotations: + helm.sh/resource-policy: "keep" +spec: + accessModes: + - ReadWriteOnce + storageClassName: {{ .Values.pvc.storage.storageClassName | quote }} + resources: + requests: + storage: {{ .Values.pvc.storage.storage | quote }} +{{- end }} diff --git a/charts/homeassistant/values.yaml b/charts/homeassistant/values.yaml new file mode 100644 index 0000000..d429c9a --- /dev/null +++ b/charts/homeassistant/values.yaml @@ -0,0 +1,69 @@ +# Default values for esphome. +# This is a YAML-formatted file. +image: + repository: ghcr.io/home-assistant/home-assistant + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + host: "" + +resources: {} + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +pvc: + config: + existingClaim: false + storageClassName: "" + storage: "1Gi" + storage: + existingClaim: false + storageClassName: "" + storage: "5Gi" + +mainConfig: + http: + server_port: 443 + ssl_certificate: /media/certificates/tls.crt + ssl_key: /media/certificates/tls.key + use_x_forwarded_for: true +config: + packages: "" + +cert: + dnsNames: [] + issuerName: ""