Skip to content

Commit 1d4e949

Browse files
AlexFenlonpdabelf5
andauthored
Cleanup stale socket files on startup (#8455)
Signed-off-by: AlexFenlon <[email protected]> Co-authored-by: Paul Abel <[email protected]>
1 parent fa3fb12 commit 1d4e949

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

cmd/nginx-ingress/main.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const (
7676
appProtectVersionPath = "/opt/app_protect/RELEASE"
7777
appProtectv4BundleFolder = "/etc/nginx/waf/bundles/"
7878
appProtectv5BundleFolder = "/etc/app_protect/bundles/"
79+
socketPath = "/var/lib/nginx"
7980
fatalEventFlushTime = 200 * time.Millisecond
8081
secretErrorReason = "SecretError"
8182
configMapErrorReason = "ConfigMapError"
@@ -88,6 +89,8 @@ func main() {
8889
ctx := initLogger(*logFormat, logLevels[*logLevel], os.Stdout)
8990
l := nl.LoggerFromContext(ctx)
9091

92+
cleanupSocketFiles(l)
93+
9194
initValidate(ctx)
9295
parsedFlags := os.Args[1:]
9396

@@ -522,7 +525,7 @@ func createPlusClient(ctx context.Context, nginxPlus bool, useFakeNginxManager b
522525
var err error
523526

524527
if nginxPlus && !useFakeNginxManager {
525-
httpClient := getSocketClient("/var/lib/nginx/nginx-plus-api.sock")
528+
httpClient := getSocketClient(filepath.Join(socketPath, "nginx-plus-api.sock"))
526529
plusClient, err = client.NewNginxClient("http://nginx-plus-api/api", client.WithHTTPClient(httpClient))
527530
if err != nil {
528531
nl.Fatalf(l, "Failed to create NginxClient for Plus: %v", err)
@@ -801,21 +804,6 @@ func handleTermination(lbc *k8s.LoadBalancerController, nginxManager nginx.Manag
801804
select {
802805
case err := <-cpcfg.nginxDone:
803806
if err != nil {
804-
// removes .sock files after nginx exits
805-
socketPath := "/var/lib/nginx/"
806-
files, readErr := os.ReadDir(socketPath)
807-
if readErr != nil {
808-
nl.Errorf(lbc.Logger, "error trying to read directory %s: %v", socketPath, readErr)
809-
} else {
810-
for _, f := range files {
811-
if !f.IsDir() && strings.HasSuffix(f.Name(), ".sock") {
812-
fullPath := filepath.Join(socketPath, f.Name())
813-
if removeErr := os.Remove(fullPath); removeErr != nil {
814-
nl.Errorf(lbc.Logger, "error trying to remove file %s: %v", fullPath, removeErr)
815-
}
816-
}
817-
}
818-
}
819807
nl.Fatalf(lbc.Logger, "nginx command exited unexpectedly with status: %v", err)
820808
} else {
821809
nl.Info(lbc.Logger, "nginx command exited successfully")
@@ -844,6 +832,24 @@ func handleTermination(lbc *k8s.LoadBalancerController, nginxManager nginx.Manag
844832
os.Exit(0)
845833
}
846834

835+
// Clean up any leftover socket files from previous runs
836+
func cleanupSocketFiles(l *slog.Logger) {
837+
files, readErr := os.ReadDir(socketPath)
838+
if readErr != nil {
839+
nl.Errorf(l, "error trying to read directory %s: %v", socketPath, readErr)
840+
} else {
841+
for _, f := range files {
842+
if !f.IsDir() && strings.HasSuffix(f.Name(), ".sock") {
843+
fullPath := filepath.Join(socketPath, f.Name())
844+
nl.Infof(l, "Removing socket file %s", fullPath)
845+
if removeErr := os.Remove(fullPath); removeErr != nil {
846+
nl.Errorf(l, "error trying to remove file %s: %v", fullPath, removeErr)
847+
}
848+
}
849+
}
850+
}
851+
}
852+
847853
func ready(lbc *k8s.LoadBalancerController) http.HandlerFunc {
848854
return func(w http.ResponseWriter, _ *http.Request) {
849855
if !lbc.IsNginxReady() {
@@ -936,7 +942,7 @@ func createPlusAndLatencyCollectors(
936942
plusCollector = nginxCollector.NewNginxPlusCollector(plusClient, "nginx_ingress_nginxplus", variableLabelNames, constLabels, l)
937943
go metrics.RunPrometheusListenerForNginxPlus(ctx, *prometheusMetricsListenPort, plusCollector, registry, prometheusSecret)
938944
} else {
939-
httpClient := getSocketClient("/var/lib/nginx/nginx-status.sock")
945+
httpClient := getSocketClient(filepath.Join(socketPath, "%s/nginx-status.sock"))
940946
client := metrics.NewNginxMetricsClient(httpClient)
941947
go metrics.RunPrometheusListenerForNginx(ctx, *prometheusMetricsListenPort, client, registry, constLabels, prometheusSecret)
942948
}
@@ -945,7 +951,7 @@ func createPlusAndLatencyCollectors(
945951
if err := lc.Register(registry); err != nil {
946952
nl.Errorf(l, "Error registering Latency Prometheus metrics: %v", err)
947953
}
948-
syslogListener = metrics.NewLatencyMetricsListener(ctx, "/var/lib/nginx/nginx-syslog.sock", lc)
954+
syslogListener = metrics.NewLatencyMetricsListener(ctx, filepath.Join(socketPath, "nginx-syslog.sock"), lc)
949955
go syslogListener.Run()
950956
}
951957
}

0 commit comments

Comments
 (0)