Merge "[AAI] Service Mesh compatibility"
[oom.git] / kubernetes / common / common / templates / _service.tpl
index 0c7660e..a488e0d 100644 (file)
@@ -13,7 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 */}}
-
 {{/*
   Resolve the name of a chart's service.
 
   {{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
 {{- end -}}
 
+{{/*
+  Resolve the prefix node port to use. We look at these different values in
+  order of priority (first found, first chosen)
+  - .Values.service.nodePortPrefixOverride: override value for nodePort which
+                                            will be use locally;
+  - .Values.global.nodePortPrefix         : global value for nodePort which will
+                                            be used for all charts (unless
+                                            previous one is used);
+  - .Values.global.nodePortPrefixExt      : global value for nodePort which will
+                                            be used for all charts (unless
+                                            previous one is used) if
+                                            useNodePortExt is set to true in
+                                            service or on port;
+  - .Values.service.nodePortPrefix        : value used on a pert chart basis if
+                                            no other version exists.
+
+  The function takes two arguments (inside a dictionary):
+     - .dot : environment (.)
+     - .useNodePortExt : does the port use the "extended" nodeport part or the
+                         normal one?
+*/}}
+{{- define "common.nodePortPrefix" -}}
+{{-   $dot := default . .dot -}}
+{{-   $useNodePortExt := default false .useNodePortExt -}}
+{{-   if or $useNodePortExt $dot.Values.service.useNodePortExt -}}
+{{      $dot.Values.service.nodePortPrefixOverride | default $dot.Values.global.nodePortPrefixExt | default $dot.Values.nodePortPrefix }}
+{{-   else -}}
+{{      $dot.Values.service.nodePortPrefixOverride | default $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}
+{{-   end -}}
+{{- end -}}
+
 {{/* Define the metadata of Service
      The function takes from one to four arguments (inside a dictionary):
      - .dot : environment (.)
@@ -63,6 +93,7 @@ annotations:
         "version": "{{ default "v1" $msb_information.version }}",
         "url": "{{ default "/" $msb_information.url }}",
         "protocol": "{{ default "REST" $msb_information.protocol }}",
+        "enable_ssl": {{ default false $msb_information.enable_ssl }},
         "port": "{{ $msb_information.port }}",
         "visualRange":"{{ default "1" $msb_information.visualRange }}"
       }
@@ -90,17 +121,27 @@ labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent
 {{-     if (include "common.needTLS" $dot) }}
 - port: {{ $port.port }}
   targetPort: {{ $port.name }}
+{{-       if $port.l4_protocol }}
+  protocol: {{ $port.l4_protocol }}
+{{-       else }}
+  protocol: TCP
+{{-       end }}
 {{-       if $port.port_protocol }}
   name: {{ printf "%ss-%s" $port.port_protocol $port.name }}
 {{-       else }}
   name: {{ $port.name }}
 {{-       end }}
 {{-       if (eq $serviceType "NodePort") }}
-  nodePort: {{ $dot.Values.global.nodePortPrefix | default $dot.Values.nodePortPrefix }}{{ $port.nodePort }}
+  nodePort: {{ include "common.nodePortPrefix" (dict "dot" $dot "useNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
 {{-       end }}
 {{-     else }}
 - port: {{ default $port.port $port.plain_port }}
   targetPort: {{ $port.name }}
+{{-       if $port.plain_port_l4_protocol }}
+  protocol: {{ $port.plain_port_l4_protocol }}
+{{-       else }}
+  protocol: {{ default "TCP" $port.l4_protocol  }}
+{{-       end }}
 {{-       if $port.port_protocol }}
   name: {{ printf "%s-%s" $port.port_protocol $port.name }}
 {{-       else }}
@@ -111,6 +152,11 @@ labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent
 {{-       if (eq $serviceType "ClusterIP")  }}
 - port: {{ $port.plain_port }}
   targetPort: {{ $port.name }}-plain
+{{-       if $port.plain_l4_port_protocol }}
+  protocol: {{ $port.plain_port_l4_protocol }}
+{{-       else }}
+  protocol: {{ default "TCP" $port.l4_protocol  }}
+{{-       end }}
 {{-         if $port.port_protocol }}
   name: {{ printf "%s-%s" $port.port_protocol $port.name }}
 {{-         else }}
@@ -149,6 +195,8 @@ labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent
 {{- $labels := default (dict) .labels -}}
 {{- $matchLabels := default (dict) .matchLabels -}}
 {{- $sessionAffinity := default "None" $dot.Values.service.sessionAffinity -}}
+{{- $kubeTargetVersion := default $dot.Capabilities.KubeVersion.Version | trimPrefix "v" -}}
+{{- $ipFamilyPolicy := default "PreferDualStack" $dot.Values.service.ipFamilyPolicy -}}
 apiVersion: v1
 kind: Service
 metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
@@ -157,6 +205,9 @@ spec:
   clusterIP: None
   {{- end }}
   ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot "add_plain_port" $add_plain_port) | nindent 4 }}
+  {{- if semverCompare ">=1.20.0" $kubeTargetVersion }}
+  ipFamilyPolicy: {{ $ipFamilyPolicy }}
+  {{- end }}
   {{- if $publishNotReadyAddresses }}
   publishNotReadyAddresses: true
   {{- end }}
@@ -191,8 +242,8 @@ spec:
 {{-   $labels := default (dict) .labels -}}
 {{-   $matchLabels := default (dict) .matchLabels -}}
 
-{{-   if (and (include "common.needTLS" .) $both_tls_and_plain) }}
-{{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "add_plain_port" true $labels "matchLabels" $matchLabels) }}
+{{-   if (and (include "common.needTLS" $dot) $both_tls_and_plain) }}
+{{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "add_plain_port" true $labels "matchLabels" $matchLabels) }}
 {{-     if (ne $serviceType "ClusterIP") }}
 ---
 {{-       if $suffix }}
@@ -200,10 +251,10 @@ spec:
 {{-       else }}
 {{-         $suffix = "external" }}
 {{-       end }}
-{{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
+{{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
 {{-     end }}
 {{-   else }}
-{{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
+{{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
 {{-   end }}
 {{- end -}}
 
@@ -271,3 +322,43 @@ true
 {{-     end }}
 {{-   end }}
 {{- end -}}
+
+{{/*
+  generate needed scheme:
+    - https if needTLS
+    - http if not
+*/}}
+
+{{- define "common.scheme" -}}
+  {{- ternary "https" "http" (eq "true" (include "common.needTLS" .)) }}
+{{- end -}}
+
+{{- define "common.port.buildCache" -}}
+  {{- $global := . }}
+  {{- if not $global.Values._DmaapDrNodePortsCache }}
+    {{- $portCache := dict }}
+    {{- range $port := .Values.service.ports }}
+      {{- $_ := set $portCache $port.name (dict "port" $port.port "plain_port" $port.plain_port) }}
+    {{- end }}
+    {{- $_ := set $global.Values "_DmaapDrNodePortsCache" $portCache }}
+  {{- end }}
+{{- end -}}
+
+{/*
+  Get Port value according to its name and if we want tls or plain port.
+  The template takes below arguments:
+    - .global: environment (.)
+    - .name: name of the port
+    - .getPlain: boolean allowing to choose between tls (false, default) or
+                 plain (true)
+    If plain_port is not set and we ask for plain, it will return empty.
+*/}
+{{- define "common.getPort" -}}
+  {{- $global := .global }}
+  {{- $name := .name }}
+  {{- $getPlain := default false .getPlain }}
+  {{- include "common.port.buildCache" $global }}
+  {{- $portCache := $global.Values._DmaapDrNodePortsCache }}
+  {{- $port := index $portCache $name }}
+  {{- ternary $port.plain_port $port.port $getPlain }}
+{{- end -}}