/
cloud_config_downloader_cleaner.go
66 lines (58 loc) · 3.08 KB
/
cloud_config_downloader_cleaner.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
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 bootstrappers
import (
"context"
"errors"
"fmt"
"path"
"github.com/go-logr/logr"
"github.com/spf13/afero"
"github.com/gardener/gardener/pkg/component/extensions/operatingsystemconfig/downloader"
"github.com/gardener/gardener/pkg/nodeagent/dbus"
)
// CloudConfigDownloaderCleaner is a runnable for cleaning up the legacy cloud-config-downloader resources.
// TODO(rfranzke): Remove this bootstrapper when the UseGardenerNodeAgent feature gate gets removed.
type CloudConfigDownloaderCleaner struct {
Log logr.Logger
FS afero.Afero
DBus dbus.DBus
}
// Start performs the cleanup logic. Note that this function does only delete the following directories/files:
// - /var/lib/cloud-config-downloader
// - /etc/systemd/system/multi-user.target.wants/cloud-config-downloader.service (typically symlinks to
// /etc/systemd/system/cloud-config-downloader.service
//
// The /etc/systemd/system/cloud-config-downloader.service file already gets removed by cloud-config-downloader itself
// when migrating to gardener-node-agent because it is no longer part of the original OperatingSystemConfig. Hence,
// cloud-config-downloader considers it as stale and cleans it up.
// All this still leaves some artefacts on the nodes (e.g., `systemctl status cloud-config-downloader` and
// `journalctl -u cloud-config-downloader` still works), however, maybe that's even a benefit in case of operations/
// debugging activities. All nodes get rolled/replaced eventually (latest with the next OS/Kubernetes version update),
// so we leave the final cleanup for then (new nodes will have no traces of cloud-config-downloader whatsoever).
func (c *CloudConfigDownloaderCleaner) Start(ctx context.Context) error {
c.Log.Info("Removing legacy directory if it exists", "path", downloader.PathCCDDirectory)
if err := c.FS.RemoveAll(downloader.PathCCDDirectory); err != nil {
return fmt.Errorf("failed to remove legacy directory %q: %w", downloader.PathCCDDirectory, err)
}
unitFilePath := path.Join("/", "etc", "systemd", "system", "multi-user.target.wants", downloader.UnitName)
c.Log.Info("Removing legacy unit file if it exists", "path", unitFilePath)
if err := c.FS.Remove(unitFilePath); err != nil {
if !errors.Is(err, afero.ErrFileNotFound) {
return fmt.Errorf("failed removing legacy unit file file %q: %w", unitFilePath, err)
}
return nil
}
return c.DBus.DaemonReload(ctx)
}