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

fix: Implement proper checking of ClusterIPs health #1103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions modules/k8s/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func WaitUntilServiceAvailable(t testing.TestingT, options *KubectlOptions, serv

// For minikube, all services will be available immediately so we only do the check if we are not on
// minikube.
if !isMinikube && !IsServiceAvailable(service) {
if !isMinikube && !IsServiceAvailable(t, options, service) {
return "", NewServiceNotAvailableError(service)
}
return "Service is now available", nil
Expand All @@ -88,13 +88,29 @@ func WaitUntilServiceAvailable(t testing.TestingT, options *KubectlOptions, serv

// IsServiceAvailable returns true if the service endpoint is ready to accept traffic. Note that for Minikube, this
// function is moot as all services, even LoadBalancer, is available immediately.
func IsServiceAvailable(service *corev1.Service) bool {
// Only the LoadBalancer type has a delay. All other service types are available if the resource exists.
func IsServiceAvailable(t testing.TestingT, options *KubectlOptions, service *corev1.Service) bool {
clientset, err := GetKubernetesClientFromOptionsE(t, options)
if err != nil {
return false
}

switch service.Spec.Type {
// type: LoadBalancer has delays as ingress controllers wire up the backend services.
case corev1.ServiceTypeLoadBalancer:
ingress := service.Status.LoadBalancer.Ingress
// The load balancer is ready if it has at least one ingress point
return len(ingress) > 0
// type: ClusterIP is available when the Pods matched by the Service selector are in the Status == Ready
case corev1.ServiceTypeClusterIP:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense to handle NodePort services too?

endpoint, err := clientset.CoreV1().Endpoints(options.Namespace).Get(context.Background(), service.Name, metav1.GetOptions{})
if err != nil {
return false
}
activeEndpointCount := 0
for _, subset := range endpoint.Subsets {
activeEndpointCount += len(subset.Addresses)
}
return activeEndpointCount > 0
default:
return true
}
Expand Down