make resources reusable

This commit is contained in:
Stavros kois
2022-12-21 17:01:29 +02:00
parent 482b5d7d71
commit b897d58e79
4 changed files with 91 additions and 48 deletions

View File

@@ -1,47 +0,0 @@
{{/* Returns the resources for the container */}}
{{- define "ix.v1.common.container.resources" -}}
{{- $resources := .resources -}}
{{- $gpu := .gpu -}}
{{- if or $resources $gpu -}}
{{- with $resources.requests -}}
{{- with (include "ix.v1.common.container.resources.cpuAndMemory" (dict "cpu" .cpu "memory" .memory)) -}}
requests:
{{- . | indent 2 -}}
{{- end -}}
{{- end -}}
{{- if or $resources.limits $gpu -}}
{{- if or $resources.limits.cpu $resources.limits.memory $gpu }}
limits:
{{- include "ix.v1.common.container.resources.cpuAndMemory" (dict "cpu" $resources.limits.cpu "memory" $resources.limits.memory) | indent 2 -}}
{{- include "ix.v1.common.container.resources.gpu" (dict "gpu" $gpu) | indent 2 -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/* Returns CPU and Memory if applicable */}}
{{- define "ix.v1.common.container.resources.cpuAndMemory" -}}
{{- $cpu := .cpu -}}
{{- $memory := .memory -}}
{{- with $cpu }}
cpu: {{ . }}
{{- end -}}
{{- with $memory }}
memory: {{ . }}
{{- end -}}
{{- end -}}
{{/* Returns GPU if applicable */}}
{{- define "ix.v1.common.container.resources.gpu" -}}
{{- $gpu := .gpu -}}
{{- range $k, $v := $gpu -}}
{{- if not $v -}}
{{- fail (printf "Value is not provided for GPU (<key> %s)" $k) -}}
{{- else }}
{{ $k }}: {{ $v | quote }}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@@ -0,0 +1,89 @@
{{/* Returns the resources for the container */}}
{{- define "ix.v1.common.container.resources" -}}
{{- $resources := .resources -}}
{{- $gpu := .gpu -}}
{{- $root := .root -}}
{{/* Get defaults from global */}}
{{- $defautlResources := $root.Values.global.defaults.resources -}}
{{- $limitsCPU := $defautlResources.limits.cpu -}}
{{- $limitsMemory := $defautlResources.limits.memory -}}
{{- $requestsCPU := $defautlResources.requests.cpu -}}
{{- $requestsMemory := $defautlResources.requests.memory -}}
{{/* Modify based on user/dev input */}}
{{- with $resources -}}
{{- with $resources.requests -}}
{{- if hasKey . "cpu" -}}
{{- if ne $requestsCPU .cpu -}}
{{- $requestsCPU = .cpu -}}
{{- else if not .cpu -}} {{/* If key exists but it's empty, means user/dev explicitly said "no limit" */}}
{{- $requestsCPU = "" -}}
{{- end -}}
{{- end -}}
{{- if hasKey . "memory" -}}
{{- if ne $requestsMemory .memory -}}
{{- $requestsMemory = .memory -}}
{{- else if not .memory -}} {{/* If key exists but it's empty, means user/dev explicitly said "no limit" */}}
{{- $requestsMemory = "" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- with $resources.limits -}}
{{- if hasKey . "cpu" -}}
{{- if ne $limitsCPU .cpu -}}
{{- $limitsCPU = .cpu -}}
{{- else if not .cpu -}} {{/* If key exists but it's empty, means user/dev explicitly said "no limit" */}}
{{- $limitsCPU = "" -}}
{{- end -}}
{{- end -}}
{{- if hasKey . "memory" -}}
{{- if ne $limitsMemory .memory -}}
{{- $limitsMemory = .memory -}}
{{- else if not .memory -}} {{/* If key exists but it's empty, means user/dev explicitly said "no limit" */}}
{{- $limitsMemory = "" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if or $resources $defautlResources $gpu -}}
{{- if or $requestsCPU $requestsMemory -}}
{{- with (include "ix.v1.common.container.resources.cpuAndMemory" (dict "cpu" $requestsCPU "memory" $requestsMemory)) }}
requests:
{{- . | indent 2 -}}
{{- end -}}
{{- end -}}
{{- if or $limitsCPU $limitsMemory $gpu }}
limits:
{{- include "ix.v1.common.container.resources.cpuAndMemory" (dict "cpu" $limitsCPU "memory" $limitsMemory) | indent 2 -}}
{{- include "ix.v1.common.container.resources.gpu" (dict "gpu" $gpu) | indent 2 -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{/* Returns CPU and Memory if applicable */}}
{{- define "ix.v1.common.container.resources.cpuAndMemory" -}}
{{- $cpu := .cpu -}}
{{- $memory := .memory -}}
{{- with $cpu }}
cpu: {{ . }}
{{- end -}}
{{- with $memory }}
memory: {{ . }}
{{- end -}}
{{- end -}}
{{/* Returns GPU if applicable */}}
{{- define "ix.v1.common.container.resources.gpu" -}}
{{- $gpu := .gpu -}}
{{- range $k, $v := $gpu -}}
{{- if not $v -}}
{{- fail (printf "Value is not provided for GPU (<key> %s)" $k) -}}
{{- else }}
{{- $k | nindent 0 }}: {{ $v | quote }}
{{- end -}}
{{- end -}}
{{- end -}}

View File

@@ -2,6 +2,7 @@
{{- $index := .index -}}
{{- $vol := .volume -}}
{{- $root := .root -}}
{{- include "ix.v1.common.controller.volumes.hostPath.validation" (dict "volume" $vol "root" $root) }} {{/* hostPath validation (if enabled) */}}
- name: {{ $index }}
hostPath:

View File

@@ -2,7 +2,7 @@
{{- $vol := .volume -}}
{{- $root := .root -}}
{{- $validate := $root.Values.global.defaults.validateHostPath -}}
W
{{- if (hasKey $vol "validateHostPath") -}}
{{- $validate = $vol.validateHostPath -}}
{{- end -}}