This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
util.go
79 lines (63 loc) · 2.91 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package kubernetes
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
)
const (
clusterDomain = "cluster.local"
defaultAppProtocol = "http"
)
// GetHostnamesForService returns a list of hostnames over which the service can be accessed within the local cluster.
// If 'sameNamespace' is set to true, then the shorthand hostnames service and service:port are also returned.
func GetHostnamesForService(service *corev1.Service, sameNamespace bool) []string {
var domains []string
if service == nil {
return domains
}
serviceName := service.Name
namespace := service.Namespace
if sameNamespace {
// Within the same namespace, service name is resolvable to its address
domains = append(domains, serviceName) // service
}
domains = append(domains, fmt.Sprintf("%s.%s", serviceName, namespace)) // service.namespace
domains = append(domains, fmt.Sprintf("%s.%s.svc", serviceName, namespace)) // service.namespace.svc
domains = append(domains, fmt.Sprintf("%s.%s.svc.cluster", serviceName, namespace)) // service.namespace.svc.cluster
domains = append(domains, fmt.Sprintf("%s.%s.svc.%s", serviceName, namespace, clusterDomain)) // service.namespace.svc.cluster.local
for _, portSpec := range service.Spec.Ports {
port := portSpec.Port
if sameNamespace {
// Within the same namespace, service name is resolvable to its address
domains = append(domains, fmt.Sprintf("%s:%d", serviceName, port)) // service:port
}
domains = append(domains, fmt.Sprintf("%s.%s:%d", serviceName, namespace, port)) // service.namespace:port
domains = append(domains, fmt.Sprintf("%s.%s.svc:%d", serviceName, namespace, port)) // service.namespace.svc:port
domains = append(domains, fmt.Sprintf("%s.%s.svc.cluster:%d", serviceName, namespace, port)) // service.namespace.svc.cluster:port
domains = append(domains, fmt.Sprintf("%s.%s.svc.%s:%d", serviceName, namespace, clusterDomain, port)) // service.namespace.svc.cluster.local:port
}
return domains
}
// GetServiceFromHostname returns the service name from its hostname
func GetServiceFromHostname(host string) string {
// The service name is the first string in the host name for a service.
// Ex. service.namespace, service.namespace.cluster.local
service := strings.Split(host, ".")[0]
// For services that are not namespaced the service name contains the port as well
// Ex. service:port
return strings.Split(service, ":")[0]
}
// GetAppProtocolFromPortName returns the port's application protocol from its name, 'defaultAppProtocol' if not specified.
func GetAppProtocolFromPortName(portName string) string {
portName = strings.ToLower(portName)
switch {
case strings.HasPrefix(portName, "http-"):
return "http"
case strings.HasPrefix(portName, "tcp-"):
return "tcp"
case strings.HasPrefix(portName, "grpc-"):
return "grpc"
default:
return defaultAppProtocol
}
}