7676
7777{{- /* Labels commonly used in our k8s resources */ -}}
7878{{- define " common.labels" -}}
79- app.kubernetes.io /name: {{ template " agent.resource.name" . }}
79+ app.kubernetes.io /name: {{ include " agent.resource.name" . }}
8080app.kubernetes.io /instance: {{ include " name.prefix" . }}
8181{{- end -}}
8282
8383{{- /* Labels commonly used in our "pod group" resources */ -}}
8484{{- define " common.labels.with.chart" -}}
8585helm.sh /chart: {{ printf " %s -%s " .Chart.name .Chart.version | replace " +" " _" | trunc 63 | trimSuffix " -" }}
86+ app.kubernetes.io /managed-by: {{ $ .Release.Service }}
87+ app.kubernetes.io /version: {{ $ .Chart.appVersion }}
88+ app.created.by.template : {{ (include " is.helm.template.command" . ) | quote }}
8689{{ template " common.labels" . }}
8790{{- end -}}
8891
@@ -258,13 +261,13 @@ key: {{ $cert.Key | b64enc }}
258261 {{- printf " {\" auths\" :{\" %s \" :{\" auth\" :\" %s \" }}}" .Values.imageRegistry.url (printf " %s :%s " $user $pass | b64enc) | b64enc -}}
259262{{- end -}}
260263
261- {{- /* validate containerRuntime is one of the allowed values.
264+ {{- /* validate containerRuntime is one of the supported values.
262265takes a context (such as $config, .Values or (dict "containerRuntime" $containerRuntime)) that has a .containerRuntime field */ -}}
263266{{- define " validate.container.runtime" -}}
264- {{- $allowedRuntimes := list " docker " " containerd " " cri-o " -}}
265- {{- if has (.containerRuntime | lower) $allowedRuntimes -}}
267+ {{- $supportedRuntimes := (include " supported.containerRuntimes " . ) | splitList " " -}}
268+ {{- if has (.containerRuntime | lower) $supportedRuntimes -}}
266269{{- else -}}
267- {{- $err := printf " \n\n ERROR: Invalid containerRuntime: %s (should be one of: %s )" .containerRuntime $allowedRuntimes -}}
270+ {{- $err := printf " \n\n ERROR: Invalid containerRuntime: %s (should be one of: %s )" .containerRuntime $supportedRuntimes -}}
268271{{- fail $err -}}
269272{{- end -}}
270273{{- end -}}
@@ -286,27 +289,40 @@ takes a context (such as $config, .Values or (dict "containerRuntime" $container
286289{{- end -}}
287290
288291
292+ {{- /* get the first node from the cluster */ -}}
293+ {{- define " get.first.node" -}}
294+ {{- $nodes := lookup " v1" " Node" " " " " -}}
295+ {{- if empty $nodes -}}
296+ {{- else if eq (len $nodes .items ) 0 -}}
297+ {{- else -}}
298+ {{- first $nodes .items | toYaml -}}
299+ {{- end -}}
300+ {{- end -}}
301+
302+
289303{{- define " get.container.runtime" -}}
290- {{- if .Values.containerRuntime -}}
291- {{- include " validate.container.runtime" .Values -}}
292- {{ .Values.containerRuntime | lower }}
293- {{- else -}}
294- {{- $nodes := lookup " v1" " Node" " " " " -}}
295- {{- if ne (len $nodes ) 0 -}}
296- {{/* examples for runtime version: docker://19.3.3, containerd://1.3.3, cri-o://1.20.3 */ }}
297- {{- $containerRuntimeVersion := (first $nodes .items ).status.nodeInfo.containerRuntimeVersion -}}
298- {{- $containerRuntime := first (regexSplit " :" $containerRuntimeVersion -1) -}}
299- {{- include " validate.container.runtime" (dict " containerRuntime" $containerRuntime ) -}}
304+ {{- if .Values.containerRuntime -}}
305+ {{- include " validate.container.runtime" .Values -}}
306+ {{ .Values.containerRuntime | lower }}
307+ {{- else -}}
308+ {{- $noRuntimeErr := " \n\n ERROR: No nodes found, cannot identify container runtime. Use '--set containerRuntime=docker' or '--set containerRuntime=containerd' or '--set containerRuntime=cri-o'" -}}
309+ {{- $firstNode := include " get.first.node" . | fromYaml -}}
310+ {{- if empty $firstNode -}}
311+ {{- fail $noRuntimeErr -}}
312+ {{- end -}}
313+ {{/* examples for runtime version: docker://19.3.3, containerd://1.3.3, cri-o://1.20.3 */ }}
314+ {{- $containerRuntimeVersion := $firstNode .status.nodeInfo.containerRuntimeVersion -}}
315+ {{- $containerRuntime := first (regexSplit " :" $containerRuntimeVersion -1) -}}
316+ {{- include " validate.container.runtime" (dict " containerRuntime" $containerRuntime ) -}}
300317{{ $containerRuntime | lower }}
301- {{- else -}}
302- {{- fail " \n\n ERROR: No nodes found, cannot identify container runtime. Use '--set containerRuntime=docker' or '--set containerRuntime=containerd' or '--set containerRuntime=cri-o'" -}}
303- {{- end -}}
304- {{- end -}}
318+ {{- end -}}
305319{{- end -}}
306320
321+
322+ {{- /* get platform value, if not provided, try to infer it from the first node */ -}}
307323{{- define " get.platform" -}}
308324{{- /* use platform value if it's a helm template command or when the provided value is not the default kubernetes */ -}}
309- {{- if or (include " is.helm.template.command" . ) (and .Values.platform (ne .Values.platform " kubernetes" )) -}}
325+ {{- if or (eq ( include " is.helm.template.command" . ) " true " ) (and .Values.platform (ne .Values.platform " kubernetes" )) -}}
310326{{- include " validate.platform" .Values -}}
311327{{- lower .Values.platform -}}
312328{{- else if has " config.openshift.io/v1" .Capabilities.APIVersions -}}
@@ -318,14 +334,18 @@ takes a context (such as $config, .Values or (dict "containerRuntime" $container
318334{{/* else if has "auto.gke.io/v1" .Capabilities.APIVersions */ }}
319335{{/* printf "gke.autopilot" */ }}
320336{{- else -}}
321- {{- $nodes := lookup " v1" " Node" " " " " -}}
337+ {{- $supportedPlatforms := (include " supported.platforms" . ) | splitList " " -}}
338+ {{- $noPlatformErr := printf " \n\n ERROR: No nodes found, cannot identify platform. Append '--set platform=<platform>', platform should be one of %s " $supportedPlatforms -}}
339+ {{- $firstNode := include " get.first.node" . | fromYaml -}}
340+ {{- if empty $firstNode -}}
341+ {{- fail $noPlatformErr -}}
342+ {{- end -}}
343+ {{- $osImage := $firstNode .status.nodeInfo.osImage -}}
322344{{/*
323345 nodeInfo.osImage example values:
324346 - "Bottlerocket OS 1.7.2 (aws-k8s-1.21)"
325347 - "Container-Optimized OS from Google"
326348*/ }}
327- {{- $firstNode := (first $nodes .items ) -}}
328- {{- $osImage := $firstNode .status.nodeInfo.osImage -}}
329349{{- if contains " Bottlerocket" $osImage -}}
330350{{- printf " eks.bottlerocket" -}}
331351{{- else if contains " Container-Optimized" $osImage -}}
@@ -334,6 +354,8 @@ takes a context (such as $config, .Values or (dict "containerRuntime" $container
334354{{- printf " kubernetes.coreos" -}}
335355{{- else if hasKey $firstNode .metadata.annotations " k3s.io/hostname" -}}
336356{{- printf " k3s" -}}
357+ {{- else if hasKey $firstNode .metadata.annotations " rke2.io/hostname" -}}
358+ {{- printf " rke2" -}}
337359{{- else if or (hasKey $firstNode .metadata.labels " eks.amazonaws.com/nodegroup" ) (hasKey $firstNode .metadata.labels " alpha.eksctl.io/nodegroup-name" ) (hasKey $firstNode .metadata.labels " eks.amazonaws.com/compute-type" ) -}}
338360{{- printf " eks" -}}
339361{{- else -}}
@@ -390,16 +412,19 @@ If a user opts for the default "preserve" option:
390412{{- end -}}
391413{{- end -}}
392414
393-
394415{{- /*
395416 use to know if we run from template (which mean wo have no connection to the cluster and cannot check Capabilities/nodes etc.)
396417 if there is no namespace probably we are running template
418+ returns string value "true" or "false"
419+ usage:
420+ `{{- if eq (include "is.helm.template.command") "true" -}}`
397421*/ -}}
398422{{- define " is.helm.template.command" -}}
423+ {{- if not (hasKey .Values " isHelmTemplateCache" ) -}}
399424{{- $namespace := lookup " v1" " Namespace" " " " " -}}
400- {{- if eq (len $namespace ) 0 -}}
401- true
425+ {{- $_ := set .Values " isHelmTemplateCache" (eq (len $namespace ) 0) -}}
402426{{- end -}}
427+ {{- .Values.isHelmTemplateCache | toYaml -}}
403428{{- end -}}
404429
405430{{- define " containerd.sock.path" -}}
@@ -412,20 +437,28 @@ true
412437{{ printf (.Values.containerRuntimeSocket | toString) }}
413438{{- else if eq .platform " eks.bottlerocket" -}}
414439{{- printf " /run/dockershim.sock" -}}
415- {{- else if eq .platform " k3s" -}}
440+ {{- else if has .platform (list " k3s" " rke2 " ) -}}
416441{{- printf " /run/k3s/containerd/containerd.sock" -}}
417442{{- else -}}
418443{{- printf " /run/containerd/containerd.sock" -}}
419444{{- end -}}
420445{{- end -}}
421446
422- {{- /* validate platform is one of the allowed values.
447+ {{- define " containerd.runtime.v2.task" -}}
448+ {{- if has .platform (list " k3s" " rke2" ) -}}
449+ {{- printf " /run/k3s/containerd/io.containerd.runtime.v2.task" -}}
450+ {{- else -}}
451+ {{- printf " /run/containerd/io.containerd.runtime.v2.task" -}}
452+ {{- end -}}
453+ {{- end -}}
454+
455+ {{- /* validate platform is one of the supported values.
423456takes a context (such as $config or .Values) that has a .platform field */ -}}
424457{{- define " validate.platform" -}}
425- {{- $allowedPlatforms := list " kubernetes " " tanzu " " openshift " " openshift.v3 " " eks " " eks.bottlerocket " " gke.cos " " gke.autopilot " " k3s " " kubernetes.coreos " -}}
426- {{- if has (.platform | lower) $allowedPlatforms -}}
458+ {{- $supportedPlatforms := (include " supported.platforms " . ) | splitList " " -}}
459+ {{- if has (.platform | lower) $supportedPlatforms -}}
427460{{- else -}}
428- {{- $err := printf " \n\n ERROR: Invalid platform: %s ( should be one of: %s ) " .platform $allowedPlatforms -}}
461+ {{- $err := printf " \n\n ERROR: Invalid platform: %s , should be one of: %s " .platform $supportedPlatforms -}}
429462{{- fail $err -}}
430463{{- end -}}
431464{{- end -}}
@@ -517,3 +550,20 @@ nodeAffinity:
517550 - {{$labelValue }}
518551{{- end -}}
519552{{- end -}}
553+
554+
555+ {{- /* list of supported platforms
556+ usage:
557+ `{{- $supportedPlatforms := (include "supported.platforms" .) | splitList " " -}}`
558+ */ -}}
559+ {{- define " supported.platforms" -}}
560+ kubernetes kubernetes.coreos tanzu openshift openshift.v3 eks eks.bottlerocket gke.cos gke.autopilot k3s rke2
561+ {{- end -}}
562+
563+ {{- /* list of supported containter runtimes
564+ usage:
565+ `{{- $supportedRuntimes := (include "supported.containerRuntimes" .) | splitList " " -}}`
566+ */ -}}
567+ {{- define " supported.containerRuntimes" -}}
568+ docker containerd cri-o
569+ {{- end -}}
0 commit comments