forked from linkerd/linkerd2
/
listener.go
213 lines (183 loc) · 5.05 KB
/
listener.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package destination
import (
pb "github.com/linkerd/linkerd2-proxy-api/go/destination"
net "github.com/linkerd/linkerd2-proxy-api/go/net"
"github.com/linkerd/linkerd2/pkg/addr"
pkgK8s "github.com/linkerd/linkerd2/pkg/k8s"
log "github.com/sirupsen/logrus"
coreV1 "k8s.io/api/core/v1"
)
type updateListener interface {
Update(add, remove []*updateAddress)
ClientClose() <-chan struct{}
ServerClose() <-chan struct{}
NoEndpoints(exists bool)
SetServiceId(id *serviceId)
Stop()
}
type ownerKindAndNameFn func(*coreV1.Pod) (string, string)
// updateAddress is a pairing of TCP address to Kubernetes pod object
type updateAddress struct {
address *net.TcpAddress
pod *coreV1.Pod
}
func diffUpdateAddresses(oldAddrs, newAddrs []*updateAddress) ([]*updateAddress, []*updateAddress) {
addSet := make(map[string]*updateAddress)
removeSet := make(map[string]*updateAddress)
for _, a := range newAddrs {
key := addr.ProxyAddressToString(a.address)
addSet[key] = a
}
for _, a := range oldAddrs {
key := addr.ProxyAddressToString(a.address)
delete(addSet, key)
removeSet[key] = a
}
for _, a := range newAddrs {
key := addr.ProxyAddressToString(a.address)
delete(removeSet, key)
}
add := make([]*updateAddress, 0)
for _, a := range addSet {
add = append(add, a)
}
remove := make([]*updateAddress, 0)
for _, a := range removeSet {
remove = append(remove, a)
}
return add, remove
}
// implements the updateListener interface
type endpointListener struct {
stream pb.Destination_GetServer
ownerKindAndName ownerKindAndNameFn
labels map[string]string
enableTLS bool
stopCh chan struct{}
}
func newEndpointListener(
stream pb.Destination_GetServer,
ownerKindAndName ownerKindAndNameFn,
enableTLS bool,
) *endpointListener {
return &endpointListener{
stream: stream,
ownerKindAndName: ownerKindAndName,
labels: make(map[string]string),
enableTLS: enableTLS,
stopCh: make(chan struct{}),
}
}
func (l *endpointListener) ClientClose() <-chan struct{} {
return l.stream.Context().Done()
}
func (l *endpointListener) ServerClose() <-chan struct{} {
return l.stopCh
}
func (l *endpointListener) Stop() {
close(l.stopCh)
}
func (l *endpointListener) SetServiceId(id *serviceId) {
if id != nil {
l.labels = map[string]string{
"namespace": id.namespace,
"service": id.name,
}
}
}
func (l *endpointListener) Update(add, remove []*updateAddress) {
if len(add) > 0 {
update := &pb.Update{
Update: &pb.Update_Add{
Add: l.toWeightedAddrSet(add),
},
}
err := l.stream.Send(update)
if err != nil {
log.Error(err)
}
}
if len(remove) > 0 {
update := &pb.Update{
Update: &pb.Update_Remove{
Remove: l.toAddrSet(remove),
},
}
err := l.stream.Send(update)
if err != nil {
log.Error(err)
}
}
}
func (l *endpointListener) NoEndpoints(exists bool) {
update := &pb.Update{
Update: &pb.Update_NoEndpoints{
NoEndpoints: &pb.NoEndpoints{
Exists: exists,
},
},
}
l.stream.Send(update)
}
func (l *endpointListener) toWeightedAddrSet(addresses []*updateAddress) *pb.WeightedAddrSet {
addrs := make([]*pb.WeightedAddr, 0)
for _, a := range addresses {
addrs = append(addrs, l.toWeightedAddr(a))
}
return &pb.WeightedAddrSet{
Addrs: addrs,
MetricLabels: l.labels,
}
}
func (l *endpointListener) toWeightedAddr(address *updateAddress) *pb.WeightedAddr {
labels, hint, tlsIdentity := l.getAddrMetadata(address.pod)
return &pb.WeightedAddr{
Addr: address.address,
Weight: 1,
MetricLabels: labels,
TlsIdentity: tlsIdentity,
ProtocolHint: hint,
}
}
func (l *endpointListener) toAddrSet(addresses []*updateAddress) *pb.AddrSet {
addrs := make([]*net.TcpAddress, 0)
for _, a := range addresses {
addrs = append(addrs, a.address)
}
return &pb.AddrSet{Addrs: addrs}
}
func (l *endpointListener) getAddrMetadata(pod *coreV1.Pod) (map[string]string, *pb.ProtocolHint, *pb.TlsIdentity) {
controllerNs := pod.Labels[pkgK8s.ControllerNSLabel]
ownerKind, ownerName := l.ownerKindAndName(pod)
labels := pkgK8s.GetPodLabels(ownerKind, ownerName, pod)
var hint *pb.ProtocolHint
// If the pod is controlled by us, then it can be hinted that this destination
// knows H2 (and handles our orig-proto translation). Note that this check
// does not verify that the pod's control plane matches the control plane
// where the destination service is running; all pods injected for all control
// planes are considered valid for providing the H2 hint.
if controllerNs != "" {
hint = &pb.ProtocolHint{
Protocol: &pb.ProtocolHint_H2_{
H2: &pb.ProtocolHint_H2{},
},
}
}
if !l.enableTLS {
return labels, hint, nil
}
identity := pkgK8s.TLSIdentity{
Name: ownerName,
Kind: ownerKind,
Namespace: pod.Namespace,
ControllerNamespace: controllerNs,
}
return labels, hint, &pb.TlsIdentity{
Strategy: &pb.TlsIdentity_K8SPodIdentity_{
K8SPodIdentity: &pb.TlsIdentity_K8SPodIdentity{
PodIdentity: identity.ToDNSName(),
ControllerNs: controllerNs,
},
},
}
}