Skip to content

Commit

Permalink
build(repo): Fixing helm charts helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jan 22, 2025
1 parent afde4ad commit 941c174
Show file tree
Hide file tree
Showing 18 changed files with 540 additions and 243 deletions.
2 changes: 1 addition & 1 deletion cluster/charts/fuel-streams/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: v2
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: fuel-streams
version: 0.9.1
version: 0.9.2
dependencies:
- name: nats
version: 1.2.9
Expand Down
111 changes: 111 additions & 0 deletions cluster/charts/fuel-streams/templates/_blocks-container.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{{/*
Configure container resource requests and limits
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
*/}}
{{ define "k8s.container-config.resources" -}}
{{- include "set-field-and-value" (dict "root" .root "context" .context "field" "resources") -}}
{{- end }}

{{/*
Configure container security context settings
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
*/}}
{{- define "k8s.container-config.securityContext" -}}
{{- include "set-field-and-value" (dict "root" .root "context" .context "field" "securityContext" "path" "config.containerSecurityContext") -}}
{{- end }}

{{/*
Configure container health check probes
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
*/}}
{{- define "k8s.container-config.probes" -}}
{{- if .root.Values.config.healthChecks }}
{{- include "set-field-and-value" (dict "root" .root "context" .context "field" "startupProbe") -}}
{{- include "set-field-and-value" (dict "root" .root "context" .context "field" "livenessProbe") -}}
{{- include "set-field-and-value" (dict "root" .root "context" .context "field" "readinessProbe") -}}
{{- end }}
{{- end }}

{{- define "k8s.container-config.ports" -}}
ports:
{{- if .context.port }}
- name: {{ .context.name | default .name }}
containerPort: {{ .context.port }}
protocol: TCP
{{- end }}
{{- with .context.ports }}
{{ toYaml . | nindent 0 }}
{{- end }}
{{- end }}

{{/*
Configure container environment variables
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration with port and env map
*/}}
{{- define "k8s.container-config.env" -}}
env:
{{- if .context.port }}
- name: PORT
value: {{ .context.port | quote }}
{{- end }}
{{- range $key, $value := .context.env }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
{{- end }}

{{/*
Configure container environment from external sources
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration with envFrom
*/}}
{{- define "k8s.container-config.envFrom" -}}
envFrom:
- configMapRef:
name: {{ include "fuel-streams.fullname" .root }}-config
optional: true
- secretRef:
name: {{ include "fuel-streams.fullname" .root }}-keys
optional: true
{{- with .context.envFrom }}
{{ toYaml . | nindent 0 }}
{{- end }}
{{- end }}

{{/*
Configure container image settings
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing image configuration
*/}}
{{- define "k8s.container-config.image" -}}
image: "{{ .context.image.repository }}:{{ .context.image.tag | default .root.Chart.AppVersion }}"
imagePullPolicy: {{ .context.image.pullPolicy }}
{{- end }}

{{/*
Configure container-level settings including resource requests, security context, and probes
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
- component: Optional component name for labels
- name: Name of the service for labels
Returns: YAML configuration for container-level settings
*/}}
{{- define "k8s.container-config" -}}
{{ include "k8s.container-config.resources" . }}
{{ include "k8s.container-config.securityContext" . }}
{{ include "k8s.container-config.probes" . }}
{{ include "k8s.container-config.ports" . }}
{{ include "k8s.container-config.env" . }}
{{ include "k8s.container-config.envFrom" . }}
{{- end }}
20 changes: 20 additions & 0 deletions cluster/charts/fuel-streams/templates/_blocks-nats.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{/*
* NATS default accounts
*/}}
{{- define "nats-accounts" -}}
data:
auth.conf: |
accounts {
SYS: {
users: [
{user: $NATS_SYSTEM_USER, password: $NATS_SYSTEM_PASS}
]
}
ADMIN: {
jetstream: enabled
users: [
{user: $NATS_ADMIN_USER, password: $NATS_ADMIN_PASS}
]
}
}
{{- end }}
102 changes: 102 additions & 0 deletions cluster/charts/fuel-streams/templates/_blocks-pod.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{{/*
Configure default affinity settings for pod scheduling
*/}}
{{- define "k8s.pod-config.affinityy" -}}
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/component: publisher
topologyKey: topology.kubernetes.io/zone
{{- end }}

{{/*
Configure pod spec header including replicas and selector labels
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
- name: Name of the service for selector labels
Returns: YAML configuration for pod spec header
*/}}
{{- define "k8s.pod-spec" -}}
{{- if not .context.autoscaling.enabled }}
replicas: {{ .context.config.replicaCount }}
{{- end }}
selector:
matchLabels:
{{- include "fuel-streams.selectorLabels" (dict "name" .name "context" .root) | nindent 4 }}
app.kubernetes.io/component: {{ .component }}
{{- end }}

{{/*
Configure service account for pod
Parameters:
- root: Root context object containing serviceAccount configuration
Returns: serviceAccountName if serviceAccount creation is enabled
*/}}
{{- define "k8s.pod-config.serviceAccount" -}}
{{- if .root.Values.serviceAccount.create }}
serviceAccountName: {{ include "fuel-streams.serviceAccountName" .root }}
{{- end }}
{{- end }}

