Merge "[CONSUL] Add limits to consul chart."
[oom.git] / kubernetes / common / common / templates / _service.tpl
index cd1595b..dddd634 100644 (file)
   {{- 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 three arguments (inside a dictionary):
+     The function takes from one to four arguments (inside a dictionary):
      - .dot : environment (.)
      - .suffix : a string which will be added at the end of the name (with a '-').
      - .annotations: the annotations to add
      - .msb_informations: msb information in order to create msb annotation
+     - .labels : labels to add
      Usage example:
       {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
       {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
@@ -45,6 +77,7 @@
   {{- $suffix := default "" .suffix -}}
   {{- $annotations := default "" .annotations -}}
   {{- $msb_informations := default "" .msb_informations -}}
+  {{- $labels := default (dict) .labels -}}
 {{- if or $annotations $msb_informations -}}
 annotations:
 {{-   if $annotations }}
@@ -52,20 +85,26 @@ annotations:
 {{-   end }}
 {{-   if $msb_informations }}
   msb.onap.org/service-info: '[
+{{-     range $index, $msb_information := $msb_informations }}
+{{-       if ne $index 0 }}
+      ,
+{{-       end }}
       {
-          "serviceName": "{{ include "common.servicename" $dot }}",
-          "version": "{{ default "v1" $msb_informations.version }}",
-          "url": "{{ default "/" $msb_informations.url }}",
-          "protocol": "{{ default "REST" $msb_informations.protocol }}",
-          "port": "{{ $msb_informations.port }}",
-          "visualRange":"{{ default "1" $msb_informations.visualRange }}"
+        "serviceName": "{{ default (include "common.servicename" $dot) $msb_information.serviceName  }}",
+        "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 }}"
       }
-      ]'
+{{-    end }}
+    ]'
 {{-   end}}
 {{- end }}
 name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
 namespace: {{ include "common.namespace" $dot }}
-labels: {{- include "common.labels" $dot | nindent 2 -}}
+labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 2 -}}
 {{- end -}}
 
 {{/* Define the ports of Service
@@ -89,7 +128,7 @@ labels: {{- include "common.labels" $dot | nindent 2 -}}
   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 "portNodePortExt" $port.useNodePortExt) }}{{ $port.nodePort }}
 {{-       end }}
 {{-     else }}
 - port: {{ default $port.port $port.plain_port }}
@@ -125,6 +164,9 @@ labels: {{- include "common.labels" $dot | nindent 2 -}}
      - .publishNotReadyAddresses: if we publish not ready address
      - .headless: if the service is headless
      - .add_plain_port: add tls port AND plain port
+     - .labels : labels to add (dict)
+     - .matchLabels: selectors/machLabels to add (dict)
+     - .sessionAffinity: ClientIP  - enables sticky sessions based on client IP, default: None
 */}}
 {{- define "common.genericService" -}}
 {{- $dot := default . .dot -}}
@@ -136,9 +178,12 @@ labels: {{- include "common.labels" $dot | nindent 2 -}}
 {{- $ports := .ports -}}
 {{- $headless := default false .headless -}}
 {{- $add_plain_port := default false .add_plain_port }}
+{{- $labels := default (dict) .labels -}}
+{{- $matchLabels := default (dict) .matchLabels -}}
+{{- $sessionAffinity := default "None" $dot.Values.service.sessionAffinity -}}
 apiVersion: v1
 kind: Service
-metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "dot" $dot) | nindent 2 }}
+metadata: {{ include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "msb_informations" $msb_informations "labels" $labels "dot" $dot) | nindent 2 }}
 spec:
   {{- if $headless }}
   clusterIP: None
@@ -148,7 +193,8 @@ spec:
   publishNotReadyAddresses: true
   {{- end }}
   type: {{ $serviceType }}
-  selector: {{- include "common.matchLabels" $dot | nindent 4 }}
+  selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
+  sessionAffinity: {{ $sessionAffinity }}
 {{- end -}}
 
 {{/*
@@ -166,15 +212,19 @@ spec:
     ports and the other one is NodePort (or LoadBalancer) with crypted port only.
 */}}
 {{- define "common.service" -}}
-{{-   $suffix := default "" .Values.service.suffix -}}
-{{-   $annotations := default "" .Values.service.annotations -}}
-{{-   $publishNotReadyAddresses := default false .Values.service.publishNotReadyAddresses -}}
-{{-   $msb_informations := default "" .Values.service.msb -}}
-{{-   $serviceType := .Values.service.type -}}
-{{-   $ports := .Values.service.ports -}}
-{{-   $both_tls_and_plain:= default false .Values.service.both_tls_and_plain }}
-{{-   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) }}
+{{-   $dot := default . .dot -}}
+{{-   $suffix := default "" $dot.Values.service.suffix -}}
+{{-   $annotations := default "" $dot.Values.service.annotations -}}
+{{-   $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
+{{-   $msb_informations := default "" $dot.Values.service.msb -}}
+{{-   $serviceType := $dot.Values.service.type -}}
+{{-   $ports := $dot.Values.service.ports -}}
+{{-   $both_tls_and_plain:= default false $dot.Values.service.both_tls_and_plain }}
+{{-   $labels := default (dict) .labels -}}
+{{-   $matchLabels := default (dict) .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 }}
@@ -182,20 +232,23 @@ spec:
 {{-       else }}
 {{-         $suffix = "external" }}
 {{-       end }}
-{{        include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType) }}
+{{        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) }}
+{{      include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" $serviceType $labels "matchLabels" $matchLabels) }}
 {{-   end }}
 {{- end -}}
 
 {{/* Create headless service template */}}
 {{- define "common.headlessService" -}}
-{{- $suffix := include "common._makeHeadlessSuffix" . -}}
-{{- $annotations := default "" .Values.service.headless.annotations -}}
-{{- $publishNotReadyAddresses := default false .Values.service.headless.publishNotReadyAddresses -}}
-{{- $ports := .Values.service.headlessPorts -}}
-{{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" . "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true ) }}
+{{- $dot := default . .dot -}}
+{{- $suffix := include "common._makeHeadlessSuffix" $dot -}}
+{{- $annotations := default "" $dot.Values.service.headless.annotations -}}
+{{- $publishNotReadyAddresses := default false $dot.Values.service.headless.publishNotReadyAddresses -}}
+{{- $ports := $dot.Values.service.headlessPorts -}}
+{{- $labels := default (dict) .labels -}}
+{{- $matchLabels := default (dict) .matchLabels -}}
+{{ include "common.genericService" (dict "suffix" $suffix "annotations" $annotations "dot" $dot "publishNotReadyAddresses" $publishNotReadyAddresses "ports" $ports "serviceType" "ClusterIP" "headless" true "labels" $labels "matchLabels" $matchLabels) }}
 {{- end -}}
 
 {{/*
@@ -250,3 +303,33 @@ true
 {{-     end }}
 {{-   end }}
 {{- 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 -}}