/
status.go
362 lines (314 loc) · 10.3 KB
/
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
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
/*
* Copyright (c) 2023 Baidu, Inc. All Rights Reserved.
*
* 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 cmd
import (
"context"
"fmt"
"os"
"time"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/datapath"
"github.com/go-openapi/strfmt"
versionapi "k8s.io/apimachinery/pkg/version"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/api/v1/models"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/backoff"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/controller"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/k8s"
k8smetrics "github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/k8s/metrics"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/lock"
nodeTypes "github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/node/types"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/option"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/rand"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/status"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/version"
)
const (
// k8sVersionCheckInterval is the interval in which the Kubernetes
// version is verified even if connectivity is given
k8sVersionCheckInterval = 15 * time.Minute
// k8sMinimumEventHearbeat is the time interval in which any received
// event will be considered proof that the apiserver connectivity is
// healthty
k8sMinimumEventHearbeat = time.Minute
)
var randGen = rand.NewSafeRand(time.Now().UnixNano())
type k8sVersion struct {
version string
lastVersionCheck time.Time
lock lock.Mutex
}
func (k *k8sVersion) cachedVersion() (string, bool) {
k.lock.Lock()
defer k.lock.Unlock()
if time.Since(k8smetrics.LastInteraction.Time()) > k8sMinimumEventHearbeat {
return "", false
}
if k.version == "" || time.Since(k.lastVersionCheck) > k8sVersionCheckInterval {
return "", false
}
return k.version, true
}
func (k *k8sVersion) update(version *versionapi.Info) string {
k.lock.Lock()
defer k.lock.Unlock()
k.version = fmt.Sprintf("%s.%s (%s) [%s]", version.Major, version.Minor, version.GitVersion, version.Platform)
k.lastVersionCheck = time.Now()
return k.version
}
var k8sVersionCache k8sVersion
func (d *Daemon) getK8sStatus() *models.K8sStatus {
if !k8s.IsEnabled() {
return &models.K8sStatus{State: models.StatusStateDisabled}
}
version, valid := k8sVersionCache.cachedVersion()
if !valid {
k8sVersion, err := k8s.Client().Discovery().ServerVersion()
if err != nil {
return &models.K8sStatus{State: models.StatusStateFailure, Msg: err.Error()}
}
version = k8sVersionCache.update(k8sVersion)
}
k8sStatus := &models.K8sStatus{
State: models.StatusStateOk,
Msg: version,
K8sAPIVersions: d.k8sWatcher.GetAPIGroups(),
}
return k8sStatus
}
func (d *Daemon) getCNIChainingStatus() *models.CNIChainingStatus {
// CNI chaining is enabled only from CILIUM_CNI_CHAINING_MODE env
mode := os.Getenv("CILIUM_CNI_CHAINING_MODE")
if len(mode) == 0 {
mode = models.CNIChainingStatusModeNone
}
return &models.CNIChainingStatus{
Mode: mode,
}
}
type getHealthz struct {
daemon *Daemon
}
func (d *Daemon) getNodeStatus() *models.ClusterStatus {
clusterStatus := models.ClusterStatus{
Self: nodeTypes.GetAbsoluteNodeName(),
}
for _, node := range d.nodeDiscovery.Manager.GetNodes() {
clusterStatus.Nodes = append(clusterStatus.Nodes, node.GetModel())
}
return &clusterStatus
}
// clientGCTimeout is the time for which the clients are kept. After timeout
// is reached, clients will be cleaned up.
const clientGCTimeout = 15 * time.Minute
type clusterNodesClient struct {
// mutex to protect the client against concurrent access
lock.RWMutex
lastSync time.Time
*models.ClusterNodeStatus
}
func (c *clusterNodesClient) NodeConfigurationChanged(config datapath.LocalNodeConfiguration) error {
return nil
}
func (c *clusterNodesClient) NodeAdd(newNode nodeTypes.Node) error {
c.Lock()
c.NodesAdded = append(c.NodesAdded, newNode.GetModel())
c.Unlock()
return nil
}
func (c *clusterNodesClient) NodeUpdate(oldNode, newNode nodeTypes.Node) error {
c.Lock()
defer c.Unlock()
// If the node is on the added list, just update it
for i, added := range c.NodesAdded {
if added.Name == newNode.Fullname() {
c.NodesAdded[i] = newNode.GetModel()
return nil
}
}
// otherwise, add the new node and remove the old one
c.NodesAdded = append(c.NodesAdded, newNode.GetModel())
c.NodesRemoved = append(c.NodesRemoved, oldNode.GetModel())
return nil
}
func (c *clusterNodesClient) NodeDelete(node nodeTypes.Node) error {
c.Lock()
// If the node was added/updated and removed before the clusterNodesClient
// was aware of it then we can safely remove it from the list of added
// nodes and not set it in the list of removed nodes.
found := -1
for i, added := range c.NodesAdded {
if added.Name == node.Fullname() {
found = i
}
}
if found != -1 {
c.NodesAdded = append(c.NodesAdded[:found], c.NodesAdded[found+1:]...)
} else {
c.NodesRemoved = append(c.NodesRemoved, node.GetModel())
}
c.Unlock()
return nil
}
func (c *clusterNodesClient) NodeValidateImplementation(node nodeTypes.Node) error {
// no-op
return nil
}
func (c *clusterNodesClient) NodeNeighDiscoveryEnabled() bool {
// no-op
return false
}
func (c *clusterNodesClient) NodeNeighborRefresh(ctx context.Context, node nodeTypes.Node) {
// no-op
return
}
func (c *clusterNodesClient) NodeCleanNeighbors(migrateOnly bool) {
// no-op
return
}
// getStatus returns the daemon status. If brief is provided a minimal version
// of the StatusResponse is provided.
func (d *Daemon) getStatus(brief bool) models.StatusResponse {
staleProbes := d.statusCollector.GetStaleProbes()
stale := make(map[string]strfmt.DateTime, len(staleProbes))
for probe, startTime := range staleProbes {
stale[probe] = strfmt.DateTime(startTime)
}
d.statusCollectMutex.RLock()
defer d.statusCollectMutex.RUnlock()
var sr models.StatusResponse
if brief {
var minimalControllers models.ControllerStatuses
if d.statusResponse.Controllers != nil {
for _, c := range d.statusResponse.Controllers {
if c.Status == nil {
continue
}
// With brief, the client should only care if a single controller
// is failing and its status so we don't need to continuing
// checking for failure messages for the remaining controllers.
if c.Status.LastFailureMsg != "" {
minimalControllers = append(minimalControllers, c.DeepCopy())
break
}
}
}
sr = models.StatusResponse{
Controllers: minimalControllers,
}
} else {
// d.statusResponse contains references, so we do a deep copy to be able to
// safely use sr after the method has returned
sr = *d.statusResponse.DeepCopy()
}
sr.Stale = stale
// CCEVersion definition
ver := version.GetCCEVersion()
cceVer := fmt.Sprintf("%s (v%s-%s)", ver.Version, ver.Version, ver.Revision)
switch {
case len(sr.Stale) > 0:
msg := "Stale status data"
sr.Cce = &models.Status{
State: models.StatusStateWarning,
Msg: fmt.Sprintf("%s %s", cceVer, msg),
}
case d.statusResponse.ContainerRuntime != nil && d.statusResponse.ContainerRuntime.State != models.StatusStateOk:
msg := "Container runtime is not ready"
if d.statusResponse.ContainerRuntime.State == models.StatusStateDisabled {
msg = "Container runtime is disabled"
}
sr.Cce = &models.Status{
State: d.statusResponse.ContainerRuntime.State,
Msg: fmt.Sprintf("%s %s", cceVer, msg),
}
default:
sr.Cce = &models.Status{
State: models.StatusStateOk,
Msg: cceVer,
}
}
return sr
}
func (d *Daemon) startStatusCollector() {
probes := []status.Probe{
{
Name: "check-locks",
Probe: func(ctx context.Context) (interface{}, error) {
// Try to acquire a couple of global locks to have the status API fail
// in case of a deadlock on these locks
option.Config.ConfigPatchMutex.Lock()
option.Config.ConfigPatchMutex.Unlock()
return nil, nil
},
OnStatusUpdate: func(status status.Status) {
d.statusCollectMutex.Lock()
defer d.statusCollectMutex.Unlock()
// FIXME we have no field for the lock status
},
},
{
Name: "kubernetes",
Interval: func(failures int) time.Duration {
if failures > 0 {
// While failing, we want an initial
// quick retry with exponential backoff
// to avoid continuous load on the
// apiserver
return backoff.CalculateDuration(5*time.Second, 2*time.Minute, 2.0, false, failures)
}
// The base interval is dependant on the
// cluster size. One status interval does not
// automatically translate to an apiserver
// interaction as any regular apiserver
// interaction is also used as an indication of
// successful connectivity so we can continue
// to be fairly aggressive.
//
// 1 | 7s
// 2 | 12s
// 4 | 15s
// 64 | 42s
// 512 | 1m02s
// 2048 | 1m15s
// 8192 | 1m30s
// 16384 | 1m32s
return d.nodeDiscovery.Manager.ClusterSizeDependantInterval(10 * time.Second)
},
Probe: func(ctx context.Context) (interface{}, error) {
return d.getK8sStatus(), nil
},
OnStatusUpdate: func(status status.Status) {
d.statusCollectMutex.Lock()
defer d.statusCollectMutex.Unlock()
},
},
{
Name: "controllers",
Probe: func(ctx context.Context) (interface{}, error) {
return controller.GetGlobalStatus(), nil
},
OnStatusUpdate: func(status status.Status) {
d.statusCollectMutex.Lock()
defer d.statusCollectMutex.Unlock()
// ControllerStatuses has no way to report errors
if status.Err == nil {
if s, ok := status.Data.(models.ControllerStatuses); ok {
d.statusResponse.Controllers = s
}
}
},
},
}
d.statusCollector = status.NewCollector(probes, status.Config{})
return
}