From dd3f51bbdc6b4b55609707d2f6dcc93dca4ab346 Mon Sep 17 00:00:00 2001 From: HenrikOssipoff Date: Wed, 7 Jun 2023 11:53:23 +0200 Subject: [PATCH] Add suppot for KEDA, PDB, Elastic logging --- charts/fastapi/Chart.yaml | 2 +- charts/fastapi/templates/_helpers.tpl | 7 ++++++ charts/fastapi/templates/deployment-web.yaml | 9 +++++-- charts/fastapi/templates/keda.yaml | 18 ++++++++++++++ .../templates/pod-disruption-budget.yaml | 19 +++++++++++++++ charts/fastapi/values.yaml | 24 ++++++++++++++++++- 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 charts/fastapi/templates/keda.yaml create mode 100644 charts/fastapi/templates/pod-disruption-budget.yaml diff --git a/charts/fastapi/Chart.yaml b/charts/fastapi/Chart.yaml index e9bf9ae..940984e 100644 --- a/charts/fastapi/Chart.yaml +++ b/charts/fastapi/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 name: fastapi description: A simple Coolshop Helm chart for deploying FastAPI applications in Kubernetes type: application -version: 1.0.6 +version: 1.1.1 appVersion: latest icon: https://static.coolshop-cdn.com/images/favicons/coolshop/favicon-256.png maintainers: diff --git a/charts/fastapi/templates/_helpers.tpl b/charts/fastapi/templates/_helpers.tpl index 26722bb..e95478a 100644 --- a/charts/fastapi/templates/_helpers.tpl +++ b/charts/fastapi/templates/_helpers.tpl @@ -82,3 +82,10 @@ ELASTIC_APM_CENTRAL_CONFIG: "false" ELASTIC_APM_SERVER_TIMEOUT: "10s" SENTRY_ENVIRONMENT: "{{ .Release.Namespace | default .Values.namespace }}" {{- end }} + +{{- define "fastapi-helm-chart.defaultPodAnnotations" -}} +co.elastic.logs/json.keys_under_root: "true" +co.elastic.logs/json.overwrite_keys: "true" +co.elastic.logs/json.add_error_key: "true" +co.elastic.logs/json.expand_keys: "true" +{{- end }} diff --git a/charts/fastapi/templates/deployment-web.yaml b/charts/fastapi/templates/deployment-web.yaml index d7fcf33..0d6ba92 100644 --- a/charts/fastapi/templates/deployment-web.yaml +++ b/charts/fastapi/templates/deployment-web.yaml @@ -7,16 +7,19 @@ metadata: labels: {{- include "fastapi-helm-chart.labels" . | nindent 4 }} spec: + {{- if not .Values.webDeployment.keda.enabled }} replicas: {{ .Values.webDeployment.replicaCount }} + {{- end }} selector: matchLabels: {{- include "fastapi-helm-chart.webSelectorLabels" . | nindent 6 }} template: metadata: - {{- with .Values.webDeployment.podAnnotations }} annotations: + {{- include "fastapi-helm-chart.defaultPodAnnotations" . | nindent 8 }} + {{- with .Values.webDeployment.podAnnotations }} {{- toYaml . | nindent 8 }} - {{- end }} + {{- end }} labels: {{- include "fastapi-helm-chart.webSelectorLabels" . | nindent 8 }} spec: @@ -41,6 +44,8 @@ spec: - name: http containerPort: {{ .Values.service.port }} protocol: TCP + lifecycle: + {{- toYaml .Values.lifecycle | nindent 12 }} livenessProbe: httpGet: path: /healthz diff --git a/charts/fastapi/templates/keda.yaml b/charts/fastapi/templates/keda.yaml new file mode 100644 index 0000000..d2a21f4 --- /dev/null +++ b/charts/fastapi/templates/keda.yaml @@ -0,0 +1,18 @@ +{{- if .Values.webDeployment.keda.enabled }} +apiVersion: keda.k8s.io/v1alpha1 +kind: ScaledObject +metadata: + name: {{ include "fastapi-helm-chart.fullname" . }}-web + namespace: {{ .Release.Namespace | default .Values.namespace }} + labels: + {{- include "fastapi-helm-chart.labels" . | nindent 4 }} +spec: + scaleTargetRef: + deploymentName: {{ include "fastapi-helm-chart.fullname" . }}-web + minReplicaCount: {{ .Values.webDeployment.keda.minReplicas }} + maxReplicaCount: {{ .Values.webDeployment.keda.maxReplicas }} + triggers: +{{- with .Values.webDeployment.keda.triggers }} +{{ toYaml . | indent 4 }} +{{ end }} +{{- end }} diff --git a/charts/fastapi/templates/pod-disruption-budget.yaml b/charts/fastapi/templates/pod-disruption-budget.yaml new file mode 100644 index 0000000..6dfac13 --- /dev/null +++ b/charts/fastapi/templates/pod-disruption-budget.yaml @@ -0,0 +1,19 @@ +{{- if .Values.webDeployment.podDisruptionBudget.enabled }} +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{ include "fastapi-helm-chart.fullname" . }}-web + namespace: {{ .Release.Namespace | default .Values.namespace }} + labels: + {{- include "fastapi-helm-chart.labels" . | nindent 4 }} +spec: + {{- if .Values.webDeployment.podDisruptionBudget.maxUnavailable }} + maxUnavailable: {{ .Values.webDeployment.podDisruptionBudget.maxUnavailable }} + {{- end }} + {{- if .Values.webDeployment.podDisruptionBudget.minAvailable }} + minAvailable: {{ .Values.webDeployment.podDisruptionBudget.minAvailable }} + {{- end }} + selector: + matchLabels: + {{- include "fastapi-helm-chart.webSelectorLabels" . | nindent 6 }} +{{- end }} diff --git a/charts/fastapi/values.yaml b/charts/fastapi/values.yaml index dfbb9a1..1b112ad 100644 --- a/charts/fastapi/values.yaml +++ b/charts/fastapi/values.yaml @@ -17,7 +17,7 @@ image: # Overrides the image tag whose default is the chart appVersion. tag: "" -configmap: {} +# configmap: {} secrets: {} @@ -37,6 +37,23 @@ webDeployment: # runAsNonRoot: true # runAsUser: 1000 + keda: + enabled: false + minReplicas: 2 + maxReplicas: 6 + triggers: [] + # - type: prometheus + # metadata: + # serverAddress: http://10.132.0.82:9090 + # metricName: haproxy_frontend_current_sessions + # threshold: "100" + # query: sum(rate(haproxy_frontend_current_sessions{proxy="http"}[2m])) + + podDisruptionBudget: + enabled: false + # maxUnavailable: 1 + # minAvailable: 1 + service: enabled: true type: ClusterIP @@ -57,3 +74,8 @@ ingress: # - secretName: chart-example-tls # hosts: # - chart-example.local + +lifecycle: + preStop: + exec: + command: ["/bin/sh", "-c", "sleep 10"]