diff --git a/library/common-test/tests/container/ports_test.yaml b/library/common-test/tests/container/ports_test.yaml new file mode 100644 index 0000000000..de85533977 --- /dev/null +++ b/library/common-test/tests/container/ports_test.yaml @@ -0,0 +1,271 @@ +suite: container ports test +templates: + - common.yaml +tests: + - it: should create the correct ports without selector + set: + some_port: 80 + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + workload-name2: + enabled: true + primary: false + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + service: + my-service1: + enabled: true + primary: true + ports: + port-name: + enabled: true + primary: true + port: "{{ .Values.some_port }}" + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + ports: + - name: port-name + containerPort: 80 + protocol: TCP + - documentIndex: &otherDeploymentDoc 1 + isKind: + of: Deployment + - documentIndex: *otherDeploymentDoc + isNull: + path: spec.template.spec.containers[0].ports + + - it: should create the correct ports with selector + set: + some_port: 53 + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + workload-name2: + enabled: true + primary: false + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + service: + my-service1: + enabled: true + primary: true + targetSelector: workload-name1 + ports: + port-name: + enabled: true + primary: true + targetSelector: container-name1 + port: 1234 + my-service2: + enabled: true + primary: false + targetSelector: workload-name2 + ports: + port-name: + enabled: true + primary: true + targetSelector: container-name1 + port: 54 + targetPort: "{{ .Values.some_port }}" + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + ports: + - name: port-name + containerPort: 1234 + protocol: TCP + - documentIndex: &otherDeploymentDoc 1 + isKind: + of: Deployment + - documentIndex: *otherDeploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + ports: + - name: port-name + containerPort: 53 + protocol: TCP + + - it: should create the correct ports with hostPort + set: + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + service: + my-service1: + enabled: true + primary: true + ports: + port-name: + enabled: true + primary: true + port: 1234 + targetPort: 5678 + hostPort: 20000 + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + ports: + - name: port-name + containerPort: 5678 + protocol: TCP + hostPort: 20000 + + - it: should create the correct protocol from tpl + set: + some_protocol: HTTPS + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + service: + my-service1: + enabled: true + primary: true + ports: + port-name: + enabled: true + primary: true + port: 1234 + protocol: "{{ .Values.some_protocol }}" + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + ports: + - name: port-name + containerPort: 1234 + protocol: TCP + + - it: should create the correct protocol + set: + image: &image + repository: nginx + tag: 1.19.0 + pullPolicy: IfNotPresent + workload: + workload-name1: + enabled: true + primary: true + type: Deployment + podSpec: + containers: + container-name1: + enabled: true + primary: true + imageSelector: image + service: + my-service1: + enabled: true + primary: true + ports: + port-name: + enabled: true + primary: true + port: 1234 + protocol: UDP + asserts: + - documentIndex: &deploymentDoc 0 + isKind: + of: Deployment + - documentIndex: *deploymentDoc + isAPIVersion: + of: apps/v1 + - documentIndex: *deploymentDoc + isSubset: + path: spec.template.spec.containers[0] + content: + ports: + - name: port-name + containerPort: 1234 + protocol: UDP diff --git a/library/common/1.0.0/docs/service/README.md b/library/common/1.0.0/docs/service/README.md index 77d2b1c98b..a82a52b823 100644 --- a/library/common/1.0.0/docs/service/README.md +++ b/library/common/1.0.0/docs/service/README.md @@ -15,7 +15,8 @@ | service.[service-name].ports.[port-name].port | `int` | ✅ | ✅ | | Define the port that will be exposed by the service | | service.[service-name].ports.[port-name].targetPort | `int` | ❌ | ✅ | `[port-name].port` | Define the target port (No named ports, as this will be used to assign the containerPort to containers) | | service.[service-name].ports.[port-name].protocol | `string` | ❌ | ✅ | `TCP` | Define the port protocol (HTTP, HTTPS, TCP, UDP). (Also used by the container ports and probes, HTTP and HTTPS are converted to TCP where needed) | -| service.[service-name].ports.[port-name].hostPort | `string` | ❌ | ❌ | | Define the hostPort, should be avoided, unless ABSOLUTELY necessary | +| service.[service-name].ports.[port-name].nodePort | `string` | ❌ | ✅ | | Define the node port | +| service.[service-name].ports.[port-name].hostPort | `string` | ❌ | ❌ | | Define the hostPort, should be **avoided**, unless **ABSOLUTELY** necessary | | service.[service-name].ports.[port-name].targetSelector | `string` | ❌ | ❌ | | Define the container to link this port (Must be on under the pod linked above) | > When `targetSelector`(s) is empty, it will define auto-select the primary pod/container diff --git a/library/common/1.0.0/templates/lib/container/_ports.tpl b/library/common/1.0.0/templates/lib/container/_ports.tpl new file mode 100644 index 0000000000..dcd8fdcb6a --- /dev/null +++ b/library/common/1.0.0/templates/lib/container/_ports.tpl @@ -0,0 +1,80 @@ +{{/* Returns ports list */}} +{{/* Call this template: +{{ include "ix.v1.common.lib.container.ports" (dict "rootCtx" $ "objectData" $objectData) }} +rootCtx: The root context of the template. It is used to access the global context. +objectData: The object data to be used to render the container. +*/}} +{{- define "ix.v1.common.lib.container.ports" -}} + {{- $rootCtx := .rootCtx -}} + {{- $objectData := .objectData -}} + + {{- range $serviceName, $serviceValues := $rootCtx.Values.service -}} + {{- $podSelected := false -}} + {{/* If service is enabled... */}} + {{- if $serviceValues.enabled -}} + + {{/* If there is a selector */}} + {{- if $serviceValues.targetSelector -}} + + {{/* And pod is selected */}} + {{- if eq $serviceValues.targetSelector $objectData.podShortName -}} + {{- $podSelected = true -}} + {{- end -}} + + {{- else -}} + {{/* If no selector is defined but pod is primary */}} + {{- if $objectData.podPrimary -}} + {{- $podSelected = true -}} + {{- end -}} + + {{- end -}} + {{- end -}} + + {{- if $podSelected -}} + {{- range $portName, $portValues := $serviceValues.ports -}} + {{- $containerSelected := false -}} + + {{/* If service is enabled... */}} + {{- if $portValues.enabled -}} + {{/* If there is a selector */}} + {{- if $portValues.targetSelector -}} + + {{/* And container is selected */}} + {{- if eq $portValues.targetSelector $objectData.shortName -}} + {{- $containerSelected = true -}} + {{- end -}} + + {{- else -}} + {{/* If no selector is defined but contaienr is primary */}} + {{- if $objectData.primary -}} + {{- $containerSelected = true -}} + {{- end -}} + + {{- end -}} + {{- end -}} + + {{/* If the container is selected render port */}} + {{- if $containerSelected -}} + {{- $containerPort := $portValues.targetPort | default $portValues.port -}} + {{- if kindIs "string" $containerPort -}} + {{- $containerPort = (tpl $containerPort $rootCtx) -}} + {{- end -}} + + {{- $tcpProtocols := (list "TCP" "HTTP" "HTTPS") -}} + {{- $protocol := tpl ($portValues.protocol | default $rootCtx.Values.fallbackDefaults.serviceProtocol) $rootCtx -}} + {{- if mustHas $protocol $tcpProtocols -}} + {{- $protocol = "TCP" -}} + {{- end }} +- name: {{ $portName }} + containerPort: {{ $containerPort }} + protocol: {{ $protocol }} + {{- with $portValues.hostPort }} + hostPort: {{ . }} + {{- end -}} + {{- end -}} + + {{- end -}} + {{- end -}} + {{- end -}} + +{{- end -}} diff --git a/library/common/1.0.0/templates/lib/pod/_container.tpl b/library/common/1.0.0/templates/lib/pod/_container.tpl index ee64f37ec4..117750cd73 100644 --- a/library/common/1.0.0/templates/lib/pod/_container.tpl +++ b/library/common/1.0.0/templates/lib/pod/_container.tpl @@ -33,15 +33,21 @@ objectData: The object data to be used to render the Pod. lifecycle: {{- . | nindent 4 }} {{- end -}} + {{- with (include "ix.v1.common.lib.container.ports" (dict "rootCtx" $rootCtx "objectData" $objectData) | trim) }} + ports: + {{- . | nindent 4 }} + {{- end -}} {{- end -}} + {{/* TODO: -securityContext -resources -probes + env envList fixedEnv envFrom -ports + +securityContext +resources +probes volumeMounts */}} diff --git a/library/common/1.0.0/values.yaml b/library/common/1.0.0/values.yaml index 59b5e3079e..5c7a61126e 100644 --- a/library/common/1.0.0/values.yaml +++ b/library/common/1.0.0/values.yaml @@ -248,7 +248,7 @@ service: # -- Node port nodePort: # -- Container to assign the port - targetSelector: container-name + # targetSelector: container-name # -- VolumeClaimTemplates (StatefulSet only) volumeClaimTemplates: @@ -474,21 +474,7 @@ workload: add: [] drop: - ALL - lifecycle: - postStart: - type: http - port: 123 - path: /asdf - httpHeaders: - key: value - key2: value2 - host: asdf - scheme: HTTPS - preStop: - type: exec - command: - - asdf - - asdf + lifecycle: {} termination: messagePath: "" messagePolicy: ""