github.com/nginxinc/kubernetes-ingress@v1.12.5/internal/configs/version2/template_executor.go (about)

     1  package version2
     2  
     3  import (
     4  	"bytes"
     5  	"path"
     6  	"text/template"
     7  )
     8  
     9  // #nosec G101
    10  const tlsPassthroughHostsTemplateString = `# mapping between TLS Passthrough hosts and unix sockets
    11  {{ range $h, $u := . }}
    12  {{ $h }} {{ $u }};
    13  {{ end }}
    14  `
    15  
    16  // TemplateExecutor executes NGINX configuration templates.
    17  type TemplateExecutor struct {
    18  	virtualServerTemplate       *template.Template
    19  	transportServerTemplate     *template.Template
    20  	tlsPassthroughHostsTemplate *template.Template
    21  }
    22  
    23  // NewTemplateExecutor creates a TemplateExecutor.
    24  func NewTemplateExecutor(virtualServerTemplatePath string, transportServerTemplatePath string) (*TemplateExecutor, error) {
    25  	// template names  must be the base name of the template file https://golang.org/pkg/text/template/#Template.ParseFiles
    26  
    27  	vsTemplate, err := template.New(path.Base(virtualServerTemplatePath)).ParseFiles(virtualServerTemplatePath)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	tsTemplate, err := template.New(path.Base(transportServerTemplatePath)).ParseFiles(transportServerTemplatePath)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	tlsPassthroughHostsTemplate, err := template.New("unixSockets").Parse(tlsPassthroughHostsTemplateString)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	return &TemplateExecutor{
    43  		virtualServerTemplate:       vsTemplate,
    44  		transportServerTemplate:     tsTemplate,
    45  		tlsPassthroughHostsTemplate: tlsPassthroughHostsTemplate,
    46  	}, nil
    47  }
    48  
    49  // UpdateVirtualServerTemplate updates the VirtualServer template.
    50  func (te *TemplateExecutor) UpdateVirtualServerTemplate(templateString *string) error {
    51  	newTemplate, err := template.New("virtualServerTemplate").Parse(*templateString)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	te.virtualServerTemplate = newTemplate
    56  
    57  	return nil
    58  }
    59  
    60  // ExecuteVirtualServerTemplate generates the content of an NGINX configuration file for a VirtualServer resource.
    61  func (te *TemplateExecutor) ExecuteVirtualServerTemplate(cfg *VirtualServerConfig) ([]byte, error) {
    62  	var configBuffer bytes.Buffer
    63  	err := te.virtualServerTemplate.Execute(&configBuffer, cfg)
    64  
    65  	return configBuffer.Bytes(), err
    66  }
    67  
    68  // ExecuteTransportServerTemplate generates the content of an NGINX configuration file for a TransportServer resource.
    69  func (te *TemplateExecutor) ExecuteTransportServerTemplate(cfg *TransportServerConfig) ([]byte, error) {
    70  	var configBuffer bytes.Buffer
    71  	err := te.transportServerTemplate.Execute(&configBuffer, cfg)
    72  
    73  	return configBuffer.Bytes(), err
    74  }
    75  
    76  // ExecuteTLSPassthroughHostsTemplate generates the content of an NGINX configuration file for mapping between
    77  // TLS Passthrough hosts and the corresponding unix sockets.
    78  func (te *TemplateExecutor) ExecuteTLSPassthroughHostsTemplate(cfg *TLSPassthroughHostsConfig) ([]byte, error) {
    79  	var configBuffer bytes.Buffer
    80  	err := te.tlsPassthroughHostsTemplate.Execute(&configBuffer, cfg)
    81  
    82  	return configBuffer.Bytes(), err
    83  }