_probes.tpl 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. {{/* Returns Probes */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.container.probes" (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.probes" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{- $probeNames := (list "liveness" "readiness" "startup") -}}
  11. {{- $probeTypes := (list "http" "https" "tcp" "grpc" "exec") -}}
  12. {{- if not $objectData.probes -}}
  13. {{- fail "Container - Expected non-empty <probes>" -}}
  14. {{- end -}}
  15. {{- range $key := $probeNames -}}
  16. {{- if not (get $objectData.probes $key) -}}
  17. {{- fail (printf "Container - Expected <probes.%s> to be defined" $key) -}}
  18. {{- end -}}
  19. {{- end -}}
  20. {{- range $probeName, $probe := $objectData.probes -}}
  21. {{- if not (mustHas $probeName $probeNames) -}}
  22. {{- fail (printf "Container - Expected probe to be one of [%s], but got [%s]" (join ", " $probeNames) $probeName) -}}
  23. {{- end -}}
  24. {{- $isEnabled := true -}}
  25. {{- if kindIs "bool" $probe.enabled -}}
  26. {{- $isEnabled = $probe.enabled -}}
  27. {{- end -}}
  28. {{- if $isEnabled -}}
  29. {{- $probeType := $rootCtx.Values.fallbackDefaults.probeType -}}
  30. {{- with $probe.type -}}
  31. {{- $probeType = tpl . $rootCtx -}}
  32. {{- end -}}
  33. {{- if not (mustHas $probeType $probeTypes) -}}
  34. {{- fail (printf "Container - Expected probe type to be one of [%s], but got [%s]" (join ", " $probeTypes) $probeType) -}}
  35. {{- end }}
  36. {{ $probeName }}Probe:
  37. {{- if (mustHas $probeType (list "http" "https")) -}}
  38. {{- include "ix.v1.common.lib.container.actions.httpGet" (dict "rootCtx" $rootCtx "objectData" $probe "caller" "probes") | trim | nindent 2 -}}
  39. {{- else if eq $probeType "tcp" -}}
  40. {{- include "ix.v1.common.lib.container.actions.tcpSocket" (dict "rootCtx" $rootCtx "objectData" $probe "caller" "probes") | trim | nindent 2 -}}
  41. {{- else if eq $probeType "grpc" -}}
  42. {{- include "ix.v1.common.lib.container.actions.grpc" (dict "rootCtx" $rootCtx "objectData" $probe "caller" "probes") | trim | nindent 2 -}}
  43. {{- else if eq $probeType "exec" -}}
  44. {{- include "ix.v1.common.lib.container.actions.exec" (dict "rootCtx" $rootCtx "objectData" $probe "caller" "probes") | trim | nindent 2 -}}
  45. {{- end -}}
  46. {{- include "ix.v1.common.lib.container.probeTimeouts" (dict "rootCtx" $rootCtx "objectData" $probe "probeName" $probeName) | trim | nindent 2 -}}
  47. {{- end -}}
  48. {{- end -}}
  49. {{- end -}}
  50. {{/* Returns Probe Timeouts */}}
  51. {{/* Call this template:
  52. {{ include "ix.v1.common.lib.container.probeTimeouts" (dict "rootCtx" $ "objectData" $objectData) }}
  53. rootCtx: The root context of the chart.
  54. objectData: The object data to be used to render the container.
  55. */}}
  56. {{- define "ix.v1.common.lib.container.probeTimeouts" -}}
  57. {{- $rootCtx := .rootCtx -}}
  58. {{- $objectData := .objectData -}}
  59. {{- $probeName := .probeName -}}
  60. {{- $timeouts := (get $rootCtx.Values.fallbackDefaults.probeTimeouts $probeName) -}}
  61. {{- if $objectData.spec -}} {{/* Overwrite with defined timeouts */}}
  62. {{- $timeouts = mustMergeOverwrite $timeouts $objectData.spec -}}
  63. {{- end -}}
  64. {{- $keys := (list "initialDelaySeconds" "failureThreshold" "successThreshold" "timeoutSeconds" "periodSeconds") -}}
  65. {{- range $key := $keys -}}
  66. {{- $number := get $timeouts $key -}}
  67. {{- if not (mustHas (kindOf $number) (list "float64" "int")) -}}
  68. {{- fail (printf "Container - Expected <probes> <%s> to be a number, but got [%s]" $key $number) -}}
  69. {{- end -}}
  70. {{- end -}}
  71. {{- if mustHas $probeName (list "liveness" "startup") -}}
  72. {{- if ne (int $timeouts.successThreshold) 1 -}}
  73. {{- fail (printf "Container - Expected <probes> <successThreshold> to be 1 on [%s] probe" $probeName) -}}
  74. {{- end -}}
  75. {{- end }}
  76. initialDelaySeconds: {{ $timeouts.initialDelaySeconds }}
  77. failureThreshold: {{ $timeouts.failureThreshold }}
  78. successThreshold: {{ $timeouts.successThreshold }}
  79. timeoutSeconds: {{ $timeouts.timeoutSeconds }}
  80. periodSeconds: {{ $timeouts.periodSeconds }}
  81. {{- end -}}