From b6c8db8a561090c008f6204fd5b95df0e550ecdd Mon Sep 17 00:00:00 2001 From: Rahul Somasundaram Date: Tue, 2 Jul 2024 17:07:08 +0530 Subject: [PATCH] Add transportserver-template config (#5194) --- .../global-configuration/configmap-resource.md | 1 + examples/shared-examples/custom-templates/README.md | 7 +++++++ internal/configs/config_params.go | 7 ++++--- internal/configs/configmaps.go | 4 ++++ internal/configs/configurator.go | 7 +++++++ internal/configs/version2/template_executor.go | 11 +++++++++++ 6 files changed, 34 insertions(+), 3 deletions(-) diff --git a/docs/content/configuration/global-configuration/configmap-resource.md b/docs/content/configuration/global-configuration/configmap-resource.md index 74bbfdb89d..3295a52389 100644 --- a/docs/content/configuration/global-configuration/configmap-resource.md +++ b/docs/content/configuration/global-configuration/configmap-resource.md @@ -191,6 +191,7 @@ For more information, view the [VirtualServer and VirtualServerRoute resources]( |*main-template* | Sets the main NGINX configuration template. | By default the template is read from the file in the container. | [Custom Templates](/nginx-ingress-controller/configuration/global-configuration/custom-templates). | |*ingress-template* | Sets the NGINX configuration template for an Ingress resource. | By default the template is read from the file on the container. | [Custom Templates](/nginx-ingress-controller/configuration/global-configuration/custom-templates). | |*virtualserver-template* | Sets the NGINX configuration template for an VirtualServer resource. | By default the template is read from the file on the container. | [Custom Templates](/nginx-ingress-controller/configuration/global-configuration/custom-templates). | +|*transportserver-template* | Sets the NGINX configuration template for a TransportServer resource. | By default the template is read from the file on the container. | [Custom Templates](/nginx-ingress-controller/configuration/global-configuration/custom-templates). | {{}} --- diff --git a/examples/shared-examples/custom-templates/README.md b/examples/shared-examples/custom-templates/README.md index 6029395458..748da26bce 100644 --- a/examples/shared-examples/custom-templates/README.md +++ b/examples/shared-examples/custom-templates/README.md @@ -7,6 +7,7 @@ via the following keys: - `main-template` - Sets the main NGINX configuration template. - `ingress-template` - Sets the Ingress NGINX configuration template for an Ingress resource. - `virtualserver-template` - Sets the NGINX configuration template for an VirtualServer resource. +- `transportserver-template` - Sets the NGINX configuration template for a TransportServer resource. ## Example @@ -35,6 +36,12 @@ data: ... } {{ end }} + transportserver-template: | + {{- range $u := .Upstreams }} + upstream {{ $u.Name }} { + zone {{ $u.Name }} 256k; + ... + } ``` **Notes:** diff --git a/internal/configs/config_params.go b/internal/configs/config_params.go index be86ae4eb9..f124a08473 100644 --- a/internal/configs/config_params.go +++ b/internal/configs/config_params.go @@ -99,9 +99,10 @@ type ConfigParams struct { MainServerSSLPreferServerCiphers bool MainServerSSLProtocols string - IngressTemplate *string - VirtualServerTemplate *string - MainTemplate *string + IngressTemplate *string + VirtualServerTemplate *string + MainTemplate *string + TransportServerTemplate *string JWTKey string JWTLoginURL string diff --git a/internal/configs/configmaps.go b/internal/configs/configmaps.go index c4041336da..e372be286f 100644 --- a/internal/configs/configmaps.go +++ b/internal/configs/configmaps.go @@ -345,6 +345,10 @@ func ParseConfigMap(cfgm *v1.ConfigMap, nginxPlus bool, hasAppProtect bool, hasA cfgParams.VirtualServerTemplate = &virtualServerTemplate } + if transportServerTemplate, exists := cfgm.Data["transportserver-template"]; exists { + cfgParams.TransportServerTemplate = &transportServerTemplate + } + if mainStreamSnippets, exists := GetMapKeyAsStringSlice(cfgm.Data, "stream-snippets", cfgm, "\n"); exists { cfgParams.MainStreamSnippets = mainStreamSnippets } diff --git a/internal/configs/configurator.go b/internal/configs/configurator.go index c891afd4e6..71f23e8897 100644 --- a/internal/configs/configurator.go +++ b/internal/configs/configurator.go @@ -1319,6 +1319,13 @@ func (cnf *Configurator) UpdateConfig(cfgParams *ConfigParams, resources Extende } } + if cfgParams.TransportServerTemplate != nil { + err := cnf.templateExecutorV2.UpdateTransportServerTemplate(cfgParams.TransportServerTemplate) + if err != nil { + return allWarnings, fmt.Errorf("error when parsing the TransportServer template: %w", err) + } + } + mainCfg := GenerateNginxMainConfig(cnf.staticCfgParams, cfgParams) mainCfgContent, err := cnf.templateExecutor.ExecuteMainConfigTemplate(mainCfg) if err != nil { diff --git a/internal/configs/version2/template_executor.go b/internal/configs/version2/template_executor.go index d7a4989ab6..dabd3ff808 100644 --- a/internal/configs/version2/template_executor.go +++ b/internal/configs/version2/template_executor.go @@ -57,6 +57,17 @@ func (te *TemplateExecutor) UpdateVirtualServerTemplate(templateString *string) return nil } +// UpdateTransportServerTemplate updates the TransportServer template. +func (te *TemplateExecutor) UpdateTransportServerTemplate(templateString *string) error { + newTemplate, err := template.New("transportServerTemplate").Funcs(helperFunctions).Parse(*templateString) + if err != nil { + return err + } + te.transportServerTemplate = newTemplate + + return nil +} + // ExecuteVirtualServerTemplate generates the content of an NGINX configuration file for a VirtualServer resource. func (te *TemplateExecutor) ExecuteVirtualServerTemplate(cfg *VirtualServerConfig) ([]byte, error) { var configBuffer bytes.Buffer