2016-11-10 22:56:29 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-11-29 01:39:17 +00:00
|
|
|
"errors"
|
2016-11-10 22:56:29 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-11-29 01:39:17 +00:00
|
|
|
"net"
|
2016-11-27 00:09:59 +00:00
|
|
|
"net/http"
|
2016-11-10 22:56:29 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2017-03-28 16:39:44 +00:00
|
|
|
"strconv"
|
2017-04-09 23:51:38 +00:00
|
|
|
"strings"
|
2016-11-29 01:39:17 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2016-11-10 22:56:29 +00:00
|
|
|
|
|
|
|
"github.com/golang/glog"
|
2017-08-28 16:06:58 +00:00
|
|
|
"github.com/mitchellh/go-ps"
|
2017-02-14 14:02:23 +00:00
|
|
|
"github.com/spf13/pflag"
|
2017-01-20 22:37:59 +00:00
|
|
|
|
2017-04-09 23:51:38 +00:00
|
|
|
proxyproto "github.com/armon/go-proxyproto"
|
2017-09-28 14:52:12 +00:00
|
|
|
"github.com/ncabatoff/process-exporter/proc"
|
2017-09-17 18:42:31 +00:00
|
|
|
apiv1 "k8s.io/api/core/v1"
|
2017-07-28 22:57:33 +00:00
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
"k8s.io/ingress/controllers/nginx/pkg/config"
|
|
|
|
ngx_template "k8s.io/ingress/controllers/nginx/pkg/template"
|
|
|
|
"k8s.io/ingress/controllers/nginx/pkg/version"
|
2016-11-29 01:39:17 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress"
|
2017-08-28 16:06:58 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress/controller"
|
2016-11-29 01:39:17 +00:00
|
|
|
"k8s.io/ingress/core/pkg/ingress/defaults"
|
2017-04-11 14:47:49 +00:00
|
|
|
"k8s.io/ingress/core/pkg/net/dns"
|
2017-03-08 13:41:55 +00:00
|
|
|
"k8s.io/ingress/core/pkg/net/ssl"
|
2016-11-29 01:39:17 +00:00
|
|
|
)
|
|
|
|
|
2017-03-10 13:01:26 +00:00
|
|
|
type statusModule string
|
|
|
|
|
2016-11-29 01:39:17 +00:00
|
|
|
const (
|
|
|
|
ngxHealthPath = "/healthz"
|
2017-03-10 13:01:26 +00:00
|
|
|
|
|
|
|
defaultStatusModule statusModule = "default"
|
|
|
|
vtsStatusModule statusModule = "vts"
|
2017-08-03 14:51:39 +00:00
|
|
|
|
|
|
|
defUpstreamName = "upstream-default-backend"
|
2016-11-10 22:56:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-03-02 15:50:31 +00:00
|
|
|
tmplPath = "/etc/nginx/template/nginx.tmpl"
|
|
|
|
cfgPath = "/etc/nginx/nginx.conf"
|
2017-09-28 14:52:12 +00:00
|
|
|
nginxBinary = "/usr/sbin/nginx"
|
2017-03-02 15:50:31 +00:00
|
|
|
defIngressClass = "nginx"
|
2016-11-10 22:56:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// newNGINXController creates a new NGINX Ingress controller.
|
|
|
|
// If the environment variable NGINX_BINARY exists it will be used
|
|
|
|
// as source for nginx commands
|
2017-08-28 16:06:58 +00:00
|
|
|
func newNGINXController() *NGINXController {
|
2016-11-10 22:56:29 +00:00
|
|
|
ngx := os.Getenv("NGINX_BINARY")
|
|
|
|
if ngx == "" {
|
2017-09-28 14:52:12 +00:00
|
|
|
ngx = nginxBinary
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
2017-04-11 14:47:49 +00:00
|
|
|
|
|
|
|
h, err := dns.GetSystemNameServers()
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("unexpected error reading system nameservers: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-03-12 15:27:05 +00:00
|
|
|
n := &NGINXController{
|
2017-08-29 19:40:03 +00:00
|
|
|
binary: ngx,
|
2017-09-17 18:42:31 +00:00
|
|
|
configmap: &apiv1.ConfigMap{},
|
2017-08-29 19:40:03 +00:00
|
|
|
isIPV6Enabled: isIPv6Enabled(),
|
|
|
|
resolver: h,
|
|
|
|
ports: &config.ListenPorts{},
|
|
|
|
backendDefaults: config.NewDefault().Backend,
|
2017-04-09 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
var onChange func()
|
|
|
|
onChange = func() {
|
|
|
|
template, err := ngx_template.NewTemplate(tmplPath, onChange)
|
|
|
|
if err != nil {
|
|
|
|
// this error is different from the rest because it must be clear why nginx is not working
|
|
|
|
glog.Errorf(`
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
Error loading new template : %v
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
`, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n.t.Close()
|
|
|
|
n.t = template
|
|
|
|
glog.Info("new NGINX template loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
ngxTpl, err := ngx_template.NewTemplate(tmplPath, onChange)
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("invalid NGINX template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.t = ngxTpl
|
2016-11-29 01:39:17 +00:00
|
|
|
|
2017-08-28 16:06:58 +00:00
|
|
|
return n
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NGINXController ...
|
|
|
|
type NGINXController struct {
|
2017-08-28 16:06:58 +00:00
|
|
|
controller *controller.GenericController
|
|
|
|
t *ngx_template.Template
|
2016-11-10 22:56:29 +00:00
|
|
|
|
2017-09-17 18:42:31 +00:00
|
|
|
configmap *apiv1.ConfigMap
|
2017-01-20 22:37:59 +00:00
|
|
|
|
2017-09-18 23:53:26 +00:00
|
|
|
storeLister *ingress.StoreLister
|
2017-02-07 18:13:08 +00:00
|
|
|
|
2017-04-09 23:51:38 +00:00
|
|
|
binary string
|
|
|
|
resolver []net.IP
|
2017-03-10 13:01:26 +00:00
|
|
|
|
|
|
|
cmdArgs []string
|
|
|
|
|
2017-03-12 15:27:05 +00:00
|
|
|
stats *statsCollector
|
2017-03-10 13:01:26 +00:00
|
|
|
statusModule statusModule
|
2017-04-09 18:03:27 +00:00
|
|
|
|
|
|
|
// returns true if IPV6 is enabled in the pod
|
|
|
|
isIPV6Enabled bool
|
2017-04-11 14:47:49 +00:00
|
|
|
|
2017-04-09 23:51:38 +00:00
|
|
|
// returns true if proxy protocol es enabled
|
|
|
|
isProxyProtocolEnabled bool
|
|
|
|
|
2017-08-21 01:34:31 +00:00
|
|
|
isSSLPassthroughEnabled bool
|
|
|
|
|
2017-08-28 16:06:58 +00:00
|
|
|
isShuttingDown bool
|
|
|
|
|
2017-04-09 23:51:38 +00:00
|
|
|
proxy *proxy
|
2017-08-24 13:33:26 +00:00
|
|
|
|
|
|
|
ports *config.ListenPorts
|
2017-08-26 03:46:17 +00:00
|
|
|
|
|
|
|
backendDefaults defaults.Backend
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
// Start start a new NGINX master process running in foreground.
|
2017-03-12 15:27:05 +00:00
|
|
|
func (n *NGINXController) Start() {
|
2017-08-28 16:06:58 +00:00
|
|
|
n.isShuttingDown = false
|
|
|
|
|
|
|
|
n.controller = controller.NewIngressController(n)
|
|
|
|
go n.controller.Start()
|
2016-11-29 01:39:17 +00:00
|
|
|
|
|
|
|
done := make(chan error, 1)
|
2016-11-10 22:56:29 +00:00
|
|
|
cmd := exec.Command(n.binary, "-c", cfgPath)
|
2017-08-28 16:06:58 +00:00
|
|
|
|
|
|
|
// put nginx in another process group to prevent it
|
|
|
|
// to receive signals meant for the controller
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Setpgid: true,
|
|
|
|
Pgid: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Info("starting NGINX process...")
|
2016-11-29 01:39:17 +00:00
|
|
|
n.start(cmd, done)
|
|
|
|
|
|
|
|
// if the nginx master process dies the workers continue to process requests,
|
|
|
|
// passing checks but in case of updates in ingress no updates will be
|
|
|
|
// reflected in the nginx configuration which can lead to confusion and report
|
|
|
|
// issues because of this behavior.
|
|
|
|
// To avoid this issue we restart nginx in case of errors.
|
|
|
|
for {
|
|
|
|
err := <-done
|
2017-08-28 16:06:58 +00:00
|
|
|
|
|
|
|
if n.isShuttingDown {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-11-29 01:39:17 +00:00
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
|
|
|
waitStatus := exitError.Sys().(syscall.WaitStatus)
|
|
|
|
glog.Warningf(`
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
NGINX master process died (%v): %v
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
`, waitStatus.ExitStatus(), err)
|
|
|
|
}
|
|
|
|
cmd.Process.Release()
|
|
|
|
cmd = exec.Command(n.binary, "-c", cfgPath)
|
|
|
|
// we wait until the workers are killed
|
|
|
|
for {
|
|
|
|
conn, err := net.DialTimeout("tcp", "127.0.0.1:80", 1*time.Second)
|
2017-02-04 07:37:06 +00:00
|
|
|
if err != nil {
|
2016-11-29 01:39:17 +00:00
|
|
|
break
|
|
|
|
}
|
2017-02-04 07:37:06 +00:00
|
|
|
conn.Close()
|
2017-09-28 14:52:12 +00:00
|
|
|
// kill nginx worker processes
|
|
|
|
fs, err := proc.NewFS("/proc")
|
|
|
|
procs, _ := fs.FS.AllProcs()
|
|
|
|
for _, p := range procs {
|
|
|
|
pn, err := p.Comm()
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("unexpected error obtaining process information: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if pn == "nginx" {
|
|
|
|
osp, err := os.FindProcess(p.PID)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("unexpected error obtaining process information: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
osp.Signal(syscall.SIGQUIT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2016-11-29 01:39:17 +00:00
|
|
|
}
|
2017-08-28 16:06:58 +00:00
|
|
|
// restart a new nginx master process if the controller
|
|
|
|
// is not being stopped
|
2016-11-29 01:39:17 +00:00
|
|
|
n.start(cmd, done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 16:06:58 +00:00
|
|
|
// Stop gracefully stops the NGINX master process.
|
|
|
|
func (n *NGINXController) Stop() error {
|
|
|
|
n.isShuttingDown = true
|
|
|
|
n.controller.Stop()
|
|
|
|
|
|
|
|
// Send stop signal to Nginx
|
|
|
|
glog.Info("stopping NGINX process...")
|
|
|
|
cmd := exec.Command(n.binary, "-c", cfgPath, "-s", "quit")
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the Nginx process disappear
|
|
|
|
waitForNginxShutdown()
|
|
|
|
glog.Info("NGINX process has stopped")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-29 01:39:17 +00:00
|
|
|
func (n *NGINXController) start(cmd *exec.Cmd, done chan error) {
|
2016-11-10 22:56:29 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
glog.Fatalf("nginx error: %v", err)
|
2016-11-29 01:39:17 +00:00
|
|
|
done <- err
|
|
|
|
return
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
2017-03-10 13:01:26 +00:00
|
|
|
|
|
|
|
n.cmdArgs = cmd.Args
|
|
|
|
|
2016-11-29 01:39:17 +00:00
|
|
|
go func() {
|
|
|
|
done <- cmd.Wait()
|
|
|
|
}()
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
// BackendDefaults returns the nginx defaults
|
|
|
|
func (n NGINXController) BackendDefaults() defaults.Backend {
|
2017-08-26 03:46:17 +00:00
|
|
|
return n.backendDefaults
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 19:56:40 +00:00
|
|
|
// printDiff returns the difference between the running configuration
|
|
|
|
// and the new one
|
|
|
|
func (n NGINXController) printDiff(data []byte) {
|
2017-08-16 07:02:30 +00:00
|
|
|
if !glog.V(2) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
in, err := os.Open(cfgPath)
|
|
|
|
if err != nil {
|
2017-06-11 19:56:40 +00:00
|
|
|
return
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
src, err := ioutil.ReadAll(in)
|
|
|
|
in.Close()
|
|
|
|
if err != nil {
|
2017-06-11 19:56:40 +00:00
|
|
|
return
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(src, data) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "nginx-cfg-diff")
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("error creating temporal file: %s", err)
|
2017-06-11 19:56:40 +00:00
|
|
|
return
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
defer tmpfile.Close()
|
|
|
|
err = ioutil.WriteFile(tmpfile.Name(), data, 0644)
|
|
|
|
if err != nil {
|
2017-06-11 19:56:40 +00:00
|
|
|
return
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
diffOutput, err := diff(src, data)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("error computing diff: %s", err)
|
2017-06-11 19:56:40 +00:00
|
|
|
return
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 07:02:30 +00:00
|
|
|
glog.Infof("NGINX configuration diff\n")
|
|
|
|
glog.Infof("%v", string(diffOutput))
|
|
|
|
|
2017-03-07 14:23:19 +00:00
|
|
|
os.Remove(tmpfile.Name())
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info return build information
|
2016-11-16 18:24:26 +00:00
|
|
|
func (n NGINXController) Info() *ingress.BackendInfo {
|
|
|
|
return &ingress.BackendInfo{
|
|
|
|
Name: "NGINX",
|
|
|
|
Release: version.RELEASE,
|
|
|
|
Build: version.COMMIT,
|
|
|
|
Repository: version.REPO,
|
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 13:33:26 +00:00
|
|
|
// DefaultEndpoint returns the default endpoint to be use as default server that returns 404.
|
|
|
|
func (n NGINXController) DefaultEndpoint() ingress.Endpoint {
|
|
|
|
return ingress.Endpoint{
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
Port: fmt.Sprintf("%v", n.ports.Default),
|
2017-09-17 18:42:31 +00:00
|
|
|
Target: &apiv1.ObjectReference{},
|
2017-08-24 13:33:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:03:52 +00:00
|
|
|
// ConfigureFlags allow to configure more flags before the parsing of
|
|
|
|
// command line arguments
|
|
|
|
func (n *NGINXController) ConfigureFlags(flags *pflag.FlagSet) {
|
2017-08-21 01:34:31 +00:00
|
|
|
flags.BoolVar(&n.isSSLPassthroughEnabled, "enable-ssl-passthrough", false, `Enable SSL passthrough feature. Default is disabled`)
|
2017-08-24 13:33:26 +00:00
|
|
|
flags.IntVar(&n.ports.HTTP, "http-port", 80, `Indicates the port to use for HTTP traffic`)
|
|
|
|
flags.IntVar(&n.ports.HTTPS, "https-port", 443, `Indicates the port to use for HTTPS traffic`)
|
|
|
|
flags.IntVar(&n.ports.Status, "status-port", 18080, `Indicates the TCP port to use for exposing the nginx status page`)
|
|
|
|
flags.IntVar(&n.ports.SSLProxy, "ssl-passtrough-proxy-port", 442, `Default port to use internally for SSL when SSL Passthgough is enabled`)
|
|
|
|
flags.IntVar(&n.ports.Default, "default-server-port", 8181, `Default port to use for exposing the default server (catch all)`)
|
2017-05-25 00:03:52 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 14:02:23 +00:00
|
|
|
// OverrideFlags customize NGINX controller flags
|
2017-03-12 15:27:05 +00:00
|
|
|
func (n *NGINXController) OverrideFlags(flags *pflag.FlagSet) {
|
2017-08-24 13:33:26 +00:00
|
|
|
// we check port collisions
|
|
|
|
if !isPortAvailable(n.ports.HTTP) {
|
|
|
|
glog.Fatalf("Port %v is already in use. Please check the flag --http-port", n.ports.HTTP)
|
|
|
|
}
|
|
|
|
if !isPortAvailable(n.ports.HTTPS) {
|
|
|
|
glog.Fatalf("Port %v is already in use. Please check the flag --https-port", n.ports.HTTPS)
|
|
|
|
}
|
|
|
|
if !isPortAvailable(n.ports.Status) {
|
|
|
|
glog.Fatalf("Port %v is already in use. Please check the flag --status-port", n.ports.Status)
|
|
|
|
}
|
|
|
|
if !isPortAvailable(n.ports.Default) {
|
|
|
|
glog.Fatalf("Port %v is already in use. Please check the flag --default-server-port", n.ports.Default)
|
|
|
|
}
|
|
|
|
|
2017-03-12 15:27:05 +00:00
|
|
|
ic, _ := flags.GetString("ingress-class")
|
|
|
|
wc, _ := flags.GetString("watch-namespace")
|
|
|
|
|
|
|
|
if ic == "" {
|
|
|
|
ic = defIngressClass
|
2017-03-04 19:50:49 +00:00
|
|
|
}
|
2017-03-12 15:27:05 +00:00
|
|
|
|
|
|
|
if ic != defIngressClass {
|
|
|
|
glog.Warningf("only Ingress with class %v will be processed by this ingress controller", ic)
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.Set("ingress-class", ic)
|
2017-08-31 17:19:32 +00:00
|
|
|
|
|
|
|
h, _ := flags.GetInt("healthz-port")
|
|
|
|
n.ports.Health = h
|
|
|
|
|
|
|
|
n.stats = newStatsCollector(wc, ic, n.binary, n.ports.Status)
|
2017-08-21 01:34:31 +00:00
|
|
|
|
|
|
|
if n.isSSLPassthroughEnabled {
|
2017-08-24 13:33:26 +00:00
|
|
|
if !isPortAvailable(n.ports.SSLProxy) {
|
|
|
|
glog.Fatalf("Port %v is already in use. Please check the flag --ssl-passtrough-proxy-port", n.ports.SSLProxy)
|
|
|
|
}
|
|
|
|
|
2017-08-21 01:34:31 +00:00
|
|
|
glog.Info("starting TLS proxy for SSL passthrough")
|
|
|
|
n.proxy = &proxy{
|
|
|
|
Default: &server{
|
|
|
|
Hostname: "localhost",
|
|
|
|
IP: "127.0.0.1",
|
2017-08-24 13:33:26 +00:00
|
|
|
Port: n.ports.SSLProxy,
|
2017-08-21 01:34:31 +00:00
|
|
|
ProxyProtocol: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-08-24 13:33:26 +00:00
|
|
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%v", n.ports.HTTPS))
|
2017-08-21 01:34:31 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyList := &proxyproto.Listener{Listener: listener}
|
|
|
|
|
|
|
|
// start goroutine that accepts tcp connections in port 443
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
var conn net.Conn
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if n.isProxyProtocolEnabled {
|
|
|
|
// we need to wrap the listener in order to decode
|
|
|
|
// proxy protocol before handling the connection
|
|
|
|
conn, err = proxyList.Accept()
|
|
|
|
} else {
|
|
|
|
conn, err = listener.Accept()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("unexpected error accepting tcp connection: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(3).Infof("remote address %s to local %s", conn.RemoteAddr(), conn.LocalAddr())
|
|
|
|
go n.proxy.Handle(conn)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2017-03-02 15:50:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultIngressClass just return the default ingress class
|
|
|
|
func (n NGINXController) DefaultIngressClass() string {
|
|
|
|
return defIngressClass
|
2017-02-14 14:02:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
// testTemplate checks if the NGINX configuration inside the byte array is valid
|
|
|
|
// running the command "nginx -t" using a temporal file.
|
|
|
|
func (n NGINXController) testTemplate(cfg []byte) error {
|
2017-04-27 02:34:36 +00:00
|
|
|
if len(cfg) == 0 {
|
|
|
|
return fmt.Errorf("invalid nginx configuration (empty)")
|
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
tmpfile, err := ioutil.TempFile("", "nginx-cfg")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tmpfile.Close()
|
2017-03-03 07:17:32 +00:00
|
|
|
err = ioutil.WriteFile(tmpfile.Name(), cfg, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-24 00:14:14 +00:00
|
|
|
out, err := exec.Command(n.binary, "-t", "-c", tmpfile.Name()).CombinedOutput()
|
2017-05-16 20:06:33 +00:00
|
|
|
if err != nil {
|
2016-11-10 22:56:29 +00:00
|
|
|
// this error is different from the rest because it must be clear why nginx is not working
|
2016-11-16 18:24:26 +00:00
|
|
|
oe := fmt.Sprintf(`
|
2016-11-10 22:56:29 +00:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
Error: %v
|
|
|
|
%v
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
`, err, string(out))
|
2016-11-16 18:24:26 +00:00
|
|
|
return errors.New(oe)
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(tmpfile.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-07 18:13:08 +00:00
|
|
|
// SetConfig sets the configured configmap
|
2017-09-17 18:42:31 +00:00
|
|
|
func (n *NGINXController) SetConfig(cmap *apiv1.ConfigMap) {
|
2017-01-20 22:37:59 +00:00
|
|
|
n.configmap = cmap
|
2017-04-09 23:51:38 +00:00
|
|
|
n.isProxyProtocolEnabled = false
|
2017-09-07 16:15:36 +00:00
|
|
|
|
|
|
|
m := map[string]string{}
|
|
|
|
if cmap != nil {
|
|
|
|
m = cmap.Data
|
2017-04-09 23:51:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 16:15:36 +00:00
|
|
|
val, ok := m["use-proxy-protocol"]
|
2017-04-09 23:51:38 +00:00
|
|
|
if ok {
|
|
|
|
b, err := strconv.ParseBool(val)
|
|
|
|
if err == nil {
|
|
|
|
n.isProxyProtocolEnabled = b
|
|
|
|
}
|
|
|
|
}
|
2017-08-26 03:46:17 +00:00
|
|
|
|
2017-09-07 16:15:36 +00:00
|
|
|
n.backendDefaults = ngx_template.ReadConfig(m).Backend
|
2017-01-20 22:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 18:13:08 +00:00
|
|
|
// SetListers sets the configured store listers in the generic ingress controller
|
2017-09-18 23:53:26 +00:00
|
|
|
func (n *NGINXController) SetListers(lister *ingress.StoreLister) {
|
2017-02-07 18:13:08 +00:00
|
|
|
n.storeLister = lister
|
|
|
|
}
|
|
|
|
|
2017-07-28 22:57:33 +00:00
|
|
|
// UpdateIngressStatus custom Ingress status update
|
2017-09-17 18:42:31 +00:00
|
|
|
func (n *NGINXController) UpdateIngressStatus(*extensions.Ingress) []apiv1.LoadBalancerIngress {
|
2017-07-28 22:57:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-15 06:25:46 +00:00
|
|
|
// OnUpdate is called by syncQueue in https://github.com/kubernetes/ingress/blob/master/core/pkg/ingress/controller/controller.go#L426
|
2016-11-10 22:56:29 +00:00
|
|
|
// periodically to keep the configuration in sync.
|
|
|
|
//
|
|
|
|
// convert configmap to custom configuration object (different in each implementation)
|
|
|
|
// write the custom template (the complexity depends on the implementation)
|
|
|
|
// write the configuration file
|
|
|
|
// returning nill implies the backend will be reloaded.
|
|
|
|
// if an error is returned means requeue the update
|
2017-06-11 19:56:40 +00:00
|
|
|
func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error {
|
2017-01-20 22:37:59 +00:00
|
|
|
cfg := ngx_template.ReadConfig(n.configmap.Data)
|
2017-04-11 14:47:49 +00:00
|
|
|
cfg.Resolver = n.resolver
|
2017-03-10 13:01:26 +00:00
|
|
|
|
2017-05-26 18:25:06 +00:00
|
|
|
servers := []*server{}
|
|
|
|
for _, pb := range ingressCfg.PassthroughBackends {
|
|
|
|
svc := pb.Service
|
|
|
|
if svc == nil {
|
|
|
|
glog.Warningf("missing service for PassthroughBackends %v", pb.Backend)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(pb.Port.String())
|
|
|
|
if err != nil {
|
|
|
|
for _, sp := range svc.Spec.Ports {
|
|
|
|
if sp.Name == pb.Port.String() {
|
|
|
|
port = int(sp.Port)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, sp := range svc.Spec.Ports {
|
|
|
|
if sp.Port == int32(port) {
|
|
|
|
port = int(sp.Port)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Allow PassthroughBackends to specify they support proxy-protocol
|
|
|
|
servers = append(servers, &server{
|
|
|
|
Hostname: pb.Hostname,
|
|
|
|
IP: svc.Spec.ClusterIP,
|
|
|
|
Port: port,
|
|
|
|
ProxyProtocol: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-21 01:34:31 +00:00
|
|
|
if n.isSSLPassthroughEnabled {
|
|
|
|
n.proxy.ServerList = servers
|
|
|
|
}
|
2017-05-26 18:25:06 +00:00
|
|
|
|
2017-03-10 13:01:26 +00:00
|
|
|
// we need to check if the status module configuration changed
|
2017-03-12 15:27:05 +00:00
|
|
|
if cfg.EnableVtsStatus {
|
|
|
|
n.setupMonitor(vtsStatusModule)
|
|
|
|
} else {
|
|
|
|
n.setupMonitor(defaultStatusModule)
|
|
|
|
}
|
2016-11-10 22:56:29 +00:00
|
|
|
|
2017-08-15 07:14:08 +00:00
|
|
|
// NGINX cannot resize the hash tables used to store server names.
|
2016-11-10 22:56:29 +00:00
|
|
|
// For this reason we check if the defined size defined is correct
|
|
|
|
// for the FQDN defined in the ingress rules adjusting the value
|
|
|
|
// if is required.
|
|
|
|
// https://trac.nginx.org/nginx/ticket/352
|
|
|
|
// https://trac.nginx.org/nginx/ticket/631
|
2017-08-16 07:02:30 +00:00
|
|
|
var longestName int
|
|
|
|
var serverNameBytes int
|
2017-08-19 21:13:02 +00:00
|
|
|
redirectServers := make(map[string]string)
|
2017-08-16 07:02:30 +00:00
|
|
|
for _, srv := range ingressCfg.Servers {
|
|
|
|
if longestName < len(srv.Hostname) {
|
|
|
|
longestName = len(srv.Hostname)
|
|
|
|
}
|
|
|
|
serverNameBytes += len(srv.Hostname)
|
2017-08-19 21:13:02 +00:00
|
|
|
if srv.RedirectFromToWWW {
|
|
|
|
var n string
|
|
|
|
if strings.HasPrefix(srv.Hostname, "www.") {
|
|
|
|
n = strings.TrimLeft(srv.Hostname, "www.")
|
|
|
|
} else {
|
|
|
|
n = fmt.Sprintf("www.%v", srv.Hostname)
|
|
|
|
}
|
2017-08-22 20:16:59 +00:00
|
|
|
glog.V(3).Infof("creating redirect from %v to %v", srv.Hostname, n)
|
2017-08-19 21:13:02 +00:00
|
|
|
if _, ok := redirectServers[n]; !ok {
|
|
|
|
found := false
|
|
|
|
for _, esrv := range ingressCfg.Servers {
|
|
|
|
if esrv.Hostname == n {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
redirectServers[n] = srv.Hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-16 07:02:30 +00:00
|
|
|
}
|
2017-04-14 23:59:10 +00:00
|
|
|
if cfg.ServerNameHashBucketSize == 0 {
|
2017-08-16 07:02:30 +00:00
|
|
|
nameHashBucketSize := nginxHashBucketSize(longestName)
|
2017-04-14 23:59:10 +00:00
|
|
|
glog.V(3).Infof("adjusting ServerNameHashBucketSize variable to %v", nameHashBucketSize)
|
2016-11-10 22:56:29 +00:00
|
|
|
cfg.ServerNameHashBucketSize = nameHashBucketSize
|
|
|
|
}
|
2017-05-24 00:02:12 +00:00
|
|
|
serverNameHashMaxSize := nextPowerOf2(serverNameBytes)
|
|
|
|
if cfg.ServerNameHashMaxSize < serverNameHashMaxSize {
|
2017-04-14 23:59:10 +00:00
|
|
|
glog.V(3).Infof("adjusting ServerNameHashMaxSize variable to %v", serverNameHashMaxSize)
|
2016-11-10 22:56:29 +00:00
|
|
|
cfg.ServerNameHashMaxSize = serverNameHashMaxSize
|
|
|
|
}
|
|
|
|
|
2017-01-19 02:31:33 +00:00
|
|
|
// the limit of open files is per worker process
|
|
|
|
// and we leave some room to avoid consuming all the FDs available
|
2017-03-28 16:39:44 +00:00
|
|
|
wp, err := strconv.Atoi(cfg.WorkerProcesses)
|
2017-06-12 20:20:40 +00:00
|
|
|
glog.V(3).Infof("number of worker processes: %v", wp)
|
2017-03-28 16:39:44 +00:00
|
|
|
if err != nil {
|
|
|
|
wp = 1
|
|
|
|
}
|
|
|
|
maxOpenFiles := (sysctlFSFileMax() / wp) - 1024
|
2017-06-12 20:20:40 +00:00
|
|
|
glog.V(3).Infof("maximum number of open file descriptors : %v", sysctlFSFileMax())
|
|
|
|
if maxOpenFiles < 1024 {
|
2017-03-30 13:10:47 +00:00
|
|
|
// this means the value of RLIMIT_NOFILE is too low.
|
|
|
|
maxOpenFiles = 1024
|
|
|
|
}
|
2017-01-19 02:31:33 +00:00
|
|
|
|
2017-02-07 18:13:08 +00:00
|
|
|
setHeaders := map[string]string{}
|
|
|
|
if cfg.ProxySetHeaders != "" {
|
|
|
|
cmap, exists, err := n.storeLister.ConfigMap.GetByKey(cfg.ProxySetHeaders)
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("unexpected error reading configmap %v: %v", cfg.ProxySetHeaders, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2017-09-17 18:42:31 +00:00
|
|
|
setHeaders = cmap.(*apiv1.ConfigMap).Data
|
2017-02-07 18:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-18 10:21:03 +00:00
|
|
|
addHeaders := map[string]string{}
|
|
|
|
if cfg.AddHeaders != "" {
|
|
|
|
cmap, exists, err := n.storeLister.ConfigMap.GetByKey(cfg.AddHeaders)
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("unexpected error reading configmap %v: %v", cfg.AddHeaders, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2017-09-17 18:42:31 +00:00
|
|
|
addHeaders = cmap.(*apiv1.ConfigMap).Data
|
2017-05-18 10:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:55 +00:00
|
|
|
sslDHParam := ""
|
|
|
|
if cfg.SSLDHParam != "" {
|
|
|
|
secretName := cfg.SSLDHParam
|
|
|
|
s, exists, err := n.storeLister.Secret.GetByKey(secretName)
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("unexpected error reading secret %v: %v", secretName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2017-09-17 18:42:31 +00:00
|
|
|
secret := s.(*apiv1.Secret)
|
2017-03-08 13:41:55 +00:00
|
|
|
nsSecName := strings.Replace(secretName, "/", "-", -1)
|
|
|
|
|
|
|
|
dh, ok := secret.Data["dhparam.pem"]
|
|
|
|
if ok {
|
|
|
|
pemFileName, err := ssl.AddOrUpdateDHParam(nsSecName, dh)
|
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("unexpected error adding or updating dhparam %v file: %v", nsSecName, err)
|
|
|
|
} else {
|
|
|
|
sslDHParam = pemFileName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.SSLDHParam = sslDHParam
|
|
|
|
|
2017-08-03 14:51:39 +00:00
|
|
|
tc := config.TemplateConfig{
|
2017-08-21 01:34:31 +00:00
|
|
|
ProxySetHeaders: setHeaders,
|
|
|
|
AddHeaders: addHeaders,
|
|
|
|
MaxOpenFiles: maxOpenFiles,
|
|
|
|
BacklogSize: sysctlSomaxconn(),
|
|
|
|
Backends: ingressCfg.Backends,
|
|
|
|
PassthroughBackends: ingressCfg.PassthroughBackends,
|
|
|
|
Servers: ingressCfg.Servers,
|
|
|
|
TCPBackends: ingressCfg.TCPEndpoints,
|
|
|
|
UDPBackends: ingressCfg.UDPEndpoints,
|
|
|
|
HealthzURI: ngxHealthPath,
|
|
|
|
CustomErrors: len(cfg.CustomHTTPErrors) > 0,
|
|
|
|
Cfg: cfg,
|
|
|
|
IsIPV6Enabled: n.isIPV6Enabled && !cfg.DisableIpv6,
|
|
|
|
RedirectServers: redirectServers,
|
|
|
|
IsSSLPassthroughEnabled: n.isSSLPassthroughEnabled,
|
2017-08-24 13:33:26 +00:00
|
|
|
ListenPorts: n.ports,
|
2017-09-17 18:03:05 +00:00
|
|
|
PublishService: n.controller.GetPublishService(),
|
2017-08-03 14:51:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
content, err := n.t.Write(tc)
|
2017-06-11 19:56:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = n.testTemplate(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.printDiff(content)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(cfgPath, content, 0644)
|
2017-02-20 02:34:05 +00:00
|
|
|
if err != nil {
|
2017-06-11 19:56:40 +00:00
|
|
|
return err
|
2017-02-20 02:34:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-23 13:55:45 +00:00
|
|
|
o, err := exec.Command(n.binary, "-s", "reload", "-c", cfgPath).CombinedOutput()
|
2017-06-11 19:56:40 +00:00
|
|
|
if err != nil {
|
2017-06-23 13:55:45 +00:00
|
|
|
return fmt.Errorf("%v\n%v", err, string(o))
|
2017-02-20 02:34:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 19:56:40 +00:00
|
|
|
return nil
|
2016-11-10 22:56:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 02:29:51 +00:00
|
|
|
// nginxHashBucketSize computes the correct nginx hash_bucket_size for a hash with the given longest key
|
|
|
|
func nginxHashBucketSize(longestString int) int {
|
|
|
|
// See https://github.com/kubernetes/ingress/issues/623 for an explanation
|
|
|
|
wordSize := 8 // Assume 64 bit CPU
|
|
|
|
n := longestString + 2
|
|
|
|
aligned := (n + wordSize - 1) & ^(wordSize - 1)
|
|
|
|
rawSize := wordSize + wordSize + aligned
|
|
|
|
return nextPowerOf2(rawSize)
|
|
|
|
}
|
|
|
|
|
2016-11-27 00:09:59 +00:00
|
|
|
// Name returns the healthcheck name
|
|
|
|
func (n NGINXController) Name() string {
|
|
|
|
return "Ingress Controller"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check returns if the nginx healthz endpoint is returning ok (status code 200)
|
|
|
|
func (n NGINXController) Check(_ *http.Request) error {
|
2017-08-24 13:33:26 +00:00
|
|
|
res, err := http.Get(fmt.Sprintf("http://localhost:%v%v", n.ports.Status, ngxHealthPath))
|
2016-11-27 00:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != 200 {
|
2017-01-06 08:12:25 +00:00
|
|
|
return fmt.Errorf("ingress controller is not healthy")
|
2016-11-27 00:09:59 +00:00
|
|
|
}
|
2017-09-28 14:52:12 +00:00
|
|
|
|
|
|
|
// check the nginx master process is running
|
|
|
|
fs, err := proc.NewFS("/proc")
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := ioutil.ReadFile("/run/nginx.pid")
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pid, err := strconv.Atoi(strings.TrimRight(string(f), "\r\n"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fs.NewProc(int(pid))
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("%v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-27 00:09:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-10 22:56:29 +00:00
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
|
|
|
// https://play.golang.org/p/TVSyCcdxUh
|
|
|
|
func nextPowerOf2(v int) int {
|
|
|
|
v--
|
|
|
|
v |= v >> 1
|
|
|
|
v |= v >> 2
|
|
|
|
v |= v >> 4
|
|
|
|
v |= v >> 8
|
|
|
|
v |= v >> 16
|
|
|
|
v++
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
2017-04-09 18:03:27 +00:00
|
|
|
|
|
|
|
func isIPv6Enabled() bool {
|
|
|
|
cmd := exec.Command("test", "-f", "/proc/net/if_inet6")
|
|
|
|
return cmd.Run() == nil
|
|
|
|
}
|
2017-08-28 16:06:58 +00:00
|
|
|
|
|
|
|
// isNginxRunning returns true if a process with the name 'nginx' is found
|
|
|
|
func isNginxProcessPresent() bool {
|
|
|
|
processes, _ := ps.Processes()
|
|
|
|
for _, p := range processes {
|
|
|
|
if p.Executable() == "nginx" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForNginxShutdown() {
|
|
|
|
timer := time.NewTicker(time.Second * 1)
|
|
|
|
defer timer.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
if !isNginxProcessPresent() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|