|
| 1 | +package workergateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/pprof" |
| 8 | + "runtime" |
| 9 | + |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/prometheus/client_golang/prometheus/collectors" |
| 12 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 13 | + "k8s.io/apiserver/pkg/server/healthz" |
| 14 | + "k8s.io/apiserver/pkg/server/routes" |
| 15 | + "k8s.io/component-base/logs" |
| 16 | + "k8s.io/component-base/metrics/legacyregistry" |
| 17 | + klog "k8s.io/klog/v2" |
| 18 | + ctrlhealthz "sigs.k8s.io/controller-runtime/pkg/healthz" |
| 19 | + ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" |
| 20 | + |
| 21 | + "gpustack.ai/gpustack/pkg/utils/gox" |
| 22 | + "gpustack.ai/gpustack/pkg/utils/httpx" |
| 23 | + "gpustack.ai/gpustack/pkg/webserver" |
| 24 | + "gpustack.ai/gpustack/pkg/workergateway/manager" |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + ctrlmetrics.Registry = struct { |
| 29 | + prometheus.Registerer |
| 30 | + prometheus.Gatherer |
| 31 | + }{ |
| 32 | + Registerer: legacyregistry.Registerer(), |
| 33 | + Gatherer: legacyregistry.DefaultGatherer, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +type WorkerGateway struct { |
| 38 | + // Manager. |
| 39 | + Manager manager.Manager |
| 40 | + |
| 41 | + // Server. |
| 42 | + Server webserver.Server |
| 43 | +} |
| 44 | + |
| 45 | +// Prepare prepares the runtime for the worker gateway, |
| 46 | +// including installing system resources, etc. |
| 47 | +func (wg *WorkerGateway) Prepare(ctx context.Context) error { |
| 48 | + // Register metric collectors. |
| 49 | + { |
| 50 | + reg := ctrlmetrics.Registry |
| 51 | + cs := []prometheus.Collector{ |
| 52 | + collectors.NewBuildInfoCollector(), |
| 53 | + gox.NewStatsCollector(), |
| 54 | + } |
| 55 | + for i := range cs { |
| 56 | + err := reg.Register(cs[i]) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("register metric collector: %w", err) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func (wg *WorkerGateway) Start(ctx context.Context) error { |
| 67 | + mu := wg.Server |
| 68 | + |
| 69 | + // Register /metrics. |
| 70 | + { |
| 71 | + h := promhttp.HandlerOpts{ |
| 72 | + ErrorLog: klog.NewStandardLogger("WARNING"), |
| 73 | + ErrorHandling: promhttp.HTTPErrorOnError, |
| 74 | + } |
| 75 | + mu.Register("/metrics", promhttp.HandlerFor(ctrlmetrics.Registry, h)) |
| 76 | + } |
| 77 | + |
| 78 | + // Register /healthz. |
| 79 | + { |
| 80 | + p := "/readyz" |
| 81 | + h := &ctrlhealthz.Handler{ |
| 82 | + Checks: map[string]ctrlhealthz.Checker{ |
| 83 | + "ping": ctrlhealthz.Ping, |
| 84 | + "log": healthz.LogHealthz.Check, |
| 85 | + }, |
| 86 | + } |
| 87 | + mu.Register(p, http.StripPrefix(p, h)) |
| 88 | + } |
| 89 | + |
| 90 | + // Register /livez. |
| 91 | + { |
| 92 | + p := "/livez" |
| 93 | + h := &ctrlhealthz.Handler{ |
| 94 | + Checks: map[string]ctrlhealthz.Checker{ |
| 95 | + "ping": ctrlhealthz.Ping, |
| 96 | + "log": healthz.LogHealthz.Check, |
| 97 | + "gopool": func(r *http.Request) error { |
| 98 | + return gox.IsHealthy() |
| 99 | + }, |
| 100 | + }, |
| 101 | + } |
| 102 | + mu.Register(p, http.StripPrefix(p, h)) |
| 103 | + } |
| 104 | + |
| 105 | + // Register /debug. |
| 106 | + { |
| 107 | + runtime.SetBlockProfileRate(1) |
| 108 | + mu.Register("/debug/pprof/", httpx.LoopbackAccessHandlerFunc(pprof.Index)) |
| 109 | + mu.Register("/debug/pprof/cmdline", httpx.LoopbackAccessHandlerFunc(pprof.Cmdline)) |
| 110 | + mu.Register("/debug/pprof/profile", httpx.LoopbackAccessHandlerFunc(pprof.Profile)) |
| 111 | + mu.Register("/debug/pprof/symbol", httpx.LoopbackAccessHandlerFunc(pprof.Symbol)) |
| 112 | + mu.Register("/debug/pprof/trace", httpx.LoopbackAccessHandlerFunc(pprof.Trace)) |
| 113 | + mu.Register("/debug/flags/v", httpx.LoopbackAccessHandlerFunc(routes.StringFlagPutHandler(logs.GlogSetter))) |
| 114 | + } |
| 115 | + |
| 116 | + // Register API routes. |
| 117 | + { |
| 118 | + p := "/apis" |
| 119 | + mu.Register(p+"/", http.StripPrefix(p, wg.getHandleApis())) |
| 120 | + } |
| 121 | + |
| 122 | + klog.Info("starting worker gateway") |
| 123 | + return mu.Start(ctx) |
| 124 | +} |
0 commit comments