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"
|
||||
"time"
|
||||
|
||||
proxyproto "github.com/armon/go-proxyproto"
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
proxyproto "github.com/armon/go-proxyproto"
|
||||
api_v1 "k8s.io/client-go/pkg/api/v1"
|
||||
|
||||
"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
|
||||
// to the specified and reload nginx if required
|
||||
func (n NGINXController) Reload(data []byte) ([]byte, bool, error) {
|
||||
if !n.isReloadRequired(data) {
|
||||
return []byte("Reload not required"), false, nil
|
||||
}
|
||||
func (n NGINXController) Reload(cfg ingress.Configuration) error {
|
||||
ncfg, err := n.newConfFile(cfg)
|
||||
|
||||
err := ioutil.WriteFile(cfgPath, data, 0644)
|
||||
n.printDiff(ncfg)
|
||||
|
||||
err = ioutil.WriteFile(cfgPath, ncfg, 0644)
|
||||
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
|
||||
|
@ -262,36 +264,35 @@ func (n NGINXController) BackendDefaults() defaults.Backend {
|
|||
return ngx_template.ReadConfig(n.configmap.Data).Backend
|
||||
}
|
||||
|
||||
// isReloadRequired check if the new configuration file is different
|
||||
// from the current one.
|
||||
func (n NGINXController) isReloadRequired(data []byte) bool {
|
||||
// printDiff prints the difference between the current configuration
|
||||
// and the new one.
|
||||
func (n NGINXController) printDiff(data []byte) {
|
||||
in, err := os.Open(cfgPath)
|
||||
if err != nil {
|
||||
return false
|
||||
return
|
||||
}
|
||||
src, err := ioutil.ReadAll(in)
|
||||
in.Close()
|
||||
if err != nil {
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
if !bytes.Equal(src, data) {
|
||||
|
||||
tmpfile, err := ioutil.TempFile("", "nginx-cfg-diff")
|
||||
if err != nil {
|
||||
glog.Errorf("error creating temporal file: %s", err)
|
||||
return false
|
||||
return
|
||||
}
|
||||
defer tmpfile.Close()
|
||||
err = ioutil.WriteFile(tmpfile.Name(), data, 0644)
|
||||
if err != nil {
|
||||
return false
|
||||
return
|
||||
}
|
||||
|
||||
diffOutput, err := diff(src, data)
|
||||
if err != nil {
|
||||
glog.Errorf("error computing diff: %s", err)
|
||||
return true
|
||||
return
|
||||
}
|
||||
|
||||
if glog.V(2) {
|
||||
|
@ -299,9 +300,7 @@ func (n NGINXController) isReloadRequired(data []byte) bool {
|
|||
glog.Infof("%v", string(diffOutput))
|
||||
}
|
||||
os.Remove(tmpfile.Name())
|
||||
return len(diffOutput) > 0
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Info return build information
|
||||
|
@ -404,7 +403,12 @@ func (n *NGINXController) SetListers(lister ingress.StoreLister) {
|
|||
// write the configuration file
|
||||
// returning nill implies the backend will be reloaded.
|
||||
// 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 serverNameBytes int
|
||||
for _, srv := range ingressCfg.Servers {
|
||||
|
@ -540,11 +544,8 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) ([]byte, er
|
|||
Cfg: cfg,
|
||||
IsIPV6Enabled: n.isIPV6Enabled && !cfg.DisableIpv6,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := n.testTemplate(content); err != nil {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -156,7 +156,7 @@ func newIngressController(config *Configuration) *GenericController {
|
|||
cfg: config,
|
||||
stopLock: &sync.Mutex{},
|
||||
stopCh: make(chan struct{}),
|
||||
syncRateLimiter: flowcontrol.NewTokenBucketRateLimiter(0.5, 1),
|
||||
syncRateLimiter: flowcontrol.NewTokenBucketRateLimiter(0.6, 1),
|
||||
recorder: eventBroadcaster.NewRecorder(scheme.Scheme, api.EventSource{
|
||||
Component: "ingress-controller",
|
||||
}),
|
||||
|
|
Loading…
Reference in a new issue