Skip to content

Commit

Permalink
check: make (Service)Address() return an IP address
Browse files Browse the repository at this point in the history
change the Address method to return an IP address when available (i.e.
when ClusterIP is not empty, which happens in case of headless services)
instead of just returning the service name.

In case the IP family is set to any, this method will keep returning the
service name.

Signed-off-by: Gilberto Bertin <[email protected]>
  • Loading branch information
jibi authored and tklauser committed May 24, 2023
1 parent 0cf8153 commit 61491cb
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions connectivity/check/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

ciliumv2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"

"github.com/cilium/cilium-cli/k8s"
)
Expand Down Expand Up @@ -152,8 +153,30 @@ func (s Service) Path() string {
}

// Address returns the network address of the Service.
func (s Service) Address(IPFamily) string {
return s.Service.Name
func (s Service) Address(family IPFamily) string {
// If the cluster IP is empty (headless service case) or the IP family is set to any, return the service name
if s.Service.Spec.ClusterIP == "" || family == IPFamilyAny {
return s.Service.Name
}

getClusterIPForIPFamily := func(family v1.IPFamily) string {
for i, f := range s.Service.Spec.IPFamilies {
if f == family {
return s.Service.Spec.ClusterIPs[i]
}
}

return ""
}

switch family {
case IPFamilyV4:
return getClusterIPForIPFamily(v1.IPv4Protocol)
case IPFamilyV6:
return getClusterIPForIPFamily(v1.IPv6Protocol)
}

return ""
}

// Port returns the first port of the Service.
Expand Down

0 comments on commit 61491cb

Please sign in to comment.