forked from cilium/cilium
/
state.go
495 lines (434 loc) · 16.3 KB
/
state.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
// 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 main
import (
"context"
"fmt"
"io/ioutil"
"net"
"os"
"sync"
"time"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/endpoint"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
"github.com/cilium/cilium/pkg/endpointmanager"
"github.com/cilium/cilium/pkg/identity/cache"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/k8s"
"github.com/cilium/cilium/pkg/labels"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maps/ctmap"
"github.com/cilium/cilium/pkg/maps/lxcmap"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/policy"
"github.com/cilium/cilium/pkg/workloads"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type endpointRestoreState struct {
restored []*endpoint.Endpoint
toClean []*endpoint.Endpoint
}
// validateEndpoint attempts to determine that the endpoint is valid, ie it
// still exists in k8s, its datapath devices are present, and Cilium is
// responsible for its workload, etc.
//
// Returns true to indicate that the endpoint is valid to restore, and an
// optional error.
func (d *Daemon) validateEndpoint(ep *endpoint.Endpoint) (valid bool, err error) {
// On each restart, the health endpoint is supposed to be recreated.
// Hence we need to clean health endpoint state unconditionally.
if ep.HasLabels(labels.LabelHealth) {
// Ignore health endpoint and don't report
// it as not restored. But we need to clean up the old
// state files, so do this now.
healthStateDir := ep.StateDirectoryPath()
scopedLog := log.WithFields(logrus.Fields{
logfields.EndpointID: ep.ID,
logfields.Path: healthStateDir,
})
scopedLog.Debug("Removing old health endpoint state directory")
if err := os.RemoveAll(healthStateDir); err != nil {
scopedLog.Warning("Cannot clean up old health state directory")
}
return false, nil
}
if ep.K8sPodName != "" && ep.K8sNamespace != "" && k8s.IsEnabled() {
_, err := k8s.Client().CoreV1().Pods(ep.K8sNamespace).Get(ep.K8sPodName, meta_v1.GetOptions{})
if err != nil && k8serrors.IsNotFound(err) {
return false, fmt.Errorf("kubernetes pod not found")
}
}
if ep.HasIpvlanDataPath() {
// FIXME: We cannot check whether ipvlan slave netdev exists,
// because it requires entering container netns which is not
// always accessible (e.g. in k8s case "/proc" has to be bind
// mounted). Instead, we check whether the tail call map exists.
if _, err := os.Stat(ep.BPFIpvlanMapPath()); err != nil {
return false, fmt.Errorf("tail call map for IPvlan unavailable: %s", err)
}
} else if _, err := netlink.LinkByName(ep.IfName); err != nil {
return false, fmt.Errorf("interface %s could not be found", ep.IfName)
}
if option.Config.WorkloadsEnabled() && !workloads.IsRunning(ep) {
return false, fmt.Errorf("no workload could be associated with endpoint")
}
if !ep.DatapathConfiguration.ExternalIPAM {
if err := d.allocateIPsLocked(ep); err != nil {
return false, fmt.Errorf("Failed to re-allocate IP of endpoint: %s", err)
}
}
return true, nil
}
// restoreOldEndpoints reads the list of existing endpoints previously managed
// Cilium when it was last run and associated it with container workloads. This
// function performs the first step in restoring the endpoint structure,
// allocating their existing IP out of the CIDR block and then inserting the
// endpoints into the endpoints list. It needs to be followed by a call to
// regenerateRestoredEndpoints() once the endpoint builder is ready.
//
// If clean is true, endpoints which cannot be associated with a container
// workloads are deleted.
func (d *Daemon) restoreOldEndpoints(dir string, clean bool) (*endpointRestoreState, error) {
failed := 0
state := &endpointRestoreState{
restored: []*endpoint.Endpoint{},
toClean: []*endpoint.Endpoint{},
}
if !option.Config.RestoreState {
log.Info("Endpoint restore is disabled, skipping restore step")
return state, nil
}
log.Info("Restoring endpoints...")
var (
existingEndpoints map[string]*lxcmap.EndpointInfo
err error
)
if !option.Config.DryMode {
existingEndpoints, err = lxcmap.DumpToMap()
if err != nil {
log.WithError(err).Warning("Unable to open endpoint map while restoring. Skipping cleanup of endpoint map on startup")
}
}
dirFiles, err := ioutil.ReadDir(dir)
if err != nil {
return state, err
}
eptsID := endpoint.FilterEPDir(dirFiles)
possibleEPs := endpoint.ReadEPsFromDirNames(d, dir, eptsID)
if len(possibleEPs) == 0 {
log.Info("No old endpoints found.")
return state, nil
}
for _, ep := range possibleEPs {
scopedLog := log.WithField(logfields.EndpointID, ep.ID)
if k8s.IsEnabled() {
scopedLog = scopedLog.WithField("k8sPodName", ep.GetK8sNamespaceAndPodNameLocked())
}
restore, err := d.validateEndpoint(ep)
if err != nil {
scopedLog.WithError(err).Warningf("Unable to restore endpoint, ignoring")
failed++
}
if !restore {
if clean {
state.toClean = append(state.toClean, ep)
}
continue
}
ep.UnconditionalLock()
scopedLog.Debug("Restoring endpoint")
ep.LogStatusOKLocked(endpoint.Other, "Restoring endpoint from previous cilium instance")
if !option.Config.KeepConfig {
ep.SetDefaultOpts(option.Config.Opts)
alwaysEnforce := policy.GetPolicyEnabled() == option.AlwaysEnforce
ep.SetDesiredIngressPolicyEnabledLocked(alwaysEnforce)
ep.SetDesiredEgressPolicyEnabledLocked(alwaysEnforce)
}
ep.Unlock()
ep.SkipStateClean()
state.restored = append(state.restored, ep)
if existingEndpoints != nil {
delete(existingEndpoints, ep.IPv4.String())
delete(existingEndpoints, ep.IPv6.String())
}
}
log.WithFields(logrus.Fields{
"restored": len(state.restored),
"failed": failed,
}).Info("Endpoints restored")
if existingEndpoints != nil {
for hostIP, info := range existingEndpoints {
if ip := net.ParseIP(hostIP); !info.IsHost() && ip != nil {
if err := lxcmap.DeleteEntry(ip); err != nil {
log.WithError(err).Warn("Unable to delete obsolete endpoint from BPF map")
} else {
log.Debugf("Removed outdated endpoint %d from endpoint map", info.LxcID)
}
}
}
}
return state, nil
}
func (d *Daemon) regenerateRestoredEndpoints(state *endpointRestoreState) (restoreComplete chan struct{}) {
restoreComplete = make(chan struct{}, 0)
log.WithField("numRestored", len(state.restored)).Info("Regenerating restored endpoints")
// Before regenerating, check whether the CT map has properties that
// match this Cilium userspace instance. If not, it must be removed
ctmap.DeleteIfUpgradeNeeded(nil)
// we need to signalize when the endpoints are regenerated, i.e., when
// they have finished to rebuild after being restored.
epRegenerated := make(chan bool, len(state.restored))
// Insert all endpoints into the endpoint list first before starting
// the regeneration. This is required to ensure that if an individual
// regeneration causes an identity change of an endpoint, the new
// identity will trigger a policy recalculation of all endpoints to
// account for the new identity during the grace period. For this
// purpose, all endpoints being restored must already be in the
// endpoint list.
for i := len(state.restored) - 1; i >= 0; i-- {
ep := state.restored[i]
// If the endpoint has local conntrack option enabled, then
// check whether the CT map needs upgrading (and do so).
if ep.Options.IsEnabled(option.ConntrackLocal) {
ctmap.DeleteIfUpgradeNeeded(ep)
}
// Insert into endpoint manager so it can be regenerated when calls to
// RegenerateAllEndpoints() are made. This must be done synchronously (i.e.,
// not in a goroutine) because regenerateRestoredEndpoints must guarantee
// upon returning that endpoints are exposed to other subsystems via
// endpointmanager.
if err := endpointmanager.Insert(ep); err != nil {
log.WithError(err).Warning("Unable to restore endpoint")
// remove endpoint from slice of endpoints to restore
state.restored = append(state.restored[:i], state.restored[i+1:]...)
}
}
for _, ep := range state.restored {
go func(ep *endpoint.Endpoint, epRegenerated chan<- bool) {
if err := ep.RLockAlive(); err != nil {
ep.LogDisconnectedMutexAction(err, "before filtering labels during regenerating restored endpoint")
epRegenerated <- false
return
}
scopedLog := log.WithField(logfields.EndpointID, ep.ID)
// Filter the restored labels with the new daemon's filter
l, _ := labels.FilterLabels(ep.OpLabels.AllLabels())
ep.RUnlock()
allocateCtx, cancel := context.WithTimeout(context.Background(), option.Config.KVstoreConnectivityTimeout)
defer cancel()
identity, _, err := cache.AllocateIdentity(allocateCtx, d, l)
if err != nil {
scopedLog.WithError(err).Warn("Unable to restore endpoint")
epRegenerated <- false
return
}
// Wait for initial identities and ipcache from the
// kvstore before doing any policy calculation for
// endpoints that don't have a fixed identity or are
// not well known.
if !identity.IsFixed() && !identity.IsWellKnown() {
identityCtx, cancel := context.WithTimeout(context.Background(), option.Config.KVstoreConnectivityTimeout)
defer cancel()
err = cache.WaitForInitialGlobalIdentities(identityCtx)
if err != nil {
scopedLog.WithError(err).Warn("Failed while waiting for initial global identities")
epRegenerated <- false
return
}
if option.Config.KVStore != "" {
ipcache.WaitForKVStoreSync()
}
}
if err := ep.LockAlive(); err != nil {
scopedLog.Warn("Endpoint to restore has been deleted")
epRegenerated <- false
return
}
ep.SetStateLocked(endpoint.StateRestoring, "Synchronizing endpoint labels with KVStore")
if ep.SecurityIdentity != nil {
if oldSecID := ep.SecurityIdentity.ID; identity.ID != oldSecID {
log.WithFields(logrus.Fields{
logfields.EndpointID: ep.ID,
logfields.IdentityLabels + ".old": oldSecID,
logfields.IdentityLabels + ".new": identity.ID,
}).Info("Security identity for endpoint is different from the security identity restored for the endpoint")
// The identity of the endpoint being
// restored has changed. This can be
// caused by two main reasons:
//
// 1) Cilium has been upgraded,
// downgraded or the configuration has
// changed and the new version or
// configuration causes different
// labels to be considered security
// relevant for this endpoint.
//
// Immediately using the identity may
// cause connectivity problems if this
// is the first endpoint in the cluster
// to use the new identity. All other
// nodes will not have had a chance to
// adjust the security policies for
// their endpoints. Hence, apply a
// grace period to allow for the
// update. It is not required to check
// any local endpoints for potential
// outdated security rules, the
// notification of the new security
// identity will have been received and
// will trigger the necessary
// recalculation of all local
// endpoints.
//
// 2) The identity is outdated as the
// state in the kvstore has changed.
// This reason would justify an
// immediate use of the new identity
// but given the current identity is
// already in place, it is also correct
// to continue using it for the
// duration of a grace period.
time.Sleep(defaults.IdentityChangeGracePeriod)
}
}
// The identity of a freshly restored endpoint is incomplete due to some
// parts of the identity not being marshaled to JSON. Hence we must set
// the identity even if has not changed.
ep.SetIdentity(identity, true)
ep.Unlock()
regenerationMetadata := ®eneration.ExternalRegenerationMetadata{
Reason: "syncing state to host",
}
if buildSuccess := <-ep.Regenerate(regenerationMetadata); !buildSuccess {
scopedLog.Warn("Failed while regenerating endpoint")
epRegenerated <- false
return
}
// NOTE: UnconditionalRLock is used here because it's used only for logging an already restored endpoint
ep.UnconditionalRLock()
scopedLog.WithField(logfields.IPAddr, []string{ep.IPv4.String(), ep.IPv6.String()}).Info("Restored endpoint")
ep.RUnlock()
epRegenerated <- true
}(ep, epRegenerated)
}
var endpointCleanupCompleted sync.WaitGroup
for _, ep := range state.toClean {
endpointCleanupCompleted.Add(1)
go func(ep *endpoint.Endpoint) {
// The IP was not allocated yet so does not need to be free.
// The identity may be allocated in the kvstore but we can't
// release it easily as it will require to block on kvstore
// connectivity which we can't do at this point. Let the lease
// expire to release the identity.
d.deleteEndpointQuiet(ep, endpoint.DeleteConfig{
NoIdentityRelease: true,
NoIPRelease: true,
})
endpointCleanupCompleted.Done()
}(ep)
}
endpointCleanupCompleted.Wait()
go func() {
regenerated, total := 0, 0
if len(state.restored) > 0 {
for buildSuccess := range epRegenerated {
if buildSuccess {
regenerated++
}
total++
if total >= len(state.restored) {
break
}
}
}
close(epRegenerated)
log.WithFields(logrus.Fields{
"regenerated": regenerated,
"total": total,
}).Info("Finished regenerating restored endpoints")
close(restoreComplete)
}()
return
}
func (d *Daemon) allocateIPsLocked(ep *endpoint.Endpoint) error {
var err error
if option.Config.EnableIPv6 && ep.IPv6 != nil {
err = d.ipam.AllocateIP(ep.IPv6.IP(), ep.HumanStringLocked()+" [restored]")
if err != nil {
return fmt.Errorf("unable to reallocate IPv6 address: %s", err)
}
defer func() {
if err != nil {
d.ipam.ReleaseIP(ep.IPv6.IP())
}
}()
}
if option.Config.EnableIPv4 && ep.IPv4 != nil {
if err = d.ipam.AllocateIP(ep.IPv4.IP(), ep.HumanStringLocked()+" [restored]"); err != nil {
return fmt.Errorf("unable to reallocate IPv4 address: %s", err)
}
}
return nil
}
func (d *Daemon) initRestore(restoredEndpoints *endpointRestoreState) chan struct{} {
bootstrapStats.restore.Start()
var restoreComplete chan struct{}
if option.Config.RestoreState {
// When we regenerate restored endpoints, it is guaranteed tha we have
// received the full list of policies present at the time the daemon
// is bootstrapped.
restoreComplete = d.regenerateRestoredEndpoints(restoredEndpoints)
go func() {
<-restoreComplete
endParallelMapMode()
}()
go func() {
if k8s.IsEnabled() {
// Start controller which removes any leftover Kubernetes
// services that may have been deleted while Cilium was not
// running. Once this controller succeeds, because it has no
// RunInterval specified, it will not run again unless updated
// elsewhere. This means that if, for instance, a user manually
// adds a service via the CLI into the BPF maps, that it will
// not be cleaned up by the daemon until it restarts.
controller.NewManager().UpdateController("sync-lb-maps-with-k8s-services",
controller.ControllerParams{
DoFunc: func(ctx context.Context) error {
return d.syncLBMapsWithK8s()
},
},
)
return
}
if err := d.SyncLBMap(); err != nil {
log.WithError(err).Warn("Error while recovering endpoints")
}
}()
} else {
log.Info("State restore is disabled. Existing endpoints on node are ignored")
// We need to read all docker containers so we know we won't
// going to allocate the same IP addresses and we will ignore
// these containers from reading.
workloads.IgnoreRunningWorkloads()
// No restore happened, end parallel map mode immediately
endParallelMapMode()
}
bootstrapStats.restore.End(true)
return restoreComplete
}