Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance pod readiness check for headless services #585

Open
mojtaba-esk opened this issue Nov 22, 2024 · 0 comments
Open

Enhance pod readiness check for headless services #585

mojtaba-esk opened this issue Nov 22, 2024 · 0 comments

Comments

@mojtaba-esk
Copy link
Member

          _:hammer_and_wrench: Refactor suggestion_

Enhance pod readiness check for headless services

The current implementation only checks if the pod phase is Running, which might not accurately reflect the readiness of the containers within the pod. Consider checking container ready conditions for more accurate readiness determination.

 func (c *Client) isHeadlessServiceReady(ctx context.Context, service *v1.Service) (bool, error) {
     pods, err := c.clientset.CoreV1().Pods(service.Namespace).List(ctx, metav1.ListOptions{
         LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{
             MatchLabels: service.Spec.Selector,
         }),
     })
     if err != nil {
         return false, ErrGettingPodsForService.WithParams(service.Name).Wrap(err)
     }
     // Check if at least one pod is ready
     for _, pod := range pods.Items {
-        if pod.Status.Phase == v1.PodRunning {
+        if pod.Status.Phase == v1.PodRunning {
+            // Check if all containers are ready
+            allContainersReady := true
+            for _, condition := range pod.Status.Conditions {
+                if condition.Type == v1.PodReady && condition.Status != v1.ConditionTrue {
+                    allContainersReady = false
+                    break
+                }
+            }
+            if allContainersReady {
                 return true, nil
+            }
         }
     }
     return false, nil
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

func (c *Client) isHeadlessServiceReady(ctx context.Context, service *v1.Service) (bool, error) {
	// For headless services, we check if the service has any pods ready
	pods, err := c.clientset.CoreV1().Pods(service.Namespace).List(ctx, metav1.ListOptions{
		LabelSelector: metav1.FormatLabelSelector(&metav1.LabelSelector{
			MatchLabels: service.Spec.Selector,
		}),
	})
	if err != nil {
		return false, ErrGettingPodsForService.WithParams(service.Name).Wrap(err)
	}
	// Check if at least one pod is ready
	for _, pod := range pods.Items {
		if pod.Status.Phase == v1.PodRunning {
			// Check if all containers are ready
			allContainersReady := true
			for _, condition := range pod.Status.Conditions {
				if condition.Type == v1.PodReady && condition.Status != v1.ConditionTrue {
					allContainersReady = false
					break
				}
			}
			if allContainersReady {
				return true, nil
			}
		}
	}
	return false, nil
}

Originally posted by @coderabbitai[bot] in #574 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant