sriov-network-operator icon indicating copy to clipboard operation
sriov-network-operator copied to clipboard

How can I query which VF is allocated to which pod?

Open Panlichen opened this issue 3 years ago • 3 comments

Now I can get this allocation info by running ibv_devices inside the pod(container), is there a more elegant way to query it? Like exposed as a k8s API resource.

Panlichen avatar Jul 16 '21 09:07 Panlichen

// infiniband_controller.go
package main

import (
    "context"
    "fmt"
    "os"
    "time"

    "github.com/kubernetes/client-go/kubernetes"
    "github.com/kubernetes/client-go/rest"
    "github.com/kubernetes/client-go/tools/clientcmd"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
    // Initialize Kubernetes client
    var config *rest.Config
    if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" {
        config, _ = clientcmd.BuildConfigFromFlags("", kubeconfig)
    } else {
        config, _ = rest.InClusterConfig()
    }
    clientset, _ := kubernetes.NewForConfig(config)

    // Watch for changes to pods
    podClient := clientset.CoreV1().Pods("")
    podWatch, _ := podClient.Watch(context.TODO(), metav1.ListOptions{})

    // Start controller loop
    for {
        select {
        case event := <-podWatch.ResultChan():
            pod := event.Object.(*corev1.Pod)
            // Logic to allocate InfiniBand devices to pod
            // Update InfiniBandDevice CRD accordingly
            fmt.Printf("Pod %s updated\n", pod.Name)
        case <-time.After(30 * time.Second):
            // Periodically reconcile state
            fmt.Println("Reconciling state...")
        }
    }
}

// infiniband_controller.go
package main

import (
    "context"
    "fmt"
    "os"
    "time"

    "github.com/kubernetes/client-go/kubernetes"
    "github.com/kubernetes/client-go/rest"
    "github.com/kubernetes/client-go/tools/clientcmd"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func main() {
    // Initialize Kubernetes client
    var config *rest.Config
    if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" {
        config, _ = clientcmd.BuildConfigFromFlags("", kubeconfig)
    } else {
        config, _ = rest.InClusterConfig()
    }
    clientset, _ := kubernetes.NewForConfig(config)

    // Watch for changes to pods
    podClient := clientset.CoreV1().Pods("")
    podWatch, _ := podClient.Watch(context.TODO(), metav1.ListOptions{})

    // Start controller loop
    for {
        select {
        case event := <-podWatch.ResultChan():
            pod := event.Object.(*corev1.Pod)
            // Logic to allocate InfiniBand devices to pod
            // Update InfiniBandDevice CRD accordingly
            fmt.Printf("Pod %s updated\n", pod.Name)
        case <-time.After(30 * time.Second):
            // Periodically reconcile state
            fmt.Println("Reconciling state...")
        }
    }
}

ljluestc avatar Mar 30 '24 17:03 ljluestc

you should also be able to see in the pod network-status annotation the PCI address

SchSeba avatar Mar 31 '24 13:03 SchSeba

@Panlichen did the comments help? can we close this issue?

SchSeba avatar Apr 24 '24 10:04 SchSeba

closing this issue

SchSeba avatar Aug 19 '24 13:08 SchSeba