Service

2 min read

Read a Service (cluster IP, node port, load balancer IP and host) and its EndpointSlices, passed as one slice or a list: whether any endpoint is ready, and the endpoints as “ip:port”.

Reference

FunctionDescription
endpointsReadyReports whether an EndpointSlice (or a core/v1 Endpoints object) has at least one ready address.
hasEndpointsReports whether any endpoint in the EndpointSlices is ready.
serviceClusterIPReturns spec.clusterIP.
serviceEndpointCountReturns the number of endpoints (address and port pairs), ready or not.
serviceEndpointsReturns the ready endpoints as “ip:port”, one per address and port.
serviceFirstEndpointReturns the first ready endpoint as “ip:port”; "" when none is ready.
serviceLoadBalancerHostReturns the hostname assigned by the load balancer (status.loadBalancer.ingress[0].hostname).
serviceLoadBalancerIPReturns the external IP assigned by the load balancer (status.loadBalancer.ingress[0].ip).
serviceNodePortReturns the nodePort of the first Service port.

Functions

endpointsReady

endpointsReady reports whether an EndpointSlice (or a core/v1 Endpoints object) has at least one ready address. Pass the Service’s EndpointSlice, not the Service.

{{ endpointsReady (index .endpointSlices 0) }}  → true

hasEndpoints

hasEndpoints reports whether any endpoint in the EndpointSlices is ready.

{{ hasEndpoints .endpointSlices }}

serviceClusterIP

serviceClusterIP returns spec.clusterIP. Returns "" before the IP is assigned or when the Service is absent.

{{ serviceClusterIP .service }}  → "10.96.14.2"

serviceEndpointCount

serviceEndpointCount returns the number of endpoints (address and port pairs), ready or not.

{{ serviceEndpointCount .endpointSlices }}  → 3

serviceEndpoints

serviceEndpoints returns the ready endpoints as “ip:port”, one per address and port.

{{ serviceEndpoints .endpointSlices }}  → [10.0.0.1:8080 10.0.0.2:8080]

serviceFirstEndpoint

serviceFirstEndpoint returns the first ready endpoint as “ip:port”; "" when none is ready.

{{ serviceFirstEndpoint .endpointSlices }}  → "10.0.0.1:8080"

serviceLoadBalancerHost

serviceLoadBalancerHost returns the hostname assigned by the load balancer (status.loadBalancer.ingress[0].hostname). Cloud providers (AWS, GCP) typically assign a hostname rather than an IP.

{{ serviceLoadBalancerHost .service }}  → "abc123.us-east-1.elb.amazonaws.com"
https://{{ serviceLoadBalancerHost .service }}

serviceLoadBalancerIP

serviceLoadBalancerIP returns the external IP assigned by the load balancer (status.loadBalancer.ingress[0].ip). Returns "" while provisioning — cloud LB provisioning typically takes 30–90 seconds.

{{ serviceLoadBalancerIP .service }}  → "34.123.45.67"

serviceNodePort

serviceNodePort returns the nodePort of the first Service port. Returns 0 before the port is assigned.

{{ serviceNodePort .service }}  → 31234