Skip to content

Commit

Permalink
Optimize monitor (Project-HAMi#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
for800000 authored Dec 5, 2024
1 parent 2066410 commit 54b8f74
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cmd/vGPUmonitor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package main

import (
"fmt"
"github.com/Project-HAMi/HAMi/pkg/util"
"log"
"net/http"
"os"
"strings"
"time"

Expand Down Expand Up @@ -229,8 +231,8 @@ func (cc ClusterManagerCollector) Collect(ch chan<- prometheus.Metric) {

}
}

pods, err := cc.ClusterManager.PodLister.List(labels.Everything())
nodeName := os.Getenv(util.NodeNameEnvName)
pods, err := cc.ClusterManager.PodLister.List(labels.SelectorFromSet(labels.Set{util.AssignedNodeAnnotations: nodeName}))
if err != nil {
klog.Error("failed to list pods with err=", err.Error())
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/monitor/nvidia/cudevshr.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"github.com/Project-HAMi/HAMi/pkg/util"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -122,7 +123,13 @@ func (l *ContainerLister) Clientset() *kubernetes.Clientset {
}

func (l *ContainerLister) Update() error {
pods, err := l.clientset.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{})
nodename := os.Getenv(util.NodeNameEnvName)
if nodename == "" {
return fmt.Errorf("env %s not set", util.NodeNameEnvName)
}
pods, err := l.clientset.CoreV1().Pods("").List(context.Background(), metav1.ListOptions{
FieldSelector: fmt.Sprintf("spec.nodeName=%s", nodename),
})
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ func PatchNodeAnnotations(node *corev1.Node, annotations map[string]string) erro
func PatchPodAnnotations(pod *corev1.Pod, annotations map[string]string) error {
type patchMetadata struct {
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}
type patchPod struct {
Metadata patchMetadata `json:"metadata"`
Expand All @@ -371,6 +372,11 @@ func PatchPodAnnotations(pod *corev1.Pod, annotations map[string]string) error {

p := patchPod{}
p.Metadata.Annotations = annotations
label := make(map[string]string)
if v, ok := annotations[AssignedNodeAnnotations]; ok && v != "" {
label[AssignedNodeAnnotations] = v
p.Metadata.Labels = label
}

bytes, err := json.Marshal(p)
if err != nil {
Expand Down

0 comments on commit 54b8f74

Please sign in to comment.