_getPortRange.tpl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. {{- $portToCheck = (tpl $portToCheck $rootCtx) | int -}}
  34. {{- end -}}
  35. {{- if or (not $portRange.low) (lt ($portToCheck | int) ($portRange.low | int)) -}}
  36. {{- $_ := set $portRange "low" $portToCheck -}}
  37. {{- end -}}
  38. {{- if or (not $portRange.high) (gt ($portToCheck | int) ($portRange.high | int)) -}}
  39. {{- $_ := set $portRange "high" $portToCheck -}}
  40. {{- end -}}
  41. {{- end -}}
  42. {{- end -}}
  43. {{- end -}}
  44. {{- end -}}
  45. {{- $portRange | toJson -}}
  46. {{- end -}}