Add option to append a base tag in the head

This commit is contained in:
Manuel de Brito Fontes 2016-05-27 11:58:13 -03:00
parent 95e85b57e3
commit f8ea58882b
7 changed files with 190 additions and 101 deletions

View file

@ -17,68 +17,57 @@ limitations under the License.
package rewrite
import (
"errors"
"strconv"
"k8s.io/kubernetes/pkg/apis/extensions"
)
const (
rewrite = "ingress-nginx.kubernetes.io/rewrite-to"
fixUrls = "ingress-nginx.kubernetes.io/fix-urls"
rewriteTo = "ingress.kubernetes.io/rewrite-target"
addBaseURL = "ingress.kubernetes.io/add-base-url"
)
// ErrMissingAnnotations is returned when the ingress rule
// does not contains annotations related with redirect or strip prefix
type ErrMissingAnnotations struct {
msg string
}
func (e ErrMissingAnnotations) Error() string {
return e.msg
}
// Redirect returns authentication configuration for an Ingress rule
type Redirect struct {
// To URI where the traffic must be redirected
To string
// Rewrite indicates if is required to change the
// links in the response from the upstream servers
Rewrite bool
// Target URI where the traffic must be redirected
Target string
// AddBaseURL indicates if is required to add a base tag in the head
// of the responses from the upstream servers
AddBaseURL bool
}
type ingAnnotations map[string]string
func (a ingAnnotations) rewrite() string {
val, ok := a[rewrite]
if ok {
return val
}
return ""
}
func (a ingAnnotations) fixUrls() bool {
val, ok := a[fixUrls]
func (a ingAnnotations) addBaseURL() bool {
val, ok := a[addBaseURL]
if ok {
if b, err := strconv.ParseBool(val); err == nil {
return b
}
}
return false
}
func (a ingAnnotations) rewriteTo() string {
val, ok := a[rewriteTo]
if ok {
return val
}
return ""
}
// ParseAnnotations parses the annotations contained in the ingress
// rule used to rewrite the defined paths
func ParseAnnotations(ing *extensions.Ingress) (*Redirect, error) {
if ing.GetAnnotations() == nil {
return &Redirect{}, ErrMissingAnnotations{"no annotations present"}
return &Redirect{}, errors.New("no annotations present")
}
rt := ingAnnotations(ing.GetAnnotations()).rewrite()
rw := ingAnnotations(ing.GetAnnotations()).fixUrls()
rt := ingAnnotations(ing.GetAnnotations()).rewriteTo()
abu := ingAnnotations(ing.GetAnnotations()).addBaseURL()
return &Redirect{
To: rt,
Rewrite: rw,
Target: rt,
AddBaseURL: abu,
}, nil
}

View file

@ -66,29 +66,29 @@ func buildIngress() *extensions.Ingress {
func TestAnnotations(t *testing.T) {
ing := buildIngress()
r := ingAnnotations(ing.GetAnnotations()).rewrite()
r := ingAnnotations(ing.GetAnnotations()).rewriteTo()
if r != "" {
t.Error("Expected no redirect")
}
f := ingAnnotations(ing.GetAnnotations()).fixUrls()
f := ingAnnotations(ing.GetAnnotations()).addBaseURL()
if f != false {
t.Error("Expected false as fix-urls but %v was returend", f)
t.Error("Expected false in add-base-url but %v was returend", f)
}
data := map[string]string{}
data[rewrite] = defRoute
data[fixUrls] = "true"
data[rewriteTo] = defRoute
data[addBaseURL] = "true"
ing.SetAnnotations(data)
r = ingAnnotations(ing.GetAnnotations()).rewrite()
r = ingAnnotations(ing.GetAnnotations()).rewriteTo()
if r != defRoute {
t.Error("Expected %v as rewrite but %v was returend", defRoute, r)
t.Error("Expected %v in rewrite but %v was returend", defRoute, r)
}
f = ingAnnotations(ing.GetAnnotations()).fixUrls()
f = ingAnnotations(ing.GetAnnotations()).addBaseURL()
if f != true {
t.Error("Expected true as fix-urls but %v was returend", f)
t.Error("Expected true in add-base-url but %v was returend", f)
}
}
@ -104,7 +104,7 @@ func TestRedirect(t *testing.T) {
ing := buildIngress()
data := map[string]string{}
data[rewrite] = defRoute
data[rewriteTo] = defRoute
ing.SetAnnotations(data)
redirect, err := ParseAnnotations(ing)
@ -112,7 +112,7 @@ func TestRedirect(t *testing.T) {
t.Errorf("Uxpected error with ingress: %v", err)
}
if redirect.To != defRoute {
t.Errorf("Expected %v as redirect but returned %s", defRoute, redirect.To)
if redirect.Target != defRoute {
t.Errorf("Expected %v as redirect but returned %s", defRoute, redirect.Target)
}
}

View file

@ -109,6 +109,8 @@ func toCamelCase(src string) string {
return string(bytes.Join(chunks, nil))
}
// buildLocation produces the location string, if the ingress has redirects
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
func buildLocation(input interface{}) string {
location, ok := input.(*Location)
if !ok {
@ -116,16 +118,17 @@ func buildLocation(input interface{}) string {
}
path := location.Path
if len(location.Redirect.To) > 0 && location.Redirect.To != path {
// if path != slash && !strings.HasSuffix(path, slash) {
// path = fmt.Sprintf("%s/", path)
// }
if len(location.Redirect.Target) > 0 && location.Redirect.Target != path {
return fmt.Sprintf("~* %s", path)
}
return path
}
// buildProxyPass produces the proxy pass string, if the ingress has redirects
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
// If the annotation ingress.kubernetes.io/add-base-url:"true" is specified it will
// add a base tag in the head of the response from the service
func buildProxyPass(input interface{}) string {
location, ok := input.(*Location)
if !ok {
@ -134,34 +137,46 @@ func buildProxyPass(input interface{}) string {
path := location.Path
if path == location.Redirect.To {
return fmt.Sprintf("proxy_pass http://%s;", location.Upstream.Name)
// defProxyPass returns the default proxy_pass, just the name of the upstream
defProxyPass := fmt.Sprintf("proxy_pass http://%s;", location.Upstream.Name)
// if the path in the ingress rule is equals to the target: no special rewrite
if path == location.Redirect.Target {
return defProxyPass
}
if path != slash && !strings.HasSuffix(path, slash) {
path = fmt.Sprintf("%s/", path)
}
if len(location.Redirect.To) > 0 {
rc := ""
if location.Redirect.Rewrite {
rc = fmt.Sprintf(`sub_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name%v">';
sub_filter_once off;`, location.Path)
if len(location.Redirect.Target) > 0 {
abu := ""
if location.Redirect.AddBaseURL {
bPath := location.Redirect.Target
if !strings.HasSuffix(bPath, slash) {
bPath = fmt.Sprintf("%s/", bPath)
}
abu = fmt.Sprintf(`subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name%v">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$server_name%v">' r;
`, bPath, bPath)
}
if location.Redirect.To == slash {
if location.Redirect.Target == slash {
// special case redirect to /
// ie /something to /
return fmt.Sprintf(`rewrite %s(.*) /$1 break;
proxy_pass http://%s;
%v`, path, location.Upstream.Name, rc)
return fmt.Sprintf(`
rewrite %s / break;
rewrite %s(.*) /$1 break;
proxy_pass http://%s;
%v`, location.Path, path, location.Upstream.Name, abu)
}
return fmt.Sprintf(`rewrite %s(.*) %s/$1 break;
proxy_pass http://%s;
%v`, path, location.Redirect.To, location.Upstream.Name, rc)
return fmt.Sprintf(`
rewrite %s(.*) %s/$1 break;
proxy_pass http://%s;
%v`, path, location.Redirect.Target, location.Upstream.Name, abu)
}
// default proxy_pass
return fmt.Sprintf("proxy_pass http://%s;", location.Upstream.Name)
return defProxyPass
}

View file

@ -17,6 +17,7 @@ limitations under the License.
package nginx
import (
"strings"
"testing"
"k8s.io/contrib/ingress/controllers/nginx/nginx/rewrite"
@ -24,36 +25,46 @@ import (
var (
tmplFuncTestcases = map[string]struct {
Path string
To string
Location string
ProxyPass string
Rewrite bool
Path string
Target string
Location string
ProxyPass string
AddBaseURL bool
}{
"invalid redirect / to /": {"/", "/", "/", "proxy_pass http://upstream-name;", false},
"redirect / to /jenkins": {"/", "/jenkins", "~* /",
`rewrite /(.*) /jenkins/$1 break;
proxy_pass http://upstream-name;
`, false},
"redirect /something to /": {"/something", "/", "~* /something/", `rewrite /something/(.*) /$1 break;
proxy_pass http://upstream-name;
`, false},
"redirect /something-complex to /not-root": {"/something-complex", "/not-root", "~* /something-complex/", `rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name;
`, false},
"redirect / to /jenkins and rewrite": {"/", "/jenkins", "~* /",
`rewrite /(.*) /jenkins/$1 break;
proxy_pass http://upstream-name;
sub_filter "//$host/" "//$host/jenkins";
sub_filter_once off;`, true},
"redirect /something to / and rewrite": {"/something", "/", "~* /something/", `rewrite /something/(.*) /$1 break;
proxy_pass http://upstream-name;
sub_filter "//$host/something" "//$host/";
sub_filter_once off;`, true},
"redirect /something-complex to /not-root and rewrite": {"/something-complex", "/not-root", "~* /something-complex/", `rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name;
sub_filter "//$host/something-complex" "//$host/not-root";
sub_filter_once off;`, true},
`
rewrite /(.*) /jenkins/$1 break;
proxy_pass http://upstream-name;
`, false},
"redirect /something to /": {"/something", "/", "~* /something", `
rewrite /something / break;
rewrite /something/(.*) /$1 break;
proxy_pass http://upstream-name;
`, false},
"redirect /something-complex to /not-root": {"/something-complex", "/not-root", "~* /something-complex", `
rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name;
`, false},
"redirect / to /jenkins and rewrite": {"/", "/jenkins", "~* /", `
rewrite /(.*) /jenkins/$1 break;
proxy_pass http://upstream-name;
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/jenkins/">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$server_name/jenkins/">' r;
`, true},
"redirect /something to / and rewrite": {"/something", "/", "~* /something", `
rewrite /something / break;
rewrite /something/(.*) /$1 break;
proxy_pass http://upstream-name;
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$server_name/">' r;
`, true},
"redirect /something-complex to /not-root and rewrite": {"/something-complex", "/not-root", "~* /something-complex", `
rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name;
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$server_name/not-root/">' r;
subs_filter '<HEAD(.*)>' '<HEAD$1><base href="$scheme://$server_name/not-root/">' r;
`, true},
}
)
@ -61,12 +72,12 @@ func TestBuildLocation(t *testing.T) {
for k, tc := range tmplFuncTestcases {
loc := &Location{
Path: tc.Path,
Redirect: rewrite.Redirect{tc.To, tc.Rewrite},
Redirect: rewrite.Redirect{tc.Target, tc.AddBaseURL},
}
newLoc := buildLocation(loc)
if tc.Location != newLoc {
t.Errorf("%s: expected %v but returned %v", k, tc.Location, newLoc)
t.Errorf("%s: expected '%v' but returned %v", k, tc.Location, newLoc)
}
}
}
@ -75,13 +86,13 @@ func TestBuildProxyPass(t *testing.T) {
for k, tc := range tmplFuncTestcases {
loc := &Location{
Path: tc.Path,
Redirect: rewrite.Redirect{tc.To, tc.Rewrite},
Redirect: rewrite.Redirect{tc.Target, tc.AddBaseURL},
Upstream: Upstream{Name: "upstream-name"},
}
pp := buildProxyPass(loc)
if tc.ProxyPass != pp {
t.Errorf("%s: expected \n%v \nbut returned \n%v", k, tc.ProxyPass, pp)
if !strings.EqualFold(tc.ProxyPass, pp) {
t.Errorf("%s: expected \n'%v'\nbut returned \n'%v'", k, tc.ProxyPass, pp)
}
}
}