Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim spaces from annotations that can contain multiple lines #4067

Merged
merged 1 commit into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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