generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 71
/
service.go
109 lines (104 loc) · 3.73 KB
/
service.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package kubernetes
import (
"context"
"net"
"net/url"
"github.com/layer5io/meshkit/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// ServiceOptions give control of which service to discover and which port to discover.
type ServiceOptions struct {
Name string // Name of the kubernetes service
Namespace string // Namespace of the kubernetes service
PortSelector string // To specify the name of the kubernetes service port
APIServerURL string // Kubernetes api-server URL (Used in-case of minikube)
WorkerNodeIP string // Kubernetes worker node IP address (Any), in case of a kubeadm based cluster orchestration
Mock *utils.MockOptions
}
// GetServiceEndpoint returns the endpoint for the given service
func GetServiceEndpoint(ctx context.Context, client kubernetes.Interface, opts *ServiceOptions) (*utils.Endpoint, error) {
obj, err := client.CoreV1().Services(opts.Namespace).Get(ctx, opts.Name, metav1.GetOptions{})
if err != nil {
return nil, ErrServiceDiscovery(err)
}
return GetEndpoint(ctx, opts, obj)
}
// GetEndpoint returns those endpoints in the given service which match the selector. Eg: service name = "client"
func GetEndpoint(ctx context.Context, opts *ServiceOptions, obj *corev1.Service) (*utils.Endpoint, error) {
var nodePort, clusterPort int32
endpoint := utils.Endpoint{}
if opts.WorkerNodeIP == "" {
opts.WorkerNodeIP = "localhost"
}
for _, port := range obj.Spec.Ports {
nodePort = port.NodePort
clusterPort = port.Port
if opts.PortSelector != "" && port.Name == opts.PortSelector {
break
}
}
// get clusterip endpoint
endpoint.Internal = &utils.HostPort{
Address: obj.Spec.ClusterIP,
Port: clusterPort,
}
// Initialize nodePort type endpoint
endpoint.External = &utils.HostPort{
Address: opts.WorkerNodeIP,
Port: nodePort,
}
if obj.Status.Size() > 0 && obj.Status.LoadBalancer.Size() > 0 && len(obj.Status.LoadBalancer.Ingress) > 0 && obj.Status.LoadBalancer.Ingress[0].Size() > 0 {
if obj.Status.LoadBalancer.Ingress[0].IP == "" {
endpoint.External.Address = obj.Status.LoadBalancer.Ingress[0].Hostname
endpoint.External.Port = clusterPort
} else if obj.Status.LoadBalancer.Ingress[0].IP == obj.Spec.ClusterIP || obj.Status.LoadBalancer.Ingress[0].IP == "<pending>" {
if opts.APIServerURL != "" {
url, err := url.Parse(opts.APIServerURL)
if err != nil {
return nil, ErrInvalidAPIServer
}
host, _, err := net.SplitHostPort(url.Host)
if err != nil {
return nil, ErrInvalidAPIServer
}
endpoint.External.Address = host
endpoint.External.Port = nodePort
} else {
endpoint.External.Address = obj.Spec.ClusterIP
endpoint.External.Port = clusterPort
}
} else {
endpoint.External.Address = obj.Status.LoadBalancer.Ingress[0].IP
endpoint.External.Port = clusterPort
}
}
// Service Type ClusterIP
if endpoint.External.Port == 0 {
return &utils.Endpoint{
Internal: endpoint.Internal,
}, nil
}
// If external endpoint not reachable
if !utils.TcpCheck(endpoint.External, opts.Mock) && endpoint.External.Address != "localhost" {
url, err := url.Parse(opts.APIServerURL)
if err != nil {
return &endpoint, ErrInvalidAPIServer
}
host, _, err := net.SplitHostPort(url.Host)
if err != nil {
return &endpoint, ErrInvalidAPIServer
}
// Set to APIServer host (For minikube specific clusters)
endpoint.External.Address = host
// If still unable to reach, change to resolve to clusterPort
if !utils.TcpCheck(endpoint.External, opts.Mock) && endpoint.External.Address != "localhost" {
endpoint.External.Port = nodePort
if !utils.TcpCheck(endpoint.External, opts.Mock) {
return &endpoint, ErrEndpointNotFound
}
}
}
return &endpoint, nil
}