Commit eef439d0 authored by Francesco-gmail's avatar Francesco-gmail
Browse files

common library

parent 0e583b71
Pipeline #177542 failed with stage
in 6 seconds
apiVersion: v2
name: common-fbk
name: fbk-common
description: Function library for fbk charts
type: library
version: 4.4.2
version: 1.0.0
kubeVersion: ">=1.19.0-0"
keywords:
- fbk
......
{{- define "fbk-common.configMap" -}}
{{- if .Values.configMap -}}
{{- range .Values.configMap -}}
apiVersion: v1
data:
{{ .subPath | indent 2 }}: |
{{ .data | indent 4 }}
kind: ConfigMap
metadata:
labels:
{{/* include "labels" . | indent 4 */}}
app: karma
name: {{ .name }}
{{/* namespace: monitoring*/}}
{{- end -}}
{{- end -}}
{{- end -}}
\ No newline at end of file
{{- define "fbk-common.deployment" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ template "name" . }}
labels:
{{ include "labels" . | indent 4 }}
# Deployment specification
spec:
# Number of pod replicas desired, only use if HPA is not defined...
{{- if not .Values.autoscaling.enabled }}
replicas: {{ required "Specify replicaCount" .Values.replicaCount }}
{{- end }}
{{- if .Values.minReadySeconds }}
# Only on deployments define minimum number of seconds before being ready
minReadySeconds: {{ .Values.minReadySeconds }}
{{- end }}
{{- if .Values.revisionHistoryLimit }}
# Only on deployments define how many revision we keep how many old replica sets for this deployment
revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
{{- end }}
# How we rollout in a rolling update type scenario, this is for deployments only
strategy:
{{- with .Values.deploymentStrategy }}
{{ toYaml . | indent 4 }}
{{- end }}
# These selectors to match this object with previous versions of this object
selector:
matchLabels:
{{ include "labels" . | indent 6 }}
# This is the podTemplate, the bread and butter
template:
metadata:
labels:
{{ include "labels" . | indent 8 }}
{{ if .Values.podAnnotations}}
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- end }}
spec:
# If we have a hostname set, set it here
{{- if .Values.hostname }}
hostname: {{ with .Values.hostname }}{{ tpl . $ | quote }}{{- end }}
{{- end }}
# If we have a priority class set, set it here
{{- if .Values.priorityClassName }}
priorityClassName: "{{ .Values.priorityClassName }}"
{{- end }}
# How long to wait till forced termination
terminationGracePeriodSeconds: {{ default "30" .Values.terminationGracePeriodSeconds }}
# To have features such as run as non root
{{- if (eq .Values.security.runAsNonRoot true) }}
securityContext:
{{ toYaml .Values.security | indent 8 }}
{{- end }}
# If we have any init containers
initContainers:
{{- with .Values.initContainers }}
{{ toYaml . | indent 8 }}
{{- end }}
# Our list of container(s)
containers:
# The primary container for our service
- name: {{ template "name" . }}
image: "{{ .Values.image.repository }}:{{ default "latest" .Values.image.tag }}"
{{- if and .Values.tty (eq (.Values.tty | toString) "true") }}
stdin: true
tty: true
{{- end }}
{{- if .Values.image.command }}
command: {{- range .Values.image.command }}
- {{ . | quote }}
{{- end }}
{{- end }}
{{- if .Values.image.args }}
args: {{- range .Values.image.args }}
- {{ . | quote }}
{{- end }}
{{- end }}
{{- if .Values.image.imagePullPolicy }}
imagePullPolicy: {{ .Values.image.imagePullPolicy }}
{{- end }}
{{- if .Values.lifecycle }}
lifecycle:
{{ toYaml .Values.lifecycle | indent 12 }}
{{- end }}
env:
# Default env variables we want all containers to have
- name: "POD_NAME"
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: "POD_NAMESPACE"
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: "NAMESPACE"
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
# Insert globals here, special handling so we can "tpl" these values and use eg: namespace in them
{{- range .Values.globalEnvs }}
- name: {{ .name | quote }}
{{- if .value }}
value: {{ with .value }}{{ tpl . $ | quote }}{{- end }}
{{- end }}
{{- if .valueFrom }}
valueFrom:
{{ .valueFrom | toYaml | indent 16 }}
{{- end }}
{{- end }}
# Insert extra envs here, for sub-values files inserts, with special handling so we can "tpl" in them
{{- range .Values.extraEnvs }}
- name: {{ .name | quote }}
{{- if .value }}
value: {{ with .value }}{{ tpl . $ | quote }}{{- end }}
{{- end }}
{{- if .valueFrom }}
valueFrom:
{{ .valueFrom | toYaml | indent 16 }}
{{- end }}
{{- end }}
# If we want to pull env variables from other confimgaps or secrets
envFrom:
{{- with .Values.envFrom }}
{{ toYaml . | indent 12 }}
{{- end }}
# If we have services enabled lets expose their ports
{{- if .Values.service.enabled }}
ports:
- name: {{ .Values.service.name }}
containerPort: {{ .Values.service.targetPort }}
protocol: TCP
# Of course a service can use additional ports if desired
{{- if .Values.additionalPorts }}
{{ toYaml .Values.additionalPorts | indent 12 }}
{{- end }}
{{ end }}
# This is for our liveness probes, which dictate if a pod is healthy or should be replaced
{{- if .Values.livenessProbe.enabled }}
livenessProbe:
initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
successThreshold: {{ .Values.livenessProbe.successThreshold }}
failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
{{- if eq .Values.livenessProbe.probeType "httpGet" }}
httpGet:
path: {{ .Values.livenessProbe.path }}
port: {{ .Values.livenessProbe.port }}
{{- else if eq .Values.livenessProbe.probeType "tcpSocket" }}
tcpSocket:
port: {{ .Values.livenessProbe.port }}
{{- else if eq .Values.livenessProbe.probeType "exec" }}
exec:
command:
{{- with .Values.livenessProbe.command }}
{{ toYaml . | indent 16 }}
{{- end -}}
{{- end -}}
{{- end }}
# This is for our readiness probes, which dictate if a pod is ready to receive traffic
{{- if .Values.readinessProbe.enabled }}
readinessProbe:
initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
successThreshold: {{ .Values.readinessProbe.successThreshold }}
failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
{{- if eq .Values.readinessProbe.probeType "httpGet" }}
httpGet:
path: {{ .Values.readinessProbe.path }}
port: {{ .Values.readinessProbe.port }}
{{- else if eq .Values.readinessProbe.probeType "tcpSocket" }}
tcpSocket:
port: {{ .Values.readinessProbe.port }}
{{- else if eq .Values.readinessProbe.probeType "exec" }}
exec:
command:
{{- with .Values.readinessProbe.command }}
{{ toYaml . | indent 16 }}
{{- end -}}
{{- end -}}
{{- end }}
# Modify /etc/resolv.conf ndots
{{- if .Values.dnsConfig.enabled }}
dnsConfig:
options:
- name: ndots
value: {{ .Values.dnsConfig.ndots | quote}}
{{- end }}
# This defines our resource limits
resources:
{{- if .Values.resources }}
{{- with .Values.resources }}
{{ toYaml . | indent 12 }}
{{- end -}}
{{- end }}
# This defines any volume mounts we might want, only occurs if set
volumeMounts:
{{- if .Values.volumeMounts }}
{{- with .Values.volumeMounts }}
{{ toYaml . | indent 12 }}
{{- end -}}
{{- end }}
{{- if .Values.configMap }}
{{- range .Values.configMap }}
- mountPath: {{ .mountPath }}
{{- if .subPath }}
subPath: {{ .subPath }}
{{- end }}
name: {{ .name }}
{{- end }}
{{- end }}
# This is for definining sidecars which DOES NOT use the same env vars
{{- with .Values.extraContainers }}
{{ toYaml . | indent 8}}
{{- end }}
{{- if .Values.image.imagePullPolicy }}
imagePullPolicy: {{ .Values.image.imagePullPolicy }}
{{- end }}
env:
# Default env variables we want all containers to have
- name: "POD_NAME"
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: "POD_NAMESPACE"
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: "NAMESPACE"
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
# Global variables placed in a "global" values file for all environments
{{- if .Values.globalEnvs }}
{{ toYaml .Values.globalEnvs | indent 12 }}
{{- end }}
# Environment specific variables placed in an environment values file
{{- if .Values.extraEnvs }}
{{ toYaml .Values.extraEnvs | indent 12 }}
{{ end }}
{{/* end */}}
# This limits which nodes we want to run on
{{- if .Values.nodeSelector }}
nodeSelector:
{{- with .Values.nodeSelector }}
{{ toYaml . | indent 8 }}
{{- end -}}
{{- end }}
# This is pod affinity and anti-affinity, to determine where to schedule pods
affinity:
# For now, by default, we want to prefer (but not require) that pods do not get
# scheduled on the same availability zone, to get actual redundancy between zone failures
# We have this priority set very low (10) so that if someone wants they can specify
# rules with higher priority in the affinity value.
#
# TODO: There is a bug in this, if the user sets `labelsEnableDefault` to false this doesn't work
# if someone has time please fix or figure out another way around this situation
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
topologyKey: "kubernetes.io/hostname"
labelSelector:
matchExpressions:
- key: {{ if .Values.usingNewRecommendedLabels }}app.kubernetes.io/name{{ else }}app{{ end }}
operator: In
values:
- {{ template "name" . }}
- weight: 100
podAffinityTerm:
topologyKey: "failure-domain.beta.kubernetes.io/zone"
labelSelector:
matchExpressions:
- key: {{ if .Values.usingNewRecommendedLabels }}app.kubernetes.io/name{{ else }}app{{ end }}
operator: In
values:
- {{ template "name" . }}
{{- /*
SORRY for this mess, this is needed because of the above
*/}}
{{- if .Values.affinity }}
{{- if .Values.affinity.podAntiAffinity }}
{{- if .Values.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml .Values.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution | indent 10 }}
{{- end }}
{{- if .Values.affinity.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
requiredDuringSchedulingIgnoredDuringExecution:
{{ toYaml .Values.affinity.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution | indent 10 }}
{{- end }}
{{- end }}
{{- if .Values.affinity.podAffinity }}
podAffinity:
{{ toYaml .Values.affinity.podAffinity | indent 10 }}
{{- end }}
{{- end }}
# Tolerations is another form of affinity/anti-affinity that is dynamic, nodes
# can be assigned tolerations which can affect pods
tolerations:
{{- with .Values.tolerations }}
{{ toYaml . | indent 8 }}
{{- end }}
# This is our volume definitions, if we have volume mounts
volumes:
{{- if .Values.volumes }}
{{- with .Values.volumes }}
{{ toYaml . | indent 8 }}
{{- end -}}
{{- end }}
{{- if .Values.configMap }}
{{- range .Values.configMap }}
- configMap:
name: {{ .name }}
name: {{ .name }}
{{- end }}
{{- end }}
# Service Account creation
{{- if or (eq .Values.serviceAccount.enabled true) (eq .Values.rbac.create true) }}
serviceAccountName: {{ template "name" . }}
{{- end }}
{{- end }}
\ No newline at end of file
{{- define "fbk-common.ingress" -}}
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "name" . }}
{{- $name := include "name" . }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
# Old without template override: name: {{ template "name" . }}
name: {{ template "ingress.name" . }}
# SHOULD USE name: {{ template "name" . }}
{{- include "labels" . | indent 2 }}
name: {{ $name }}
labels:
{{ include "labels" . | indent 4 }}
annotations:
kubernetes.io/ingress.class: {{ .Values.ingress.nginx_class | quote }}
{{ if .Values.ingress.nginx_affinity }}
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/session-cookie-name: "route"
nginx.ingress.kubernetes.io/session-cookie-path: "/"
nginx.ingress.kubernetes.io/session-cookie-expires: "172800"
nginx.ingress.kubernetes.io/session-cookie-max-age: "172800"
{{- end }}
{{- with .Values.ingress.annotations }}
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
rules:
{{- range .Values.ingress.hosts }}
- host: {{ tpl .host $ | quote }}
- host: {{ quote .host }}
http:
paths:
{{- if .enableMultiPath -}}
{{- range .paths }}
- path: {{ .path }}
backend:
serviceName: {{ .serviceName }}
servicePort: {{ .servicePort }}
{{- end }}
{{- else }}
{{- range .paths }}
- path: {{ . }}
{{- range $path := .paths }}
- path: {{ $path }}
pathType: Prefix
backend:
serviceName: {{ $fullName }}
servicePort: 80
service:
name: {{ $name }}
port:
number: 80
{{/* .Values.service.port */}}
{{- end }}
{{- end }}
{{- end }}
{{- with .Values.ingress.tls }}
tls:
......@@ -52,9 +41,10 @@ spec:
- {{ tpl $elemVal $ | quote }}
{{- end }}
{{- if .secretName }}
secretName: {{ tpl .secretName $ | quote }}
secretName: {{ .secretName | quote }}
{{- end }}
{{- end }}
{{- end -}}
{{- end }}
{{- end }}
\ No newline at end of file
{{- define "fbk-common.service" -}}
{{- if .Values.service.enabled }}
################################################
# Standardized Templates - Service
# Author/Maintainer: Farley <farley@neonsurge.com>
################################################
apiVersion: v1
kind: Service
metadata:
# Should use name: {{ template "name" . }}
name: {{ template "name" . }}
labels:
{{ include "labels" . | indent 4 }}
annotations:
# Our service annotations
{{- with .Values.service.annotations }}
{{ toYaml . | indent 4 }}
{{- end }}
# include labels for this service to identify it
{{- include "labels" . | indent 4 }}
# Service definition
spec:
type: {{ .Values.service.type }}
{{- if .Values.service.externalTrafficPolicy }}
externalTrafficPolicy: {{ .Values.service.externalTrafficPolicy }}
{{- end }}
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort }}
protocol: TCP
name: {{ .Values.service.name }}
{{- if .Values.service.additionalPorts }}
{{ toYaml .Values.service.additionalPorts | indent 4 }}
{{- end }}
selector:
app.kubernetes.io/name: {{ template "name" . }}
{{- if .Values.labels }}
{{ toYaml .Values.labels | indent 4 }}
{{- end }}
{{- end }}
{{- end }}
{{- define "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 "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 "chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}
\ No newline at end of file
{{/*
Create the name of the service account to use
*/}}
{{- define "serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "default-chart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
{{/*
Common labels
*/}}
{{- define "labels" -}}
app.kubernetes.io/name: {{ include "name" . }}
app.kubernetes.io/version: {{ .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" | quote }}
app.kubernetes.io/component: {{ .Chart.Name | replace "+" "_" | trunc 63 | trimSuffix "-" | quote }}
app.kubernetes.io/created-with: "fbk-common-library"
app.kubernetes.io/managed-by: "helm"
helm.sh/chart: {{ include "chart" . }}
{{ include "selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- if .Values.labels }}
{{ toYaml .Values.labels }}
{{- end }}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "selectorLabels" -}}
app.kubernetes.io/name: {{ include "name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{/* Template that defines the annotations used by the Gitlab integration service to find the resources of a project in the cluster */}}
{{- define "common.gitlabAnnotations" -}}
{{- if .Values.gitlabApp -}}
app.gitlab.com/app: {{ .Values.gitlabApp }}
app.gitlab.com/env: {{ .Values.gitlabEnvironment }}
{{/* Add the incremental Gitlab job ID as an annotation to force a new deployment of the pods */}}
app.gitlab.com/jobid: {{ .Values.gitlabJobID | quote }}
{{- end -}}
{{- end -}}
\ No newline at end of file
# Default values for the common chart
####################################
# deployment.yaml variables
replicaCount: 1
image: