_getPortRange.tpl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. {{/* Returns Lowest and Highest ports assigned to the any container in the pod */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.helpers.securityContext.getPortRange" (dict "rootCtx" $ "objectData" $objectData) }}
  4. rootCtx: The root context of the chart.
  5. objectData: The object data to be used to render the Pod.
  6. */}}
  7. {{- define "ix.v1.common.lib.helpers.securityContext.getPortRange" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{ $portRange := (dict "high" 0 "low" 0) }}
  11. {{- range $name, $service := $rootCtx.Values.service -}}
  12. {{- $selected := false -}}
  13. {{/* If service is enabled... */}}
  14. {{- if $service.enabled -}}
  15. {{/* If there is a selector */}}
  16. {{- if $service.targetSelector -}}
  17. {{/* And pod is selected */}}
  18. {{- if eq $service.targetSelector $objectData.shortName -}}
  19. {{- $selected = true -}}
  20. {{- end -}}
  21. {{- else -}}
  22. {{/* If no selector is defined but pod is primary */}}
  23. {{- if $objectData.primary -}}
  24. {{- $selected = true -}}
  25. {{- end -}}
  26. {{- end -}}
  27. {{- end -}}
  28. {{- if $selected -}}
  29. {{- range $name, $portValues := $service.ports -}}
  30. {{- if $portValues.enabled -}}
  31. {{- $portToCheck := ($portValues.targetPort | default $portValues.port) -}}
  32. {{- if kindIs "string" $portToCheck -}}
  33. {{/* Helm stores ints as floats, so convert string to float before comparing */}}
  34. {{- $portToCheck = (tpl $portToCheck $rootCtx) | float64 -}}
  35. {{- end -}}
  36. {{- if or (not $portRange.low) (lt $portToCheck ($portRange.low | float64)) -}}
  37. {{- $_ := set $portRange "low" $portToCheck -}}
  38. {{- end -}}
  39. {{- if or (not $portRange.high) (gt $portToCheck ($portRange.high | float64)) -}}
  40. {{- $_ := set $portRange "high" $portToCheck -}}
  41. {{- end -}}
  42. {{- end -}}
  43. {{- end -}}
  44. {{- end -}}
  45. {{- end -}}
  46. {{- $portRange | toJson -}}
  47. {{- end -}}