_resources.tpl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. {{/* Returns Resources */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.container.resources" (dict "rootCtx" $ "objectData" $objectData) }}
  4. rootCtx: The root context of the chart.
  5. objectData: The object data to be used to render the container.
  6. */}}
  7. {{- define "ix.v1.common.lib.container.resources" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{- $resources := $rootCtx.Values.resources -}}
  11. {{- if $objectData.resources -}}
  12. {{- $resources = mustMergeOverwrite $resources $objectData.resources -}}
  13. {{- end -}}
  14. {{- include "ix.v1.common.lib.container.resources.validation" (dict "resources" $resources) -}}
  15. requests:
  16. cpu: {{ $resources.requests.cpu }}
  17. memory: {{ $resources.requests.memory }}
  18. {{- if $resources.limits }}
  19. limits:
  20. {{- with $resources.limits.cpu }} {{/* Passing 0, will not render it, meaning unlimited */}}
  21. cpu: {{ . }}
  22. {{- end -}}
  23. {{- with $resources.limits.memory }} {{/* Passing 0, will not render it, meaning unlimited */}}
  24. memory: {{ . }}
  25. {{- end -}}
  26. {{- include "ix.v1.common.lib.container.resources.gpu" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 -}}
  27. {{- end -}}
  28. {{- end -}}
  29. {{/* Returns GPU resource */}}
  30. {{/* Call this template:
  31. {{ include "ix.v1.common.lib.container.resources.gpu" (dict "rootCtx" $rootCtx "objectData" $objectData) }}
  32. rootCtx: The root context of the chart.
  33. objectData: The object data to be used to render the container.
  34. */}}
  35. {{- define "ix.v1.common.lib.container.resources.gpu" -}}
  36. {{- $objectData := .objectData -}}
  37. {{- $rootCtx := .rootCtx -}}
  38. {{- $returnBool := .returnBool -}}
  39. {{- $gpuResource := list -}}
  40. {{- range $GPUValues := $rootCtx.Values.scaleGPU -}}
  41. {{- if not $GPUValues.gpu -}}
  42. {{- fail "Container - Expected non-empty <scaleGPU.gpu>" -}}
  43. {{- end -}}
  44. {{- $selected := false -}}
  45. {{/* Parse selector if defined */}}
  46. {{- if $GPUValues.targetSelector -}}
  47. {{- range $podName, $containers := $GPUValues.targetSelector -}}
  48. {{- if not $containers -}}
  49. {{- fail "Container - Expected non-empty list under pod in <scaleGPU.targetSelector>" -}}
  50. {{- end -}}
  51. {{- if and (eq $podName $objectData.podShortName) (mustHas $objectData.shortName $containers) -}}
  52. {{- $selected = true -}}
  53. {{- end -}}
  54. {{- end -}}
  55. {{/* If no selector, select primary pod/container */}}
  56. {{- else if and $objectData.podPrimary $objectData.primary -}}
  57. {{- $selected = true -}}
  58. {{- end -}}
  59. {{- if $selected -}}
  60. {{- $gpuResource = mustAppend $gpuResource $GPUValues.gpu -}}
  61. {{- end -}}
  62. {{- end -}}
  63. {{- if not $returnBool -}}
  64. {{- range $gpu := $gpuResource -}}
  65. {{- range $k, $v := $gpu -}}
  66. {{- if or (kindIs "invalid" $v) (eq (toString $v) "") -}}
  67. {{- fail "Container - Expected non-empty <scaleGPU> <value>" -}}
  68. {{- end -}} {{/* Dont try to schedule 0 GPUs */}}
  69. {{- if gt (int $v) 0 }}
  70. {{ $k }}: {{ $v | quote }}
  71. {{- end -}}
  72. {{- end -}}
  73. {{- end -}}
  74. {{- else -}}
  75. {{- if $gpuResource -}}
  76. {{- "true" -}}
  77. {{- end -}}
  78. {{- end -}}
  79. {{- end -}}
  80. {{/* Validates resources to match a pattern */}}
  81. {{/* Call this template:
  82. {{ include "ix.v1.common.lib.container.resources.validation" (dict "resources" $resources) }}
  83. rootCtx: The root context of the chart.
  84. resources: The resources object
  85. */}}
  86. {{- define "ix.v1.common.lib.container.resources.validation" -}}
  87. {{- $resources := .resources -}}
  88. {{/* CPU: https://regex101.com/r/D4HouI/1 */}}
  89. {{/* MEM: https://regex101.com/r/NNPV2D/1 */}}
  90. {{- $regex := (dict
  91. "cpu" "^(0\\.[1-9]|[1-9][0-9]*)(\\.[0-9]|m?)$"
  92. "memory" "^[1-9][0-9]*([EPTGMK]i?|e[0-9]+)?$") -}}
  93. {{- $errorMsg := (dict
  94. "cpu" "(Plain Integer - eg. 1), (Float - eg. 0.5), (Milicpu - eg. 500m)"
  95. "memory" "(Suffixed with E/P/T/G/M/K - eg. 1G), (Suffixed with Ei/Pi/Ti/Gi/Mi/Ki - eg. 1Gi), (Plain Integer in bytes - eg. 1024), (Exponent - eg. 134e6)") -}}
  96. {{- $resourceTypes := (list "cpu" "memory") -}}
  97. {{- range $category := (list "requests") -}} {{/* We can also add "limits" here if we want to require them */}}
  98. {{- if not (get $resources $category) -}}
  99. {{- fail (printf "Container - Expected non-empty <resources.%s>" $category) -}}
  100. {{- end -}}
  101. {{- range $type := $resourceTypes -}}
  102. {{- if not (get (get $resources $category) $type) -}}
  103. {{- fail (printf "Container - Expected non-empty <resources.%s.%s>" $category $type) -}}
  104. {{- end -}}
  105. {{- end -}}
  106. {{- end -}}
  107. {{- range $key := (list "requests" "limits") -}}
  108. {{- $resourceCategory := (get $resources $key) -}}
  109. {{- if $resourceCategory -}}
  110. {{- range $type := $resourceTypes -}}
  111. {{- $resourceValue := (get $resourceCategory $type) -}}
  112. {{- if $resourceValue -}} {{/* Only try to match defined values */}}
  113. {{- if not (mustRegexMatch (get $regex $type) (toString $resourceValue)) -}}
  114. {{- fail (printf "Container - Expected <resources.%s.%s> to have one of the following formats [%s], but got [%s]" $key $type (get $errorMsg $type) $resourceValue) -}}
  115. {{- end -}}
  116. {{- end -}}
  117. {{- end -}}
  118. {{- end -}}
  119. {{- end -}}
  120. {{- end -}}