_pod.tpl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {{- define "vm.port.from.flag" -}}
  2. {{- $port := .default -}}
  3. {{- with .flag -}}
  4. {{- $port = regexReplaceAll ".*:(\\d+)" . "${1}" -}}
  5. {{- end -}}
  6. {{- $port -}}
  7. {{- end }}
  8. {{- /*
  9. Return true if the detected platform is Openshift
  10. Usage:
  11. {{- include "vm.isOpenshift" . -}}
  12. */ -}}
  13. {{- define "vm.isOpenshift" -}}
  14. {{- $Capabilities := (.helm).Capabilities | default .Capabilities -}}
  15. {{- if $Capabilities.APIVersions.Has "security.openshift.io/v1" -}}
  16. {{- true -}}
  17. {{- end -}}
  18. {{- end -}}
  19. {{- /*
  20. Render a compatible securityContext depending on the platform.
  21. Usage:
  22. {{- include "vm.securityContext" (dict "securityContext" .Values.containerSecurityContext "helm" .) -}}
  23. */ -}}
  24. {{- define "vm.securityContext" -}}
  25. {{- $securityContext := .securityContext -}}
  26. {{- $Values := (.helm).Values | default .Values -}}
  27. {{- $adaptMode := (((($Values).global).compatibility).openshift).adaptSecurityContext | default "" -}}
  28. {{- if or (eq $adaptMode "force") (and (eq $adaptMode "auto") (include "vm.isOpenshift" .)) -}}
  29. {{- $securityContext = omit $securityContext "fsGroup" "runAsUser" "runAsGroup" -}}
  30. {{- if not $securityContext.seLinuxOptions -}}
  31. {{- $securityContext = omit $securityContext "seLinuxOptions" -}}
  32. {{- end -}}
  33. {{- end -}}
  34. {{- omit $securityContext "enabled" | toYaml -}}
  35. {{- end -}}
  36. {{- /*
  37. Render probe
  38. */ -}}
  39. {{- define "vm.probe" -}}
  40. {{- /* undefined value */ -}}
  41. {{- $null := (fromYaml "value: null").value -}}
  42. {{- $probe := dig .type (default dict) .app.probe -}}
  43. {{- $probeType := "" -}}
  44. {{- $defaultProbe := default dict -}}
  45. {{- if ne (dig "httpGet" $null $probe) $null -}}
  46. {{- /* httpGet probe */ -}}
  47. {{- $defaultProbe = dict "path" (include "vm.probe.http.path" .) "scheme" (include "vm.probe.http.scheme" .) "port" (include "vm.probe.port" .) -}}
  48. {{- $probeType = "httpGet" -}}
  49. {{- else if ne (dig "tcpSocket" $null $probe) $null -}}
  50. {{- /* tcpSocket probe */ -}}
  51. {{- $defaultProbe = dict "port" (include "vm.probe.port" .) -}}
  52. {{- $probeType = "tcpSocket" -}}
  53. {{- end -}}
  54. {{- $defaultProbe = ternary (default dict) (dict $probeType $defaultProbe) (empty $probeType) -}}
  55. {{- $probe = mergeOverwrite $defaultProbe $probe -}}
  56. {{- range $key, $value := $probe -}}
  57. {{- if and (has (kindOf $value) (list "object" "map")) (ne $key $probeType) -}}
  58. {{- $_ := unset $probe $key -}}
  59. {{- end -}}
  60. {{- end -}}
  61. {{- tpl (toYaml $probe) . -}}
  62. {{- end -}}
  63. {{- /*
  64. HTTP GET probe path
  65. */ -}}
  66. {{- define "vm.probe.http.path" -}}
  67. {{- index .app.extraArgs "http.pathPrefix" | default "" | trimSuffix "/" -}}/health
  68. {{- end -}}
  69. {{- /*
  70. HTTP GET probe scheme
  71. */ -}}
  72. {{- define "vm.probe.http.scheme" -}}
  73. {{- ternary "HTTPS" "HTTP" (.app.extraArgs.tls | default false) -}}
  74. {{- end -}}
  75. {{- /*
  76. Net probe port
  77. */ -}}
  78. {{- define "vm.probe.port" -}}
  79. {{- dig "ports" "name" "http" (.app | dict) -}}
  80. {{- end -}}
  81. {{- define "vm.arg" -}}
  82. {{- if and (empty .value) (not (kindIs "bool" .value)) }}
  83. {{- .key -}}
  84. {{- else if and (kindIs "bool" .value) .value -}}
  85. -{{ ternary "" "-" (eq (len .key) 1) }}{{ .key }}
  86. {{- else -}}
  87. -{{ ternary "" "-" (eq (len .key) 1) }}{{ .key }}={{ .value }}
  88. {{- end -}}
  89. {{- end -}}
  90. {{- /*
  91. command line arguments
  92. */ -}}
  93. {{- define "vm.args" -}}
  94. {{- $args := default list -}}
  95. {{- range $key, $value := . -}}
  96. {{- if not $key -}}
  97. {{- fail "Empty key in command line args is not allowed" -}}
  98. {{- end -}}
  99. {{- if kindIs "slice" $value -}}
  100. {{- range $v := $value -}}
  101. {{- $args = append $args (include "vm.arg" (dict "key" $key "value" $v)) -}}
  102. {{- end -}}
  103. {{- else -}}
  104. {{- $args = append $args (include "vm.arg" (dict "key" $key "value" $value)) -}}
  105. {{- end -}}
  106. {{- end -}}
  107. {{- toYaml (dict "args" $args) -}}
  108. {{- end -}}