mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-26 07:14:12 +00:00
Add RunWithContext method for debugsocket
This commit is contained in:
@@ -531,7 +531,7 @@ func (s preparedGenericAPIServer) RunWithContext(ctx context.Context) error {
|
||||
if s.UnprotectedDebugSocket != nil {
|
||||
go func() {
|
||||
defer utilruntime.HandleCrash()
|
||||
klog.Error(s.UnprotectedDebugSocket.Run(stopCh))
|
||||
klog.Error(s.UnprotectedDebugSocket.RunWithContext(ctx))
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
@@ -17,12 +17,15 @@ limitations under the License.
|
||||
package routes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
)
|
||||
|
||||
// DebugSocket installs profiling and debugflag as a Unix-Domain socket.
|
||||
@@ -62,7 +65,14 @@ func (s *DebugSocket) InstallDebugFlag(flag string, handler func(http.ResponseWr
|
||||
}
|
||||
|
||||
// Run starts the server and waits for stopCh to be closed to close the server.
|
||||
//
|
||||
//logcheck:context // RunWithContext should be used instead of Run in code which supports contextual logging.
|
||||
func (s *DebugSocket) Run(stopCh <-chan struct{}) error {
|
||||
return s.RunWithContext(wait.ContextForChannel(stopCh))
|
||||
}
|
||||
|
||||
// RunWithContext starts the server and waits for the context to be cancelled to close the server.
|
||||
func (s *DebugSocket) RunWithContext(ctx context.Context) error {
|
||||
if err := os.Remove(s.path); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("failed to remove (%v): %v", s.path, err)
|
||||
}
|
||||
@@ -75,7 +85,7 @@ func (s *DebugSocket) Run(stopCh <-chan struct{}) error {
|
||||
|
||||
srv := http.Server{Handler: s.mux}
|
||||
go func() {
|
||||
<-stopCh
|
||||
<-ctx.Done()
|
||||
srv.Close()
|
||||
}()
|
||||
return srv.Serve(l)
|
||||
|
||||
Reference in New Issue
Block a user