Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove temporal configuration file after a while #3507

Merged
merged 1 commit into from
Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import (
const (
ngxHealthPath = "/healthz"
nginxStreamSocket = "/tmp/ingress-stream.sock"
tempNginxPattern = "nginx-cfg"
)

var (
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand All @@ -423,6 +436,7 @@ Error: %v
%v
-------------------------------------------------------------------------------
`, err, string(out))

return errors.New(oe)
}

Expand Down Expand Up @@ -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
}
57 changes: 57 additions & 0 deletions internal/ingress/controller/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -404,3 +406,58 @@ func TestNextPowerOf2(t *testing.T) {
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better coverage I'd also create another temp file with creation time less than 5m and assert that it does not get deleted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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))
}
}