-
Notifications
You must be signed in to change notification settings - Fork 450
/
reconciler.go
411 lines (338 loc) · 15.7 KB
/
reconciler.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package operatingsystemconfig
import (
"bytes"
"context"
"errors"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"time"
"github.com/go-logr/logr"
"github.com/spf13/afero"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
extensionsv1alpha1helper "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1/helper"
"github.com/gardener/gardener/pkg/component/extensions/operatingsystemconfig/original/components/kubelet"
"github.com/gardener/gardener/pkg/controllerutils"
"github.com/gardener/gardener/pkg/nodeagent"
"github.com/gardener/gardener/pkg/nodeagent/apis/config"
nodeagentv1alpha1 "github.com/gardener/gardener/pkg/nodeagent/apis/config/v1alpha1"
"github.com/gardener/gardener/pkg/nodeagent/dbus"
filespkg "github.com/gardener/gardener/pkg/nodeagent/files"
"github.com/gardener/gardener/pkg/nodeagent/registry"
"github.com/gardener/gardener/pkg/utils/flow"
)
const lastAppliedOperatingSystemConfigFilePath = nodeagentv1alpha1.BaseDir + "/last-applied-osc.yaml"
// Reconciler decodes the OperatingSystemConfig resources from secrets and applies the systemd units and files to the
// node.
type Reconciler struct {
Client client.Client
Config config.OperatingSystemConfigControllerConfig
Recorder record.EventRecorder
DBus dbus.DBus
FS afero.Afero
Extractor registry.Extractor
CancelContext context.CancelFunc
HostName string
NodeName string
}
// Reconcile decodes the OperatingSystemConfig resources from secrets and applies the systemd units and files to the
// node.
func (r *Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := logf.FromContext(ctx)
ctx, cancel := controllerutils.GetMainReconciliationContext(ctx, controllerutils.DefaultReconciliationTimeout)
defer cancel()
secret := &corev1.Secret{}
if err := r.Client.Get(ctx, request.NamespacedName, secret); err != nil {
if apierrors.IsNotFound(err) {
log.V(1).Info("Object is gone, stop reconciling")
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("error retrieving object from store: %w", err)
}
node, err := r.getNode(ctx)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed getting node: %w", err)
}
osc, oscRaw, oscChecksum, err := extractOSCFromSecret(secret)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed extracting OSC from secret: %w", err)
}
oscChanges, err := computeOperatingSystemConfigChanges(r.FS, osc)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed calculating the OSC changes: %w", err)
}
if node != nil && node.Annotations[nodeagentv1alpha1.AnnotationKeyChecksumAppliedOperatingSystemConfig] == oscChecksum {
log.Info("Configuration on this node is up to date, nothing to be done")
return reconcile.Result{}, nil
}
log.Info("Applying new or changed files")
if err := r.applyChangedFiles(ctx, log, oscChanges.files.changed); err != nil {
return reconcile.Result{}, fmt.Errorf("failed applying changed files: %w", err)
}
log.Info("Applying new or changed units")
if err := r.applyChangedUnits(ctx, log, oscChanges.units.changed); err != nil {
return reconcile.Result{}, fmt.Errorf("failed applying changed units: %w", err)
}
log.Info("Removing no longer needed units")
if err := r.removeDeletedUnits(ctx, log, node, oscChanges.units.deleted); err != nil {
return reconcile.Result{}, fmt.Errorf("failed removing deleted units: %w", err)
}
log.Info("Reloading systemd daemon")
if err := r.DBus.DaemonReload(ctx); err != nil {
return reconcile.Result{}, fmt.Errorf("failed reloading systemd daemon: %w", err)
}
log.Info("Executing unit commands (start/stop)")
mustRestartGardenerNodeAgent, err := r.executeUnitCommands(ctx, log, node, oscChanges.units.changed)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed executing unit commands: %w", err)
}
log.Info("Removing no longer needed files")
if err := r.removeDeletedFiles(log, oscChanges.files.deleted); err != nil {
return reconcile.Result{}, fmt.Errorf("failed removing deleted files: %w", err)
}
log.Info("Successfully applied operating system config",
"changedFiles", len(oscChanges.files.changed),
"deletedFiles", len(oscChanges.files.deleted),
"changedUnits", len(oscChanges.units.changed),
"deletedUnits", len(oscChanges.units.deleted),
)
log.Info("Persisting current operating system config as 'last-applied' file to the disk", "path", lastAppliedOperatingSystemConfigFilePath)
if err := r.FS.WriteFile(lastAppliedOperatingSystemConfigFilePath, oscRaw, 0644); err != nil {
return reconcile.Result{}, fmt.Errorf("unable to write current OSC to file path %q: %w", lastAppliedOperatingSystemConfigFilePath, err)
}
if mustRestartGardenerNodeAgent {
log.Info("Must restart myself (gardener-node-agent unit), canceling the context to initiate graceful shutdown")
r.CancelContext()
return reconcile.Result{}, nil
}
if node == nil {
log.Info("Waiting for Node to get registered by kubelet, requeuing")
return reconcile.Result{RequeueAfter: 5 * time.Second}, nil
}
log.Info("Deleting kubelet bootstrap kubeconfig file (in case it still exists)")
if err := r.FS.Remove(kubelet.PathKubeconfigBootstrap); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return reconcile.Result{}, fmt.Errorf("failed removing kubelet bootstrap kubeconfig file %q: %w", kubelet.PathKubeconfigBootstrap, err)
}
if err := r.FS.Remove(nodeagentv1alpha1.BootstrapTokenFilePath); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return reconcile.Result{}, fmt.Errorf("failed removing bootstrap token file %q: %w", nodeagentv1alpha1.BootstrapTokenFilePath, err)
}
r.Recorder.Event(node, corev1.EventTypeNormal, "OSCApplied", "Operating system config has been applied successfully")
patch := client.MergeFrom(node.DeepCopy())
metav1.SetMetaDataLabel(&node.ObjectMeta, v1beta1constants.LabelWorkerKubernetesVersion, r.Config.KubernetesVersion.String())
metav1.SetMetaDataAnnotation(&node.ObjectMeta, nodeagentv1alpha1.AnnotationKeyChecksumAppliedOperatingSystemConfig, oscChecksum)
return reconcile.Result{RequeueAfter: r.Config.SyncPeriod.Duration}, r.Client.Patch(ctx, node, patch)
}
func (r *Reconciler) getNode(ctx context.Context) (*corev1.Node, error) {
if r.NodeName != "" {
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: r.NodeName}}
if err := r.Client.Get(ctx, client.ObjectKeyFromObject(node), node); err != nil {
return nil, fmt.Errorf("unable to fetch node %q: %w", r.NodeName, err)
}
return node, nil
}
node, err := nodeagent.FetchNodeByHostName(ctx, r.Client, r.HostName)
if err != nil {
return nil, err
}
if node != nil {
r.NodeName = node.Name
}
return node, nil
}
var (
etcSystemdSystem = path.Join("/", "etc", "systemd", "system")
defaultFilePermissions os.FileMode = 0600
defaultDirPermissions os.FileMode = 0755
)
func (r *Reconciler) applyChangedFiles(ctx context.Context, log logr.Logger, files []extensionsv1alpha1.File) error {
tmpDir, err := r.FS.TempDir(nodeagentv1alpha1.TempDir, "osc-reconciliation-file-")
if err != nil {
return fmt.Errorf("unable to create temporary directory: %w", err)
}
defer func() { utilruntime.HandleError(r.FS.RemoveAll(tmpDir)) }()
for _, file := range files {
permissions := defaultFilePermissions
if file.Permissions != nil {
permissions = fs.FileMode(*file.Permissions)
}
switch {
case file.Content.Inline != nil:
if err := r.FS.MkdirAll(filepath.Dir(file.Path), defaultDirPermissions); err != nil {
return fmt.Errorf("unable to create directory %q: %w", file.Path, err)
}
data, err := extensionsv1alpha1helper.Decode(file.Content.Inline.Encoding, []byte(file.Content.Inline.Data))
if err != nil {
return fmt.Errorf("unable to decode data of file %q: %w", file.Path, err)
}
tmpFilePath := filepath.Join(tmpDir, filepath.Base(file.Path))
if err := r.FS.WriteFile(tmpFilePath, data, permissions); err != nil {
return fmt.Errorf("unable to create temporary file %q: %w", tmpFilePath, err)
}
if err := filespkg.Move(r.FS, tmpFilePath, file.Path); err != nil {
return fmt.Errorf("unable to rename temporary file %q to %q: %w", tmpFilePath, file.Path, err)
}
log.Info("Successfully applied new or changed file", "path", file.Path)
case file.Content.ImageRef != nil:
if err := r.Extractor.CopyFromImage(ctx, file.Content.ImageRef.Image, file.Content.ImageRef.FilePathInImage, file.Path, permissions); err != nil {
return fmt.Errorf("unable to copy file %q from image %q to %q: %w", file.Content.ImageRef.FilePathInImage, file.Content.ImageRef.Image, file.Path, err)
}
log.Info("Successfully applied new or changed file from image", "path", file.Path, "image", file.Content.ImageRef.Image)
}
}
return nil
}
func (r *Reconciler) removeDeletedFiles(log logr.Logger, files []extensionsv1alpha1.File) error {
for _, file := range files {
if err := r.FS.Remove(file.Path); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return fmt.Errorf("unable to delete no longer needed file %q: %w", file.Path, err)
}
log.Info("Successfully removed no longer needed file", "path", file.Path)
}
return nil
}
func (r *Reconciler) applyChangedUnits(ctx context.Context, log logr.Logger, units []changedUnit) error {
for _, unit := range units {
unitFilePath := path.Join(etcSystemdSystem, unit.Name)
if unit.Content != nil {
oldUnitContent, err := r.FS.ReadFile(unitFilePath)
if err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return fmt.Errorf("unable to read existing unit file %q for %q: %w", unitFilePath, unit.Name, err)
}
newUnitContent := []byte(*unit.Content)
if !bytes.Equal(newUnitContent, oldUnitContent) {
if err := r.FS.WriteFile(unitFilePath, newUnitContent, defaultFilePermissions); err != nil {
return fmt.Errorf("unable to write unit file %q for %q: %w", unitFilePath, unit.Name, err)
}
log.Info("Successfully applied new or changed unit file", "path", unitFilePath)
}
// ensure file permissions are restored in case somebody changed them manually
if err := r.FS.Chmod(unitFilePath, defaultFilePermissions); err != nil {
return fmt.Errorf("unable to ensure permissions for unit file %q for %q: %w", unitFilePath, unit.Name, err)
}
}
dropInDirectory := unitFilePath + ".d"
if len(unit.DropIns) == 0 {
if err := r.FS.RemoveAll(dropInDirectory); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return fmt.Errorf("unable to delete systemd drop-in folder for unit %q: %w", unit.Name, err)
}
} else {
if err := r.FS.MkdirAll(dropInDirectory, defaultDirPermissions); err != nil {
return fmt.Errorf("unable to create drop-in directory %q for unit %q: %w", dropInDirectory, unit.Name, err)
}
for _, dropIn := range unit.dropIns.changed {
dropInFilePath := path.Join(dropInDirectory, dropIn.Name)
oldDropInContent, err := r.FS.ReadFile(dropInFilePath)
if err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return fmt.Errorf("unable to read existing drop-in file %q for unit %q: %w", dropInFilePath, unit.Name, err)
}
newDropInContent := []byte(dropIn.Content)
if !bytes.Equal(newDropInContent, oldDropInContent) {
if err := r.FS.WriteFile(dropInFilePath, newDropInContent, defaultFilePermissions); err != nil {
return fmt.Errorf("unable to write drop-in file %q for unit %q: %w", dropInFilePath, unit.Name, err)
}
log.Info("Successfully applied new or changed drop-in file for unit", "path", dropInFilePath, "unit", unit.Name)
}
// ensure file permissions are restored in case somebody changed them manually
if err := r.FS.Chmod(dropInFilePath, defaultFilePermissions); err != nil {
return fmt.Errorf("unable to ensure permissions for drop-in file %q for unit %q: %w", unitFilePath, unit.Name, err)
}
}
for _, dropIn := range unit.dropIns.deleted {
dropInFilePath := path.Join(dropInDirectory, dropIn.Name)
if err := r.FS.Remove(dropInFilePath); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return fmt.Errorf("unable to delete drop-in file %q for unit %q: %w", dropInFilePath, unit.Name, err)
}
log.Info("Successfully removed no longer needed drop-in file for unit", "path", dropInFilePath, "unitName", unit.Name)
}
}
if unit.Name == nodeagentv1alpha1.UnitName || ptr.Deref(unit.Enable, true) {
if err := r.DBus.Enable(ctx, unit.Name); err != nil {
return fmt.Errorf("unable to enable unit %q: %w", unit.Name, err)
}
log.Info("Successfully enabled unit", "unitName", unit.Name)
} else {
if err := r.DBus.Disable(ctx, unit.Name); err != nil {
return fmt.Errorf("unable to disable unit %q: %w", unit.Name, err)
}
log.Info("Successfully disabled unit", "unitName", unit.Name)
}
}
return nil
}
func (r *Reconciler) removeDeletedUnits(ctx context.Context, log logr.Logger, node client.Object, units []extensionsv1alpha1.Unit) error {
for _, unit := range units {
unitFilePath := path.Join(etcSystemdSystem, unit.Name)
unitFileExists, err := r.fileExists(unitFilePath)
if err != nil {
return fmt.Errorf("unable to check whether unit file %q exists: %w", unitFilePath, err)
}
if unitFileExists {
if err := r.DBus.Disable(ctx, unit.Name); err != nil {
return fmt.Errorf("unable to disable deleted unit %q: %w", unit.Name, err)
}
if err := r.DBus.Stop(ctx, r.Recorder, node, unit.Name); err != nil {
return fmt.Errorf("unable to stop deleted unit %q: %w", unit.Name, err)
}
if err := r.FS.Remove(unitFilePath); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return fmt.Errorf("unable to delete systemd unit file of deleted unit %q: %w", unit.Name, err)
}
}
if err := r.FS.RemoveAll(unitFilePath + ".d"); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
return fmt.Errorf("unable to delete systemd drop-in folder of deleted unit %q: %w", unit.Name, err)
}
log.Info("Successfully removed no longer needed unit", "unitName", unit.Name)
}
return nil
}
func (r *Reconciler) executeUnitCommands(ctx context.Context, log logr.Logger, node client.Object, units []changedUnit) (bool, error) {
var (
mustRestartGardenerNodeAgent bool
fns []flow.TaskFn
)
for _, u := range units {
unit := u
if unit.Name == nodeagentv1alpha1.UnitName {
mustRestartGardenerNodeAgent = true
continue
}
fns = append(fns, func(ctx context.Context) error {
if !ptr.Deref(unit.Enable, true) || (unit.Command != nil && *unit.Command == extensionsv1alpha1.CommandStop) {
if err := r.DBus.Stop(ctx, r.Recorder, node, unit.Name); err != nil {
return fmt.Errorf("unable to stop unit %q: %w", unit.Name, err)
}
log.Info("Successfully stopped unit", "unitName", unit.Name)
} else {
if err := r.DBus.Restart(ctx, r.Recorder, node, unit.Name); err != nil {
return fmt.Errorf("unable to restart unit %q: %w", unit.Name, err)
}
log.Info("Successfully restarted unit", "unitName", unit.Name)
}
return nil
})
}
return mustRestartGardenerNodeAgent, flow.Parallel(fns...)(ctx)
}
func (r *Reconciler) fileExists(path string) (bool, error) {
if _, err := r.FS.Stat(path); err != nil {
if errors.Is(err, afero.ErrFileNotFound) {
return false, nil
}
return false, err
}
return true, nil
}