/
policy.go
773 lines (663 loc) · 25.9 KB
/
policy.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// Copyright 2016-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 (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/cilium/cilium/common/addressing"
"github.com/cilium/cilium/pkg/completion"
"github.com/cilium/cilium/pkg/controller"
endpointid "github.com/cilium/cilium/pkg/endpoint/id"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/eventqueue"
identityPkg "github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/identity/identitymanager"
"github.com/cilium/cilium/pkg/ipcache"
k8sConst "github.com/cilium/cilium/pkg/k8s/apis/cilium.io"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/logging/logfields"
monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/policy"
"github.com/cilium/cilium/pkg/revert"
"github.com/sirupsen/logrus"
)
// ProxyID returns a unique string to identify a proxy mapping.
func (e *Endpoint) ProxyID(l4 *policy.L4Filter) string {
return policy.ProxyIDFromFilter(e.ID, l4)
}
// lookupRedirectPort returns the redirect L4 proxy port for the given L4
// policy map key, in host byte order. Returns 0 if not found or the
// filter doesn't require a redirect.
// Must be called with Endpoint.Mutex held.
func (e *Endpoint) LookupRedirectPort(l4Filter *policy.L4Filter) uint16 {
if !l4Filter.IsRedirect() {
return 0
}
proxyID := e.ProxyID(l4Filter)
return e.realizedRedirects[proxyID]
}
// Note that this function assumes that endpoint policy has already been generated!
// must be called with endpoint.Mutex held for reading
func (e *Endpoint) updateNetworkPolicy(proxyWaitGroup *completion.WaitGroup) (reterr error, revertFunc revert.RevertFunc) {
// Skip updating the NetworkPolicy if no identity has been computed for this
// endpoint.
// This breaks a circular dependency between configuring NetworkPolicies in
// sidecar Envoy proxies and those proxies needing network connectivity
// to get their initial configuration, which is required for them to ACK
// the NetworkPolicies.
if e.SecurityIdentity == nil {
return nil, nil
}
// If desired L4Policy is nil then no policy change is needed.
if e.desiredPolicy == nil || e.desiredPolicy.L4Policy == nil {
return nil, nil
}
if e.isProxyDisabled() {
return nil, nil
}
// Publish the updated policy to L7 proxies.
return e.proxy.UpdateNetworkPolicy(e, e.desiredPolicy.L4Policy, e.desiredPolicy.IngressPolicyEnabled, e.desiredPolicy.EgressPolicyEnabled, proxyWaitGroup)
}
func (e *Endpoint) useCurrentNetworkPolicy(proxyWaitGroup *completion.WaitGroup) {
if e.SecurityIdentity == nil {
return
}
// If desired L4Policy is nil then no policy change is needed.
if e.desiredPolicy == nil || e.desiredPolicy.L4Policy == nil {
return
}
if e.proxy != nil {
// Wait for the current network policy to be acked
e.proxy.UseCurrentNetworkPolicy(e, e.desiredPolicy.L4Policy, proxyWaitGroup)
}
}
// setNextPolicyRevision updates the desired policy revision field
// Must be called with the endpoint lock held for at least reading
func (e *Endpoint) setNextPolicyRevision(revision uint64) {
e.nextPolicyRevision = revision
e.UpdateLogger(map[string]interface{}{
logfields.DesiredPolicyRevision: e.nextPolicyRevision,
})
}
// regeneratePolicy computes the policy for the given endpoint based off of the
// rules in regeneration.Owner's policy repository.
//
// Policy generation may fail, and in that case we exit before actually changing
// the policy in any way, so that the last policy remains fully in effect if the
// new policy can not be implemented. This is done on a per endpoint-basis,
// however, and it is possible that policy update succeeds for some endpoints,
// while it fails for other endpoints.
//
// Returns:
// - err: any error in obtaining information for computing policy, or if
// policy could not be generated given the current set of rules in the
// repository.
// Must be called with endpoint mutex held.
func (e *Endpoint) regeneratePolicy() (retErr error) {
var forceRegeneration bool
// No point in calculating policy if endpoint does not have an identity yet.
if e.SecurityIdentity == nil {
e.getLogger().Warn("Endpoint lacks identity, skipping policy calculation")
return nil
}
e.getLogger().Debug("Starting policy recalculation...")
stats := &policyRegenerationStatistics{}
stats.totalTime.Start()
stats.waitingForPolicyRepository.Start()
repo := e.owner.GetPolicyRepository()
repo.Mutex.RLock()
revision := repo.GetRevision()
defer repo.Mutex.RUnlock()
stats.waitingForPolicyRepository.End(true)
// Recompute policy for this endpoint only if not already done for this revision.
if !e.forcePolicyCompute && e.nextPolicyRevision >= revision {
e.getLogger().WithFields(logrus.Fields{
"policyRevision.next": e.nextPolicyRevision,
"policyRevision.repo": revision,
"policyChanged": e.nextPolicyRevision > e.policyRevision,
}).Debug("Skipping unnecessary endpoint policy recalculation")
return nil
}
stats.policyCalculation.Start()
if e.selectorPolicy == nil {
// Upon initial insertion or restore, there's currently no good
// trigger point to ensure that the security Identity is
// assigned after the endpoint is added to the endpointmanager
// (and hence also the identitymanager). In that case, detect
// that the selectorPolicy is not set and find it.
e.selectorPolicy = repo.GetPolicyCache().Lookup(e.SecurityIdentity)
if e.selectorPolicy == nil {
err := fmt.Errorf("no cached selectorPolicy found")
e.getLogger().WithError(err).Warning("Failed to regenerate from cached policy")
return err
}
}
// TODO: GH-7515: This should be triggered closer to policy change
// handlers, but for now let's just update it here.
if err := repo.GetPolicyCache().UpdatePolicy(e.SecurityIdentity); err != nil {
e.getLogger().WithError(err).Warning("Failed to update policy")
return err
}
calculatedPolicy := e.selectorPolicy.Consume(e)
stats.policyCalculation.End(true)
// This marks the e.desiredPolicy different from the previously realized policy
e.desiredPolicy = calculatedPolicy
if e.forcePolicyCompute {
forceRegeneration = true // Options were changed by the caller.
e.forcePolicyCompute = false // Policies just computed
e.getLogger().Debug("Forced policy recalculation")
}
// Set the revision of this endpoint to the current revision of the policy
// repository.
e.setNextPolicyRevision(revision)
e.updatePolicyRegenerationStatistics(stats, forceRegeneration, retErr)
return nil
}
func (e *Endpoint) updatePolicyRegenerationStatistics(stats *policyRegenerationStatistics, forceRegeneration bool, err error) {
success := err == nil
stats.totalTime.End(success)
stats.success = success
stats.SendMetrics()
fields := logrus.Fields{
"waitingForIdentityCache": stats.waitingForIdentityCache,
"waitingForPolicyRepository": stats.waitingForPolicyRepository,
"policyCalculation": stats.policyCalculation,
"forcedRegeneration": forceRegeneration,
}
scopedLog := e.getLogger().WithFields(fields)
if err != nil {
scopedLog.WithError(err).Warn("Regeneration of policy failed")
return
}
scopedLog.Debug("Completed endpoint policy recalculation")
}
// updateAndOverrideEndpointOptions updates the boolean configuration options for the endpoint
// based off of policy configuration, daemon policy enforcement mode, and any
// configuration options provided in opts. Returns whether the options changed
// from prior endpoint configuration. Note that the policy which applies
// to the endpoint, as well as the daemon's policy enforcement, may override
// configuration changes which were made via the API that were provided in opts.
// Must be called with endpoint mutex held.
func (e *Endpoint) updateAndOverrideEndpointOptions(opts option.OptionMap) (optsChanged bool) {
if opts == nil {
opts = make(option.OptionMap)
}
// Apply possible option changes before regenerating maps, as map regeneration
// depends on the conntrack options
if e.desiredPolicy != nil && e.desiredPolicy.L4Policy != nil {
if e.desiredPolicy.L4Policy.RequiresConntrack() {
opts[option.Conntrack] = option.OptionEnabled
}
}
optsChanged = e.applyOptsLocked(opts)
return
}
// Called with e.Mutex UNlocked
func (e *Endpoint) regenerate(context *regenerationContext) (retErr error) {
var revision uint64
var compilationExecuted bool
var err error
context.Stats = regenerationStatistics{}
stats := &context.Stats
stats.totalTime.Start()
e.getLogger().WithFields(logrus.Fields{
logfields.StartTime: time.Now(),
logfields.Reason: context.Reason,
}).Debug("Regenerating endpoint")
defer func() {
// This has to be within a func(), not deferred directly, so that the
// value of retErr is passed in from when regenerate returns.
e.updateRegenerationStatistics(context, retErr)
}()
e.buildMutex.Lock()
defer e.buildMutex.Unlock()
stats.waitingForLock.Start()
// Check if endpoints is still alive before doing any build
err = e.lockAlive()
stats.waitingForLock.End(err == nil)
if err != nil {
return err
}
// When building the initial drop policy in waiting-for-identity state
// the state remains unchanged
//
// GH-5350: Remove this special case to require checking for StateWaitingForIdentity
if e.getState() != StateWaitingForIdentity &&
!e.BuilderSetStateLocked(StateRegenerating, "Regenerating endpoint: "+context.Reason) {
e.getLogger().WithField(logfields.EndpointState, e.state).Debug("Skipping build due to invalid state")
e.unlock()
return fmt.Errorf("Skipping build due to invalid state: %s", e.state)
}
e.unlock()
stats.prepareBuild.Start()
origDir := e.StateDirectoryPath()
context.datapathRegenerationContext.currentDir = origDir
// This is the temporary directory to store the generated headers,
// the original existing directory is not overwritten until the
// entire generation process has succeeded.
tmpDir := e.NextDirectoryPath()
context.datapathRegenerationContext.nextDir = tmpDir
// Remove an eventual existing temporary directory that has been left
// over to make sure we can start the build from scratch
if err := e.removeDirectory(tmpDir); err != nil && !os.IsNotExist(err) {
stats.prepareBuild.End(false)
return fmt.Errorf("unable to remove old temporary directory: %s", err)
}
// Create temporary endpoint directory if it does not exist yet
if err := os.MkdirAll(tmpDir, 0777); err != nil {
stats.prepareBuild.End(false)
return fmt.Errorf("Failed to create endpoint directory: %s", err)
}
stats.prepareBuild.End(true)
defer func() {
if err := e.lockAlive(); err != nil {
if retErr == nil {
retErr = err
} else {
e.logDisconnectedMutexAction(err, "after regenerate")
}
return
}
// Guarntee removal of temporary directory regardless of outcome of
// build. If the build was successful, the temporary directory will
// have been moved to a new permanent location. If the build failed,
// the temporary directory will still exist and we will reomve it.
e.removeDirectory(tmpDir)
// Set to Ready, but only if no other changes are pending.
// State will remain as waiting-to-regenerate if further
// changes are needed. There should be an another regenerate
// queued for taking care of it.
e.BuilderSetStateLocked(StateReady, "Completed endpoint regeneration with no pending regeneration requests")
e.unlock()
}()
revision, compilationExecuted, err = e.regenerateBPF(context)
if err != nil {
failDir := e.FailedDirectoryPath()
e.getLogger().WithFields(logrus.Fields{
logfields.Path: failDir,
}).Warn("generating BPF for endpoint failed, keeping stale directory.")
// Remove an eventual existing previous failure directory
e.removeDirectory(failDir)
os.Rename(tmpDir, failDir)
return err
}
return e.updateRealizedState(stats, origDir, revision, compilationExecuted)
}
// updateRealizedState sets any realized state fields within the endpoint to
// be the desired state of the endpoint. This is only called after a successful
// regeneration of the endpoint.
func (e *Endpoint) updateRealizedState(stats *regenerationStatistics, origDir string, revision uint64, compilationExecuted bool) error {
// Update desired policy for endpoint because policy has now been realized
// in the datapath. PolicyMap state is not updated here, because that is
// performed in endpoint.syncPolicyMap().
stats.waitingForLock.Start()
err := e.lockAlive()
stats.waitingForLock.End(err == nil)
if err != nil {
return err
}
defer e.unlock()
// Depending upon result of BPF regeneration (compilation executed),
// shift endpoint directories to match said BPF regeneration
// results.
err = e.synchronizeDirectories(origDir, compilationExecuted)
if err != nil {
return fmt.Errorf("error synchronizing endpoint BPF program directories: %s", err)
}
// Keep PolicyMap for this endpoint in sync with desired / realized state.
if !option.Config.DryMode {
e.syncPolicyMapController()
}
// Set realized state to desired state.
e.realizedPolicy = e.desiredPolicy
// Mark the endpoint to be running the policy revision it was
// compiled for
e.setPolicyRevision(revision)
return nil
}
func (e *Endpoint) updateRegenerationStatistics(context *regenerationContext, err error) {
success := err == nil
stats := &context.Stats
stats.totalTime.End(success)
stats.success = success
e.mutex.RLock()
stats.endpointID = e.ID
stats.policyStatus = e.policyStatus()
e.runlock()
stats.SendMetrics()
fields := logrus.Fields{
logfields.Reason: context.Reason,
}
for field, stat := range stats.GetMap() {
fields[field] = stat.Total()
}
for field, stat := range stats.datapathRealization.GetMap() {
fields[field] = stat.Total()
}
scopedLog := e.getLogger().WithFields(fields)
if err != nil {
scopedLog.WithError(err).Warn("Regeneration of endpoint failed")
e.LogStatus(BPF, Failure, "Error regenerating endpoint: "+err.Error())
return
}
scopedLog.Debug("Completed endpoint regeneration")
e.LogStatusOK(BPF, "Successfully regenerated endpoint program (Reason: "+context.Reason+")")
}
// RegenerateIfAlive queue a regeneration of this endpoint into the build queue
// of the endpoint and returns a channel that is closed when the regeneration of
// the endpoint is complete. The channel returns:
// - false if the regeneration failed
// - true if the regeneration succeed
// - nothing and the channel is closed if the regeneration did not happen
func (e *Endpoint) RegenerateIfAlive(regenMetadata *regeneration.ExternalRegenerationMetadata) <-chan bool {
if err := e.lockAlive(); err != nil {
log.WithError(err).Warnf("Endpoint disappeared while queued to be regenerated: %s", regenMetadata.Reason)
e.LogStatus(Policy, Failure, "Error while handling policy updates for endpoint: "+err.Error())
} else {
var regen bool
state := e.getState()
switch state {
case StateRestoring, StateWaitingToRegenerate:
e.setState(state, fmt.Sprintf("Skipped duplicate endpoint regeneration trigger due to %s", regenMetadata.Reason))
regen = false
default:
regen = e.setState(StateWaitingToRegenerate, fmt.Sprintf("Triggering endpoint regeneration due to %s", regenMetadata.Reason))
}
e.unlock()
if regen {
// Regenerate logs status according to the build success/failure
return e.Regenerate(regenMetadata)
}
}
ch := make(chan bool)
close(ch)
return ch
}
// Regenerate forces the regeneration of endpoint programs & policy
// Should only be called with e.state == StateWaitingToRegenerate or with
// e.state == StateWaitingForIdentity
func (e *Endpoint) Regenerate(regenMetadata *regeneration.ExternalRegenerationMetadata) <-chan bool {
done := make(chan bool, 1)
var (
ctx context.Context
cFunc context.CancelFunc
)
if regenMetadata.ParentContext != nil {
ctx, cFunc = context.WithCancel(regenMetadata.ParentContext)
} else {
ctx, cFunc = context.WithCancel(e.aliveCtx)
}
regenContext := ParseExternalRegenerationMetadata(ctx, cFunc, regenMetadata)
epEvent := eventqueue.NewEvent(&EndpointRegenerationEvent{
regenContext: regenContext,
ep: e,
})
// This may block if the Endpoint's EventQueue is full. This has to be done
// synchronously as some callers depend on the fact that the event is
// synchronously enqueued.
resChan, err := e.eventQueue.Enqueue(epEvent)
if err != nil {
e.getLogger().Errorf("enqueue of EndpointRegenerationEvent failed: %s", err)
done <- false
close(done)
return done
}
go func() {
// Free up resources with context.
defer cFunc()
var (
buildSuccess bool
regenError error
canceled bool
)
select {
case result, ok := <-resChan:
if ok {
regenResult := result.(*EndpointRegenerationResult)
regenError = regenResult.err
buildSuccess = regenError == nil
if regenError != nil {
e.getLogger().WithError(regenError).Error("endpoint regeneration failed")
}
} else {
// This may be unnecessary(?) since 'closing' of the results
// channel means that event has been cancelled?
e.getLogger().Debug("regeneration was cancelled")
canceled = true
}
}
// If a build is canceled, that means that the Endpoint is being deleted
// not that the build failed.
if !buildSuccess && !canceled {
select {
case e.regenFailedChan <- struct{}{}:
default:
// If we can't write to the channel, that means that it is
// full / a regeneration will occur - we don't have to
// do anything.
}
}
done <- buildSuccess
close(done)
}()
return done
}
var reasonRegenRetry = "retrying regeneration"
// startRegenerationFailureHandler waits for a build of the Endpoint to fail.
// Terminates when the given Endpoint is deleted.
// If a build fails, the controller tries to regenerate the
// Endpoint until it succeeds. Once the controller succeeds, it will not be
// ran again unless another build failure occurs. If the call to `Regenerate`
// fails inside of the controller,
func (e *Endpoint) startRegenerationFailureHandler() {
e.controllers.UpdateController(fmt.Sprintf("endpoint-%s-regeneration-recovery", e.StringID()), controller.ControllerParams{
DoFunc: func(ctx context.Context) error {
select {
case <-e.regenFailedChan:
e.getLogger().Debug("received signal that regeneration failed")
case <-ctx.Done():
e.getLogger().Debug("exiting retrying regeneration goroutine due to endpoint being deleted")
return nil
}
if err := e.lockAlive(); err != nil {
// We don't need to regenerate because the endpoint is d
// disconnecting / is disconnected, exit gracefully.
return nil
}
stateTransitionSucceeded := e.setState(StateWaitingToRegenerate, reasonRegenRetry)
e.unlock()
if !stateTransitionSucceeded {
// Another regeneration has already been enqueued.
return nil
}
r := ®eneration.ExternalRegenerationMetadata{
// TODO (ianvernon) - is there a way we can plumb a parent
// context to a controller (e.g., endpoint.aliveCtx)?
ParentContext: ctx,
Reason: reasonRegenRetry,
// Completely rewrite the endpoint - we don't know the nature
// of the failure, simply that something failed.
RegenerationLevel: regeneration.RegenerateWithDatapathRewrite,
}
if success := <-e.Regenerate(r); success {
return nil
}
return fmt.Errorf("regeneration recovery failed")
},
ErrorRetryBaseDuration: 2 * time.Second,
Context: e.aliveCtx,
})
}
func (e *Endpoint) notifyEndpointRegeneration(err error) {
repr, reprerr := monitorAPI.EndpointRegenRepr(e, err)
if reprerr != nil {
e.getLogger().WithError(reprerr).Warn("Notifying monitor about endpoint regeneration failed")
}
if err != nil {
if reprerr == nil && !option.Config.DryMode {
e.owner.SendNotification(monitorAPI.AgentNotifyEndpointRegenerateFail, repr)
}
} else {
if reprerr == nil && !option.Config.DryMode {
e.owner.SendNotification(monitorAPI.AgentNotifyEndpointRegenerateSuccess, repr)
}
}
}
// FormatGlobalEndpointID returns the global ID of endpoint in the format
// / <global ID Prefix>:<cluster name>:<node name>:<endpoint ID> as a string.
func (e *Endpoint) FormatGlobalEndpointID() string {
localNodeName := node.GetName()
metadata := []string{endpointid.CiliumGlobalIdPrefix.String(), ipcache.AddressSpace, localNodeName, strconv.Itoa(int(e.ID))}
return strings.Join(metadata, ":")
}
// This synchronizes the key-value store with a mapping of the endpoint's IP
// with the numerical ID representing its security identity.
func (e *Endpoint) runIPIdentitySync(endpointIP addressing.CiliumIP) {
if option.Config.KVStore == "" || !endpointIP.IsSet() {
return
}
addressFamily := endpointIP.GetFamilyString()
e.controllers.UpdateController(fmt.Sprintf("sync-%s-identity-mapping (%d)", addressFamily, e.ID),
controller.ControllerParams{
DoFunc: func(ctx context.Context) error {
if err := e.rlockAlive(); err != nil {
return controller.NewExitReason("Endpoint disappeared")
}
if e.SecurityIdentity == nil {
e.runlock()
return nil
}
IP := endpointIP.IP()
ID := e.SecurityIdentity.ID
hostIP := node.GetExternalIPv4()
key := node.GetIPsecKeyIdentity()
metadata := e.FormatGlobalEndpointID()
k8sNamespace := e.K8sNamespace
k8sPodName := e.K8sPodName
// Release lock as we do not want to have long-lasting key-value
// store operations resulting in lock being held for a long time.
e.runlock()
if err := ipcache.UpsertIPToKVStore(ctx, IP, hostIP, ID, key, metadata, k8sNamespace, k8sPodName); err != nil {
return fmt.Errorf("unable to add endpoint IP mapping '%s'->'%d': %s", IP.String(), ID, err)
}
return nil
},
StopFunc: func(ctx context.Context) error {
ip := endpointIP.String()
if err := ipcache.DeleteIPFromKVStore(ctx, ip); err != nil {
return fmt.Errorf("unable to delete endpoint IP '%s' from ipcache: %s", ip, err)
}
return nil
},
RunInterval: 5 * time.Minute,
Context: e.aliveCtx,
},
)
}
// SetIdentity resets endpoint's policy identity to 'id'.
// Caller triggers policy regeneration if needed.
// Called with e.Mutex Locked
func (e *Endpoint) SetIdentity(identity *identityPkg.Identity, newEndpoint bool) {
// Set a boolean flag to indicate whether the endpoint has been injected by
// Istio with a Cilium-compatible sidecar proxy.
istioSidecarProxyLabel, found := identity.Labels[k8sConst.PolicyLabelIstioSidecarProxy]
e.hasSidecarProxy = found &&
istioSidecarProxyLabel.Source == labels.LabelSourceK8s &&
strings.ToLower(istioSidecarProxyLabel.Value) == "true"
oldIdentity := "no identity"
if e.SecurityIdentity != nil {
oldIdentity = e.SecurityIdentity.StringID()
}
// Current security identity for endpoint is its old identity - delete its
// reference from global identity manager, add add a reference to the new
// identity for the endpoint.
if newEndpoint {
// TODO - GH-9354.
identitymanager.Add(identity)
} else {
identitymanager.RemoveOldAddNew(e.SecurityIdentity, identity)
}
e.SecurityIdentity = identity
e.replaceIdentityLabels(identity.Labels)
// Clear selectorPolicy. It will be determined at next regeneration.
e.selectorPolicy = nil
// Sets endpoint state to ready if was waiting for identity
if e.getState() == StateWaitingForIdentity {
e.setState(StateReady, "Set identity for this endpoint")
}
// Whenever the identity is updated, propagate change to key-value store
// of IP to identity mapping.
e.runIPIdentitySync(e.IPv4)
e.runIPIdentitySync(e.IPv6)
if oldIdentity != identity.StringID() {
e.getLogger().WithFields(logrus.Fields{
logfields.Identity: identity.StringID(),
logfields.OldIdentity: oldIdentity,
logfields.IdentityLabels: identity.Labels.String(),
}).Info("Identity of endpoint changed")
}
e.UpdateLogger(map[string]interface{}{
logfields.Identity: identity.StringID(),
})
}
// GetCIDRPrefixLengths returns the sorted list of unique prefix lengths used
// for CIDR policy or IPcache lookup from this endpoint.
func (e *Endpoint) GetCIDRPrefixLengths() (s6, s4 []int) {
if e.desiredPolicy == nil || e.desiredPolicy.CIDRPolicy == nil {
return policy.GetDefaultPrefixLengths()
}
return e.desiredPolicy.CIDRPolicy.ToBPFData()
}
// AnnotationsResolverCB provides an implementation for resolving the pod
// annotations.
type AnnotationsResolverCB func(ns, podName string) (proxyVisibility string, err error)
// UpdateVisibilityPolicy updates the visibility policy of this endpoint to
// reflect the state stored in the provided proxy visibility annotation. If anno
// is empty, then the VisibilityPolicy for the Endpoint will be empty, and will
// have no effect. If the proxy visibility annotation cannot be parsed, an empty
// visibility policy is assigned to the Endpoint.
func (e *Endpoint) UpdateVisibilityPolicy(annoCB AnnotationsResolverCB) {
ch, err := e.eventQueue.Enqueue(eventqueue.NewEvent(&EndpointPolicyVisibilityEvent{
ep: e,
annoCB: annoCB,
}))
if err != nil {
e.getLogger().WithError(err).Error("Unable to enqueue endpoint policy visibility event")
return
}
<-ch
}
// GetRealizedPolicyRuleLabelsForKey returns the list of policy rule labels
// which match a given flow key (in host byte-order). The returned
// LabelArrayList must not be modified. This function is exported to be
// accessed by code outside of the Cilium code base (e.g. Hubble).
func (e *Endpoint) GetRealizedPolicyRuleLabelsForKey(key policy.Key) (
derivedFrom labels.LabelArrayList,
revision uint64,
ok bool,
) {
e.mutex.RLock()
defer e.mutex.RUnlock()
entry, ok := e.realizedPolicy.PolicyMapState[key]
if !ok {
return nil, 0, false
}
return entry.DerivedFromRules, e.policyRevision, true
}