_dns.tpl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. {{/* Returns DNS Policy and Config */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.pod.dns" (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.pod.dns" -}}
  8. {{- $rootCtx := .rootCtx -}}
  9. {{- $objectData := .objectData -}}
  10. {{- $policy := "ClusterFirst" -}}
  11. {{- $config := dict -}}
  12. {{/* Initialize from the "global" option */}}
  13. {{- with $rootCtx.Values.podOptions.dnsPolicy -}}
  14. {{- $policy = . -}}
  15. {{- end -}}
  16. {{- with $rootCtx.Values.podOptions.dnsConfig -}}
  17. {{- $config = . -}}
  18. {{- end -}}
  19. {{/* Override with pod's option */}}
  20. {{- with $objectData.podSpec.dnsPolicy -}}
  21. {{- $policy = . -}}
  22. {{- end -}}
  23. {{- with $objectData.podSpec.dnsConfig -}}
  24. {{- $config = . -}}
  25. {{- end -}}
  26. {{/* Expand policy */}}
  27. {{- $policy = (tpl $policy $rootCtx) -}}
  28. {{/* If hostNetwork is enabled, then use ClusterFirstWithHostNet */}}
  29. {{- $hostNet := include "ix.v1.common.lib.pod.hostNetwork" (dict "rootCtx" $rootCtx "objectData" $objectData) -}}
  30. {{- if or (and (kindIs "string" $hostNet) (eq $hostNet "true")) (and (kindIs "bool" $hostNet) $hostNet) -}}
  31. {{- $policy = "ClusterFirstWithHostNet" -}}
  32. {{- end -}}
  33. {{- $policies := (list "ClusterFirst" "ClusterFirstWithHostNet" "Default" "None") -}}
  34. {{- if not (mustHas $policy $policies) -}}
  35. {{- fail (printf "Expected <dnsPolicy> to be one of [%s], but got [%s]" (join ", " $policies) $policy) -}}
  36. {{- end -}}
  37. {{/* When policy is set to None all keys are required */}}
  38. {{- if eq $policy "None" -}}
  39. {{- range $key := (list "nameservers" "searches" "options") -}}
  40. {{- if not (get $config $key) -}}
  41. {{- fail (printf "Expected non-empty <dnsConfig.%s> with <dnsPolicy> set to [None]." $key) -}}
  42. {{- end -}}
  43. {{- end -}}
  44. {{- end }}
  45. dnsPolicy: {{ $policy }}
  46. {{- if or $config.nameservers $config.options $config.searches }}
  47. dnsConfig:
  48. {{- with $config.nameservers -}}
  49. {{- if gt (len .) 3 -}}
  50. {{- fail (printf "Expected no more than [3] <dnsConfig.nameservers>, but got [%v]" (len .)) -}}
  51. {{- end }}
  52. nameservers:
  53. {{- range . }}
  54. - {{ tpl . $rootCtx }}
  55. {{- end -}}
  56. {{- end -}}
  57. {{- with $config.searches -}}
  58. {{- if gt (len .) 6 -}}
  59. {{- fail (printf "Expected no more than [6] <dnsConfig.searches>, but got [%v]" (len .)) -}}
  60. {{- end }}
  61. searches:
  62. {{- range . }}
  63. - {{ tpl . $rootCtx }}
  64. {{- end -}}
  65. {{- end -}}
  66. {{- with $config.options }}
  67. options:
  68. {{- range . }}
  69. - name: {{ tpl .name $rootCtx }}
  70. {{- with .value }}
  71. value: {{ tpl . $rootCtx | quote }}
  72. {{- end -}}
  73. {{- end -}}
  74. {{- end -}}
  75. {{- end -}}
  76. {{- end -}}