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

auto backend protocol for HTTP/HTTPS #6985

Merged
merged 3 commits into from
Jul 29, 2021
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
2 changes: 1 addition & 1 deletion internal/ingress/annotations/backendprotocol/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
const HTTP = "HTTP"

var (
validProtocols = regexp.MustCompile(`^(HTTP|HTTPS|AJP|GRPC|GRPCS|FCGI)$`)
validProtocols = regexp.MustCompile(`^(AUTO_HTTP|HTTP|HTTPS|AJP|GRPC|GRPCS|FCGI)$`)
)

type backendProtocol struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/ingress/controller/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ func buildProxyPass(host string, b interface{}, loc interface{}) string {
proxyPass := "proxy_pass"

switch location.BackendProtocol {
case "AUTO_HTTP":
proto = "$scheme://"
case "HTTPS":
proto = "https://"
case "GRPC":
Expand Down
93 changes: 78 additions & 15 deletions internal/ingress/controller/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,22 @@ func init() {
var (
// TODO: add tests for SSLPassthrough
tmplFuncTestcases = map[string]struct {
Path string
Target string
Location string
ProxyPass string
Sticky bool
XForwardedPrefix string
SecureBackend bool
enforceRegex bool
Path string
Target string
Location string
ProxyPass string
AutoHttpProxyPass string
Sticky bool
XForwardedPrefix string
SecureBackend bool
enforceRegex bool
}{
"when secure backend enabled": {
"/",
"/",
"/",
"proxy_pass https://upstream_balancer;",
"proxy_pass https://upstream_balancer;",
false,
"",
true,
Expand All @@ -81,6 +83,7 @@ var (
"/",
"/",
"proxy_pass https://upstream_balancer;",
"proxy_pass https://upstream_balancer;",
false,
"",
true,
Expand All @@ -91,6 +94,7 @@ var (
"/",
"/",
"proxy_pass https://upstream_balancer;",
"proxy_pass https://upstream_balancer;",
true,
"",
true,
Expand All @@ -101,6 +105,7 @@ var (
"/",
"/",
"proxy_pass http://upstream_balancer;",
"proxy_pass $scheme://upstream_balancer;",
false,
"",
false,
Expand All @@ -111,6 +116,7 @@ var (
"/",
"/",
"proxy_pass http://upstream_balancer;",
"proxy_pass $scheme://upstream_balancer;",
false,
"",
false,
Expand All @@ -123,6 +129,9 @@ var (
`
rewrite "(?i)/" /jenkins break;
proxy_pass http://upstream_balancer;`,
`
rewrite "(?i)/" /jenkins break;
proxy_pass $scheme://upstream_balancer;`,
false,
"",
false,
Expand All @@ -135,6 +144,9 @@ proxy_pass http://upstream_balancer;`,
`
rewrite "(?i)/" /something break;
proxy_pass http://upstream_balancer;`,
`
rewrite "(?i)/" /something break;
proxy_pass $scheme://upstream_balancer;`,
true,
"",
false,
Expand All @@ -147,6 +159,9 @@ proxy_pass http://upstream_balancer;`,
`
rewrite "(?i)/" /something break;
proxy_pass http://upstream_balancer;`,
`
rewrite "(?i)/" /something break;
proxy_pass $scheme://upstream_balancer;`,
true,
"",
false,
Expand All @@ -160,6 +175,10 @@ proxy_pass http://upstream_balancer;`,
rewrite "(?i)/there" /something break;
proxy_set_header X-Forwarded-Prefix "/there";
proxy_pass http://upstream_balancer;`,
`
rewrite "(?i)/there" /something break;
proxy_set_header X-Forwarded-Prefix "/there";
proxy_pass $scheme://upstream_balancer;`,
true,
"/there",
false,
Expand All @@ -170,6 +189,7 @@ proxy_pass http://upstream_balancer;`,
"/something",
`~* "^/something"`,
"proxy_pass http://upstream_balancer;",
"proxy_pass $scheme://upstream_balancer;",
false,
"",
false,
Expand Down Expand Up @@ -334,6 +354,48 @@ func TestBuildProxyPass(t *testing.T) {
}
}

func TestBuildProxyPassAutoHttp(t *testing.T) {
defaultBackend := "upstream-name"
defaultHost := "example.com"

for k, tc := range tmplFuncTestcases {
loc := &ingress.Location{
Path: tc.Path,
Rewrite: rewrite.Config{Target: tc.Target},
Backend: defaultBackend,
XForwardedPrefix: tc.XForwardedPrefix,
}

if tc.SecureBackend {
loc.BackendProtocol = "HTTPS"
} else {
loc.BackendProtocol = "AUTO_HTTP"
}

backend := &ingress.Backend{
Name: defaultBackend,
}

if tc.Sticky {
backend.SessionAffinity = ingress.SessionAffinityConfig{
AffinityType: "cookie",
CookieSessionAffinity: ingress.CookieSessionAffinity{
Locations: map[string][]string{
defaultHost: {tc.Path},
},
},
}
}

backends := []*ingress.Backend{backend}

pp := buildProxyPass(defaultHost, backends, loc)
if !strings.EqualFold(tc.AutoHttpProxyPass, pp) {
t.Errorf("%s: expected \n'%v'\nbut returned \n'%v'", k, tc.ProxyPass, pp)
}
}
}

func TestBuildAuthLocation(t *testing.T) {
invalidType := &ingress.Ingress{}
expected := ""
Expand Down Expand Up @@ -889,13 +951,14 @@ func TestEscapeLiteralDollar(t *testing.T) {

func TestOpentracingPropagateContext(t *testing.T) {
tests := map[*ingress.Location]string{
{BackendProtocol: "HTTP"}: "opentracing_propagate_context;",
{BackendProtocol: "HTTPS"}: "opentracing_propagate_context;",
{BackendProtocol: "GRPC"}: "opentracing_grpc_propagate_context;",
{BackendProtocol: "GRPCS"}: "opentracing_grpc_propagate_context;",
{BackendProtocol: "AJP"}: "opentracing_propagate_context;",
{BackendProtocol: "FCGI"}: "opentracing_propagate_context;",
nil: "",
{BackendProtocol: "HTTP"}: "opentracing_propagate_context;",
{BackendProtocol: "HTTPS"}: "opentracing_propagate_context;",
{BackendProtocol: "AUTO_HTTP"}: "opentracing_propagate_context;",
{BackendProtocol: "GRPC"}: "opentracing_grpc_propagate_context;",
{BackendProtocol: "GRPCS"}: "opentracing_grpc_propagate_context;",
{BackendProtocol: "AJP"}: "opentracing_propagate_context;",
{BackendProtocol: "FCGI"}: "opentracing_propagate_context;",
nil: "",
}

for loc, expectedDirective := range tests {
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/annotations/backendprotocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ var _ = framework.DescribeAnnotation("backend-protocol", func() {
})
})

ginkgo.It("should set backend protocol to $scheme:// and use proxy_pass", func() {
host := "backendprotocol.foo.com"
annotations := map[string]string{
"nginx.ingress.kubernetes.io/backend-protocol": "AUTO_HTTP",
}

ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return strings.Contains(server, "proxy_pass $scheme://upstream_balancer;")
})
})

ginkgo.It("should set backend protocol to grpc:// and use grpc_pass", func() {
host := "backendprotocol.foo.com"
annotations := map[string]string{
Expand Down