fix: kubeclt describe service endpoints shows endpoints that are not ready

Kubernetes-commit: 64e2066e5c823eaa958fcbdd6024d0253688f36c
This commit is contained in:
zhifei92 2024-08-27 12:42:37 +08:00 committed by Kubernetes Publisher
parent 5f5894cd61
commit f3a7f811d0
2 changed files with 93 additions and 10 deletions

View File

@ -5452,6 +5452,15 @@ func formatEndpointSlices(endpointSlices []discoveryv1.EndpointSlice, ports sets
if len(list) == max {
more = true
}
isReady := endpointSlices[i].Endpoints[j].Conditions.Ready == nil || *endpointSlices[i].Endpoints[j].Conditions.Ready
if !isReady {
// ready indicates that this endpoint is prepared to receive traffic,
// according to whatever system is managing the endpoint. A nil value
// indicates an unknown state. In most cases consumers should interpret this
// unknown state as ready.
// More info: vendor/k8s.io/api/discovery/v1/types.go
continue
}
if !more {
list = append(list, endpointSlices[i].Endpoints[j].Addresses[0])
}
@ -5467,6 +5476,15 @@ func formatEndpointSlices(endpointSlices []discoveryv1.EndpointSlice, ports sets
more = true
}
addr := endpointSlices[i].Endpoints[k].Addresses[0]
isReady := endpointSlices[i].Endpoints[k].Conditions.Ready == nil || *endpointSlices[i].Endpoints[k].Conditions.Ready
if !isReady {
// ready indicates that this endpoint is prepared to receive traffic,
// according to whatever system is managing the endpoint. A nil value
// indicates an unknown state. In most cases consumers should interpret this
// unknown state as ready.
// More info: vendor/k8s.io/api/discovery/v1/types.go
continue
}
if !more {
hostPort := net.JoinHostPort(addr, strconv.Itoa(int(*port.Port)))
list = append(list, hostPort)

View File

@ -779,9 +779,9 @@ func TestDescribeService(t *testing.T) {
},
},
Endpoints: []discoveryv1.Endpoint{
{Addresses: []string{"10.244.0.1"}},
{Addresses: []string{"10.244.0.2"}},
{Addresses: []string{"10.244.0.3"}},
{Addresses: []string{"10.244.0.1"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)}},
{Addresses: []string{"10.244.0.2"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)}},
{Addresses: []string{"10.244.0.3"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)}},
},
Ports: []discoveryv1.EndpointPort{{
Name: ptr.To("port-tcp"),
@ -857,8 +857,8 @@ func TestDescribeService(t *testing.T) {
},
},
Endpoints: []discoveryv1.Endpoint{
{Addresses: []string{"10.244.0.1"}},
{Addresses: []string{"10.244.0.2"}},
{Addresses: []string{"10.244.0.1"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)}},
{Addresses: []string{"10.244.0.2"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)}},
},
Ports: []discoveryv1.EndpointPort{{
Name: ptr.To("port-tcp"),
@ -875,9 +875,9 @@ func TestDescribeService(t *testing.T) {
},
},
Endpoints: []discoveryv1.Endpoint{
{Addresses: []string{"10.244.0.3"}},
{Addresses: []string{"10.244.0.4"}},
{Addresses: []string{"10.244.0.5"}},
{Addresses: []string{"10.244.0.3"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(false)}},
{Addresses: []string{"10.244.0.4"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)}},
{Addresses: []string{"10.244.0.5"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)}},
},
Ports: []discoveryv1.EndpointPort{{
Name: ptr.To("port-tcp"),
@ -901,7 +901,7 @@ func TestDescribeService(t *testing.T) {
Port: port-tcp 8080/TCP
TargetPort: targetPort/TCP
NodePort: port-tcp 31111/TCP
Endpoints: 10.244.0.1:9527,10.244.0.2:9527,10.244.0.3:9527 + 2 more...
Endpoints: 10.244.0.1:9527,10.244.0.2:9527,10.244.0.4:9527 + 1 more...
Session Affinity: None
External Traffic Policy: Local
Internal Traffic Policy: Local
@ -909,6 +909,71 @@ func TestDescribeService(t *testing.T) {
Events: <none>
`)[1:],
},
{
name: "test-ready-field-empty",
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "foo",
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
Ports: []corev1.ServicePort{{
Name: "port-tcp",
Port: 8080,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromInt32(9527),
NodePort: 31111,
}},
Selector: map[string]string{"blah": "heh"},
ClusterIP: "1.2.3.4",
IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol},
LoadBalancerIP: "5.6.7.8",
SessionAffinity: corev1.ServiceAffinityNone,
ExternalTrafficPolicy: corev1.ServiceExternalTrafficPolicyLocal,
InternalTrafficPolicy: ptr.To(corev1.ServiceInternalTrafficPolicyCluster),
HealthCheckNodePort: 32222,
},
},
endpointSlices: []*discoveryv1.EndpointSlice{{
ObjectMeta: metav1.ObjectMeta{
Name: "bar-abcdef",
Namespace: "foo",
Labels: map[string]string{
"kubernetes.io/service-name": "bar",
},
},
Endpoints: []discoveryv1.Endpoint{
{Addresses: []string{"10.244.0.1"}},
},
Ports: []discoveryv1.EndpointPort{{
Name: ptr.To("port-tcp"),
Port: ptr.To[int32](9527),
Protocol: ptr.To(corev1.ProtocolTCP),
}},
}},
expected: dedent.Dedent(`
Name: bar
Namespace: foo
Labels: <none>
Annotations: <none>
Selector: blah=heh
Type: LoadBalancer
IP Families: IPv4
IP: 1.2.3.4
IPs: <none>
Desired LoadBalancer IP: 5.6.7.8
Port: port-tcp 8080/TCP
TargetPort: 9527/TCP
NodePort: port-tcp 31111/TCP
Endpoints: 10.244.0.1:9527
Session Affinity: None
External Traffic Policy: Local
Internal Traffic Policy: Cluster
HealthCheck NodePort: 32222
Events: <none>
`)[1:],
},
{
name: "test-ServiceIPFamily",
service: &corev1.Service{
@ -943,7 +1008,7 @@ func TestDescribeService(t *testing.T) {
},
},
Endpoints: []discoveryv1.Endpoint{
{Addresses: []string{"10.244.0.1"}},
{Addresses: []string{"10.244.0.1"}, Conditions: discoveryv1.EndpointConditions{Ready: ptr.To(true)}},
},
Ports: []discoveryv1.EndpointPort{{
Name: ptr.To("port-tcp"),