* Rewrite clean-nginx-conf.sh to speed up admission webhook * Less diff with original clean-nginx-conf.sh * Add error handling, add documentation, add unit test * indent code * Don't ignore Getwd() error
This commit is contained in:
parent
f5c80783bf
commit
a064337621
8 changed files with 462 additions and 60 deletions
|
|
@ -23,12 +23,12 @@ import (
|
|||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand" // #nosec
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
|
|
@ -50,9 +50,15 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
slash = "/"
|
||||
nonIdempotent = "non_idempotent"
|
||||
defBufferSize = 65535
|
||||
slash = "/"
|
||||
nonIdempotent = "non_idempotent"
|
||||
defBufferSize = 65535
|
||||
writeIndentOnEmptyLines = true // backward-compatibility
|
||||
)
|
||||
|
||||
const (
|
||||
stateCode = iota
|
||||
stateComment
|
||||
)
|
||||
|
||||
// TemplateWriter is the interface to render a template
|
||||
|
|
@ -86,6 +92,87 @@ func NewTemplate(file string) (*Template, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
// 1. Removes carriage return symbol (\r)
|
||||
// 2. Collapses multiple empty lines to single one
|
||||
// 3. Re-indent
|
||||
// (ATW: always returns nil)
|
||||
func cleanConf(in *bytes.Buffer, out *bytes.Buffer) error {
|
||||
depth := 0
|
||||
lineStarted := false
|
||||
emptyLineWritten := false
|
||||
state := stateCode
|
||||
for {
|
||||
c, err := in.ReadByte()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
return err // unreachable
|
||||
}
|
||||
|
||||
needOutput := false
|
||||
nextDepth := depth
|
||||
nextLineStarted := lineStarted
|
||||
|
||||
switch state {
|
||||
case stateCode:
|
||||
switch c {
|
||||
case '{':
|
||||
needOutput = true
|
||||
nextDepth = depth + 1
|
||||
nextLineStarted = true
|
||||
case '}':
|
||||
needOutput = true
|
||||
depth--
|
||||
nextDepth = depth
|
||||
nextLineStarted = true
|
||||
case ' ', '\t':
|
||||
needOutput = lineStarted
|
||||
case '\r':
|
||||
case '\n':
|
||||
needOutput = !(!lineStarted && emptyLineWritten)
|
||||
nextLineStarted = false
|
||||
case '#':
|
||||
needOutput = true
|
||||
nextLineStarted = true
|
||||
state = stateComment
|
||||
default:
|
||||
needOutput = true
|
||||
nextLineStarted = true
|
||||
}
|
||||
case stateComment:
|
||||
switch c {
|
||||
case '\r':
|
||||
case '\n':
|
||||
needOutput = true
|
||||
nextLineStarted = false
|
||||
state = stateCode
|
||||
default:
|
||||
needOutput = true
|
||||
}
|
||||
}
|
||||
|
||||
if needOutput {
|
||||
if !lineStarted && (writeIndentOnEmptyLines || c != '\n') {
|
||||
for i := 0; i < depth; i++ {
|
||||
err = out.WriteByte('\t') // always nil
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
emptyLineWritten = !lineStarted
|
||||
err = out.WriteByte(c) // always nil
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
depth = nextDepth
|
||||
lineStarted = nextLineStarted
|
||||
}
|
||||
}
|
||||
|
||||
// Write populates a buffer using a template with NGINX configuration
|
||||
// and the servers and upstreams created by Ingress rules
|
||||
func (t *Template) Write(conf config.TemplateConfig) ([]byte, error) {
|
||||
|
|
@ -110,12 +197,9 @@ func (t *Template) Write(conf config.TemplateConfig) ([]byte, error) {
|
|||
|
||||
// squeezes multiple adjacent empty lines to be single
|
||||
// spaced this is to avoid the use of regular expressions
|
||||
cmd := exec.Command("/ingress-controller/clean-nginx-conf.sh")
|
||||
cmd.Stdin = tmplBuf
|
||||
cmd.Stdout = outCmdBuf
|
||||
if err := cmd.Run(); err != nil {
|
||||
klog.Warningf("unexpected error cleaning template: %v", err)
|
||||
return tmplBuf.Bytes(), nil
|
||||
err = cleanConf(tmplBuf, outCmdBuf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return outCmdBuf.Bytes(), nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue