Remove temporal configuration file after a while

This commit is contained in:
Manuel Alejandro de Brito Fontes 2018-12-03 14:27:29 -03:00
parent da5b75ebaf
commit c830a73255
No known key found for this signature in database
GPG key ID: 786136016A8BA02A
2 changed files with 101 additions and 1 deletions

View file

@ -66,6 +66,7 @@ import (
const (
ngxHealthPath = "/healthz"
nginxStreamSocket = "/tmp/ingress-stream.sock"
tempNginxPattern = "nginx-cfg"
)
var (
@ -289,6 +290,18 @@ func (n *NGINXController) Start() {
// force 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 {
select {
case err := <-n.ngxErrCh:
@ -405,7 +418,7 @@ func (n NGINXController) testTemplate(cfg []byte) error {
if len(cfg) == 0 {
return fmt.Errorf("invalid NGINX configuration (empty)")
}
tmpfile, err := ioutil.TempFile("", "nginx-cfg")
tmpfile, err := ioutil.TempFile("", tempNginxPattern)
if err != nil {
return err
}
@ -423,6 +436,7 @@ Error: %v
%v
-------------------------------------------------------------------------------
`, err, string(out))
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)
}
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
}