Skip to content

Commit

Permalink
Add transportserver-template config (#5194)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrahul96 authored Jul 2, 2024
1 parent 4ab76e7 commit b6c8db8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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). |
{{</bootstrap-table>}}
---
Expand Down
7 changes: 7 additions & 0 deletions examples/shared-examples/custom-templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -35,6 +36,12 @@ data:
...
}
{{ end }}
transportserver-template: |
{{- range $u := .Upstreams }}
upstream {{ $u.Name }} {
zone {{ $u.Name }} 256k;
...
}
```
**Notes:**
Expand Down
7 changes: 4 additions & 3 deletions internal/configs/config_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions internal/configs/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions internal/configs/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions internal/configs/version2/template_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b6c8db8

Please sign in to comment.