Skip to content

Commit

Permalink
Don't wait too long for an answer from API Server
Browse files Browse the repository at this point in the history
If Multus plugin gets a DEL request, but the API Server is down (e.g.
via 'crictl rmp'), the call takes so long, it actually never finishes.
This prevents CRI-O from deleting the Pods.
  • Loading branch information
pmtk committed Dec 19, 2024
1 parent 4fc16b3 commit 1f49553
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkg/k8sclient/k8sclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ func (c *ClientInfo) GetPod(namespace, name string) (*v1.Pod, error) {
return c.Client.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
}

// GetPodContext gets pod from kubernetes with context
func (c *ClientInfo) GetPodContext(ctx context.Context, namespace, name string) (*v1.Pod, error) {
if c.PodInformer != nil {
logging.Debugf("GetPod for [%s/%s] will use informer cache", namespace, name)
return listers.NewPodLister(c.PodInformer.GetIndexer()).Pods(namespace).Get(name)
}
return c.Client.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
}

// DeletePod deletes a pod from kubernetes
func (c *ClientInfo) DeletePod(namespace, name string) error {
return c.Client.CoreV1().Pods(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
Expand Down
7 changes: 5 additions & 2 deletions pkg/multus/multus.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,9 @@ func GetPod(kubeClient *k8s.ClientInfo, k8sArgs *types.K8sArgs, isDel bool) (*v1
var pod *v1.Pod
if err := wait.PollImmediate(pollDuration, shortPollTimeout, func() (bool, error) {
var getErr error
pod, getErr = kubeClient.GetPod(podNamespace, podName)
// Use context with a short timeout so the call to API server doesn't take too long.
ctx, _ := context.WithTimeout(context.TODO(), pollDuration)

Check failure on line 546 in pkg/multus/multus.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak

Check failure on line 546 in pkg/multus/multus.go

View workflow job for this annotation

GitHub Actions / test (1.23.x, ubuntu-latest)

the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak
pod, getErr = kubeClient.GetPodContext(ctx, podNamespace, podName)
if isCriticalRequestRetriable(getErr) || retryOnNotFound(getErr) {
return false, nil
}
Expand All @@ -555,7 +557,8 @@ func GetPod(kubeClient *k8s.ClientInfo, k8sArgs *types.K8sArgs, isDel bool) (*v1
// Try one more time to get the pod directly from the apiserver;
// TODO: figure out why static pods don't show up via the informer
// and always hit this case.
pod, err = kubeClient.GetPod(podNamespace, podName)
ctx, _ := context.WithTimeout(context.TODO(), pollDuration)

Check failure on line 560 in pkg/multus/multus.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak

Check failure on line 560 in pkg/multus/multus.go

View workflow job for this annotation

GitHub Actions / test (1.23.x, ubuntu-latest)

the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak
pod, err = kubeClient.GetPodContext(ctx, podNamespace, podName)
if err != nil {
return nil, cmdErr(k8sArgs, "error waiting for pod: %v", err)
}
Expand Down

0 comments on commit 1f49553

Please sign in to comment.