Update nginx interface
This commit is contained in:
parent
6b71314b39
commit
a8c6385634
2 changed files with 28 additions and 27 deletions
|
@ -30,10 +30,9 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
proxyproto "github.com/armon/go-proxyproto"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
|
||||||
proxyproto "github.com/armon/go-proxyproto"
|
|
||||||
api_v1 "k8s.io/client-go/pkg/api/v1"
|
api_v1 "k8s.io/client-go/pkg/api/v1"
|
||||||
|
|
||||||
"k8s.io/ingress/controllers/nginx/pkg/config"
|
"k8s.io/ingress/controllers/nginx/pkg/config"
|
||||||
|
@ -237,19 +236,22 @@ func (n *NGINXController) start(cmd *exec.Cmd, done chan error) {
|
||||||
|
|
||||||
// Reload checks if the running configuration file is different
|
// Reload checks if the running configuration file is different
|
||||||
// to the specified and reload nginx if required
|
// to the specified and reload nginx if required
|
||||||
func (n NGINXController) Reload(data []byte) ([]byte, bool, error) {
|
func (n NGINXController) Reload(cfg ingress.Configuration) error {
|
||||||
if !n.isReloadRequired(data) {
|
ncfg, err := n.newConfFile(cfg)
|
||||||
return []byte("Reload not required"), false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err := ioutil.WriteFile(cfgPath, data, 0644)
|
n.printDiff(ncfg)
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(cfgPath, ncfg, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
o, e := exec.Command(n.binary, "-s", "reload", "-c", cfgPath).CombinedOutput()
|
out, err := exec.Command(n.binary, "-s", "reload", "-c", cfgPath).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%v\n%v", err, string(out))
|
||||||
|
}
|
||||||
|
|
||||||
return o, true, e
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackendDefaults returns the nginx defaults
|
// BackendDefaults returns the nginx defaults
|
||||||
|
@ -262,36 +264,35 @@ func (n NGINXController) BackendDefaults() defaults.Backend {
|
||||||
return ngx_template.ReadConfig(n.configmap.Data).Backend
|
return ngx_template.ReadConfig(n.configmap.Data).Backend
|
||||||
}
|
}
|
||||||
|
|
||||||
// isReloadRequired check if the new configuration file is different
|
// printDiff prints the difference between the current configuration
|
||||||
// from the current one.
|
// and the new one.
|
||||||
func (n NGINXController) isReloadRequired(data []byte) bool {
|
func (n NGINXController) printDiff(data []byte) {
|
||||||
in, err := os.Open(cfgPath)
|
in, err := os.Open(cfgPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
src, err := ioutil.ReadAll(in)
|
src, err := ioutil.ReadAll(in)
|
||||||
in.Close()
|
in.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(src, data) {
|
if !bytes.Equal(src, data) {
|
||||||
|
|
||||||
tmpfile, err := ioutil.TempFile("", "nginx-cfg-diff")
|
tmpfile, err := ioutil.TempFile("", "nginx-cfg-diff")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("error creating temporal file: %s", err)
|
glog.Errorf("error creating temporal file: %s", err)
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
defer tmpfile.Close()
|
defer tmpfile.Close()
|
||||||
err = ioutil.WriteFile(tmpfile.Name(), data, 0644)
|
err = ioutil.WriteFile(tmpfile.Name(), data, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
diffOutput, err := diff(src, data)
|
diffOutput, err := diff(src, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("error computing diff: %s", err)
|
glog.Errorf("error computing diff: %s", err)
|
||||||
return true
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if glog.V(2) {
|
if glog.V(2) {
|
||||||
|
@ -299,9 +300,7 @@ func (n NGINXController) isReloadRequired(data []byte) bool {
|
||||||
glog.Infof("%v", string(diffOutput))
|
glog.Infof("%v", string(diffOutput))
|
||||||
}
|
}
|
||||||
os.Remove(tmpfile.Name())
|
os.Remove(tmpfile.Name())
|
||||||
return len(diffOutput) > 0
|
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info return build information
|
// Info return build information
|
||||||
|
@ -404,7 +403,12 @@ func (n *NGINXController) SetListers(lister ingress.StoreLister) {
|
||||||
// write the configuration file
|
// write the configuration file
|
||||||
// returning nill implies the backend will be reloaded.
|
// returning nill implies the backend will be reloaded.
|
||||||
// if an error is returned means requeue the update
|
// if an error is returned means requeue the update
|
||||||
func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) ([]byte, error) {
|
func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) error {
|
||||||
|
_, err := n.newConfFile(ingressCfg)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NGINXController) newConfFile(ingressCfg ingress.Configuration) ([]byte, error) {
|
||||||
var longestName int
|
var longestName int
|
||||||
var serverNameBytes int
|
var serverNameBytes int
|
||||||
for _, srv := range ingressCfg.Servers {
|
for _, srv := range ingressCfg.Servers {
|
||||||
|
@ -540,11 +544,8 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) ([]byte, er
|
||||||
Cfg: cfg,
|
Cfg: cfg,
|
||||||
IsIPV6Enabled: n.isIPV6Enabled && !cfg.DisableIpv6,
|
IsIPV6Enabled: n.isIPV6Enabled && !cfg.DisableIpv6,
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := n.testTemplate(content); err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -156,7 +156,7 @@ func newIngressController(config *Configuration) *GenericController {
|
||||||
cfg: config,
|
cfg: config,
|
||||||
stopLock: &sync.Mutex{},
|
stopLock: &sync.Mutex{},
|
||||||
stopCh: make(chan struct{}),
|
stopCh: make(chan struct{}),
|
||||||
syncRateLimiter: flowcontrol.NewTokenBucketRateLimiter(0.5, 1),
|
syncRateLimiter: flowcontrol.NewTokenBucketRateLimiter(0.6, 1),
|
||||||
recorder: eventBroadcaster.NewRecorder(scheme.Scheme, api.EventSource{
|
recorder: eventBroadcaster.NewRecorder(scheme.Scheme, api.EventSource{
|
||||||
Component: "ingress-controller",
|
Component: "ingress-controller",
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in a new issue