2016-02-22 00:13:08 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-02-22 00:13:08 +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.
|
|
|
|
*/
|
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
package template
|
2016-02-22 00:13:08 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-15 15:31:39 +00:00
|
|
|
"bytes"
|
2016-08-19 14:51:40 +00:00
|
|
|
"encoding/base64"
|
2016-02-22 00:13:08 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-12-22 03:00:27 +00:00
|
|
|
"net"
|
2016-11-10 22:56:29 +00:00
|
|
|
"os/exec"
|
2016-05-25 21:04:34 +00:00
|
|
|
"strings"
|
2016-08-07 22:53:08 +00:00
|
|
|
text_template "text/template"
|
2016-02-22 00:13:08 +00:00
|
|
|
|
2017-01-24 08:19:28 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
|
|
|
|
2016-02-22 00:13:08 +00:00
|
|
|
"github.com/golang/glog"
|
2016-06-05 13:36:00 +00:00
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
"k8s.io/ingress/controllers/nginx/pkg/config"
|
2017-02-27 10:00:31 +00:00
|
|
|
nginxconfig "k8s.io/ingress/controllers/nginx/pkg/config"
|
2016-11-10 22:56:29 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress"
|
2016-12-22 03:00:27 +00:00
|
|
|
ing_net "k8s.io/ingress/core/pkg/net"
|
2016-11-10 22:56:29 +00:00
|
|
|
"k8s.io/ingress/core/pkg/watch"
|
2016-02-22 00:13:08 +00:00
|
|
|
)
|
|
|
|
|
2016-05-25 21:04:34 +00:00
|
|
|
const (
|
2016-11-10 22:56:29 +00:00
|
|
|
slash = "/"
|
|
|
|
defBufferSize = 65535
|
2016-03-22 18:01:04 +00:00
|
|
|
)
|
2016-02-22 00:13:08 +00:00
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
// Template ...
|
|
|
|
type Template struct {
|
2016-11-10 22:56:29 +00:00
|
|
|
tmpl *text_template.Template
|
|
|
|
fw watch.FileWatcher
|
|
|
|
s int
|
|
|
|
tmplBuf *bytes.Buffer
|
|
|
|
outCmdBuf *bytes.Buffer
|
2016-08-07 22:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//NewTemplate returns a new Template instance or an
|
|
|
|
//error if the specified template file contains errors
|
|
|
|
func NewTemplate(file string, onChange func()) (*Template, error) {
|
|
|
|
tmpl, err := text_template.New("nginx.tmpl").Funcs(funcMap).ParseFiles(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
fw, err := watch.NewFileWatcher(file, onChange)
|
2016-07-28 21:35:36 +00:00
|
|
|
if err != nil {
|
2016-08-07 22:53:08 +00:00
|
|
|
return nil, err
|
2016-07-28 21:35:36 +00:00
|
|
|
}
|
2016-08-07 22:53:08 +00:00
|
|
|
|
|
|
|
return &Template{
|
2016-11-10 22:56:29 +00:00
|
|
|
tmpl: tmpl,
|
|
|
|
fw: fw,
|
|
|
|
s: defBufferSize,
|
|
|
|
tmplBuf: bytes.NewBuffer(make([]byte, 0, defBufferSize)),
|
|
|
|
outCmdBuf: bytes.NewBuffer(make([]byte, 0, defBufferSize)),
|
2016-08-07 22:53:08 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close removes the file watcher
|
|
|
|
func (t *Template) Close() {
|
2016-11-10 22:56:29 +00:00
|
|
|
t.fw.Close()
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
// Write populates a buffer using a template with NGINX configuration
|
|
|
|
// and the servers and upstreams created by Ingress rules
|
2017-02-20 02:34:05 +00:00
|
|
|
func (t *Template) Write(conf config.TemplateConfig) ([]byte, error) {
|
2016-11-10 22:56:29 +00:00
|
|
|
defer t.tmplBuf.Reset()
|
|
|
|
defer t.outCmdBuf.Reset()
|
2016-02-22 00:13:08 +00:00
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
defer func() {
|
|
|
|
if t.s < t.tmplBuf.Cap() {
|
|
|
|
glog.V(2).Infof("adjusting template buffer size from %v to %v", t.s, t.tmplBuf.Cap())
|
|
|
|
t.s = t.tmplBuf.Cap()
|
|
|
|
t.tmplBuf = bytes.NewBuffer(make([]byte, 0, t.tmplBuf.Cap()))
|
|
|
|
t.outCmdBuf = bytes.NewBuffer(make([]byte, 0, t.outCmdBuf.Cap()))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-03-15 15:31:39 +00:00
|
|
|
if glog.V(3) {
|
2016-02-22 00:13:08 +00:00
|
|
|
b, err := json.Marshal(conf)
|
|
|
|
if err != nil {
|
2016-08-07 22:53:08 +00:00
|
|
|
glog.Errorf("unexpected error: %v", err)
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-03-19 20:17:58 +00:00
|
|
|
glog.Infof("NGINX configuration: %v", string(b))
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
err := t.tmpl.Execute(t.tmplBuf, conf)
|
2016-11-16 18:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// squeezes multiple adjacent empty lines to be single
|
|
|
|
// spaced this is to avoid the use of regular expressions
|
|
|
|
cmd := exec.Command("/ingress-controller/clean-nginx-conf.sh")
|
|
|
|
cmd.Stdin = t.tmplBuf
|
|
|
|
cmd.Stdout = t.outCmdBuf
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
glog.Warningf("unexpected error cleaning template: %v", err)
|
|
|
|
return t.tmplBuf.Bytes(), nil
|
2016-09-22 17:08:35 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 02:34:05 +00:00
|
|
|
return t.outCmdBuf.Bytes(), nil
|
2016-02-22 00:13:08 +00:00
|
|
|
}
|
2016-03-22 18:01:04 +00:00
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
var (
|
|
|
|
funcMap = text_template.FuncMap{
|
|
|
|
"empty": func(input interface{}) bool {
|
|
|
|
check, ok := input.(string)
|
|
|
|
if ok {
|
|
|
|
return len(check) == 0
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
2017-02-24 11:05:28 +00:00
|
|
|
"buildLocation": buildLocation,
|
|
|
|
"buildAuthLocation": buildAuthLocation,
|
|
|
|
"buildProxyPass": buildProxyPass,
|
|
|
|
"buildRateLimitZones": buildRateLimitZones,
|
|
|
|
"buildRateLimit": buildRateLimit,
|
2017-02-20 02:30:37 +00:00
|
|
|
"buildSSLPassthroughUpstreams": buildSSLPassthroughUpstreams,
|
2017-02-24 11:05:28 +00:00
|
|
|
"buildResolvers": buildResolvers,
|
|
|
|
"isLocationAllowed": isLocationAllowed,
|
2017-02-27 10:00:31 +00:00
|
|
|
"buildLogFormatUpstream": buildLogFormatUpstream,
|
|
|
|
"contains": strings.Contains,
|
|
|
|
"hasPrefix": strings.HasPrefix,
|
|
|
|
"hasSuffix": strings.HasSuffix,
|
|
|
|
"toUpper": strings.ToUpper,
|
|
|
|
"toLower": strings.ToLower,
|
2016-03-22 18:01:04 +00:00
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
)
|
2016-03-22 18:01:04 +00:00
|
|
|
|
2016-12-22 03:00:27 +00:00
|
|
|
// buildResolvers returns the resolvers reading the /etc/resolv.conf file
|
|
|
|
func buildResolvers(a interface{}) string {
|
|
|
|
// NGINX need IPV6 addresses to be surrounded by brakets
|
|
|
|
nss := a.([]net.IP)
|
|
|
|
if len(nss) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
r := []string{"resolver"}
|
|
|
|
for _, ns := range nss {
|
|
|
|
if ing_net.IsIPV6(ns) {
|
|
|
|
r = append(r, fmt.Sprintf("[%v]", ns))
|
|
|
|
} else {
|
|
|
|
r = append(r, fmt.Sprintf("%v", ns))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r = append(r, "valid=30s;")
|
|
|
|
|
|
|
|
return strings.Join(r, " ")
|
|
|
|
}
|
|
|
|
|
2017-02-20 02:30:37 +00:00
|
|
|
func buildSSLPassthroughUpstreams(b interface{}, sslb interface{}) string {
|
2016-11-16 18:24:26 +00:00
|
|
|
backends := b.([]*ingress.Backend)
|
|
|
|
sslBackends := sslb.([]*ingress.SSLPassthroughBackend)
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, 10))
|
|
|
|
|
|
|
|
// multiple services can use the same upstream.
|
|
|
|
// avoid duplications using a map[name]=true
|
|
|
|
u := make(map[string]bool)
|
|
|
|
for _, passthrough := range sslBackends {
|
|
|
|
if u[passthrough.Backend] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
u[passthrough.Backend] = true
|
|
|
|
fmt.Fprintf(buf, "upstream %v {\n", passthrough.Backend)
|
|
|
|
for _, backend := range backends {
|
|
|
|
if backend.Name == passthrough.Backend {
|
|
|
|
for _, server := range backend.Endpoints {
|
|
|
|
fmt.Fprintf(buf, "\t\tserver %v:%v;\n", server.Address, server.Port)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprint(buf, "\t}\n\n")
|
2016-03-22 18:01:04 +00:00
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
return buf.String()
|
2016-03-22 18:01:04 +00:00
|
|
|
}
|
2016-05-25 21:04:34 +00:00
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
// buildLocation produces the location string, if the ingress has redirects
|
|
|
|
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
|
2016-05-25 21:04:34 +00:00
|
|
|
func buildLocation(input interface{}) string {
|
2016-08-07 22:53:08 +00:00
|
|
|
location, ok := input.(*ingress.Location)
|
2016-05-25 21:04:34 +00:00
|
|
|
if !ok {
|
|
|
|
return slash
|
|
|
|
}
|
|
|
|
|
|
|
|
path := location.Path
|
2016-05-27 14:58:13 +00:00
|
|
|
if len(location.Redirect.Target) > 0 && location.Redirect.Target != path {
|
2017-02-17 13:38:52 +00:00
|
|
|
if path == "/" {
|
|
|
|
return fmt.Sprintf("~* %s", path)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("~* ^%s", path)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2016-08-19 14:51:40 +00:00
|
|
|
func buildAuthLocation(input interface{}) string {
|
|
|
|
location, ok := input.(*ingress.Location)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
if location.ExternalAuth.URL == "" {
|
2016-08-19 14:51:40 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
str := base64.URLEncoding.EncodeToString([]byte(location.Path))
|
|
|
|
// avoid locations containing the = char
|
|
|
|
str = strings.Replace(str, "=", "", -1)
|
|
|
|
return fmt.Sprintf("/_external-auth-%v", str)
|
|
|
|
}
|
|
|
|
|
2017-02-27 10:00:31 +00:00
|
|
|
func buildLogFormatUpstream(input interface{}) string {
|
|
|
|
config, ok := input.(config.Configuration)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
glog.Errorf("error an ingress.buildLogFormatUpstream type but %T was returned", input)
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:02:36 +00:00
|
|
|
return nginxconfig.BuildLogFormatUpstream(config.UseProxyProtocol, config.LogFormatUpstream)
|
2017-02-27 10:00:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
// 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
|
2016-11-16 18:24:26 +00:00
|
|
|
func buildProxyPass(b interface{}, loc interface{}) string {
|
|
|
|
backends := b.([]*ingress.Backend)
|
|
|
|
location, ok := loc.(*ingress.Location)
|
2016-05-25 21:04:34 +00:00
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
path := location.Path
|
2016-06-01 18:47:37 +00:00
|
|
|
proto := "http"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
|
|
|
for _, backend := range backends {
|
|
|
|
if backend.Name == location.Backend {
|
|
|
|
if backend.Secure {
|
|
|
|
proto = "https"
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2016-06-01 18:47:37 +00:00
|
|
|
}
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
// defProxyPass returns the default proxy_pass, just the name of the upstream
|
2016-11-16 18:24:26 +00:00
|
|
|
defProxyPass := fmt.Sprintf("proxy_pass %s://%s;", proto, location.Backend)
|
2016-05-27 14:58:13 +00:00
|
|
|
// if the path in the ingress rule is equals to the target: no special rewrite
|
|
|
|
if path == location.Redirect.Target {
|
|
|
|
return defProxyPass
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if path != slash && !strings.HasSuffix(path, slash) {
|
|
|
|
path = fmt.Sprintf("%s/", path)
|
|
|
|
}
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
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)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
if location.Redirect.Target == slash {
|
2016-05-25 21:04:34 +00:00
|
|
|
// special case redirect to /
|
|
|
|
// ie /something to /
|
2016-05-27 14:58:13 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
rewrite %s(.*) /$1 break;
|
2016-06-04 18:06:18 +00:00
|
|
|
rewrite %s / break;
|
2016-06-01 18:47:37 +00:00
|
|
|
proxy_pass %s://%s;
|
2016-11-16 18:24:26 +00:00
|
|
|
%v`, path, location.Path, proto, location.Backend, abu)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 14:58:13 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
rewrite %s(.*) %s/$1 break;
|
2016-06-01 18:47:37 +00:00
|
|
|
proxy_pass %s://%s;
|
2016-11-16 18:24:26 +00:00
|
|
|
%v`, path, location.Redirect.Target, proto, location.Backend, abu)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// default proxy_pass
|
2016-05-27 14:58:13 +00:00
|
|
|
return defProxyPass
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
2016-05-30 17:39:10 +00:00
|
|
|
|
|
|
|
// buildRateLimitZones produces an array of limit_conn_zone in order to allow
|
|
|
|
// rate limiting of request. Each Ingress rule could have up to two zones, one
|
|
|
|
// for connection limit by IP address and other for limiting request per second
|
|
|
|
func buildRateLimitZones(input interface{}) []string {
|
2017-01-24 08:19:28 +00:00
|
|
|
zones := sets.String{}
|
2016-05-30 17:39:10 +00:00
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
servers, ok := input.([]*ingress.Server)
|
2016-05-30 17:39:10 +00:00
|
|
|
if !ok {
|
2017-01-24 08:19:28 +00:00
|
|
|
return zones.List()
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, server := range servers {
|
|
|
|
for _, loc := range server.Locations {
|
|
|
|
|
2016-06-01 14:39:12 +00:00
|
|
|
if loc.RateLimit.Connections.Limit > 0 {
|
|
|
|
zone := fmt.Sprintf("limit_conn_zone $binary_remote_addr zone=%v:%vm;",
|
2016-09-22 17:08:35 +00:00
|
|
|
loc.RateLimit.Connections.Name,
|
|
|
|
loc.RateLimit.Connections.SharedSize)
|
2017-01-24 08:19:28 +00:00
|
|
|
if !zones.Has(zone) {
|
|
|
|
zones.Insert(zone)
|
|
|
|
}
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 14:39:12 +00:00
|
|
|
if loc.RateLimit.RPS.Limit > 0 {
|
2017-01-23 02:01:51 +00:00
|
|
|
zone := fmt.Sprintf("limit_req_zone $binary_remote_addr zone=%v:%vm rate=%vr/s;",
|
|
|
|
loc.RateLimit.RPS.Name,
|
|
|
|
loc.RateLimit.RPS.SharedSize,
|
|
|
|
loc.RateLimit.RPS.Limit)
|
2017-01-24 08:19:28 +00:00
|
|
|
if !zones.Has(zone) {
|
|
|
|
zones.Insert(zone)
|
|
|
|
}
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 08:19:28 +00:00
|
|
|
return zones.List()
|
2016-05-30 17:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// buildRateLimit produces an array of limit_req to be used inside the Path of
|
|
|
|
// Ingress rules. The order: connections by IP first and RPS next.
|
|
|
|
func buildRateLimit(input interface{}) []string {
|
|
|
|
limits := []string{}
|
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
loc, ok := input.(*ingress.Location)
|
2016-05-30 17:39:10 +00:00
|
|
|
if !ok {
|
|
|
|
return limits
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:39:12 +00:00
|
|
|
if loc.RateLimit.Connections.Limit > 0 {
|
2016-05-30 17:39:10 +00:00
|
|
|
limit := fmt.Sprintf("limit_conn %v %v;",
|
|
|
|
loc.RateLimit.Connections.Name, loc.RateLimit.Connections.Limit)
|
|
|
|
limits = append(limits, limit)
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:39:12 +00:00
|
|
|
if loc.RateLimit.RPS.Limit > 0 {
|
2016-05-30 17:39:10 +00:00
|
|
|
limit := fmt.Sprintf("limit_req zone=%v burst=%v nodelay;",
|
2017-01-23 02:01:51 +00:00
|
|
|
loc.RateLimit.RPS.Name, loc.RateLimit.RPS.Burst)
|
2016-05-30 17:39:10 +00:00
|
|
|
limits = append(limits, limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
return limits
|
|
|
|
}
|
2016-12-29 20:02:06 +00:00
|
|
|
|
|
|
|
func isLocationAllowed(input interface{}) bool {
|
|
|
|
loc, ok := input.(*ingress.Location)
|
|
|
|
if !ok {
|
|
|
|
glog.Errorf("expected an ingress.Location type but %T was returned", input)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return loc.Denied == nil
|
|
|
|
}
|