forked from cilium/cilium
/
endpoint_status.go
319 lines (281 loc) · 9.61 KB
/
endpoint_status.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2019 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package endpoint
import (
"sort"
"strings"
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/identity"
identitycache "github.com/cilium/cilium/pkg/identity/cache"
cilium_v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/policy"
"github.com/cilium/cilium/pkg/policy/trafficdirection"
)
func getEndpointStatusControllers(status *models.EndpointStatus) (controllers cilium_v2.ControllerList) {
for _, c := range status.Controllers {
if c.Status == nil {
continue
}
if c.Status.ConsecutiveFailureCount > 0 {
s := cilium_v2.ControllerStatus{
Configuration: c.Configuration,
Name: c.Name,
UUID: string(c.UUID),
Status: cilium_v2.ControllerStatusStatus{
ConsecutiveFailureCount: c.Status.ConsecutiveFailureCount,
FailureCount: c.Status.FailureCount,
LastFailureMsg: c.Status.LastFailureMsg,
LastFailureTimestamp: c.Status.LastFailureTimestamp.String(),
LastSuccessTimestamp: c.Status.LastSuccessTimestamp.String(),
SuccessCount: c.Status.SuccessCount,
},
}
if controllers == nil {
controllers = cilium_v2.ControllerList{s}
} else {
controllers = append(controllers, s)
}
}
}
if controllers != nil {
controllers.Sort()
}
return
}
func (e *Endpoint) getEndpointStatusLog() (log []*models.EndpointStatusChange) {
added := 0
if s := e.Status; s != nil {
s.indexMU.RLock()
defer s.indexMU.RUnlock()
for i := s.lastIndex(); ; i-- {
if i < 0 {
i = maxLogs - 1
}
if i < len(s.Log) && s.Log[i] != nil {
l := &models.EndpointStatusChange{
Timestamp: s.Log[i].Timestamp.Format(time.RFC3339),
Code: s.Log[i].Status.Code.String(),
Message: s.Log[i].Status.Msg,
State: models.EndpointState(s.Log[i].Status.State),
}
if strings.ToLower(l.Code) != models.EndpointStatusChangeCodeOk {
if log == nil {
log = []*models.EndpointStatusChange{l}
} else {
log = append(log, l)
}
// Limit the number of endpoint log
// entries to keep the size of the
// EndpointStatus low.
added++
if added >= cilium_v2.EndpointStatusLogEntries {
break
}
}
}
if i == s.Index {
break
}
}
}
return
}
func getEndpointIdentity(status *models.EndpointStatus) (identity *cilium_v2.EndpointIdentity) {
if status.Identity != nil {
identity = &cilium_v2.EndpointIdentity{
ID: status.Identity.ID,
}
identity.Labels = make([]string, len(status.Identity.Labels))
copy(identity.Labels, status.Identity.Labels)
sort.Strings(identity.Labels)
}
return
}
func getEndpointNetworking(status *models.EndpointStatus) (networking *cilium_v2.EndpointNetworking) {
if status.Networking != nil {
networking = &cilium_v2.EndpointNetworking{
Addressing: make(cilium_v2.AddressPairList, len(status.Networking.Addressing)),
}
if option.Config.EnableIPv4 {
networking.NodeIP = node.GetExternalIPv4().String()
} else {
networking.NodeIP = node.GetIPv6().String()
}
i := 0
for _, pair := range status.Networking.Addressing {
networking.Addressing[i] = &cilium_v2.AddressPair{
IPV4: pair.IPV4,
IPV6: pair.IPV6,
}
i++
}
networking.Addressing.Sort()
}
return
}
// updateLabels inserts the labels correnspoding to the specified identity into
// the AllowedIdentityTuple.
func updateLabels(allowedIdentityTuple *cilium_v2.AllowedIdentityTuple, secID identity.NumericIdentity) {
// IdentityUnknown denotes that this is an L4-only BPF
// allow, so it applies to all identities. In this case
// we should skip resolving the labels, because the
// value 0 does not denote an allow for the "unknown"
// identity, but instead an allow of all identities for
// that port.
if secID != identity.IdentityUnknown {
identity := identitycache.LookupIdentityByID(secID)
if identity != nil {
var l labels.Labels
if identity.CIDRLabel != nil {
l = identity.CIDRLabel
} else {
l = identity.Labels
}
allowedIdentityTuple.IdentityLabels = l.StringMap()
}
}
}
// populateResponseWithPolicyKey inserts an AllowedIdentityTuple element into 'policy'
// which corresponds to the specified 'desiredPolicy'.
func populateResponseWithPolicyKey(policy *cilium_v2.EndpointPolicy, policyKey *policy.Key) {
allowedIdentityTuple := cilium_v2.AllowedIdentityTuple{
DestPort: policyKey.DestPort,
Protocol: policyKey.Nexthdr,
Identity: uint64(policyKey.Identity),
}
secID := identity.NumericIdentity(policyKey.Identity)
updateLabels(&allowedIdentityTuple, secID)
switch {
case policyKey.IsIngress():
if policy.Ingress.Allowed == nil {
policy.Ingress.Allowed = cilium_v2.AllowedIdentityList{allowedIdentityTuple}
} else {
policy.Ingress.Allowed = append(policy.Ingress.Allowed, allowedIdentityTuple)
}
case policyKey.IsEgress():
if policy.Egress.Allowed == nil {
policy.Egress.Allowed = cilium_v2.AllowedIdentityList{allowedIdentityTuple}
} else {
policy.Egress.Allowed = append(policy.Egress.Allowed, allowedIdentityTuple)
}
}
}
// desiredPolicyAllowsIdentity returns whether the specified policy allows
// ingress and egress traffic for the specified numeric security identity.
// If the 'secID' is zero, it will check if all traffic is allowed.
//
// Returing true for either return value indicates all traffic is allowed.
func desiredPolicyAllowsIdentity(desired *policy.EndpointPolicy, identity identity.NumericIdentity) (ingress, egress bool) {
key := policy.Key{
Identity: uint32(identity),
}
key.TrafficDirection = trafficdirection.Ingress.Uint8()
if _, ok := desired.PolicyMapState[key]; ok || !desired.IngressPolicyEnabled {
ingress = true
}
key.TrafficDirection = trafficdirection.Egress.Uint8()
if _, ok := desired.PolicyMapState[key]; ok || !desired.EgressPolicyEnabled {
egress = true
}
return ingress, egress
}
// getEndpointPolicy returns an API representation of the policy that the
// received Endpoint intends to apply.
func (e *Endpoint) getEndpointPolicy() (policy *cilium_v2.EndpointPolicy) {
if e.desiredPolicy != nil {
policy = &cilium_v2.EndpointPolicy{
Ingress: &cilium_v2.EndpointPolicyDirection{
Enforcing: e.desiredPolicy.IngressPolicyEnabled,
},
Egress: &cilium_v2.EndpointPolicyDirection{
Enforcing: e.desiredPolicy.EgressPolicyEnabled,
},
}
// Handle allow-all cases
allowsAllIngress, allowsAllEgress := desiredPolicyAllowsIdentity(e.desiredPolicy, identity.IdentityUnknown)
if allowsAllIngress {
policy.Ingress.Allowed = cilium_v2.AllowedIdentityList{{}}
}
if allowsAllEgress {
policy.Egress.Allowed = cilium_v2.AllowedIdentityList{{}}
}
// If either ingress or egress policy is enabled, go through
// the desired policy to populate the values.
if !allowsAllIngress || !allowsAllEgress {
allowsWorldIngress, allowsWorldEgress := desiredPolicyAllowsIdentity(e.desiredPolicy, identity.ReservedIdentityWorld)
for policyKey := range e.desiredPolicy.PolicyMapState {
// Skip listing identities if enforcement is disabled in direction,
// or if the identity corresponds to a CIDR identity and the world is allowed.
id := identity.NumericIdentity(policyKey.Identity)
switch {
case policyKey.IsIngress():
if allowsAllIngress || (id.HasLocalScope() && allowsWorldIngress) {
continue
}
case policyKey.IsEgress():
if allowsAllEgress || (id.HasLocalScope() && allowsWorldEgress) {
continue
}
}
populateResponseWithPolicyKey(policy, &policyKey)
}
}
if policy.Ingress.Allowed != nil {
policy.Ingress.Allowed.Sort()
}
if policy.Egress.Allowed != nil {
policy.Egress.Allowed.Sort()
}
}
return
}
// GetCiliumEndpointStatus creates a cilium_v2.EndpointStatus of an endpoint.
// See cilium_v2.EndpointStatus for a detailed explanation of each field.
func (e *Endpoint) GetCiliumEndpointStatus() *cilium_v2.EndpointStatus {
e.mutex.RLock()
defer e.mutex.RUnlock()
model := e.GetModelRLocked()
modelStatus := model.Status
controllers := getEndpointStatusControllers(modelStatus)
identity := getEndpointIdentity(modelStatus)
log := e.getEndpointStatusLog()
networking := getEndpointNetworking(modelStatus)
return &cilium_v2.EndpointStatus{
ID: int64(e.ID),
ExternalIdentifiers: modelStatus.ExternalIdentifiers,
Controllers: controllers,
Identity: identity,
Log: log,
Networking: networking,
Health: modelStatus.Health,
State: string(modelStatus.State),
Policy: e.getEndpointPolicy(),
Encryption: cilium_v2.EncryptionSpec{Key: int(node.GetIPsecKeyIdentity())},
// Scheduled for deprecation in 1.5
//
// Status is deprecated but we have some users depending on
// these fields so they continue to be populated until version
// 1.5
Status: &cilium_v2.DeprecatedEndpointStatus{
Controllers: controllers,
Identity: identity,
Log: log,
Networking: networking,
},
}
}