Improve oran_oom helm charts
[it/dep.git] / smo-install / oran_oom / nonrtric-common / templates / _service.tpl
1 {{/*
2 # Copyright © 2017 Amdocs, Bell Canada
3 # Modifications Copyright (c) 2024 NYCU WINLab.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 */}}
17 {{/*
18   Resolve the name of a chart's service.
19
20   The default will be the chart name (or .Values.nameOverride if set).
21   And the use of .Values.service.name overrides all.
22
23   - .Values.service.name: override default service (ie. chart) name
24 */}}
25 {{/*
26   Expand the service name for a chart.
27 */}}
28 {{- define "common.servicename" -}}
29   {{- $name := default .Chart.Name .Values.nameOverride -}}
30   {{- default $name .Values.service.name | trunc 63 | trimSuffix "-" -}}
31 {{- end -}}
32
33 {{/* Define the metadata of Service
34      The function takes from one to four arguments (inside a dictionary):
35      - .dot : environment (.)
36      - .suffix : a string which will be added at the end of the name (with a '-').
37      - .annotations: the annotations to add
38      - .labels : labels to add
39      Usage example:
40       {{ include "common.serviceMetadata" ( dict "suffix" "myService" "dot" .) }}
41       {{ include "common.serviceMetadata" ( dict "annotations" .Values.service.annotation "dot" .) }}
42 */}}
43
44 {{- define "common.serviceMetadata" -}}
45   {{- $dot := default . .dot -}}
46   {{- $suffix := default "" .suffix -}}
47   {{- $annotations := default "" .annotations -}}
48   {{- $labels := default (dict) .labels -}}
49 {{- if $annotations -}}
50 annotations:
51 {{  include "common.tplValue" (dict "value" $annotations "context" $dot) | indent 2 }}
52 {{- end -}}
53 name: {{ include "common.servicename" $dot }}{{ if $suffix }}{{ print "-" $suffix }}{{ end }}
54 namespace: {{ include "common.namespace" $dot }}
55 labels: {{- include "common.labels" (dict "labels" $labels "dot" $dot) | nindent 2 -}}
56 {{- end -}}
57
58 {{/* Define the ports of Service
59      The function takes three arguments (inside a dictionary):
60      - .dot : environment (.)
61      - .ports : an array of ports
62      - .serviceType: the type of the service
63 */}}
64 {{- define "common.servicePorts" -}}
65 {{- $serviceType := .serviceType -}}
66 {{- $dot := .dot -}}
67 {{-   range $index, $port := .ports -}}
68 {{-   if $port.targetPort -}}
69 - name: {{ $port.name }}
70   port: {{ default $port.targetPort $port.internalPort }}
71   targetPort: {{ $port.targetPort }}
72 {{-       if (and (eq $serviceType "NodePort") $port.nodePort) }}
73   nodePort: {{ $port.nodePort }}
74 {{-       end }}
75 {{-       if $port.l4_protocol }}
76   protocol: {{ $port.l4_protocol }}
77 {{-       else }}
78   protocol: TCP
79 {{-       end }}
80 {{-       if $port.app_protocol }}
81   appProtocol: {{ $port.app_protocol }}
82 {{-       end }}
83 {{-     end }}
84 {{-     if $port.internalPort_tls }}
85 - name: {{ $port.name_tls }}
86   port: {{ default $port.targetPort_tls $port.internalPort_tls }}
87   targetPort: {{ $port.targetPort_tls }}
88 {{-       if (and (eq $serviceType "NodePort") $port.nodePort_tls) }}
89   nodePort: {{ $port.nodePort_tls }}
90 {{-       end }}
91 {{-       if $port.l4_protocol }}
92   protocol: {{ $port.l4_protocol }}
93 {{-       else }}
94   protocol: TCP
95 {{-       end }}
96 {{-       if $port.app_protocol }}
97   appProtocol: {{ $port.app_protocol }}
98 {{-       end }}
99 {{-     end }}
100 {{   end }}
101 {{- end -}}
102
103 {{/* Create generic service template
104      The function takes several arguments (inside a dictionary):
105      - .dot : environment (.)
106      - .ports : an array of ports
107      - .serviceType: the type of the service
108      - .suffix : a string which will be added at the end of the name (with a '-')
109      - .annotations: the annotations to add
110      - .publishNotReadyAddresses: if we publish not ready address
111      - .headless: if the service is headless
112      - .add_plain_port: add tls port AND plain port
113      - .labels : labels to add (dict)
114      - .matchLabels: selectors/machLabels to add (dict)
115 */}}
116 {{- define "common.service" -}}
117 {{-   $dot := default . .dot -}}
118 {{-   $suffix := default "" $dot.Values.service.suffix -}}
119 {{-   $annotations := default "" $dot.Values.service.annotations -}}
120 {{-   $publishNotReadyAddresses := default false $dot.Values.service.publishNotReadyAddresses -}}
121 {{-   $serviceType := $dot.Values.service.type -}}
122 {{-   $ports := $dot.Values.service.ports -}}
123 {{-   $labels := default (dict) .labels -}}
124 {{-   $matchLabels := default (dict) .matchLabels -}}
125 {{-   $ipFamilyPolicy := default "PreferDualStack" $dot.Values.service.ipFamilyPolicy -}}
126 apiVersion: v1
127 kind: Service
128 metadata: {{- include "common.serviceMetadata" (dict "suffix" $suffix "annotations" $annotations "labels" $labels "dot" $dot) | nindent 2 }}
129 spec:
130   ports: {{- include "common.servicePorts" (dict "serviceType" $serviceType "ports" $ports "dot" $dot) | nindent 4 }}
131   ipFamilyPolicy: {{ $ipFamilyPolicy }}
132   {{- if $publishNotReadyAddresses }}
133   publishNotReadyAddresses: true
134   {{- end }}
135   type: {{ $serviceType }}
136   selector: {{- include "common.matchLabels" (dict "matchLabels" $matchLabels "dot" $dot) | nindent 4 }}
137 {{- end -}}