diff --git a/library/common-test/tests/service/cluster_ip_test.yaml b/library/common-test/tests/service/cluster_ip_test.yaml index 1071199016..3700e13e38 100644 --- a/library/common-test/tests/service/cluster_ip_test.yaml +++ b/library/common-test/tests/service/cluster_ip_test.yaml @@ -2,15 +2,13 @@ suite: service clusterIP test templates: - common.yaml tests: - - it: should pass with type ClusterIP and custom cluster ip + - it: should pass with type ClusterIP set: - some_ip: 172.16.20.35 service: my-service: enabled: true primary: true type: ClusterIP - clusterIP: "{{ .Values.some_ip }}" ports: port-name: enabled: true @@ -37,17 +35,18 @@ tests: path: spec value: type: ClusterIP - clusterIP: 172.16.20.35 - - it: should pass with type ClusterIP and ipFamilyPolicy set + - it: should pass with type ClusterIP and available options set set: some_policy: PreferDualStack some_family: IPv6 + some_ip: 172.16.20.35 service: my-service: enabled: true primary: true type: ClusterIP + clusterIP: "{{ .Values.some_ip }}" ipFamilyPolicy: "{{ .Values.some_policy }}" ipFamilies: - IPv4 @@ -68,6 +67,7 @@ tests: path: spec value: type: ClusterIP + clusterIP: 172.16.20.35 ipFamilyPolicy: PreferDualStack ipFamilies: - IPv4 diff --git a/library/common-test/tests/service/load_balancer_test.yaml b/library/common-test/tests/service/load_balancer_test.yaml new file mode 100644 index 0000000000..b0484491c0 --- /dev/null +++ b/library/common-test/tests/service/load_balancer_test.yaml @@ -0,0 +1,84 @@ +suite: service loadBalancer test +templates: + - common.yaml +tests: + - it: should pass with type LoadBalancer + set: + service: + my-service: + enabled: true + primary: true + type: LoadBalancer + ports: + port-name: + enabled: true + primary: true + workload: + my-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: &serviceDoc 1 + isKind: + of: Service + - documentIndex: *serviceDoc + isAPIVersion: + of: v1 + - documentIndex: *serviceDoc + equal: + path: metadata.name + value: release-name-common-test + - documentIndex: *serviceDoc + equal: + path: spec + value: + type: LoadBalancer + + - it: should pass with type LoadBalancer and available options set + set: + some_ip: 172.16.20.35 + some_other_ip: 10.100.200.45 + some_range: 11.100.200.0/24 + service: + my-service: + enabled: true + primary: true + type: LoadBalancer + clusterIP: "{{ .Values.some_ip }}" + loadBalancerIP: "{{ .Values.some_other_ip }}" + loadBalancerSourceRanges: + - "{{ .Values.some_range }}" + - 10.100.200.0/24 + externalTrafficPolicy: Local + ipFamilyPolicy: PreferDualStack + ipFamilies: + - IPv4 + - IPv6 + ports: + port-name: + enabled: true + primary: true + workload: + my-workload: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - documentIndex: *serviceDoc + equal: + path: spec + value: + type: LoadBalancer + clusterIP: 172.16.20.35 + loadBalancerIP: 10.100.200.45 + loadBalancerSourceRanges: + - 11.100.200.0/24 + - 10.100.200.0/24 + externalTrafficPolicy: Local + ipFamilyPolicy: PreferDualStack + ipFamilies: + - IPv4 + - IPv6 diff --git a/library/common-test/tests/service/validation_test.yaml b/library/common-test/tests/service/validation_test.yaml index cf4d561630..cebc4f15e9 100644 --- a/library/common-test/tests/service/validation_test.yaml +++ b/library/common-test/tests/service/validation_test.yaml @@ -271,3 +271,25 @@ tests: asserts: - failedTemplate: errorMessage: Service - Expected to be one of [IPv4, IPv6], but got [not-a-family] + + - it: should fail with invalid externalTrafficPolicy + set: + service: + service-name1: + enabled: true + primary: true + type: LoadBalancer + externalTrafficPolicy: not-a-policy + ports: + port-name1: + enabled: true + primary: true + workload: + some-pod-name: + enabled: true + primary: true + type: Deployment + podSpec: {} + asserts: + - failedTemplate: + errorMessage: Service - Expected to be one of [Cluster, Local], but got [not-a-policy] diff --git a/library/common/1.0.0/docs/service.md b/library/common/1.0.0/docs/service.md index 5e4aec2378..994bb7d489 100644 --- a/library/common/1.0.0/docs/service.md +++ b/library/common/1.0.0/docs/service.md @@ -1,20 +1,24 @@ # Service -| Key | Type | Required | Helm Template | Default | Description | -| :------------------------------------------- | :-------: | :------: | :----------------: | :---------: | :------------------------------------------------------------------------------------ | -| service | `dict` | ❌ | ❌ | `{}` | Define the service as dicts | -| service.[service-name] | `dict` | ✅ | ❌ | `{}` | Holds service definition | -| service.[service-name].enabled | `boolean` | ✅ | ❌ | `false` | Enables or Disables the service | -| service.[service-name].labels | `dict` | ❌ | ✅ (On value only) | `{}` | Additional labels for service | -| service.[service-name].annotations | `dict` | ❌ | ✅ (On value only) | `{}` | Additional annotations for service | -| service.[service-name].type | `string` | ❌ | ✅ | `ClusterIP` | Define the service type (ClusterIP, LoadBalancer, NodePort, ExternalIP, ExternalName) | -| service.[service-name].sharedKey | `string` | ❌ | ✅ | `$FullName` | Custom Shared Key for MetalLB Annotation | -| service.[service-name].clusterIP | `string` | ❌ | ✅ | | Custom Cluster IP | -| service.[service-name].ipFamilyPolicy | `string` | ❌ | ✅ | | Define the ipFamilyPolicy (SingleStack, PreferDualStack, RequireDualStack) | -| service.[service-name].ipFamilies | `list` | ❌ | ❌ | | Define the ipFamilies | -| service.[service-name].ipFamilies.[ipFamily] | `string` | ❌ | ✅ | | Define the ipFamily (IPv4, IPv6) | -| service.[service-name].targetSelector | `string` | ❌ | ✅ | `""` | Define the pod to link the service, by default will use the primary pod | -| service.[service-name].ports | `list` | ✅ | ❌ | `{}` | Define the ports of the service | +| Key | Type | Required | Helm Template | Default | Description | +| :------------------------------------------------------------- | :-------: | :------: | :----------------: | :---------: | :------------------------------------------------------------------------------------ | +| service | `dict` | ❌ | ❌ | `{}` | Define the service as dicts | +| service.[service-name] | `dict` | ✅ | ❌ | `{}` | Holds service definition | +| service.[service-name].enabled | `boolean` | ✅ | ❌ | `false` | Enables or Disables the service | +| service.[service-name].labels | `dict` | ❌ | ✅ (On value only) | `{}` | Additional labels for service | +| service.[service-name].annotations | `dict` | ❌ | ✅ (On value only) | `{}` | Additional annotations for service | +| service.[service-name].type | `string` | ❌ | ✅ | `ClusterIP` | Define the service type (ClusterIP, LoadBalancer, NodePort, ExternalIP, ExternalName) | +| service.[service-name].sharedKey | `string` | ❌ | ✅ | `$FullName` | Custom Shared Key for MetalLB Annotation | +| service.[service-name].clusterIP | `string` | ❌ | ✅ | | Custom Cluster IP | +| service.[service-name].ipFamilyPolicy | `string` | ❌ | ✅ | | Define the ipFamilyPolicy (SingleStack, PreferDualStack, RequireDualStack) | +| service.[service-name].ipFamilies | `list` | ❌ | ❌ | | Define the ipFamilies | +| service.[service-name].ipFamilies.[ipFamily] | `string` | ✅ | ✅ | | Define the ipFamily (IPv4, IPv6) | +| service.[service-name].loadBalancerIP | `string` | ❌ | ✅ | | Define the load balancer IP | +| service.[service-name].loadBalancerSourceRanges | `list` | ❌ | ❌ | | Define the load balancer source ranges | +| service.[service-name].loadBalancerSourceRanges.[source-range] | `string` | ✅ | ✅ | | Define the load balancer source range | +| service.[service-name].externalTrafficPolicy | `string` | ❌ | ✅ | | Define the external traffic policy (Cluster, Local) | +| service.[service-name].targetSelector | `string` | ❌ | ✅ | `""` | Define the pod to link the service, by default will use the primary pod | +| service.[service-name].ports | `list` | ✅ | ❌ | `{}` | Define the ports of the service | --- @@ -35,12 +39,34 @@ Examples: ```yaml service: - service-name: + service-clusterip: enabled: true primary: true - type: ClusterIP + clusterIP: 172.16.20.233 + ipFamilyPolicy: SingleStack + ipFamilies: + - IPv4 targetSelector: pod-name + ports: + port-name: + enabled: true + primary: true + container-name: container-name + + service-lb: + enabled: true + primary: true + type: LoadBalancer + loadBalancerIP: 10.100.100.2 + loadBalancerSourceRanges: + - 10.100.100.0/24 + clusterIP: 172.16.20.233 sharedKey: custom-shared-key + ipFamilyPolicy: SingleStack + ipFamilies: + - IPv4 + externalTrafficPolicy: Cluster + targetSelector: pod-name ports: port-name: enabled: true diff --git a/library/common/1.0.0/templates/classes/_service.tpl b/library/common/1.0.0/templates/classes/_service.tpl index b98be81608..958759e13d 100644 --- a/library/common/1.0.0/templates/classes/_service.tpl +++ b/library/common/1.0.0/templates/classes/_service.tpl @@ -69,7 +69,9 @@ metadata: {{- end }} spec: {{- if eq $svcType "ClusterIP" -}} - {{- include "ix.v1.common.lib.service.clusterIP" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 }} + {{- include "ix.v1.common.lib.service.spec.clusterIP" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 }} + {{- else if eq $svcType "LoadBalancer" -}} + {{- include "ix.v1.common.lib.service.spec.loadBalancer" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 2 }} {{- end }} {{- if not (mustHas $svcType (list "ExternalName" "ExternalIP")) }} selector: diff --git a/library/common/1.0.0/templates/lib/service/serviceTypes/_clusterIP.tpl b/library/common/1.0.0/templates/lib/service/serviceTypeConfig/_cluster_ip.tpl similarity index 71% rename from library/common/1.0.0/templates/lib/service/serviceTypes/_clusterIP.tpl rename to library/common/1.0.0/templates/lib/service/serviceTypeConfig/_cluster_ip.tpl index 297900810f..f4bf7bde63 100644 --- a/library/common/1.0.0/templates/lib/service/serviceTypes/_clusterIP.tpl +++ b/library/common/1.0.0/templates/lib/service/serviceTypeConfig/_cluster_ip.tpl @@ -1,4 +1,4 @@ -{{/* Service - ClusterIP */}} +{{/* Service - clusterIP */}} {{/* Call this template: {{ include "ix.v1.common.lib.service.clusterIP" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} rootCtx: The root context of the service @@ -9,9 +9,8 @@ objectData: The service object data {{- $rootCtx := .rootCtx -}} {{- $objectData := .objectData }} -type: ClusterIP {{- with $objectData.clusterIP }} clusterIP: {{ tpl . $rootCtx }} {{- end -}} - {{- include "ix.v1.common.lib.service.ipFamily" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/serviceTypeConfig/_externalTrafficPolicy.tpl b/library/common/1.0.0/templates/lib/service/serviceTypeConfig/_externalTrafficPolicy.tpl new file mode 100644 index 0000000000..577503cdc4 --- /dev/null +++ b/library/common/1.0.0/templates/lib/service/serviceTypeConfig/_externalTrafficPolicy.tpl @@ -0,0 +1,22 @@ +{{/* Service - externalTrafficPolicy */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.service.externalTrafficPolicy" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} +rootCtx: The root context of the service +objectData: The service object data +*/}} + +{{- define "ix.v1.common.lib.service.externalTrafficPolicy" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData }} + + {{- with $objectData.externalTrafficPolicy }} + {{- $policy := tpl . $rootCtx -}} + {{- $policies := (list "Cluster" "Local") -}} + + {{- if not (mustHas $policy $policies) -}} + {{- fail (printf "Service - Expected to be one of [%s], but got [%s]" (join ", " $policies) $policy) -}} + {{- end }} +externalTrafficPolicy: {{ $policy }} + {{- end -}} + +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/_ipFamily.tpl b/library/common/1.0.0/templates/lib/service/serviceTypeConfig/_ipFamily.tpl similarity index 100% rename from library/common/1.0.0/templates/lib/service/_ipFamily.tpl rename to library/common/1.0.0/templates/lib/service/serviceTypeConfig/_ipFamily.tpl diff --git a/library/common/1.0.0/templates/lib/service/serviceTypeSpecs/_clusterIP.tpl b/library/common/1.0.0/templates/lib/service/serviceTypeSpecs/_clusterIP.tpl new file mode 100644 index 0000000000..8805939711 --- /dev/null +++ b/library/common/1.0.0/templates/lib/service/serviceTypeSpecs/_clusterIP.tpl @@ -0,0 +1,16 @@ +{{/* Service - ClusterIP Spec */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.service.spec.clusterIP" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} +rootCtx: The root context of the service +objectData: The service object data +*/}} + +{{- define "ix.v1.common.lib.service.spec.clusterIP" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData }} + +type: ClusterIP + + {{- include "ix.v1.common.lib.service.clusterIP" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- include "ix.v1.common.lib.service.ipFamily" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/service/serviceTypeSpecs/_loadBalancer.tpl b/library/common/1.0.0/templates/lib/service/serviceTypeSpecs/_loadBalancer.tpl new file mode 100644 index 0000000000..294c27e7b6 --- /dev/null +++ b/library/common/1.0.0/templates/lib/service/serviceTypeSpecs/_loadBalancer.tpl @@ -0,0 +1,27 @@ +{{/* Service - LoadBalancer Spec */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.service.spec.loadBalancer" (dict "rootCtx" $rootCtx "objectData" $objectData) -}} +rootCtx: The root context of the service +objectData: The service object data +*/}} + +{{- define "ix.v1.common.lib.service.spec.loadBalancer" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData }} + +type: LoadBalancer + {{- with $objectData.loadBalancerIP }} +loadBalancerIP: {{ tpl . $rootCtx }} + {{- end -}} + + {{- with $objectData.loadBalancerSourceRanges }} +loadBalancerSourceRanges: + {{- range . }} + - {{ tpl . $rootCtx }} + {{- end -}} + {{- end -}} + + {{- include "ix.v1.common.lib.service.clusterIP" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- include "ix.v1.common.lib.service.ipFamily" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} + {{- include "ix.v1.common.lib.service.externalTrafficPolicy" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim | nindent 0 }} +{{- end -}}