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

Support rewrite-target annotation #720

Merged
merged 1 commit into from Nov 25, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions apis/voyager/v1beta1/annotations.go
Expand Up @@ -244,6 +244,9 @@ const (
// https://www.haproxy.com/documentation/aloha/7-0/haproxy/healthchecks/
CheckHealth = EngressKey + "/" + "check"
CheckHealthPort = EngressKey + "/" + "check-port"

// https://github.com/kubernetes/ingress-nginx/blob/master/docs/examples/rewrite/README.md
RewriteTarget = IngressKey + "/" + "rewrite-target"
)

const (
Expand Down Expand Up @@ -577,6 +580,10 @@ func (r Ingress) BasicAuthEnabled() bool {
return true
}

func (r Ingress) RewriteTarget() string {
return meta.GetString(r.Annotations, RewriteTarget)
}

func (r Ingress) AuthRealm() string {
return meta.GetString(r.Annotations, AuthRealm)
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/ingress/parser.go
Expand Up @@ -218,6 +218,27 @@ func getBackendName(r *api.Ingress, be api.IngressBackend) string {
return rand.WithUniqSuffix(seed)
}

// ref: https://github.com/jcmoraisjr/haproxy-ingress/pull/57
// ref: https://github.com/jcmoraisjr/haproxy-ingress/blob/939bd129c86d9b27b12e6d7a50c799d8496ab8f3/rootfs/etc/haproxy/template/haproxy.tmpl#L318
func (c *controller) rewriteTarget(path string, rewriteRules []string) []string {
target := c.Ingress.RewriteTarget()
switch target {
case "":
return rewriteRules
case "/":
// ^([^\ :]*)\ {{ $location.Path }}/?(.*$) \1\ {{ $rewriteTarget }}\2
rule := fmt.Sprintf(`^([^\ :]*)\ %s/?(.*$) \1\ %s\2`, path, target)
return append([]string{rule}, rewriteRules...)
default:
// ^([^\ :]*)\ {{ $location.Path }}(.*$) \1\ {{ $rewriteTarget }}{{ if hasSuffix $location.Path "/" }}/{{ end }}\2
if strings.HasSuffix(path, "/") {
target = target + "/"
}
rule := fmt.Sprintf(`^([^\ :]*)\ %s(.*$) \1\ %s\2`, path, target)
return append([]string{rule}, rewriteRules...)
}
}

func (c *controller) generateConfig() error {
if c.Ingress.SSLPassthrough() {
if err := c.convertRulesForSSLPassthrough(); err != nil {
Expand Down Expand Up @@ -417,7 +438,7 @@ func (c *controller) generateConfig() error {
BasicAuth: bk.BasicAuth,
Endpoints: bk.Endpoints,
BackendRules: path.Backend.BackendRule,
RewriteRules: path.Backend.RewriteRule,
RewriteRules: c.rewriteTarget(path.Path, path.Backend.RewriteRule),
HeaderRules: path.Backend.HeaderRule,
Sticky: bk.Sticky,
StickyCookieName: bk.StickyCookieName,
Expand Down