-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
cell.go
58 lines (48 loc) · 1.49 KB
/
cell.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package utime
import (
"context"
"fmt"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/hive/cell"
"github.com/cilium/cilium/pkg/maps/configmap"
"github.com/cilium/cilium/pkg/time"
)
const (
syncControllerName = "sync-utime"
syncControllerInterval = 1 * time.Minute
)
var syncControllerGroupName = controller.NewGroup("sync-utime")
// Cell initializes and manages the utime offset synchronization.
var Cell = cell.Module(
"utime",
"Synchronizes utime offset between userspace and datapath",
cell.Invoke(initUtimeSync),
)
func initUtimeSync(lifecycle cell.Lifecycle, configMap configmap.Map) {
controllerManager := controller.NewManager()
lifecycle.Append(cell.Hook{
OnStart: func(startCtx cell.HookContext) error {
ctrl := &utimeController{configMap: configMap}
// Add controller for keeping clock in sync for NTP time jumps and any difference
// between monotonic and boottime clocks.
controllerManager.UpdateController(syncControllerName,
controller.ControllerParams{
Group: syncControllerGroupName,
DoFunc: func(ctx context.Context) error {
return ctrl.sync()
},
RunInterval: syncControllerInterval,
},
)
return nil
},
OnStop: func(stopCtx cell.HookContext) error {
if err := controllerManager.RemoveController(syncControllerName); err != nil {
return fmt.Errorf("failed to remove controller: %w", err)
}
return nil
},
})
}