func GetServiceAddressAndPort()

in pkg/helpers/k8s_utils.go [28:65]


func GetServiceAddressAndPort(service *corev1.Service) (string, int32) {
	var servicePort int32
	if len(service.Spec.Ports) > 0 {
		servicePort = service.Spec.Ports[0].Port
	}

	switch service.Spec.Type {
	case corev1.ServiceTypeLoadBalancer:
		if !IsAppRunningInsideK8s() {
			// The Application is running outside the K8s Cluster.
			// The service should be accessible via load balancer if the provider supports it.
			ingressPoints := service.Status.LoadBalancer.Ingress
			if len(ingressPoints) != 0 {
				ingressPoint := ingressPoints[0]
				if ingressPoint.IP != "" {
					return ingressPoint.IP, servicePort
				} else {
					return ingressPoint.Hostname, servicePort
				}
			} else {
				// ingress points not available
				return "", servicePort
			}
		}
		// The application is running inside the K8s Cluster - return ClusterIP
		fallthrough
	case corev1.ServiceTypeNodePort, corev1.ServiceTypeClusterIP:
		// The service should be accessible via ClusterIP.
		clusterIP := service.Spec.ClusterIP
		if clusterIP == corev1.ClusterIPNone {
			// this is a headless service
			return "", servicePort
		}
		return clusterIP, servicePort
	default:
		panic("service type not supported by GetServiceAddress yet")
	}
}