|
| 1 | +/* |
| 2 | +Copyright 2026 The HAMi Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Command k8s-vgpu-monitor serves Prometheus metrics for the AMD GPUs of one |
| 18 | +// node: per-container VRAM from the dmem cgroup controller and host memory, |
| 19 | +// load, temperature and power from sysfs, under the metric names of the HAMi |
| 20 | +// NVIDIA vGPUmonitor. |
| 21 | +package main |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "errors" |
| 26 | + "flag" |
| 27 | + "fmt" |
| 28 | + "net" |
| 29 | + "net/http" |
| 30 | + "os" |
| 31 | + "os/signal" |
| 32 | + "syscall" |
| 33 | + "time" |
| 34 | + |
| 35 | + "github.com/golang/glog" |
| 36 | + "github.com/prometheus/client_golang/prometheus" |
| 37 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
| 38 | + corev1 "k8s.io/api/core/v1" |
| 39 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 40 | + "k8s.io/apimachinery/pkg/labels" |
| 41 | + "k8s.io/client-go/informers" |
| 42 | + "k8s.io/client-go/kubernetes" |
| 43 | + "k8s.io/client-go/rest" |
| 44 | + |
| 45 | + "github.com/Project-HAMi/amd-device-plugin/internal/pkg/utils" |
| 46 | + "github.com/Project-HAMi/amd-device-plugin/internal/pkg/vgpumonitor" |
| 47 | +) |
| 48 | + |
| 49 | +func main() { |
| 50 | + var bind, cgroupRoot, drmRoot string |
| 51 | + flag.StringVar(&bind, "metrics_bind_address", ":9394", "TCP address to serve /metrics on") |
| 52 | + flag.StringVar(&cgroupRoot, "cgroup_root", vgpumonitor.DefaultCgroupRoot, "cgroup v2 mount point of the host") |
| 53 | + flag.StringVar(&drmRoot, "drm_root", vgpumonitor.DefaultDRMRoot, "sysfs DRM class directory of the host") |
| 54 | + flag.Parse() |
| 55 | + |
| 56 | + if err := run(bind, cgroupRoot, drmRoot); err != nil { |
| 57 | + glog.Errorf("%v", err) |
| 58 | + glog.Flush() |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func run(bind, cgroupRoot, drmRoot string) error { |
| 64 | + nodeName := os.Getenv(utils.NodeNameEnvName) |
| 65 | + if nodeName == "" { |
| 66 | + return fmt.Errorf("env %s not set", utils.NodeNameEnvName) |
| 67 | + } |
| 68 | + config, err := rest.InClusterConfig() |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("failed to load the in-cluster config: %w", err) |
| 71 | + } |
| 72 | + clientset, err := kubernetes.NewForConfig(config) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("failed to build clientset: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) |
| 78 | + defer cancel() |
| 79 | + |
| 80 | + // Bind before the informer starts: a taken port never heals, so fail fast. |
| 81 | + listener, err := net.Listen("tcp", bind) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("failed to listen on %s: %w", bind, err) |
| 84 | + } |
| 85 | + defer func() { _ = listener.Close() }() |
| 86 | + return serve(ctx, clientset, nodeName, listener, cgroupRoot, drmRoot) |
| 87 | +} |
| 88 | + |
| 89 | +// serve runs the collector for nodeName until ctx is done. |
| 90 | +func serve(ctx context.Context, clientset kubernetes.Interface, nodeName string, listener net.Listener, cgroupRoot, drmRoot string) error { |
| 91 | + factory := informers.NewSharedInformerFactoryWithOptions(clientset, 5*time.Minute, |
| 92 | + informers.WithTweakListOptions(func(o *metav1.ListOptions) { o.FieldSelector = "spec.nodeName=" + nodeName })) |
| 93 | + podInformer := factory.Core().V1().Pods() |
| 94 | + podLister := podInformer.Lister() |
| 95 | + synced := podInformer.Informer().HasSynced |
| 96 | + factory.Start(ctx.Done()) |
| 97 | + if !waitForSync(ctx, synced) { |
| 98 | + return errors.New("failed to sync pod informer cache") |
| 99 | + } |
| 100 | + |
| 101 | + reg := prometheus.NewRegistry() |
| 102 | + reg.MustRegister(&vgpumonitor.Collector{ |
| 103 | + NodeName: nodeName, |
| 104 | + CgroupRoot: cgroupRoot, |
| 105 | + DRMRoot: drmRoot, |
| 106 | + Pods: func() ([]*corev1.Pod, error) { |
| 107 | + return podLister.List(labels.Everything()) |
| 108 | + }, |
| 109 | + Node: func() (*corev1.Node, error) { |
| 110 | + return clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{}) |
| 111 | + }, |
| 112 | + }) |
| 113 | + glog.Infof("Serving AMD metrics for node %s on %s", nodeName, listener.Addr()) |
| 114 | + return serveMetrics(ctx, listener, reg) |
| 115 | +} |
| 116 | + |
| 117 | +func waitForSync(ctx context.Context, synced func() bool) bool { |
| 118 | + for !synced() { |
| 119 | + select { |
| 120 | + case <-ctx.Done(): |
| 121 | + return false |
| 122 | + case <-time.After(100 * time.Millisecond): |
| 123 | + } |
| 124 | + } |
| 125 | + return true |
| 126 | +} |
| 127 | + |
| 128 | +// serveMetrics serves reg on listener until ctx is done or the server fails. |
| 129 | +func serveMetrics(ctx context.Context, listener net.Listener, reg *prometheus.Registry) error { |
| 130 | + mux := http.NewServeMux() |
| 131 | + mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) |
| 132 | + server := &http.Server{Handler: mux, ReadHeaderTimeout: 15 * time.Second, ReadTimeout: 60 * time.Second} |
| 133 | + |
| 134 | + errCh := make(chan error, 1) |
| 135 | + go func() { errCh <- server.Serve(listener) }() |
| 136 | + select { |
| 137 | + case err := <-errCh: |
| 138 | + return err |
| 139 | + case <-ctx.Done(): |
| 140 | + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 141 | + defer cancel() |
| 142 | + return server.Shutdown(shutdownCtx) |
| 143 | + } |
| 144 | +} |
0 commit comments