ingress-nginx-helm/internal/ingress/annotations/rewrite/main.go

107 lines
3.1 KiB
Go
Raw Normal View History

2016-05-25 21:04:34 +00:00
/*
Copyright 2016 The Kubernetes Authors.
2016-05-25 21:04:34 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rewrite
import (
2017-07-16 19:19:59 +00:00
extensions "k8s.io/api/extensions/v1beta1"
2016-11-16 18:24:26 +00:00
2017-11-07 22:02:12 +00:00
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
"k8s.io/ingress-nginx/internal/ingress/resolver"
2016-05-25 21:04:34 +00:00
)
2017-11-07 16:36:51 +00:00
// Config describes the per location redirect config
type Config struct {
// Target URI where the traffic must be redirected
2016-11-16 18:24:26 +00:00
Target string `json:"target"`
// AddBaseURL indicates if is required to add a base tag in the head
// of the responses from the upstream servers
2016-11-16 18:24:26 +00:00
AddBaseURL bool `json:"addBaseUrl"`
// BaseURLScheme override for the scheme passed to the base tag
BaseURLScheme string `json:"baseUrlScheme"`
// SSLRedirect indicates if the location section is accessible SSL only
2016-11-16 18:24:26 +00:00
SSLRedirect bool `json:"sslRedirect"`
// ForceSSLRedirect indicates if the location section is accessible SSL only
ForceSSLRedirect bool `json:"forceSSLRedirect"`
// AppRoot defines the Application Root that the Controller must redirect if it's not in '/' context
AppRoot string `json:"appRoot"`
2016-05-25 21:04:34 +00:00
}
2017-07-27 03:07:59 +00:00
// Equal tests for equality between two Redirect types
2017-11-07 16:36:51 +00:00
func (r1 *Config) Equal(r2 *Config) bool {
if r1 == r2 {
return true
}
if r1 == nil || r2 == nil {
return false
}
if r1.Target != r2.Target {
return false
}
if r1.AddBaseURL != r2.AddBaseURL {
return false
}
if r1.BaseURLScheme != r2.BaseURLScheme {
return false
}
if r1.SSLRedirect != r2.SSLRedirect {
return false
}
if r1.ForceSSLRedirect != r2.ForceSSLRedirect {
return false
}
if r1.AppRoot != r2.AppRoot {
return false
}
return true
}
type rewrite struct {
2017-11-08 20:58:57 +00:00
r resolver.Resolver
}
// NewParser creates a new reqrite annotation parser
2017-11-08 20:58:57 +00:00
func NewParser(r resolver.Resolver) parser.IngressAnnotation {
return rewrite{r}
}
2016-05-25 21:04:34 +00:00
// ParseAnnotations parses the annotations contained in the ingress
// rule used to rewrite the defined paths
func (a rewrite) Parse(ing *extensions.Ingress) (interface{}, error) {
2017-11-08 20:58:57 +00:00
rt, _ := parser.GetStringAnnotation("rewrite-target", ing, a.r)
sslRe, err := parser.GetBoolAnnotation("ssl-redirect", ing, a.r)
if err != nil {
2017-11-08 20:58:57 +00:00
sslRe = a.r.GetDefaultBackend().SSLRedirect
}
2017-11-08 20:58:57 +00:00
fSslRe, err := parser.GetBoolAnnotation("force-ssl-redirect", ing, a.r)
if err != nil {
2017-11-08 20:58:57 +00:00
fSslRe = a.r.GetDefaultBackend().ForceSSLRedirect
}
2017-11-08 20:58:57 +00:00
abu, _ := parser.GetBoolAnnotation("add-base-url", ing, a.r)
bus, _ := parser.GetStringAnnotation("base-url-scheme", ing, a.r)
ar, _ := parser.GetStringAnnotation("app-root", ing, a.r)
2017-11-07 16:36:51 +00:00
return &Config{
Target: rt,
AddBaseURL: abu,
BaseURLScheme: bus,
SSLRedirect: sslRe,
ForceSSLRedirect: fSslRe,
AppRoot: ar,
2016-05-25 21:04:34 +00:00
}, nil
}