-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
163 lines (150 loc) · 4.67 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package agent
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/datawire/ambassador-agent/rpc/agent"
"github.com/datawire/dlib/dhttp"
"github.com/datawire/dlib/dlog"
emissaryApi "github.com/emissary-ingress/emissary/v3/pkg/api/getambassador.io/v3alpha1"
"github.com/emissary-ingress/emissary/v3/pkg/snapshot/v1"
)
func (a *Agent) Service(ctx context.Context) error {
svr := grpc.NewServer()
agent.RegisterAgentServer(svr, a)
sc := &dhttp.ServerConfig{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
svr.ServeHTTP(w, r)
}),
}
return sc.ListenAndServe(ctx, net.JoinHostPort(a.ServerHost, strconv.Itoa(int(a.ServerPort))))
}
func (a *Agent) ResolveIngress(ctx context.Context, request *agent.IngressInfoRequest) (*agent.IngressInfoResponse, error) {
r, msg := a.getSnapshotIngress(request)
if r != nil {
return r, nil
}
dlog.Debugf(ctx, "%s, returning default ingress info", msg)
return a.getDefaultIngressInfo(request), nil
}
func (a *Agent) Version(context.Context, *emptypb.Empty) (*agent.VersionInfo, error) {
return &agent.VersionInfo{
Name: "ambassador-agent",
Version: Version,
}, nil
}
func (a *Agent) getDefaultIngressInfo(request *agent.IngressInfoRequest) *agent.IngressInfoResponse {
fqdn := fmt.Sprintf("%s.%s.svc.%s", request.ServiceName, request.Namespace, a.clusterDomain)
return &agent.IngressInfoResponse{
L3Host: fqdn,
Port: request.ServicePortNumber,
L4Proto: request.ServicePortProto,
L5Host: fqdn,
UseTls: request.ServicePortName == "https" || request.ServicePortNumber == 443,
}
}
func (a *Agent) getSnapshotIngress(request *agent.IngressInfoRequest) (*agent.IngressInfoResponse, string) {
a.currentSnapshotMutex.Lock()
sn := a.currentSnapshot
a.currentSnapshotMutex.Unlock()
if sn == nil {
return nil, "No current snapshot"
}
ksn := sn.Kubernetes
if ksn == nil {
return nil, "No Kubernetes snapshot in current snapshot"
}
svc := findServiceInSnapshot(ksn, types.UID(request.ServiceId))
if svc == nil {
return nil, fmt.Sprintf("No snapshot found for service %q", request.ServiceId)
}
mappings := findServiceMappingsInSnapshot(ksn, svc.Name, svc.Namespace)
hostName := findHostname(mappings)
if hostName == "" {
return nil, fmt.Sprintf("Could not resolve hostname in mappings of service %q", request.ServiceId)
}
ingressSvc := findIngressByNameInSnapshot(ksn, "emissary-ingress", "edge-stack", "ambassador")
if ingressSvc == nil {
return nil, "No ingress candidate found in cluster"
}
port, proto, useTLS := resolveIngressPort(ingressSvc.Spec.Ports, core.Protocol(request.ServicePortProto))
return &agent.IngressInfoResponse{
L3Host: fmt.Sprintf("%s.%s.svc.%s", ingressSvc.Name, ingressSvc.Namespace, a.clusterDomain),
L4Proto: string(proto),
Port: port,
L5Host: hostName,
UseTls: useTLS,
}, ""
}
func findServiceInSnapshot(snapshot *snapshot.KubernetesSnapshot, serviceID types.UID) *core.Service {
for _, svc := range snapshot.Services {
if svc.UID == serviceID {
return svc
}
}
return nil
}
func findIngressByNameInSnapshot(snapshot *snapshot.KubernetesSnapshot, names ...string) *core.Service {
for _, name := range names {
for _, svc := range snapshot.Services {
if svc.Name == name && len(svc.Spec.Ports) > 0 {
return svc
}
}
}
return nil
}
func findServiceMappingsInSnapshot(snapshot *snapshot.KubernetesSnapshot, name, namespace string) []*emissaryApi.Mapping {
mm := make(map[types.UID]*emissaryApi.Mapping)
for _, m := range snapshot.Mappings {
if m.Namespace == namespace {
// Parse the service. It might be a URL, or simply a hostname.
svc := m.Spec.Service
if svc == name {
mm[m.UID] = m
} else if parsedURL, err := url.Parse(svc); err == nil && parsedURL.Hostname() == name {
mm[m.UID] = m
}
}
}
sm := make([]*emissaryApi.Mapping, len(mm))
i := 0
for _, m := range mm {
sm[i] = m
i++
}
return sm
}
func findHostname(mappings []*emissaryApi.Mapping) string {
for _, m := range mappings {
hostname := m.Spec.Hostname
if hostname != "" && !strings.HasPrefix(hostname, "*") {
return hostname
}
}
return ""
}
func resolveIngressPort(ports []core.ServicePort, proto core.Protocol) (int32, core.Protocol, bool) {
for i := range ports {
p := &ports[i]
if p.Protocol == proto && p.Port == 443 || p.Name == "https" {
return p.Port, p.Protocol, true
}
}
for i := range ports {
p := &ports[i]
if p.Protocol == proto && p.Port == 80 || p.Name == "http" {
return p.Port, p.Protocol, false
}
}
p := &ports[0]
return p.Port, p.Protocol, false
}