_resources.tpl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 not $v -}}
  67. {{- fail "Container - Expected non-empty <scaleGPU> <value>" -}}
  68. {{- end }}
  69. {{ $k }}: {{ $v | quote }}
  70. {{- end -}}
  71. {{- end -}}
  72. {{- else -}}
  73. {{- if $gpuResource -}}
  74. {{- "true" -}}
  75. {{- end -}}
  76. {{- end -}}
  77. {{- end -}}
  78. {{/* Validates resources to match a pattern */}}
  79. {{/* Call this template:
  80. {{ include "ix.v1.common.lib.container.resources.validation" (dict "resources" $resources) }}
  81. rootCtx: The root context of the chart.
  82. resources: The resources object
  83. */}}
  84. {{- define "ix.v1.common.lib.container.resources.validation" -}}
  85. {{- $resources := .resources -}}
  86. {{/* CPU: https://regex101.com/r/D4HouI/1 */}}
  87. {{/* MEM: https://regex101.com/r/NNPV2D/1 */}}
  88. {{- $regex := (dict
  89. "cpu" "^(0\\.[1-9]|[1-9][0-9]*)(\\.[0-9]|m?)$"
  90. "memory" "^[1-9][0-9]*([EPTGMK]i?|e[0-9]+)?$") -}}
  91. {{- $errorMsg := (dict
  92. "cpu" "(Plain Integer - eg. 1), (Float - eg. 0.5), (Milicpu - eg. 500m)"
  93. "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)") -}}
  94. {{- $resourceTypes := (list "cpu" "memory") -}}
  95. {{- range $category := (list "requests") -}} {{/* We can also add "limits" here if we want to require them */}}
  96. {{- if not (get $resources $category) -}}
  97. {{- fail (printf "Container - Expected non-empty <resources.%s>" $category) -}}
  98. {{- end -}}
  99. {{- range $type := $resourceTypes -}}
  100. {{- if not (get (get $resources $category) $type) -}}
  101. {{- fail (printf "Container - Expected non-empty <resources.%s.%s>" $category $type) -}}
  102. {{- end -}}
  103. {{- end -}}
  104. {{- end -}}
  105. {{- range $key := (list "requests" "limits") -}}
  106. {{- $resourceCategory := (get $resources $key) -}}
  107. {{- if $resourceCategory -}}
  108. {{- range $type := $resourceTypes -}}
  109. {{- $resourceValue := (get $resourceCategory $type) -}}
  110. {{- if $resourceValue -}} {{/* Only try to match defined values */}}
  111. {{- if not (mustRegexMatch (get $regex $type) (toString $resourceValue)) -}}
  112. {{- fail (printf "Container - Expected <resources.%s.%s> to have one of the following formats [%s], but got [%s]" $key $type (get $errorMsg $type) $resourceValue) -}}
  113. {{- end -}}
  114. {{- end -}}
  115. {{- end -}}
  116. {{- end -}}
  117. {{- end -}}
  118. {{- end -}}