diff --git a/charts/gateway/templates/_helpers.tpl b/charts/gateway/templates/_helpers.tpl index af4dfbd..a5e0bd6 100644 --- a/charts/gateway/templates/_helpers.tpl +++ b/charts/gateway/templates/_helpers.tpl @@ -104,3 +104,95 @@ Scheme to use while connecting etcd {{- "http" }} {{- end }} {{- end }} + +{{/* +Expand a port value (which may contain a port range) into a list of individual +port numbers for K8s resource definitions. +Input: a string like "2000-2100" or "127.0.0.1:2000-2100" or "9100" +Output: JSON {"ports": [2000, 2001, ..., 2100]} +*/}} +{{- define "gateway.expandPorts" -}} +{{- $result := list -}} +{{- $s := . | toString -}} +{{- $portPart := splitList ":" $s | last -}} +{{- if contains "-" $portPart -}} + {{- $bounds := splitList "-" $portPart -}} + {{- $start := index $bounds 0 | trim | int -}} + {{- $end := index $bounds 1 | trim | int -}} + {{- if gt $start $end -}} + {{- $tmp := $start -}} + {{- $start = $end -}} + {{- $end = $tmp -}} + {{- end -}} + {{- $count := add1 (sub $end $start) | int -}} + {{- range $i := until $count -}} + {{- $result = append $result (add $start $i | int) -}} + {{- end -}} +{{- else -}} + {{- $result = append $result ($portPart | trim | int) -}} +{{- end -}} +{{- dict "ports" $result | toJson -}} +{{- end -}} + +{{/* +Normalize a TCP port list into expanded individual port entries for K8s resources. +Handles integers, strings (with ranges), and map entries (with addr ranges). +Input: the .tcp array from values +Output: JSON {"entries": [{"port": 9100, "nodePort": ""}, ...]} +*/}} +{{- define "gateway.normalizedTcpPorts" -}} +{{- $result := list -}} +{{- range $item := . -}} + {{- $addrStr := "" -}} + {{- $nodePort := "" -}} + {{- if kindIs "map" $item -}} + {{- $addrStr = $item.addr | toString -}} + {{- if hasKey $item "nodePort" -}} + {{- $nodePort = $item.nodePort | toString -}} + {{- end -}} + {{- else -}} + {{- $addrStr = $item | toString -}} + {{- end -}} + {{- $portPart := splitList ":" $addrStr | last -}} + {{- if or (contains "," $addrStr) (contains "-" $portPart) -}} + {{- $expanded := include "gateway.expandPorts" $addrStr | fromJson -}} + {{- range $port := $expanded.ports -}} + {{- $result = append $result (dict "port" $port "nodePort" "") -}} + {{- end -}} + {{- else -}} + {{- $result = append $result (dict "port" ($portPart | int) "nodePort" $nodePort) -}} + {{- end -}} +{{- end -}} +{{- dict "entries" $result | toJson -}} +{{- end -}} + +{{/* +Normalize a UDP port list into expanded individual port entries for K8s resources. +Input: the .udp array from values +Output: JSON {"entries": [{"port": 9200, "nodePort": ""}, ...]} +*/}} +{{- define "gateway.normalizedUdpPorts" -}} +{{- $result := list -}} +{{- range $item := . -}} + {{- $addrStr := "" -}} + {{- $nodePort := "" -}} + {{- if kindIs "map" $item -}} + {{- $addrStr = $item.addr | toString -}} + {{- if hasKey $item "nodePort" -}} + {{- $nodePort = $item.nodePort | toString -}} + {{- end -}} + {{- else -}} + {{- $addrStr = $item | toString -}} + {{- end -}} + {{- $portPart := splitList ":" $addrStr | last -}} + {{- if or (contains "," $addrStr) (contains "-" $portPart) -}} + {{- $expanded := include "gateway.expandPorts" $addrStr | fromJson -}} + {{- range $port := $expanded.ports -}} + {{- $result = append $result (dict "port" $port "nodePort" "") -}} + {{- end -}} + {{- else -}} + {{- $result = append $result (dict "port" ($portPart | int) "nodePort" $nodePort) -}} + {{- end -}} +{{- end -}} +{{- dict "entries" $result | toJson -}} +{{- end -}} diff --git a/charts/gateway/templates/_pod.tpl b/charts/gateway/templates/_pod.tpl index d887516..b7a174e 100644 --- a/charts/gateway/templates/_pod.tpl +++ b/charts/gateway/templates/_pod.tpl @@ -130,31 +130,23 @@ spec: protocol: TCP {{- end }} {{- if and .Values.gateway.stream.enabled (or (gt (len .Values.gateway.stream.tcp) 0) (gt (len .Values.gateway.stream.udp) 0)) }} - {{- with .Values.gateway.stream }} - {{- if (gt (len .tcp) 0) }} - {{- range $index, $port := .tcp }} + {{- if (gt (len .Values.gateway.stream.tcp) 0) }} + {{- $normalized := include "gateway.normalizedTcpPorts" .Values.gateway.stream.tcp | fromJson }} + {{- range $index, $entry := $normalized.entries }} - name: proxy-tcp-{{ $index | toString }} - {{- if kindIs "map" $port }} - containerPort: {{ splitList ":" ($port.addr | toString) | last }} - {{- else }} - containerPort: {{ $port }} - {{- end }} + containerPort: {{ $entry.port | int }} protocol: TCP {{- end }} {{- end }} - {{- if (gt (len .udp) 0) }} - {{- range $index, $port := .udp }} + {{- if (gt (len .Values.gateway.stream.udp) 0) }} + {{- $normalized := include "gateway.normalizedUdpPorts" .Values.gateway.stream.udp | fromJson }} + {{- range $index, $entry := $normalized.entries }} - name: proxy-udp-{{ $index | toString }} - {{- if kindIs "map" $port }} - containerPort: {{ splitList ":" ($port.addr | toString) | last }} - {{- else }} - containerPort: {{ $port }} - {{- end }} + containerPort: {{ $entry.port | int }} protocol: UDP {{- end }} {{- end }} {{- end }} - {{- end }} {{- if and .Values.deployment.fallback_cp.mode (eq .Values.deployment.fallback_cp.mode "write") }} {{- /* Disable probes when fallback_cp mode is set to write */ -}} {{- else }} diff --git a/charts/gateway/templates/configmap.yaml b/charts/gateway/templates/configmap.yaml index 5b7716b..c4562ff 100644 --- a/charts/gateway/templates/configmap.yaml +++ b/charts/gateway/templates/configmap.yaml @@ -85,10 +85,12 @@ data: tcp: # TCP proxy port list {{- range .Values.gateway.stream.tcp }} {{- if kindIs "map" . }} - - addr: {{ .addr }} + - addr: {{ .addr | quote }} {{- if hasKey . "tls" }} tls: {{ .tls }} {{- end }} + {{- else if kindIs "string" . }} + - {{ . | quote }} {{- else }} - {{ . }} {{- end }} @@ -98,7 +100,9 @@ data: udp: # UDP proxy port list {{- range .Values.gateway.stream.udp }} {{- if kindIs "map" . }} - - addr: {{ .addr }} + - addr: {{ .addr | quote }} + {{- else if kindIs "string" . }} + - {{ . | quote }} {{- else }} - {{ . }} {{- end }} diff --git a/charts/gateway/templates/service-gateway.yaml b/charts/gateway/templates/service-gateway.yaml index 04eba1a..1fde4ca 100644 --- a/charts/gateway/templates/service-gateway.yaml +++ b/charts/gateway/templates/service-gateway.yaml @@ -89,60 +89,36 @@ spec: protocol: TCP {{- end }} {{- if and .Values.gateway.stream.enabled (or (gt (len .Values.gateway.stream.tcp) 0) (gt (len .Values.gateway.stream.udp) 0)) }} - {{- with .Values.gateway.stream }} - {{- if (gt (len .tcp) 0) }} - {{- range $index, $port := .tcp }} + {{- if (gt (len .Values.gateway.stream.tcp) 0) }} + {{- $normalized := include "gateway.normalizedTcpPorts" .Values.gateway.stream.tcp | fromJson }} + {{- range $index, $entry := $normalized.entries }} - name: proxy-tcp-{{ $index | toString }} protocol: TCP - {{- if kindIs "map" $port }} - port: {{ splitList ":" ($port.addr | toString) | last }} - targetPort: {{ splitList ":" ($port.addr | toString) | last }} + port: {{ $entry.port | int }} + targetPort: {{ $entry.port | int }} {{- if eq $global.Values.gateway.type "NodePort" }} {{- if $global.Values.gateway.stream.autoAssignNodePort }} - nodePort: {{ splitList ":" ($port.addr | toString) | last }} - {{- else }} - {{- if not (empty $port.nodePort) }} - nodePort: {{ $port.nodePort }} + nodePort: {{ $entry.port | int }} + {{- else if ne ($entry.nodePort | toString) "" }} + nodePort: {{ $entry.nodePort }} {{- end }} {{- end }} - {{- end }} - {{- else }} - port: {{ $port }} - targetPort: {{ $port }} - {{- if eq $global.Values.gateway.type "NodePort" }} - {{- if $global.Values.gateway.stream.autoAssignNodePort }} - nodePort: {{ $port }} - {{- end }} - {{- end }} - {{- end }} {{- end }} {{- end }} - {{- if (gt (len .udp) 0) }} - {{- range $index, $port := .udp }} + {{- if (gt (len .Values.gateway.stream.udp) 0) }} + {{- $normalized := include "gateway.normalizedUdpPorts" .Values.gateway.stream.udp | fromJson }} + {{- range $index, $entry := $normalized.entries }} - name: proxy-udp-{{ $index | toString }} protocol: UDP - {{- if kindIs "map" $port }} - port: {{ splitList ":" ($port.addr | toString) | last }} - targetPort: {{ splitList ":" ($port.addr | toString) | last }} + port: {{ $entry.port | int }} + targetPort: {{ $entry.port | int }} {{- if eq $global.Values.gateway.type "NodePort" }} {{- if $global.Values.gateway.stream.autoAssignNodePort }} - nodePort: {{ splitList ":" ($port.addr | toString) | last }} - {{- else }} - {{- if not (empty $port.nodePort) }} - nodePort: {{ $port.nodePort }} - {{- end }} + nodePort: {{ $entry.port | int }} + {{- else if ne ($entry.nodePort | toString) "" }} + nodePort: {{ $entry.nodePort }} {{- end }} {{- end }} - {{- else }} - port: {{ $port }} - targetPort: {{ $port }} - {{- if eq $global.Values.gateway.type "NodePort" }} - {{- if $global.Values.gateway.stream.autoAssignNodePort }} - nodePort: {{ $port }} - {{- end }} - {{- end }} - {{- end }} - {{- end }} {{- end }} {{- end }} {{- end }} diff --git a/charts/gateway/values.yaml b/charts/gateway/values.yaml index 53afcbb..9b62007 100644 --- a/charts/gateway/values.yaml +++ b/charts/gateway/values.yaml @@ -364,10 +364,15 @@ gateway: # - addr: 192.168.31.10:5432 # - addr: 3302 # nodePort: 31302 + # - "2000-2100" # port range (nginx native support) + # - addr: "3000-3100" # port range in table form + # - addr: "127.0.0.1:4000-4100" # address with port range + # tls: true udp: [] # - addr: 192.168.31.10:53 # - addr: 5353 # nodePort: 31353 + # - "9300-9310" # port range # -- Using ingress access API7 Gateway service ingress: enabled: false