_ports.tpl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. {{/* Returns ports list */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.container.ports" (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.ports" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{- range $serviceName, $serviceValues := $rootCtx.Values.service -}}
  11. {{- $podSelected := false -}}
  12. {{/* If service is enabled... */}}
  13. {{- if $serviceValues.enabled -}}
  14. {{/* If there is a selector */}}
  15. {{- if $serviceValues.targetSelector -}}
  16. {{/* And pod is selected */}}
  17. {{- if eq $serviceValues.targetSelector $objectData.podShortName -}}
  18. {{- $podSelected = true -}}
  19. {{- end -}}
  20. {{- else -}}
  21. {{/* If no selector is defined but pod is primary */}}
  22. {{- if $objectData.podPrimary -}}
  23. {{- $podSelected = true -}}
  24. {{- end -}}
  25. {{- end -}}
  26. {{- end -}}
  27. {{- if $podSelected -}}
  28. {{- range $portName, $portValues := $serviceValues.ports -}}
  29. {{- $containerSelected := false -}}
  30. {{/* If service is enabled... */}}
  31. {{- if $portValues.enabled -}}
  32. {{/* If there is a selector */}}
  33. {{- if $portValues.targetSelector -}}
  34. {{/* And container is selected */}}
  35. {{- if eq $portValues.targetSelector $objectData.shortName -}}
  36. {{- $containerSelected = true -}}
  37. {{- end -}}
  38. {{- else -}}
  39. {{/* If no selector is defined but container is primary */}}
  40. {{- if $objectData.primary -}}
  41. {{- $containerSelected = true -}}
  42. {{- end -}}
  43. {{- end -}}
  44. {{- end -}}
  45. {{/* If the container is selected render port */}}
  46. {{- if $containerSelected -}}
  47. {{- $containerPort := $portValues.targetPort | default $portValues.port -}}
  48. {{- if kindIs "string" $containerPort -}}
  49. {{- $containerPort = (tpl $containerPort $rootCtx) -}}
  50. {{- end -}}
  51. {{- $tcpProtocols := (list "tcp" "http" "https") -}}
  52. {{- $protocol := tpl ($portValues.protocol | default $rootCtx.Values.fallbackDefaults.serviceProtocol) $rootCtx -}}
  53. {{- if mustHas $protocol $tcpProtocols -}}
  54. {{- $protocol = "tcp" -}}
  55. {{- end }}
  56. - name: {{ $portName }}
  57. containerPort: {{ $containerPort }}
  58. protocol: {{ $protocol | upper }}
  59. {{- with $portValues.hostPort }}
  60. hostPort: {{ . }}
  61. {{- else }}
  62. hostPort: null
  63. {{- end -}}
  64. {{- end -}}
  65. {{- end -}}
  66. {{- end -}}
  67. {{- end -}}
  68. {{- end -}}
  69. {{/* Turning hostNetwork on, it creates hostPort automatically and turning it back off does not remove them. Setting hostPort explicitly to null will remove them.
  70. There are still cases that hostPort is not removed, for example, if you have a TCP and UDP port with the same port number. Only the one of the hostPorts will
  71. be removed. This is due to how merging is happening, (See kubernetes/kubernetes#105610). Also note that setting hostPort to null always, it will NOT affect
  72. hostNetwork, as it will still create the hostPorts. It only helps to remove them when hostNetwork is turned off.
  73. */}}