Skip to content

Instantly share code, notes, and snippets.

@ingydotnet
Created November 15, 2024 07:38
Show Gist options
  • Save ingydotnet/ff0638edf1bcb53c45161dce2d777f74 to your computer and use it in GitHub Desktop.
Save ingydotnet/ff0638edf1bcb53c45161dce2d777f74 to your computer and use it in GitHub Desktop.

Side by Side File Comparisons

Generated by sbs (a YAMLScript program).


templates/helpers.ys

orig/templates/_helpers.tpl

!yamlscript/v0

# Load the 3 main Helm data sources into variables:
Chart =: yaml/load-file('Chart.yaml')
Values =: yaml/load-file('values.yaml')

# The 'Release' data is built into Helm but the 'helmys' renders the special
# 'helmys.yaml' template and puts the JSON data in the 'HELM_RELEASE' variable.
Release =:
  if ENV.HELM_RELEASE:
    json/load: ENV.HELM_RELEASE
    else::
      Name: unknown
      Service: unknown

# Define 2 trunc funcs:
defn trunc(s): take(63 s).str(*).replace(/-$/)
defn trunc+(s): s:trunc.replace(/\+/ '_')

# Expand the name of the chart:
chart-name =: trunc(Values.nameOverride ||| Chart.name)

# 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.
chart-fullname =:
  if Values.fullnameOverride.?:
    then: Values.fullnameOverride:trunc
    else:
      name =: Values.nameOverride ||| Chart.name
      if name.has?(Release.Name):
        trunc: Release.Name
        format "%s-%s": Release.Name name

# Create chart name and version as used by the chart label:
defn chart-chart():
  trunc+: format("%s-%s" Chart.Name Chart.Version)

# Common labels:
defn common-labels():
  apply merge::
  - helm.sh/chart:: "$(Chart.name)-$(Chart.version)"
  - ! selectorLabels()
  - app.kubernetes.io/version:: Chart.appVersion
    app.kubernetes.io/managed-by:: Release.Service

# Selector labels:
defn selectorLabels()::
  app.kubernetes.io/name:: Chart.name
  app.kubernetes.io/instance:: Release.Name

# Create the name of the service account to use:
serviceAccountName =:
  or? Values.serviceAccount.name:
    if Values.serviceAccount.create:
      chart-fullname
      'default'





{{/*
Expand the name of the chart.
*/}}
{{- define "ys-test-chart.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 "ys-test-chart.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 "ys-test-chart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "ys-test-chart.labels" -}}
helm.sh/chart: {{ include "ys-test-chart.chart" . }}
{{ include "ys-test-chart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

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

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

templates/helmys.yaml

_

HELMYS: true
Name: {{ default .Release.Name }}
Service: Helm






templates/serviceaccount.yaml

orig/templates/serviceaccount.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name:: serviceAccountName
  labels:: common-labels()
  :when+ Values.serviceAccount.annotations.?::
   annotations:: _
automountServiceAccountToken::
  Values.serviceAccount.automount




{{- if .Values.serviceAccount.create -}}
apiVersion: v1
kind: ServiceAccount
metadata:
  name: {{ include "ys-test-chart.serviceAccountName" . }}
  labels:
    {{- include "ys-test-chart.labels" . | nindent 4 }}
  {{- with .Values.serviceAccount.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
automountServiceAccountToken: {{ .Values.serviceAccount.automount }}
{{- end }}

templates/service.yaml

orig/templates/service.yaml

apiVersion: v1
kind: Service
metadata:
  name:: chart-fullname
  labels:: common-labels()
spec:
  type:: Values.service.type
  ports:
  - port:: Values.service.port
    targetPort: http
    protocol: TCP
    name: http
  selector:: selectorLabels()


apiVersion: v1
kind: Service
metadata:
  name: {{ include "ys-test-chart.fullname" . }}
  labels:
    {{- include "ys-test-chart.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    {{- include "ys-test-chart.selectorLabels" . | nindent 4 }}

templates/deployment.yaml

orig/templates/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name:: chart-fullname
  labels:: common-labels()
spec:
  :when-not Values.autoscaling.enabled.?::
   replicas:: Values.replicaCount
  selector:
    matchLabels:: selectorLabels()
  template:
    metadata:
      :when+ Values.podAnnotations.?::
       annotations:: _
      labels:: common-labels()
    spec:
      :when+ Values.imagePullSecrets.?::
       imagePullSecrets:: _
      serviceAccountName:: serviceAccountName
      securityContext:: Values.podSecurityContext
      containers:
      - name:: Chart.name
        securityContext:: Values.securityContext
        image:: "$(Values.image.repository):\
                 $(Values.image.tag ||| Chart.appVersion)"
        imagePullPolicy:: Values.image.pullPolicy
        ports:
        - name: http
          containerPort:: Values.service.port
          protocol: TCP
        livenessProbe:: Values.livenessProbe
        readinessProbe:: Values.readinessProbe
        resources:: Values.resources
        :when+ Values.volumeMounts.?:: {volumeMounts:: _}
      :when+ Values.volumes.?::      {volumes:: _}
      :when+ Values.nodeSelector.?:: {nodeSelector:: _}
      :when+ Values.affinity.?::     {affinity:: _}
      :when+ Values.tolerations.?::  {tolerations:: _}






























apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "ys-test-chart.fullname" . }}
  labels:
    {{- include "ys-test-chart.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "ys-test-chart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "ys-test-chart.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "ys-test-chart.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            {{- toYaml .Values.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | nindent 12 }}
          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 }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment