diff --git a/cmd/nginx-ingress/main.go b/cmd/nginx-ingress/main.go index 850920eee..e397cc7fe 100644 --- a/cmd/nginx-ingress/main.go +++ b/cmd/nginx-ingress/main.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "os/signal" + "path/filepath" "regexp" "runtime" "strings" @@ -788,6 +789,21 @@ func handleTermination(lbc *k8s.LoadBalancerController, nginxManager nginx.Manag select { case err := <-cpcfg.nginxDone: if err != nil { + // removes .sock files after nginx exits + socketPath := "/var/lib/nginx/" + files, readErr := os.ReadDir(socketPath) + if readErr != nil { + nl.Errorf(lbc.Logger, "error trying to read directory %s: %v", socketPath, readErr) + } else { + for _, f := range files { + if !f.IsDir() && strings.HasSuffix(f.Name(), ".sock") { + fullPath := filepath.Join(socketPath, f.Name()) + if removeErr := os.Remove(fullPath); removeErr != nil { + nl.Errorf(lbc.Logger, "error trying to remove file %s: %v", fullPath, removeErr) + } + } + } + } nl.Fatalf(lbc.Logger, "nginx command exited unexpectedly with status: %v", err) } else { nl.Info(lbc.Logger, "nginx command exited successfully")