Skip to content

Commit

Permalink
Support rewrite-target annotation (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha committed Dec 12, 2017
1 parent 62b077e commit f080987
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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

0 comments on commit f080987

Please sign in to comment.