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

[fix] fix nginx temp configs cleanup #12223

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
9 changes: 4 additions & 5 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,7 @@ func (n *NGINXController) testTemplate(cfg []byte) error {
if len(cfg) == 0 {
return fmt.Errorf("invalid NGINX configuration (empty)")
}
tmpDir := os.TempDir() + "/nginx"
tmpfile, err := os.CreateTemp(tmpDir, tempNginxPattern)
tmpfile, err := os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
if err != nil {
return err
}
Expand Down Expand Up @@ -1082,11 +1081,11 @@ func createOpentelemetryCfg(cfg *ngx_config.Configuration) error {
func cleanTempNginxCfg() error {
var files []string

err := filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(filepath.Join(os.TempDir(), "nginx"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && os.TempDir() != path {
if info.IsDir() && path != filepath.Join(os.TempDir(), "nginx") {
return filepath.SkipDir
}

Expand All @@ -1105,7 +1104,7 @@ func cleanTempNginxCfg() error {
}

for _, file := range files {
err := os.Remove(file)
err = os.Remove(file)
if err != nil {
return err
}
Expand Down
24 changes: 18 additions & 6 deletions internal/ingress/controller/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,11 @@ func TestCleanTempNginxCfg(t *testing.T) {
t.Fatal(err)
}

tmpfile, err := os.CreateTemp("", tempNginxPattern)
tmpfile, err := os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
if err != nil {
t.Fatal(err)
}
expectedDeletedFile := tmpfile.Name()
defer tmpfile.Close()

dur, err := time.ParseDuration("-10m")
Expand All @@ -378,10 +379,11 @@ func TestCleanTempNginxCfg(t *testing.T) {
t.Fatal(err)
}

tmpfile, err = os.CreateTemp("", tempNginxPattern)
tmpfile, err = os.CreateTemp(filepath.Join(os.TempDir(), "nginx"), tempNginxPattern)
if err != nil {
t.Fatal(err)
}
expectedFile := tmpfile.Name()
defer tmpfile.Close()

err = cleanTempNginxCfg()
Expand All @@ -391,8 +393,8 @@ func TestCleanTempNginxCfg(t *testing.T) {

var files []string

err = filepath.Walk(os.TempDir(), func(path string, info os.FileInfo, _ error) error {
if info.IsDir() && os.TempDir() != path {
err = filepath.Walk(filepath.Join(os.TempDir(), "nginx"), func(path string, info os.FileInfo, _ error) error {
if info.IsDir() && filepath.Join(os.TempDir(), "nginx") != path {
return filepath.SkipDir
}

Expand All @@ -405,8 +407,18 @@ func TestCleanTempNginxCfg(t *testing.T) {
t.Fatal(err)
}

if len(files) != 1 {
t.Errorf("expected one file but %d were found", len(files))
// some other files can be created by other tests
var found bool
for _, file := range files {
if file == expectedDeletedFile {
t.Errorf("file %s should be deleted", file)
}
if file == expectedFile {
found = true
}
}
if !found {
t.Errorf("file %s should not be deleted", expectedFile)
}
}

Expand Down