-
Notifications
You must be signed in to change notification settings - Fork 466
/
sync.go
241 lines (196 loc) · 5.79 KB
/
sync.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package time
import (
"context"
"fmt"
"sync"
stdtime "time"
"github.com/AlekSi/pointer"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/state"
"go.uber.org/zap"
v1alpha1runtime "github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
"github.com/talos-systems/talos/internal/pkg/ntp"
"github.com/talos-systems/talos/pkg/resources/config"
"github.com/talos-systems/talos/pkg/resources/network"
"github.com/talos-systems/talos/pkg/resources/time"
)
// SyncController manages v1alpha1.TimeSync based on configuration and NTP sync process.
type SyncController struct {
V1Alpha1Mode v1alpha1runtime.Mode
NewNTPSyncer NewNTPSyncerFunc
bootTime stdtime.Time
}
// Name implements controller.Controller interface.
func (ctrl *SyncController) Name() string {
return "time.SyncController"
}
// Inputs implements controller.Controller interface.
func (ctrl *SyncController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: network.NamespaceName,
Type: network.TimeServerStatusType,
ID: pointer.ToString(network.TimeServerID),
Kind: controller.InputWeak,
},
{
Namespace: config.NamespaceName,
Type: config.MachineConfigType,
ID: pointer.ToString(config.V1Alpha1ID),
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *SyncController) Outputs() []controller.Output {
return []controller.Output{
{
Type: time.StatusType,
Kind: controller.OutputExclusive,
},
}
}
// NTPSyncer interface is implemented by ntp.Syncer, interface for mocking.
type NTPSyncer interface {
Run(ctx context.Context)
Synced() <-chan struct{}
EpochChange() <-chan struct{}
SetTimeServers([]string)
}
// NewNTPSyncerFunc function allows to replace ntp.Syncer with the mock.
type NewNTPSyncerFunc func(*zap.Logger, []string) NTPSyncer
// Run implements controller.Controller interface.
//
//nolint:gocyclo,cyclop
func (ctrl *SyncController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
if ctrl.bootTime.IsZero() {
ctrl.bootTime = stdtime.Now()
}
if ctrl.NewNTPSyncer == nil {
ctrl.NewNTPSyncer = func(logger *zap.Logger, timeServers []string) NTPSyncer {
return ntp.NewSyncer(logger, timeServers)
}
}
var (
syncCtx context.Context
syncCtxCancel context.CancelFunc
syncWg sync.WaitGroup
syncCh <-chan struct{}
epochCh <-chan struct{}
syncer NTPSyncer
timeSynced bool
epoch int
timeSyncTimeoutTimer *stdtime.Timer
timeSyncTimeoutCh <-chan stdtime.Time
)
defer func() {
if syncer != nil {
syncCtxCancel()
syncWg.Wait()
}
if timeSyncTimeoutTimer != nil {
timeSyncTimeoutTimer.Stop()
}
}()
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
case <-syncCh:
syncCh = nil
timeSynced = true
case <-epochCh:
epoch++
case <-timeSyncTimeoutCh:
timeSynced = true
timeSyncTimeoutTimer = nil
}
timeServersStatus, err := r.Get(ctx, resource.NewMetadata(network.NamespaceName, network.TimeServerStatusType, network.TimeServerID, resource.VersionUndefined))
if err != nil {
if !state.IsNotFoundError(err) {
return fmt.Errorf("error getting time server status: %w", err)
}
// time server list is not ready yet, wait for the next reconcile
continue
}
timeServers := timeServersStatus.(*network.TimeServerStatus).TypedSpec().NTPServers
cfg, err := r.Get(ctx, resource.NewMetadata(config.NamespaceName, config.MachineConfigType, config.V1Alpha1ID, resource.VersionUndefined))
if err != nil {
if !state.IsNotFoundError(err) {
return fmt.Errorf("error getting config: %w", err)
}
}
var syncTimeout stdtime.Duration
syncDisabled := false
if ctrl.V1Alpha1Mode == v1alpha1runtime.ModeContainer {
syncDisabled = true
}
if cfg != nil && cfg.(*config.MachineConfig).Config().Machine().Time().Disabled() {
syncDisabled = true
}
if cfg != nil {
syncTimeout = cfg.(*config.MachineConfig).Config().Machine().Time().BootTimeout()
}
if !timeSynced {
sinceBoot := stdtime.Since(ctrl.bootTime)
switch {
case syncTimeout == 0:
// disable sync timeout
if timeSyncTimeoutTimer != nil {
timeSyncTimeoutTimer.Stop()
}
timeSyncTimeoutCh = nil
case sinceBoot > syncTimeout:
// over sync timeout already, so in sync
timeSynced = true
default:
// make sure timer fires in whatever time is left till the timeout
if timeSyncTimeoutTimer == nil || !timeSyncTimeoutTimer.Reset(syncTimeout-sinceBoot) {
timeSyncTimeoutTimer = stdtime.NewTimer(syncTimeout - sinceBoot)
timeSyncTimeoutCh = timeSyncTimeoutTimer.C
}
}
}
switch {
case syncDisabled && syncer != nil:
// stop syncing
syncCtxCancel()
syncWg.Wait()
syncer = nil
syncCh = nil
epochCh = nil
case !syncDisabled && syncer == nil:
// start syncing
syncer = ctrl.NewNTPSyncer(logger, timeServers)
syncCh = syncer.Synced()
epochCh = syncer.EpochChange()
timeSynced = false
syncCtx, syncCtxCancel = context.WithCancel(ctx) //nolint:govet
syncWg.Add(1)
go func() {
defer syncWg.Done()
syncer.Run(syncCtx)
}()
}
if syncer != nil {
syncer.SetTimeServers(timeServers)
}
if syncDisabled {
timeSynced = true
}
if err = r.Modify(ctx, time.NewStatus(), func(r resource.Resource) error {
r.(*time.Status).SetStatus(time.StatusSpec{
Epoch: epoch,
Synced: timeSynced,
SyncDisabled: syncDisabled,
})
return nil
}); err != nil {
return fmt.Errorf("error updating objects: %w", err) //nolint:govet
}
}
}