// GetSandboxPodIP gets the IP address of the pod corresponding to the Sandbox
func (c *K8sClient) GetSandboxPodIP(_ context.Context, namespace, sandboxName, podName string) (string, error) {
// If podName is provided, try to get it directly from cache first
if podName != "" {
pod, err := c.podLister.Pods(namespace).Get(podName)
if err == nil && pod != nil {
return validateAndGetPodIP(pod)
}
klog.Infof("failed to get sandbox pod %s/%s: %v, try get pod by sandbox-name label", namespace, podName, err)
}
// Find pod through label selector (sandbox-name label we set)
pods, err := c.podLister.Pods(namespace).List(labels.SelectorFromSet(map[string]string{SandboxNameLabelKey: sandboxName}))
if err != nil {
return "", fmt.Errorf("failed to list pods from cache: %w", err)
}
// Find the pod that belongs to this sandbox by checking ownerReferences
for _, pod := range pods {
for _, ownerRef := range pod.OwnerReferences {
if ownerRef.Kind == "Sandbox" && ownerRef.Name == sandboxName {
if ownerRef.Controller == nil || *ownerRef.Controller {
return validateAndGetPodIP(pod)
}
}
}
}
return "", fmt.Errorf("no pod found for sandbox %s", sandboxName)
}```
After
kubernetes-sigs/agent-sandbox#272 merged, each sanbox will have an annotation pointed to the pod.
So we donot need to do
label selectormatching, not this depend on agent-sandbox v0.3.10