_service.tpl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. {{- end -}}
  21. {{- range $portName, $port := $objectData.ports -}}
  22. {{- if $port.enabled -}}
  23. {{- if and (hasKey $port "hostPort") $port.hostPort -}}
  24. {{- $hasHostPort = true -}}
  25. {{- end -}}
  26. {{- end -}}
  27. {{- end -}}
  28. {{/* When hostNetwork is set on the pod, force ClusterIP, so services wont try to bind the same ports on the host */}}
  29. {{- if or (and (kindIs "bool" $hostNetwork) $hostNetwork) (and (kindIs "string" $hostNetwork) (eq $hostNetwork "true")) -}}
  30. {{- $svcType = "ClusterIP" -}}
  31. {{- end -}}
  32. {{/* When hostPort is defined, force ClusterIP aswell */}}
  33. {{- if $hasHostPort -}}
  34. {{- $svcType = "ClusterIP" -}}
  35. {{- end }}
  36. ---
  37. apiVersion: v1
  38. kind: Service
  39. metadata:
  40. name: {{ $objectData.name }}
  41. {{- $labels := (mustMerge ($objectData.labels | default dict) (include "ix.v1.common.lib.metadata.allLabels" $rootCtx | fromYaml)
  42. (include "ix.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "objectType" "service" "objectName" $objectData.name) | fromYaml)) -}}
  43. {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "labels" $labels) | trim) }}
  44. labels:
  45. {{- . | nindent 4 }}
  46. {{- end -}}
  47. {{- $annotations := (mustMerge ($objectData.annotations | default dict) (include "ix.v1.common.lib.metadata.allAnnotations" $rootCtx | fromYaml)) -}}
  48. {{- with (include "ix.v1.common.lib.metadata.render" (dict "rootCtx" $rootCtx "annotations" $annotations) | trim) }}
  49. annotations:
  50. {{- . | nindent 4 }}
  51. {{- end }}
  52. spec:
  53. {{- if eq $svcType "ClusterIP" -}}
  54. {{- include "ix.v1.common.lib.service.spec.clusterIP" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 -}}
  55. {{- else if eq $svcType "NodePort" -}}
  56. {{- include "ix.v1.common.lib.service.spec.nodePort" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 -}}
  57. {{- end -}}
  58. {{- with (include "ix.v1.common.lib.service.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }}
  59. ports:
  60. {{- . | nindent 4 }}
  61. {{- end }}
  62. selector:
  63. {{- include "ix.v1.common.lib.metadata.selectorLabels" (dict "rootCtx" $rootCtx "objectType" "pod" "objectName" $podValues.shortName) | trim | nindent 4 -}}
  64. {{- end -}}