@@ -2,14 +2,17 @@ package main
22
33import (
44 "context"
5- "fl_sidecar/exporter"
6- "fl_sidecar/watcher"
75 "flag"
86 "fmt"
97 "log"
108 "os"
9+ "os/exec"
1110 "os/signal"
1211 "syscall"
12+ "time"
13+
14+ "fl_sidecar/exporter"
15+ "fl_sidecar/watcher"
1316)
1417
1518// Command-line flags
@@ -65,12 +68,32 @@ func main() {
6568
6669 log .Printf ("Start watching file %s" , metricFile )
6770
71+ // Check if main container process is still running periodically
72+ go func () {
73+ for {
74+ // Check if main container is still running (when shareProcessNamespace is enabled)
75+ if checkMainContainerExited () {
76+ log .Println ("Main container exited, exiting sidecar..." )
77+ cancel ()
78+ return
79+ }
80+
81+ select {
82+ case <- ctx .Done ():
83+ return
84+ default :
85+ // Check every 5 seconds
86+ time .Sleep (5 * time .Second )
87+ }
88+ }
89+ }()
90+
6891 // Main loop to process file updates and handle shutdown
6992 for {
7093 select {
7194 case content , ok := <- updateChan :
7295 if ! ok {
73- log .Fatalf ("Watcher channel closed" )
96+ log .Println ("Watcher channel closed" )
7497 return
7598 }
7699
@@ -84,6 +107,30 @@ func main() {
84107 }
85108}
86109
110+ // checkMainContainerExited checks if the main container (flower-server or flower-client) process has exited
111+ // This works when shareProcessNamespace is enabled in the pod spec
112+ func checkMainContainerExited () bool {
113+ // Check if the main flower server or client process is still running
114+ // Try different patterns for server and client containers
115+ patterns := []string {
116+ ".*server.*--num-rounds" , // Server process pattern
117+ ".*client.*--data-config" , // Client process pattern
118+ ".*client.*--server-address" , // Alternative client pattern
119+ }
120+
121+ for _ , pattern := range patterns {
122+ cmd := exec .Command ("pgrep" , "-f" , pattern )
123+ err := cmd .Run ()
124+ if err == nil {
125+ // Found a matching process, main container is still running
126+ return false
127+ }
128+ }
129+
130+ // No matching processes found, main container has likely exited
131+ return true
132+ }
133+
87134// parseAndPushMtrics parses the content of the metric file and pushes the metrics to the reporter.
88135func parseAndPushMtrics (reporter * exporter.Reporter , content []byte ) {
89136 metrics , labels , err := exporter .ParseContetnt (content )
0 commit comments