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

Fix mirror-target values without path separator and port #9889

Merged
merged 4 commits into from
Jun 11, 2023
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
7 changes: 4 additions & 3 deletions internal/ingress/annotations/mirror/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ func (a mirror) Parse(ing *networking.Ingress) (interface{}, error) {
config.Host, err = parser.GetStringAnnotation("mirror-host", ing)
if err != nil {
if config.Target != "" {
url, err := parser.StringToURL(config.Target)
target := strings.Split(config.Target, "$")

url, err := parser.StringToURL(target[0])
if err != nil {
config.Host = ""
} else {
hostname := strings.Split(url.Hostname(), "$")
config.Host = hostname[0]
config.Host = url.Hostname()
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions internal/ingress/annotations/mirror/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ func TestParse(t *testing.T) {
Target: "https://test.env.com/$request_uri",
Host: "test.env.com",
}},
{map[string]string{backendURL: "https://test.env.com$request_uri"}, &Config{
Source: ngxURI,
RequestBody: "on",
Target: "https://test.env.com$request_uri",
Host: "test.env.com",
}},
{map[string]string{backendURL: "https://test.env.com:8080$request_uri"}, &Config{
Source: ngxURI,
RequestBody: "on",
Target: "https://test.env.com:8080$request_uri",
Host: "test.env.com",
}},
{map[string]string{backendURL: "https://test.env.com:8080/$request_uri"}, &Config{
Source: ngxURI,
RequestBody: "on",
Target: "https://test.env.com:8080/$request_uri",
Host: "test.env.com",
}},
{map[string]string{requestBody: "off"}, &Config{
Source: "",
RequestBody: "off",
Expand Down
6 changes: 3 additions & 3 deletions internal/ingress/controller/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ func buildMirrorLocations(locs []*ingress.Location) string {
mapped := sets.Set[string]{}

for _, loc := range locs {
if loc.Mirror.Source == "" || loc.Mirror.Target == "" {
if loc.Mirror.Source == "" || loc.Mirror.Target == "" || loc.Mirror.Host == "" {
continue
}

Expand All @@ -1738,8 +1738,8 @@ func buildMirrorLocations(locs []*ingress.Location) string {
mapped.Insert(loc.Mirror.Source)
buffer.WriteString(fmt.Sprintf(`location = %v {
internal;
proxy_set_header Host %v;
proxy_pass %v;
proxy_set_header Host "%v";
proxy_pass "%v";
}

`, loc.Mirror.Source, loc.Mirror.Host, loc.Mirror.Target))
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/annotations/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var _ = framework.DescribeAnnotation("mirror-*", func() {
func(server string) bool {
return strings.Contains(server, fmt.Sprintf("mirror /_mirror-%v;", ing.UID)) &&
strings.Contains(server, "mirror_request_body on;") &&
strings.Contains(server, "proxy_pass https://test.env.com/$request_uri;")
strings.Contains(server, `proxy_pass "https://test.env.com/$request_uri";`)
})
})

Expand Down