_validation.tpl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. {{/* Service Validation */}}
  2. {{/* Call this template:
  3. {{ include "ix.v1.common.lib.service.validation" (dict "objectData" $objectData) -}}
  4. objectData:
  5. rootCtx: The root context of the chart.
  6. objectData: The service object.
  7. */}}
  8. {{- define "ix.v1.common.lib.service.validation" -}}
  9. {{- $rootCtx := .rootCtx -}}
  10. {{- $objectData := .objectData -}}
  11. {{- if and $objectData.targetSelector (not (kindIs "string" $objectData.targetSelector)) -}}
  12. {{- fail (printf "Service - Expected <targetSelector> to be [string], but got [%s]" (kindOf $objectData.targetSelector)) -}}
  13. {{- end -}}
  14. {{- $svcTypes := (list "ClusterIP" "NodePort") -}}
  15. {{- if and $objectData.type (not (mustHas $objectData.type $svcTypes)) -}}
  16. {{- fail (printf "Service - Expected <type> to be one of [%s] but got [%s]" (join ", " $svcTypes) $objectData.type) -}}
  17. {{- end -}}
  18. {{- $hasEnabledPort := false -}}
  19. {{- range $name, $port := $objectData.ports -}}
  20. {{- if $port.enabled -}}
  21. {{- $hasEnabledPort = true -}}
  22. {{- if and $port.targetSelector (not (kindIs "string" $port.targetSelector)) -}}
  23. {{- fail (printf "Service - Expected <port.targetSelector> to be [string], but got [%s]" (kindOf $port.targetSelector)) -}}
  24. {{- end -}}
  25. {{- if not $port.port -}}
  26. {{- fail (printf "Service - Expected non-empty <port.port>") -}}
  27. {{- end -}}
  28. {{- $protocolTypes := (list "tcp" "udp" "http" "https") -}}
  29. {{- if $port.protocol -}}
  30. {{- if not (mustHas (tpl $port.protocol $rootCtx) $protocolTypes) -}}
  31. {{- fail (printf "Service - Expected <port.protocol> to be one of [%s] but got [%s]" (join ", " $protocolTypes) $port.protocol) -}}
  32. {{- end -}}
  33. {{- end -}}
  34. {{- end -}}
  35. {{- end -}}
  36. {{- if not $hasEnabledPort -}}
  37. {{- fail "Service - Expected enabled service to have at least one port" -}}
  38. {{- end -}}
  39. {{- end -}}
  40. {{/* Service Primary Validation */}}
  41. {{/* Call this template:
  42. {{ include "ix.v1.common.lib.service.primaryValidation" $ -}}
  43. */}}
  44. {{- define "ix.v1.common.lib.service.primaryValidation" -}}
  45. {{/* Initialize values */}}
  46. {{- $hasPrimary := false -}}
  47. {{- $hasEnabled := false -}}
  48. {{- range $name, $service := .Values.service -}}
  49. {{/* If service is enabled */}}
  50. {{- if $service.enabled -}}
  51. {{- $hasEnabled = true -}}
  52. {{/* And service is primary */}}
  53. {{- if and (hasKey $service "primary") ($service.primary) -}}
  54. {{/* Fail if there is already a primary service */}}
  55. {{- if $hasPrimary -}}
  56. {{- fail "Service - Only one service can be primary" -}}
  57. {{- end -}}
  58. {{- $hasPrimary = true -}}
  59. {{- include "ix.v1.common.lib.servicePort.primaryValidation" (dict "objectData" $service.ports) -}}
  60. {{- end -}}
  61. {{- end -}}
  62. {{- end -}}
  63. {{/* Require at least one primary service, if any enabled */}}
  64. {{- if and $hasEnabled (not $hasPrimary) -}}
  65. {{- fail "Service - At least one enabled service must be primary" -}}
  66. {{- end -}}
  67. {{- end -}}
  68. {{/* Service Port Primary Validation */}}
  69. {{/* Call this template:
  70. {{ include "ix.v1.common.lib.service.primaryValidation" (dict "objectData" $objectData -}}
  71. objectData:
  72. The ports of the service.
  73. */}}
  74. {{- define "ix.v1.common.lib.servicePort.primaryValidation" -}}
  75. {{- $objectData := .objectData -}}
  76. {{/* Initialize values */}}
  77. {{- $hasPrimary := false -}}
  78. {{- $hasEnabled := false -}}
  79. {{- range $name, $port := $objectData -}}
  80. {{/* If service is enabled */}}
  81. {{- if $port.enabled -}}
  82. {{- $hasEnabled = true -}}
  83. {{/* And service is primary */}}
  84. {{- if and (hasKey $port "primary") ($port.primary) -}}
  85. {{/* Fail if there is already a primary port */}}
  86. {{- if $hasPrimary -}}
  87. {{- fail "Service - Only one port per service can be primary" -}}
  88. {{- end -}}
  89. {{- $hasPrimary = true -}}
  90. {{- end -}}
  91. {{- end -}}
  92. {{- end -}}
  93. {{/* Require at least one primary service, if any enabled */}}
  94. {{- if and $hasEnabled (not $hasPrimary) -}}
  95. {{- fail "Service - At least one enabled port in service must be primary" -}}
  96. {{- end -}}
  97. {{- end -}}