Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fnndsc/util version 0.2.0 #29

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/util/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: util
description: Library chart providing helper functions for FNNDSC Helm charts
type: library
version: 0.1.0
version: 0.2.0

38 changes: 38 additions & 0 deletions charts/util/templates/_deployment.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{- define "util.deploymentTpl" }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "util.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "util.labels" . | nindent 4 }}
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "util.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "util.labels" . | nindent 8 }}
{{- with .Values.podLabels }}
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
containers:
{{- include "util.container" (mustMerge (dict "containerName" (include "util.name" .) "portName" "http") .) | nindent 8 }}
{{- include "util.podSpec" . | nindent 6 }}
{{- end }}
158 changes: 136 additions & 22 deletions charts/util/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -1,25 +1,139 @@
{{- /*
Helper function to use a value. If the value is unset, try looking up the previous value
from a secret. If the secret does not exist, generate a random value with a specified length.
*/ -}}
{{- define "util.valueOrLookupOrRandom" -}}
{{- if .value -}}
{{- .value | b64enc | quote -}}
{{- else -}}
{{- $length := .length | default 32 -}}
{{- $name := .name -}}
{{- if (not $name) -}}
{{- fail (printf "util.valueOrLookupOrRandom was not called with required parameter 'name'. Given parameters: %s" (keys .)) -}}
{{- end -}}
{{- with (lookup "v1" "Secret" .root.Release.Namespace .secret) -}}
{{- if (hasKey .data $name) -}}
{{- (index .data $name) | quote -}}
{{- else -}}
{{- randAlphaNum $length | b64enc | quote -}}
{{- end -}}
{{- else -}}
{{- randAlphaNum $length | b64enc | quote -}}
{{- end -}}
{{/*
Expand the name of the chart.
*/}}
{{- define "util.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 "util.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 "util.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "util.labels" -}}
helm.sh/chart: {{ include "util.chart" . }}
{{ include "util.selectorLabels" . }}
{{- with (default .Chart.AppVersion .appVersion) }}
app.kubernetes.io/version: {{ quote . }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "util.selectorLabels" -}}
app.kubernetes.io/name: {{ include "util.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}


{{/*
Create the name of the service account to use
*/}}
{{- define "util.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "util.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{- define "util.image" -}}
{{ if .Values.image.registry }}{{ .Values.image.registry }}/{{ end }}{{ .Values.image.repository }}:{{ .Values.image.tag | default .appVersion | default .Chart.AppVersion }}
{{- end -}}

{{- define "util.podSpec" -}}
{{- with (concat (.Values.imagePullSecrets | default (list)) ((.Values.global).imagePullSecrets | default (list)) | mustUniq) }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- if (or .Values.serviceAccount.name .Values.serviceAccount.create) }}
serviceAccountName: {{ include "util.util.serviceAccountName" . }}
{{- end }}
{{- with (.Values.podSecurityContext | deepCopy | mustMergeOverwrite (deepCopy ((.Values.global).podSecurityContext | default (dict)))) }}
securityContext:
{{- 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 }}
{{- end -}}

{{- define "util.container" -}}
- image: {{ include "util.image" . }}
{{- if .containerName }}
name: {{ .containerName }}
{{- end }}
{{- if .Values.image.pullPolicy }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
{{- end }}
ports:
- containerPort: {{ .Values.containerPort | default .Values.service.port }}
{{- if .Values.hostPort }}
hostPort: {{ .Values.hostPort }}
{{- end }}
protocol: TCP
{{- if .portName }}
name: {{ .portName }}
{{- end }}
{{- if .Values.extraEnv }}
env:
{{- range $k, $v := .Values.extraEnv }}
- name: {{ $k }}
value: {{ $v | quote }}
{{- end }}
{{- end }}
{{- with .Values.securityContext }}
securityContext:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.resources }}
resources:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.livenessProbe }}
livenessProbe:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.readinessProbe }}
readinessProbe:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.startupProbe }}
startupProbe:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end -}}
35 changes: 35 additions & 0 deletions charts/util/templates/_hpa.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{{- define "util.hpaTpl" }}
{{- if .Values.autoscaling.enabled }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: {{ include "util.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "util.labels" . | nindent 4 }}
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: {{ include "util.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 }}
{{- end }}
46 changes: 46 additions & 0 deletions charts/util/templates/_ingress.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{- define "util.ingressTpl" }}
{{- if .Values.ingress.enabled -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ include "util.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "util.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 "util.fullname" $ }}
port:
number: {{ $.Values.service.port }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
34 changes: 34 additions & 0 deletions charts/util/templates/_knative.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{- define "util.knativeServiceTpl" }}
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: {{ include "util.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "util.labels" . | nindent 4 }}
{{- with .Values.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
template:
metadata:
{{- with .Values.revisionAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.podLabels }}
labels:
{{- toYaml . | nindent 8 }}
{{- end }}
spec:
{{- if .Values.containerConcurrency }}
containerConcurrency: {{ .Values.containerConcurrency }}
{{- end }}
containers:
{{- include "util.container" . | nindent 8 }}
{{- mustMerge (omit . "Values") (dict "Values" (omit .Values "startupProbe")) | include "util.podSpec" | nindent 6 }}
{{- end }}
24 changes: 24 additions & 0 deletions charts/util/templates/_knative_or_deployment.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{- define "util.knativeOrDeployment" -}}
{{- include "util.serviceAccountTpl" . }}
{{- if (eq .Values.kind "Service") }}
{{- include "util.knativeServiceTpl" . }}
{{- else if (eq .Values.kind "Deployment") }}
{{- include "util.deploymentEverythingTpl" . }}
{{- else }}
{{- fail (printf "Unsupported kind: %s" .kind) }}
{{- end -}}
{{- end -}}

{{- define "util.deploymentEverythingTpl" }}
{{- include "util.deploymentTpl" . }}
---
{{- include "util.hpaTpl" . }}
---
{{- include "util.ingressTpl" . }}
---
{{- include "util.serviceTpl" . }}
---
{{- include "util.testServiceConnectionTpl" . }}
---
{{- include "util.openshiftRouteTpl" . }}
{{- end }}
25 changes: 25 additions & 0 deletions charts/util/templates/_route.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{- define "util.openshiftRouteTpl" }}
{{- if .Values.route.enabled -}}
kind: Route
apiVersion: route.openshift.io/v1
metadata:
name: {{ include "util.fullname" . }}
labels:
{{- include "util.labels" . | nindent 4 }}
{{- with .Values.route.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
to:
kind: Service
name: {{ include "util.fullname" . }}
{{- with .Values.route.tls }}
tls:
{{- toYaml . | nindent 4 }}
{{- end }}
port:
targetPort: http
host: {{ required ".Values.route.host is required." .Values.route.host }}
{{- end }}
{{- end }}
Loading