Merge pull request #3507 from aledbf/remove-temporal
Remove temporal configuration file after a while
This commit is contained in:
commit
c326c55414
2 changed files with 101 additions and 1 deletions
|
@ -66,6 +66,7 @@ import (
|
||||||
const (
|
const (
|
||||||
ngxHealthPath = "/healthz"
|
ngxHealthPath = "/healthz"
|
||||||
nginxStreamSocket = "/tmp/ingress-stream.sock"
|
nginxStreamSocket = "/tmp/ingress-stream.sock"
|
||||||
|
tempNginxPattern = "nginx-cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -289,6 +290,18 @@ func (n *NGINXController) Start() {
|
||||||
// force initial sync
|
// force initial sync
|
||||||
n.syncQueue.EnqueueTask(task.GetDummyObject("initial-sync"))
|
n.syncQueue.EnqueueTask(task.GetDummyObject("initial-sync"))
|
||||||
|
|
||||||
|
// In case of error the temporal configuration file will
|
||||||
|
// be available up to five minutes after the error
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
time.Sleep(5 * time.Minute)
|
||||||
|
err := cleanTempNginxCfg()
|
||||||
|
if err != nil {
|
||||||
|
klog.Infof("Unexpected error removing temporal configuration files: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case err := <-n.ngxErrCh:
|
case err := <-n.ngxErrCh:
|
||||||
|
@ -405,7 +418,7 @@ func (n NGINXController) testTemplate(cfg []byte) error {
|
||||||
if len(cfg) == 0 {
|
if len(cfg) == 0 {
|
||||||
return fmt.Errorf("invalid NGINX configuration (empty)")
|
return fmt.Errorf("invalid NGINX configuration (empty)")
|
||||||
}
|
}
|
||||||
tmpfile, err := ioutil.TempFile("", "nginx-cfg")
|
tmpfile, err := ioutil.TempFile("", tempNginxPattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -423,6 +436,7 @@ Error: %v
|
||||||
%v
|
%v
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
`, err, string(out))
|
`, err, string(out))
|
||||||
|
|
||||||
return errors.New(oe)
|
return errors.New(oe)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -978,3 +992,32 @@ func createOpentracingCfg(cfg ngx_config.Configuration) error {
|
||||||
|
|
||||||
return ioutil.WriteFile("/etc/nginx/opentracing.json", tmplBuf.Bytes(), file.ReadWriteByUser)
|
return ioutil.WriteFile("/etc/nginx/opentracing.json", tmplBuf.Bytes(), file.ReadWriteByUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanTempNginxCfg() error {
|
||||||
|
var files []string
|
||||||
|
|
||||||
|
err := filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, err error) error {
|
||||||
|
if info.IsDir() && os.TempDir() != path {
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
|
||||||
|
dur, _ := time.ParseDuration("-5m")
|
||||||
|
fiveMinutesAgo := time.Now().Add(dur)
|
||||||
|
if strings.HasPrefix(info.Name(), tempNginxPattern) && info.ModTime().Before(fiveMinutesAgo) {
|
||||||
|
files = append(files, path)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
err := os.Remove(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -404,3 +406,58 @@ func TestNextPowerOf2(t *testing.T) {
|
||||||
t.Errorf("TestNextPowerOf2: expected %d but returned %d.", 0, actual)
|
t.Errorf("TestNextPowerOf2: expected %d but returned %d.", 0, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCleanTempNginxCfg(t *testing.T) {
|
||||||
|
err := cleanTempNginxCfg()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpfile, err := ioutil.TempFile("", tempNginxPattern)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
|
dur, err := time.ParseDuration("-10m")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
oldTime := time.Now().Add(dur)
|
||||||
|
err = os.Chtimes(tmpfile.Name(), oldTime, oldTime)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpfile, err = ioutil.TempFile("", tempNginxPattern)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
|
err = cleanTempNginxCfg()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var files []string
|
||||||
|
|
||||||
|
err = filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, err error) error {
|
||||||
|
if info.IsDir() && os.TempDir() != path {
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(info.Name(), tempNginxPattern) {
|
||||||
|
files = append(files, path)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(files) != 1 {
|
||||||
|
t.Errorf("expected one file but %d were found", len(files))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue