Allow a 'location-modifier' annotation for regex paths
In nginx a modifier can be passed which decides how locations will be parsed, i.e. regex or no regex. This change allows a location modifier to be passed as an annotatione e.g. ingress.kubernetes.io/location-modifier: "~" If no location-modifier is passed but 'rewrite-target' is provided, then a default of "*~" is assumed.
This commit is contained in:
parent
b68feb9c59
commit
7d90d9f68d
2 changed files with 46 additions and 8 deletions
|
@ -24,18 +24,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
rewriteTo = "ingress.kubernetes.io/rewrite-target"
|
rewriteTo = "ingress.kubernetes.io/rewrite-target"
|
||||||
addBaseURL = "ingress.kubernetes.io/add-base-url"
|
locationModifier = "ingress.kubernetes.io/location-modifier"
|
||||||
baseURLScheme = "ingress.kubernetes.io/base-url-scheme"
|
defaultRewriteLocationModifier = "~*"
|
||||||
sslRedirect = "ingress.kubernetes.io/ssl-redirect"
|
addBaseURL = "ingress.kubernetes.io/add-base-url"
|
||||||
forceSSLRedirect = "ingress.kubernetes.io/force-ssl-redirect"
|
baseURLScheme = "ingress.kubernetes.io/base-url-scheme"
|
||||||
appRoot = "ingress.kubernetes.io/app-root"
|
sslRedirect = "ingress.kubernetes.io/ssl-redirect"
|
||||||
|
forceSSLRedirect = "ingress.kubernetes.io/force-ssl-redirect"
|
||||||
|
appRoot = "ingress.kubernetes.io/app-root"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Redirect describes the per location redirect config
|
// Redirect describes the per location redirect config
|
||||||
type Redirect struct {
|
type Redirect struct {
|
||||||
// Target URI where the traffic must be redirected
|
// Target URI where the traffic must be redirected
|
||||||
Target string `json:"target"`
|
Target string `json:"target"`
|
||||||
|
// Location modifier
|
||||||
|
LocationModifier string `json:"locationModifier"`
|
||||||
// AddBaseURL indicates if is required to add a base tag in the head
|
// AddBaseURL indicates if is required to add a base tag in the head
|
||||||
// of the responses from the upstream servers
|
// of the responses from the upstream servers
|
||||||
AddBaseURL bool `json:"addBaseUrl"`
|
AddBaseURL bool `json:"addBaseUrl"`
|
||||||
|
@ -92,6 +96,11 @@ func NewParser(br resolver.DefaultBackend) parser.IngressAnnotation {
|
||||||
// rule used to rewrite the defined paths
|
// rule used to rewrite the defined paths
|
||||||
func (a rewrite) Parse(ing *extensions.Ingress) (interface{}, error) {
|
func (a rewrite) Parse(ing *extensions.Ingress) (interface{}, error) {
|
||||||
rt, _ := parser.GetStringAnnotation(rewriteTo, ing)
|
rt, _ := parser.GetStringAnnotation(rewriteTo, ing)
|
||||||
|
locMod, _ := parser.GetStringAnnotation(locationModifier, ing)
|
||||||
|
|
||||||
|
if rt != "" && locMod == "" {
|
||||||
|
locMod = defaultRewriteLocationModifier
|
||||||
|
}
|
||||||
sslRe, err := parser.GetBoolAnnotation(sslRedirect, ing)
|
sslRe, err := parser.GetBoolAnnotation(sslRedirect, ing)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sslRe = a.backendResolver.GetDefaultBackend().SSLRedirect
|
sslRe = a.backendResolver.GetDefaultBackend().SSLRedirect
|
||||||
|
@ -103,12 +112,16 @@ func (a rewrite) Parse(ing *extensions.Ingress) (interface{}, error) {
|
||||||
abu, _ := parser.GetBoolAnnotation(addBaseURL, ing)
|
abu, _ := parser.GetBoolAnnotation(addBaseURL, ing)
|
||||||
bus, _ := parser.GetStringAnnotation(baseURLScheme, ing)
|
bus, _ := parser.GetStringAnnotation(baseURLScheme, ing)
|
||||||
ar, _ := parser.GetStringAnnotation(appRoot, ing)
|
ar, _ := parser.GetStringAnnotation(appRoot, ing)
|
||||||
return &Redirect{
|
|
||||||
|
redirect := &Redirect{
|
||||||
Target: rt,
|
Target: rt,
|
||||||
|
LocationModifier: locMod,
|
||||||
AddBaseURL: abu,
|
AddBaseURL: abu,
|
||||||
BaseURLScheme: bus,
|
BaseURLScheme: bus,
|
||||||
SSLRedirect: sslRe,
|
SSLRedirect: sslRe,
|
||||||
ForceSSLRedirect: fSslRe,
|
ForceSSLRedirect: fSslRe,
|
||||||
AppRoot: ar,
|
AppRoot: ar,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
return redirect, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,6 +100,31 @@ func TestRedirect(t *testing.T) {
|
||||||
if redirect.Target != defRoute {
|
if redirect.Target != defRoute {
|
||||||
t.Errorf("Expected %v as redirect but returned %s", defRoute, redirect.Target)
|
t.Errorf("Expected %v as redirect but returned %s", defRoute, redirect.Target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if redirect.LocationModifier != defaultRewriteLocationModifier {
|
||||||
|
t.Errorf("Expected %v as implicit location modifier but returned %s", defaultRewriteLocationModifier, redirect.LocationModifier)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegex(t *testing.T) {
|
||||||
|
ing := buildIngress()
|
||||||
|
|
||||||
|
data := map[string]string{}
|
||||||
|
modifier := "~"
|
||||||
|
data[locationModifier] = modifier
|
||||||
|
ing.SetAnnotations(data)
|
||||||
|
|
||||||
|
i, err := NewParser(mockBackend{}).Parse(ing)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error with ingress: %v", err)
|
||||||
|
}
|
||||||
|
redirect, ok := i.(*Redirect)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("expected a Redirect type")
|
||||||
|
}
|
||||||
|
if redirect.LocationModifier != modifier {
|
||||||
|
t.Errorf("Expected %v as location modifier but returned %s", modifier, redirect.LocationModifier)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSSLRedirect(t *testing.T) {
|
func TestSSLRedirect(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue