diff --git a/charts/rails-application/.helmignore b/charts/rails-application/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/charts/rails-application/.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/rails-application/Chart.yaml b/charts/rails-application/Chart.yaml new file mode 100644 index 0000000..27af851 --- /dev/null +++ b/charts/rails-application/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v2 +name: rails-application +description: Base chart for my rails applications +type: application +version: "0.1.0" diff --git a/charts/rails-application/templates/_helpers.tpl b/charts/rails-application/templates/_helpers.tpl new file mode 100644 index 0000000..26e901c --- /dev/null +++ b/charts/rails-application/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "rails-application.name" -}} +{{- default .Values.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 "rails-application.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Values.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 "rails-application.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "rails-application.labels" -}} +helm.sh/chart: {{ include "rails-application.chart" . }} +{{ include "rails-application.selectorLabels" . }} +{{- if .Values.version }} +app.kubernetes.io/version: {{ .Values.version | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "rails-application.selectorLabels" -}} +app.kubernetes.io/name: {{ include "rails-application.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "rails-application.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "rails-application.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/charts/rails-application/templates/configmap.yaml b/charts/rails-application/templates/configmap.yaml new file mode 100644 index 0000000..c72a31f --- /dev/null +++ b/charts/rails-application/templates/configmap.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "rails-application.fullname" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "rails-application.labels" . | nindent 4 }} +data: + {{- range $key, $value := .Values.environment.plain }} + {{ $key }}: {{ $value | quote }} +{{- end -}} diff --git a/charts/rails-application/templates/deployment.yaml b/charts/rails-application/templates/deployment.yaml new file mode 100644 index 0000000..b0d6f7d --- /dev/null +++ b/charts/rails-application/templates/deployment.yaml @@ -0,0 +1,84 @@ +{{- range $deploymentName, $deploymentValues := .Values.deployments }} +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ printf "%s-%s" (include "rails-application.fullname" $) $deploymentName }} + labels: + {{- include "rails-application.labels" $ | nindent 4 }} +spec: + {{- if not $.Values.autoscaling.enabled }} + replicas: {{ $deploymentValues.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "rails-application.selectorLabels" $ | nindent 6 }} + template: + metadata: + {{- with $.Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + "k8s.klaus-meyer.net/deployment-type": {{ $deploymentName }} + {{- include "rails-application.labels" $ | nindent 8 }} + {{- with $.Values.podLabels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + {{- with $.Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "rails-application.serviceAccountName" $ }} + securityContext: + {{- toYaml $.Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ printf "%s-%s" (include "rails-application.fullname" $) $deploymentName }} + securityContext: + {{- toYaml $.Values.securityContext | nindent 12 }} + image: "{{ $.Values.image.repository }}:{{ $.Values.image.tag | default $.Values.version }}" + imagePullPolicy: {{ $.Values.image.pullPolicy }} + args: ["{{ $deploymentName }}"] + envFrom: + - configMapRef: + name: {{ include "rails-application.fullname" $ }} + - secretRef: + name: {{ include "rails-application.fullname" $ }} + {{- if $deploymentValues.ports }} + {{- with $deploymentValues.ports }} + ports: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- end }} + {{- if $deploymentValues.livenessProbe }} + livenessProbe: + {{- toYaml $deploymentValues.livenessProbe | nindent 12 }} + {{- end -}} + {{- if $deploymentValues.readinessProbe }} + readinessProbe: + {{- toYaml $deploymentValues.readinessProbe | nindent 12 }} + {{ end }} + resources: + {{- toYaml $.Values.resources | nindent 12 }} + {{- with $.Values.volumeMounts }} + volumeMounts: + {{- toYaml . | nindent 12 }} + {{- end }} + {{- with $.Values.volumes }} + 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 }} +--- +{{- end }} diff --git a/charts/rails-application/templates/hpa.yaml b/charts/rails-application/templates/hpa.yaml new file mode 100644 index 0000000..79aa8d0 --- /dev/null +++ b/charts/rails-application/templates/hpa.yaml @@ -0,0 +1,32 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "rails-application.fullname" . }} + labels: + {{- include "rails-application.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "rails-application.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/rails-application/templates/ingress.yaml b/charts/rails-application/templates/ingress.yaml new file mode 100644 index 0000000..039bedb --- /dev/null +++ b/charts/rails-application/templates/ingress.yaml @@ -0,0 +1,62 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "rails-application.fullname" . -}} +{{- $svcName := printf "%s-%s" $fullName "web" -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "rails-application.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- 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 }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $svcName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $svcName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/charts/rails-application/templates/secret.yaml b/charts/rails-application/templates/secret.yaml new file mode 100644 index 0000000..536b0ad --- /dev/null +++ b/charts/rails-application/templates/secret.yaml @@ -0,0 +1,29 @@ +{{ if .Values.environment.sealedSecrets }} +apiVersion: bitnami.com/v1alpha1 +kind: SealedSecret +metadata: + name: {{ include "rails-application.fullname" . }} + namespace: {{ .Release.Namespace }} + annotations: + sealedsecrets.bitnami.com/namespace-wide: "true" + labels: + {{- include "rails-application.labels" . | nindent 4 }} +spec: + encryptedData: + {{- range $key, $value := .Values.environment.secret }} + {{ $key }}: {{ $value | quote }} +{{- end -}} +{{ else }} +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "rails-application.fullname" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "rails-application.labels" . | nindent 4 }} +type: Opaque +data: + {{- range $key, $value := .Values.environment.secret }} + {{ $key }}: {{ $value | b64enc | quote }} +{{- end -}} +{{ end }} diff --git a/charts/rails-application/templates/service.yaml b/charts/rails-application/templates/service.yaml new file mode 100644 index 0000000..e07e2b2 --- /dev/null +++ b/charts/rails-application/templates/service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "rails-application.fullname" . }}-web + labels: + {{- include "rails-application.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + k8s.klaus-meyer.net/deployment-type: web + {{- include "rails-application.selectorLabels" . | nindent 4 }} diff --git a/charts/rails-application/templates/serviceaccount.yaml b/charts/rails-application/templates/serviceaccount.yaml new file mode 100644 index 0000000..13b6082 --- /dev/null +++ b/charts/rails-application/templates/serviceaccount.yaml @@ -0,0 +1,13 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "rails-application.serviceAccountName" . }} + labels: + {{- include "rails-application.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automount }} +{{- end }} diff --git a/charts/rails-application/values.yaml b/charts/rails-application/values.yaml new file mode 100644 index 0000000..52dad9e --- /dev/null +++ b/charts/rails-application/values.yaml @@ -0,0 +1,118 @@ +# Default values for rails-application. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +image: + repository: my-application + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the `app.version` value. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +environment: + sealedSecrets: false + plain: {} + secret: {} + +deployments: + web: + enabled: true + replicaCount: 1 + ports: + - name: http + containerPort: 8080 + protocol: TCP + livenessProbe: + httpGet: + path: /health + port: http + readinessProbe: + httpGet: + path: /health/all + port: http + +serviceAccount: + # Specifies whether a service account should be created + create: true + # 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: "" + +podAnnotations: {} +podLabels: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +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 + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +# Additional volumes on the output Deployment definition. +volumes: [] +# - name: foo +# secret: +# secretName: mysecret +# optional: false + +# Additional volumeMounts on the output Deployment definition. +volumeMounts: [] +# - name: foo +# mountPath: "/etc/foo" +# readOnly: true + +nodeSelector: {} + +tolerations: [] + +affinity: {}