-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
handler.go
191 lines (174 loc) · 5.68 KB
/
handler.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package peer
import (
"strings"
peerpb "github.com/cilium/cilium/api/v1/peer"
datapath "github.com/cilium/cilium/pkg/datapath/types"
ciliumDefaults "github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/hubble/defaults"
"github.com/cilium/cilium/pkg/hubble/peer/serviceoption"
"github.com/cilium/cilium/pkg/node/types"
)
// handler implements the datapath.NodeHandler interface so that it can be
// subscribed to a node manager and receives update about nodes being
// added/updated/deleted. Node update information is turned into
// peerpb.ChangeNotification and sent to the channel C. As this channel is
// unbuffered, clients must be ready to read from it before subscribing the
// handler to the node manager.
// Once not used anymore, Close must be called to free resources.
type handler struct {
stop chan struct{}
C chan *peerpb.ChangeNotification
tls bool
addressPref serviceoption.AddressFamilyPreference
}
func newHandler(withoutTLSInfo bool, addressPref serviceoption.AddressFamilyPreference) *handler {
return &handler{
stop: make(chan struct{}),
C: make(chan *peerpb.ChangeNotification),
tls: !withoutTLSInfo,
addressPref: addressPref,
}
}
func (h *handler) Name() string {
return "hubble-peer"
}
// Ensure that Service implements the NodeHandler interface so that it can be
// notified of nodes updates by the daemon's node manager.
var _ datapath.NodeHandler = (*handler)(nil)
// NodeAdd implements datapath.NodeHandler.NodeAdd.
func (h *handler) NodeAdd(n types.Node) error {
cn := h.newChangeNotification(n, peerpb.ChangeNotificationType_PEER_ADDED)
select {
case h.C <- cn:
case <-h.stop:
}
return nil
}
// NodeUpdate implements datapath.NodeHandler.NodeUpdate.
func (h *handler) NodeUpdate(o, n types.Node) error {
oAddr, nAddr := nodeAddress(o, h.addressPref), nodeAddress(n, h.addressPref)
if o.Fullname() == n.Fullname() {
if oAddr == nAddr {
// this corresponds to the same peer
// => no need to send a notification
return nil
}
cn := h.newChangeNotification(n, peerpb.ChangeNotificationType_PEER_UPDATED)
select {
case h.C <- cn:
case <-h.stop:
}
return nil
}
// the name has changed; from a service consumer perspective, this is the
// same as if the peer with the old name was removed and a new one added
ocn := h.newChangeNotification(o, peerpb.ChangeNotificationType_PEER_DELETED)
select {
case h.C <- ocn:
case <-h.stop:
return nil
}
ncn := h.newChangeNotification(n, peerpb.ChangeNotificationType_PEER_ADDED)
select {
case h.C <- ncn:
case <-h.stop:
}
return nil
}
// NodeDelete implements datapath.NodeHandler.NodeDelete.
func (h *handler) NodeDelete(n types.Node) error {
cn := h.newChangeNotification(n, peerpb.ChangeNotificationType_PEER_DELETED)
select {
case h.C <- cn:
case <-h.stop:
}
return nil
}
// AllNodeValidateImplementation implements
func (h handler) AllNodeValidateImplementation() {
}
// NodeValidateImplementation implements
// datapath.NodeHandler.NodeValidateImplementation. It is a no-op.
func (h handler) NodeValidateImplementation(_ types.Node) error {
// no-op
return nil
}
// NodeConfigurationChanged implements
// datapath.NodeHandler.NodeValidateImplementation. It is a no-op.
func (h handler) NodeConfigurationChanged(_ datapath.LocalNodeConfiguration) error {
// no-op
return nil
}
// Close frees handler resources.
func (h *handler) Close() {
close(h.stop)
}
// newChangeNotification creates a new change notification with the provided
// information. If withTLS is true, the TLS field is populated with the server
// name derived from the node and cluster names.
func (h *handler) newChangeNotification(n types.Node, t peerpb.ChangeNotificationType) *peerpb.ChangeNotification {
var tls *peerpb.TLS
if h.tls {
tls = &peerpb.TLS{
ServerName: TLSServerName(n.Name, n.Cluster),
}
}
return &peerpb.ChangeNotification{
Name: n.Fullname(),
Address: nodeAddress(n, h.addressPref),
Type: t,
Tls: tls,
}
}
// nodeAddress returns the node's address. If the node has both IPv4 and IPv6
// addresses, pref controls which address type is returned.
func nodeAddress(n types.Node, pref serviceoption.AddressFamilyPreference) string {
for _, family := range pref {
switch family {
case serviceoption.AddressFamilyIPv4:
if addr := n.GetNodeIP(false); addr.To4() != nil {
return addr.String()
}
case serviceoption.AddressFamilyIPv6:
if addr := n.GetNodeIP(true); addr.To4() == nil {
return addr.String()
}
}
}
return ""
}
// TLSServerName constructs a server name to be used as the TLS server name.
// The server name is of the following form:
//
// <nodeName>.<clusterName>.<hubble-grpc-svc-name>.<domain>
//
// For example, with nodeName=moseisley and clusterName=tatooine, the following
// server name is returned:
//
// moseisley.tatooine.hubble-grpc.cilium.io
//
// When nodeName is not provided, an empty string is returned. All Dot (.) in
// nodeName are replaced by Hyphen (-). When clusterName is not provided, it
// defaults to the default cluster name. All Dot (.) in clusterName are
// replaced by Hypen (-).
func TLSServerName(nodeName, clusterName string) string {
if nodeName == "" {
return ""
}
// To ensure that each node's ServerName is at the same DNS domain level,
// we have to lookout for Dot (.) as Kubernetes allows them in Node names.
nn := strings.ReplaceAll(nodeName, ".", "-")
if clusterName == "" {
clusterName = ciliumDefaults.ClusterName
}
// The cluster name may also contain dots.
cn := strings.ReplaceAll(clusterName, ".", "-")
return strings.Join([]string{
nn,
cn,
defaults.GRPCServiceName,
defaults.DomainName,
}, ".")
}