-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_status.go
215 lines (175 loc) · 5.5 KB
/
node_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
// 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 k8s
import (
"context"
"fmt"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/aenix-io/talm/internal/app/machined/pkg/controllers/k8s/internal/nodewatch"
"github.com/siderolabs/talos/pkg/conditions"
"github.com/siderolabs/talos/pkg/kubernetes"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
)
// watchErrorsThreshold is the number of consecutive watch errors before the controller stops watching.
const watchErrorsThreshold = 5
// NodeStatusController pulls list of Affiliate resource from the Kubernetes registry.
type NodeStatusController struct{}
// Name implements controller.Controller interface.
func (ctrl *NodeStatusController) Name() string {
return "k8s.NodeStatusController"
}
// Inputs implements controller.Controller interface.
func (ctrl *NodeStatusController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: k8s.NamespaceName,
Type: k8s.NodenameType,
ID: optional.Some(k8s.NodenameID),
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *NodeStatusController) Outputs() []controller.Output {
return []controller.Output{
{
Type: k8s.NodeStatusType,
Kind: controller.OutputExclusive,
},
}
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo,cyclop
func (ctrl *NodeStatusController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
var (
kubernetesClient *kubernetes.Client
nodewatcher *nodewatch.NodeWatcher
watchCtxCancel context.CancelFunc
notifyCh <-chan struct{}
watchErrCh <-chan error
notifyCloser func()
watchErrors int
)
closeWatcher := func() {
if watchCtxCancel != nil {
watchCtxCancel()
watchCtxCancel = nil
}
if notifyCloser != nil {
notifyCloser()
notifyCloser = nil
notifyCh = nil
watchErrCh = nil
watchErrors = 0
}
if kubernetesClient != nil {
kubernetesClient.Close() //nolint:errcheck
kubernetesClient = nil
}
nodewatcher = nil
}
defer closeWatcher()
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
case <-notifyCh:
watchErrors = 0
case watchErr := <-watchErrCh:
logger.Error("node watch error", zap.Error(watchErr), zap.Int("error_count", watchErrors))
watchErrors++
if watchErrors >= watchErrorsThreshold {
closeWatcher()
watchErrors = 0
} else {
// keep waiting
continue
}
}
nodename, err := safe.ReaderGetByID[*k8s.Nodename](ctx, r, k8s.NodenameID)
if err != nil {
if !state.IsNotFoundError(err) {
return fmt.Errorf("error getting nodename: %w", err)
}
continue
}
if nodename.TypedSpec().SkipNodeRegistration {
// node is not registered with Kubernetes, so we can't pull the status
closeWatcher()
continue
}
if err = conditions.WaitForKubeconfigReady(constants.KubeletKubeconfig).Wait(ctx); err != nil {
return err
}
if nodewatcher != nil && nodewatcher.Nodename() != nodename.TypedSpec().Nodename {
// nodename changed, so we need to reinitialize the watcher
closeWatcher()
}
if kubernetesClient == nil {
kubernetesClient, err = kubernetes.NewClientFromKubeletKubeconfig()
if err != nil {
return fmt.Errorf("error building kubernetes client: %w", err)
}
}
if nodewatcher == nil {
nodewatcher = nodewatch.NewNodeWatcher(kubernetesClient, nodename.TypedSpec().Nodename)
}
if notifyCh == nil {
var watchCtx context.Context
watchCtx, watchCtxCancel = context.WithCancel(ctx) //nolint:govet
notifyCh, watchErrCh, notifyCloser, err = nodewatcher.Watch(watchCtx)
if err != nil {
return fmt.Errorf("error setting up registry watcher: %w", err) //nolint:govet
}
}
touchedIDs := make(map[resource.ID]struct{})
node, err := nodewatcher.Get()
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("error getting node: %w", err)
}
if node != nil {
if err = safe.WriterModify[*k8s.NodeStatus](ctx, r, k8s.NewNodeStatus(k8s.NamespaceName, node.Name),
func(res *k8s.NodeStatus) error {
res.TypedSpec().Nodename = node.Name
res.TypedSpec().Unschedulable = node.Spec.Unschedulable
res.TypedSpec().Labels = node.Labels
res.TypedSpec().Annotations = node.Annotations
res.TypedSpec().NodeReady = false
for _, condition := range node.Status.Conditions {
if condition.Type == v1.NodeReady {
res.TypedSpec().NodeReady = condition.Status == v1.ConditionTrue
}
}
return nil
},
); err != nil {
return err
}
touchedIDs[node.Name] = struct{}{}
}
items, err := safe.ReaderListAll[*k8s.NodeStatus](ctx, r)
if err != nil {
return fmt.Errorf("error listing node statuses: %w", err)
}
for iter := items.Iterator(); iter.Next(); {
if _, touched := touchedIDs[iter.Value().Metadata().ID()]; touched {
continue
}
if err = r.Destroy(ctx, iter.Value().Metadata()); err != nil {
return fmt.Errorf("error destroying node status: %w", err)
}
}
r.ResetRestartBackoff()
}
}