{{/*
Configure image pull secrets for pod
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
Returns: imagePullSecrets configuration if specified
*/}}
{{- define "k8s.pod-config.imagePullSecrets" -}}
{{ include "set-field-and-value" (dict "root" .root "context" .context "field" "imagePullSecrets") -}}
{{- end }}

{{/*
Configure node selector for pod scheduling
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
Returns: nodeSelector configuration if specified
*/}}
{{- define "k8s.pod-config.nodeSelector" -}}
{{ include "set-field-and-value" (dict "root" .root "context" .context "field" "nodeSelector") -}}
{{- end }}

{{/*
Configure pod tolerations
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
Returns: tolerations configuration if specified
*/}}
{{- define "k8s.pod-config.tolerations" -}}
{{ include "set-field-and-value" (dict "root" .root "context" .context "field" "tolerations") -}}
{{- end }}

{{/*
Configure pod security context
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
Returns: securityContext configuration from config.podSecurityContext if specified
*/}}
{{- define "k8s.pod-config.securityContext" -}}
{{ include "set-field-and-value" (dict "root" .root "context" .context "field" "securityContext" "path" "config.podSecurityContext") -}}
{{- end }}

{{/*
Configure pod-level settings including security, scheduling and image pull configuration
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
Returns: YAML configuration for pod-level settings
*/}}
{{- define "k8s.pod-config" -}}
{{ include "k8s.pod-config.serviceAccount" (dict "root" .root "context" .context) }}
{{ include "k8s.pod-config.imagePullSecrets" (dict "root" .root "context" .context) }}
{{ include "k8s.pod-config.nodeSelector" (dict "root" .root "context" .context) }}
{{ include "k8s.pod-config.tolerations" (dict "root" .root "context" .context) }}
{{ include "k8s.pod-config.securityContext" (dict "root" .root "context" .context) }}
{{- end }}
53 changes: 53 additions & 0 deletions cluster/charts/fuel-streams/templates/_blocks-resource.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{{/*
Configure resource annotations
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
Returns: Annotations from config.annotations
*/}}
{{- define "k8s.resource-metadata.annotations" -}}
{{- include "set-value" (dict "root" .root "context" .context "path" "config.annotations") }}
{{- end }}

{{/*
Configure resource labels
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
- name: Name of the service for labels
- component: Optional component name for labels
Returns: Labels including common labels, custom labels from config.labels, and component if specified
*/}}
{{- define "k8s.resource-metadata.labels" -}}
{{- include "fuel-streams.labels" (dict "name" .name "context" .root) }}
{{- include "set-value" (dict "root" .root "context" .context "path" "config.labels") }}
{{- if .component }}
app.kubernetes.io/component: {{ .component }}
{{- end }}
{{- end }}

{{/*
Configure resource metadata including name, namespace, labels and annotations
Parameters:
- context: Root context object containing Release and Chart info
- name: Name to use for the resource
- suffix: Optional suffix to append to resource name
- component: Optional component name for labels
- noAnnotations: If true, skip adding annotations
Returns: Complete resource metadata configuration
*/}}
{{- define "k8s.resource-metadata" -}}
{{- $fullname := include "fuel-streams.fullname" .root -}}
{{- $name := printf "%s-%s" $fullname .name -}}
{{- $noAnnotations := .noAnnotations | default false -}}
name: {{ $name }}
namespace: {{ .root.Release.Namespace }}
app: {{ .root.Chart.Name }}
labels:
{{- include "k8s.resource-metadata.labels" (dict "root" .root "context" .context "name" .name "component" .component) | nindent 2 -}}
{{ $annotations := include "k8s.resource-metadata.annotations" (dict "root" .root "context" .context) }}
{{ if and $annotations (not $noAnnotations) }}
annotations:
{{- $annotations | nindent 2 }}
{{- end }}
{{- end }}
49 changes: 49 additions & 0 deletions cluster/charts/fuel-streams/templates/_blocks-template.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

{{/*
Configure pod template metadata including annotations and labels
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
- name: Name of the service for labels
- component: Optional component name for labels
Returns: YAML configuration for pod template metadata
*/}}
{{- define "k8s.template-labels" -}}
{{- $component := .component | default .name }}
{{- include "fuel-streams.labels" (dict "name" .name "context" .root) }}
{{- include "set-value" (dict "root" .root "context" .context "path" "config.labels") }}
{{- if $component }}
app.kubernetes.io/component: {{ $component }}
{{- end }}
{{- end }}

{{/*
Configure pod template metadata including annotations and labels
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
- name: Name of the service for labels
Returns: YAML configuration for pod template metadata
*/}}
{{- define "k8s.template-annotations" -}}
{{- include "set-field-and-value" (dict "root" .root "context" .context "field" "annotations" "path" "config.podAnnotations") }}
{{- end }}

{{/*
Configure pod template metadata including annotations and labels
Parameters:
- root: Root context object for fallback values
- context: Service-specific context object containing configuration
- component: Optional component name for labels
- name: Name of the service for labels
Returns: YAML configuration for pod template metadata including:
- Annotations from config.podAnnotations
- Labels from k8s.template-labels helper
*/}}
{{- define "k8s.template-metadata" -}}
metadata:
{{- include "k8s.template-annotations" (dict "root" .root "context" .context) | nindent 2 }}
labels:
{{- include "k8s.template-labels" (dict "root" .root "context" .context "component" .component "name" .name) | nindent 4 }}
{{- end }}
Loading

0 comments on commit 941c174

Please sign in to comment.