_service.tpl 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. {{/* Service Class */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.class.service" (dict "rootCtx" $ "objectData" $objectData) }}
  4. rootCtx: The root context of the chart.
  5. objectData: The service data, that will be used to render the Service object.
  6. */}}
  7. {{- define "ix.v1.common.class.service" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{- $svcType := $objectData.type | default $rootCtx.Values.fallbackDefaults.serviceType -}}
  11. {{/* Init variables */}}
  12. {{- $hasHostPort := false -}}
  13. {{- $hostNetwork := false -}}
  14. {{- $podValues := dict -}}
  15. {{/* Get Pod Values based on the selector (or the absence of it) */}}
  16. {{- $podValues = fromJson (include "ix.v1.common.lib.helpers.getSelectedPodValues" (dict "rootCtx" $rootCtx "objectData" $objectData "caller" "Service")) -}}
  17. {{- if $podValues -}}
  18. {{/* Get Pod hostNetwork configuration */}}
  19. {{- $hostNetwork = include "ix.v1.common.lib.pod.hostNetwork" (dict "rootCtx" $rootCtx "objectData" $podValues) -}}
  20. {{/* When hostNetwork is set on the pod, force ClusterIP, so services wont try to bind the same ports on the host */}}
  21. {{- if or (and (kindIs "bool" $hostNetwork) $hostNetwork) (and (kindIs "string" $hostNetwork) (eq $hostNetwork "true")) -}}
  22. {{- $svcType = "ClusterIP" -}}
  23. {{- end -}}
  24. {{- end -}}
  25. {{- range $portName, $port := $objectData.ports -}}
  26. {{- if $port.enabled -}}
  27. {{- if and (hasKey $port "hostPort") $port.hostPort -}}
  28. {{- $hasHostPort = true -}}
  29. {{- end -}}
  30. {{- end -}}
  31. {{- end -}}
  32. {{/* When hostPort is defined, force ClusterIP aswell */}}
  33. {{- if $hasHostPort -}}
  34. {{- $svcType = "ClusterIP" -}}
  35. {{- end -}}
  36. {{- $_ := set $objectData "type" $svcType }}
  37. ---
  38. apiVersion: v1
  39. kind: Service
  40. metadata:
  41. name: {{ $objectData.name }}
  42. {{- $labels := (mustMerge ($objectData.labels | default dict) (include "ix.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)
  43. (include "ix.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "objectType" "service" "objectName" $objectData.shortName) | fromYaml)) -}}
  44. {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }}
  45. labels:
  46. {{- . | nindent 4 }}
  47. {{- end -}}
  48. {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "ix.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}}
  49. {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }}
  50. annotations:
  51. {{- . | nindent 4 }}
  52. {{- end }}
  53. spec:
  54. {{- if eq $objectData.type "ClusterIP" -}}
  55. {{- include "ix.v1.common.lib.service.spec.clusterIP" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 -}}
  56. {{- else if eq $objectData.type "NodePort" -}}
  57. {{- include "ix.v1.common.lib.service.spec.nodePort" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 -}}
  58. {{- end -}}
  59. {{- with (include "ix.v1.common.lib.service.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }}
  60. ports:
  61. {{- . | nindent 4 }}
  62. {{- end }}
  63. selector:
  64. {{- include "ix.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "objectType" "pod" "objectName" $podValues.shortName) | trim | nindent 4 -}}
  65. {{- end -}}