Skip to content

Commit

Permalink
Merge pull request #4067 from aledbf/normalize
Browse files Browse the repository at this point in the history
Trim spaces from annotations that can contain multiple lines
  • Loading branch information
k8s-ci-robot authored May 9, 2019
2 parents 6d82cd6 + 23e7423 commit fafa0a6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 13 additions & 2 deletions internal/ingress/annotations/parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package parser
import (
"fmt"
"strconv"
"strings"

extensions "k8s.io/api/extensions/v1beta1"

Expand Down Expand Up @@ -52,11 +53,12 @@ func (a ingAnnotations) parseBool(name string) (bool, error) {
func (a ingAnnotations) parseString(name string) (string, error) {
val, ok := a[name]
if ok {
if len(val) == 0 {
s := normalizeString(val)
if len(s) == 0 {
return "", errors.NewInvalidAnnotationContent(name, val)
}

return val, nil
return s, nil
}
return "", errors.ErrMissingAnnotations
}
Expand Down Expand Up @@ -119,3 +121,12 @@ func GetIntAnnotation(name string, ing *extensions.Ingress) (int, error) {
func GetAnnotationWithPrefix(suffix string) string {
return fmt.Sprintf("%v/%v", AnnotationsPrefix, suffix)
}

func normalizeString(input string) string {
trimmedContent := []string{}
for _, line := range strings.Split(input, "\n") {
trimmedContent = append(trimmedContent, strings.TrimSpace(line))
}

return strings.Join(trimmedContent, "\n")
}
14 changes: 11 additions & 3 deletions internal/ingress/annotations/parser/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ func TestGetStringAnnotation(t *testing.T) {
exp string
expErr bool
}{
{"valid - A", "string", "A", "A", false},
{"valid - B", "string", "B", "B", false},
{"empty", "string", "", "", true},
{"valid - A", "string", "A ", "A", false},
{"valid - B", "string", " B", "B", false},
{"empty", "string", " ", "", true},
{"valid multiline", "string", `
rewrite (?i)/arcgis/rest/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/rest/services/Utilities/Geometry/GeometryServer$1 break;
rewrite (?i)/arcgis/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/services/Utilities/Geometry/GeometryServer$1 break;
`, `
rewrite (?i)/arcgis/rest/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/rest/services/Utilities/Geometry/GeometryServer$1 break;
rewrite (?i)/arcgis/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/services/Utilities/Geometry/GeometryServer$1 break;
`,
false},
}

data := map[string]string{}
Expand Down

0 comments on commit fafa0a6

Please sign in to comment.