Skip to content

Commit e957538

Browse files
committed
[debug / test - nodeadm uninstall] Adding os.remove logs
1 parent d182d6a commit e957538

File tree

15 files changed

+447
-39
lines changed

15 files changed

+447
-39
lines changed

internal/artifact/install.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/pkg/errors"
15+
"go.uber.org/zap"
1516
)
1617

1718
// DefaultDirPerms are the permissions assigned to a directory when an Install* func is called
@@ -38,6 +39,62 @@ func InstallFile(dst string, src io.Reader, perms fs.FileMode) error {
3839
return err
3940
}
4041

42+
// InstallFileWithLogging installs src to dst with perms permissions and provides detailed logging.
43+
// It ensures any base paths exist before installing.
44+
func InstallFileWithLogging(dst string, src io.Reader, perms fs.FileMode, logger *zap.Logger) error {
45+
46+
// Check if /etc/passwd file is present
47+
passwdFile := "/etc/passwd"
48+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
49+
logger.Warn("Before /etc/passwd file does not exist", zap.String("path", passwdFile))
50+
} else if err != nil {
51+
logger.Error("Before Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
52+
} else {
53+
logger.Info("Before /etc/passwd file is present", zap.String("path", passwdFile))
54+
}
55+
56+
logger.Info("Installing file", zap.String("destination", dst))
57+
58+
logger.Debug("Removing existing file if present", zap.String("path", dst))
59+
if err := os.RemoveAll(dst); err != nil {
60+
logger.Error("Failed to remove existing file", zap.String("path", dst), zap.Error(err))
61+
return err
62+
}
63+
64+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
65+
logger.Warn("After /etc/passwd file does not exist", zap.String("path", passwdFile))
66+
} else if err != nil {
67+
logger.Error("After Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
68+
} else {
69+
logger.Info("After /etc/passwd file is present", zap.String("path", passwdFile))
70+
}
71+
72+
parentDir := path.Dir(dst)
73+
logger.Debug("Creating parent directories", zap.String("path", parentDir))
74+
if err := os.MkdirAll(parentDir, DefaultDirPerms); err != nil {
75+
logger.Error("Failed to create parent directories", zap.String("path", parentDir), zap.Error(err))
76+
return err
77+
}
78+
79+
logger.Debug("Creating destination file", zap.String("path", dst))
80+
fh, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, perms)
81+
if err != nil {
82+
logger.Error("Failed to create destination file", zap.String("path", dst), zap.Error(err))
83+
return err
84+
}
85+
defer fh.Close()
86+
87+
logger.Debug("Copying content to destination file", zap.String("path", dst))
88+
_, err = io.Copy(fh, src)
89+
if err != nil {
90+
logger.Error("Failed to copy content to destination file", zap.String("path", dst), zap.Error(err))
91+
return err
92+
}
93+
94+
logger.Info("Successfully installed file", zap.String("destination", dst))
95+
return nil
96+
}
97+
4198
// InstallTarGz untars the src file into the dst directory and deletes the src tgz file
4299
func InstallTarGz(dst, src string) error {
43100
if err := os.MkdirAll(dst, DefaultDirPerms); err != nil {

internal/cleanup/cleanup.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,38 @@ func (c *Force) Cleanup() error {
5656
}
5757

5858
func (c *Force) removeDir(dir string) error {
59-
c.logger.Info("Removing directory", zap.String("path", dir))
60-
return os.RemoveAll(dir)
59+
// Check if /etc/passwd file is present
60+
passwdFile := "/etc/passwd"
61+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
62+
c.logger.Warn("Before /etc/passwd file does not exist", zap.String("path", passwdFile))
63+
} else if err != nil {
64+
c.logger.Error("Before Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
65+
} else {
66+
c.logger.Info("Before /etc/passwd file is present", zap.String("path", passwdFile))
67+
}
68+
69+
// Check if directory exists before attempting to remove
70+
if _, err := os.Stat(dir); os.IsNotExist(err) {
71+
c.logger.Info("Directory does not exist, skipping removal", zap.String("path", dir))
72+
return nil
73+
} else if err != nil {
74+
c.logger.Error("Error checking directory status", zap.String("path", dir), zap.Error(err))
75+
return err
76+
}
77+
78+
c.logger.Info("--- SAIB Removing directory (force cleanup)", zap.String("path", dir))
79+
if err := os.RemoveAll(dir); err != nil {
80+
c.logger.Error("--- SAIB Failed to remove directory", zap.String("path", dir), zap.Error(err))
81+
return err
82+
}
83+
c.logger.Info("Successfully removed directory", zap.String("path", dir))
84+
85+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
86+
c.logger.Warn("After /etc/passwd file does not exist", zap.String("path", passwdFile))
87+
} else if err != nil {
88+
c.logger.Error("After Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
89+
} else {
90+
c.logger.Info("After /etc/passwd file is present", zap.String("path", passwdFile))
91+
}
92+
return nil
6193
}

internal/cni/install.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,25 @@ func downloadFileTo(ctx context.Context, opts InstallOptions) error {
9292
return nil
9393
}
9494

95-
func Uninstall() error {
96-
return os.RemoveAll(rootDir)
95+
func Uninstall(logger *zap.Logger) error {
96+
// Check if directory exists before attempting to remove
97+
if _, err := os.Stat(rootDir); os.IsNotExist(err) {
98+
logger.Info("CNI directory does not exist, skipping removal", zap.String("path", rootDir))
99+
return nil
100+
} else if err != nil {
101+
logger.Error("Error checking CNI directory status", zap.String("path", rootDir), zap.Error(err))
102+
return err
103+
}
104+
105+
// Remove the CNI root directory
106+
logger.Info("Removing CNI directory", zap.String("path", rootDir))
107+
if err := os.RemoveAll(rootDir); err != nil {
108+
logger.Error("Failed to remove CNI directory", zap.String("path", rootDir), zap.Error(err))
109+
return err
110+
}
111+
logger.Info("Successfully removed CNI directory", zap.String("path", rootDir))
112+
113+
return nil
97114
}
98115

99116
// Upgrade re-installs the cni-plugins available from the source

internal/containerd/install.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/pkg/errors"
11+
"go.uber.org/zap"
1112

1213
"github.com/aws/eks-hybrid/internal/artifact"
1314
"github.com/aws/eks-hybrid/internal/daemon"
@@ -52,17 +53,48 @@ func Install(ctx context.Context, artifactsTracker *tracker.Tracker, source Sour
5253
return nil
5354
}
5455

55-
func Uninstall(ctx context.Context, source Source) error {
56+
func Uninstall(ctx context.Context, source Source, logger *zap.Logger) error {
57+
logger.Info("Starting containerd uninstall process")
58+
5659
if isContainerdInstalled() {
60+
logger.Info("Containerd is installed, proceeding with uninstall")
61+
5762
containerd := source.GetContainerd(ContainerdVersion)
63+
logger.Info("Executing containerd uninstall command")
5864
if err := cmd.Retry(ctx, containerd.UninstallCmd, 5*time.Second); err != nil {
65+
logger.Error("Failed to uninstall containerd package", zap.Error(err))
5966
return errors.Wrap(err, "uninstalling containerd")
6067
}
68+
logger.Info("Successfully uninstalled containerd package")
69+
70+
passwdFile := "/etc/passwd"
71+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
72+
logger.Warn("Before /etc/passwd file does not exist", zap.String("path", passwdFile))
73+
} else if err != nil {
74+
logger.Error("Before Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
75+
} else {
76+
logger.Info("Before /etc/passwd file is present", zap.String("path", passwdFile))
77+
}
6178

79+
logger.Info("Removing containerd config directory", zap.String("path", containerdConfigDir))
6280
if err := os.RemoveAll(containerdConfigDir); err != nil {
81+
logger.Error("Failed to remove containerd config directory", zap.String("path", containerdConfigDir), zap.Error(err))
6382
return errors.Wrap(err, "removing containerd config files")
6483
}
84+
logger.Info("Successfully removed containerd config directory", zap.String("path", containerdConfigDir))
85+
86+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
87+
logger.Warn("After /etc/passwd file does not exist", zap.String("path", passwdFile))
88+
} else if err != nil {
89+
logger.Error("After Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
90+
} else {
91+
logger.Info("After /etc/passwd file is present", zap.String("path", passwdFile))
92+
}
93+
} else {
94+
logger.Info("Containerd is not installed, skipping uninstall")
6595
}
96+
97+
logger.Info("Containerd uninstall process completed successfully")
6698
return nil
6799
}
68100

internal/flows/uninstall.go

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
const eksConfigDir = "/etc/eks"
2828

2929
type (
30-
CNIUninstall func() error
30+
CNIUninstall func(*zap.Logger) error
3131
)
3232

3333
type Uninstaller struct {
@@ -53,7 +53,7 @@ func (u *Uninstaller) Run(ctx context.Context) error {
5353

5454
u.Logger.Info("Finished uninstallation tasks...")
5555

56-
return tracker.Clear()
56+
return tracker.Clear(u.Logger)
5757
}
5858

5959
func (u *Uninstaller) uninstallDaemons(ctx context.Context) error {
@@ -62,9 +62,27 @@ func (u *Uninstaller) uninstallDaemons(ctx context.Context) error {
6262
if err := u.DaemonManager.StopDaemon(kubelet.KubeletDaemonName); err != nil {
6363
return err
6464
}
65-
if err := kubelet.Uninstall(kubelet.UninstallOptions{}); err != nil {
65+
66+
// Log the kubelet directories that will be removed
67+
kubeletPaths := []string{
68+
"/usr/bin/kubelet",
69+
"/etc/systemd/system/kubelet.service",
70+
"/etc/kubernetes/kubelet/kubeconfig",
71+
"/etc/kubernetes",
72+
"/etc/kubernetes/kubelet/pki/kubelet-server-current.pem",
73+
}
74+
75+
for _, kubeletPath := range kubeletPaths {
76+
if _, err := os.Stat(kubeletPath); err == nil {
77+
u.Logger.Info("Removing kubelet path", zap.String("path", kubeletPath))
78+
}
79+
}
80+
81+
if err := kubelet.Uninstall(kubelet.UninstallOptions{Logger: u.Logger}); err != nil {
82+
u.Logger.Error("Failed to uninstall kubelet", zap.Error(err))
6683
return err
6784
}
85+
u.Logger.Info("Successfully uninstalled kubelet")
6886
}
6987
if u.Artifacts.Ssm {
7088
u.Logger.Info("Stopping SSM daemon...")
@@ -114,7 +132,7 @@ func (u *Uninstaller) uninstallDaemons(ctx context.Context) error {
114132
if err := u.DaemonManager.StopDaemon(containerd.ContainerdDaemonName); err != nil {
115133
return err
116134
}
117-
if err := containerd.Uninstall(ctx, u.PackageManager); err != nil {
135+
if err := containerd.Uninstall(ctx, u.PackageManager, u.Logger); err != nil {
118136
return err
119137
}
120138
}
@@ -124,31 +142,39 @@ func (u *Uninstaller) uninstallDaemons(ctx context.Context) error {
124142
func (u *Uninstaller) uninstallBinaries(ctx context.Context) error {
125143
if u.Artifacts.Kubectl {
126144
u.Logger.Info("Uninstalling kubectl...")
127-
if err := kubectl.Uninstall(); err != nil {
145+
if err := kubectl.Uninstall(u.Logger); err != nil {
128146
return err
129147
}
130148
}
131149
if u.Artifacts.CniPlugins {
132150
u.Logger.Info("Uninstalling cni-plugins...")
133-
if err := u.CNIUninstall(); err != nil {
151+
// Use the existing CNI uninstall function but add our own logging
152+
if _, err := os.Stat("/opt/cni"); os.IsNotExist(err) {
153+
u.Logger.Info("CNI directory does not exist, skipping removal", zap.String("path", "/opt/cni"))
154+
} else if err != nil {
155+
u.Logger.Error("Error checking CNI directory status", zap.String("path", "/opt/cni"), zap.Error(err))
134156
return err
157+
} else {
158+
if err := u.CNIUninstall(u.Logger); err != nil {
159+
return err
160+
}
135161
}
136162
}
137163
if u.Artifacts.IamAuthenticator {
138164
u.Logger.Info("Uninstalling IAM authenticator...")
139-
if err := iamauthenticator.Uninstall(); err != nil {
165+
if err := iamauthenticator.Uninstall(u.Logger); err != nil {
140166
return err
141167
}
142168
}
143169
if u.Artifacts.IamRolesAnywhere {
144170
u.Logger.Info("Uninstalling AWS signing helper...")
145-
if err := iamrolesanywhere.Uninstall(); err != nil {
171+
if err := iamrolesanywhere.Uninstall(u.Logger); err != nil {
146172
return err
147173
}
148174
}
149175
if u.Artifacts.ImageCredentialProvider {
150176
u.Logger.Info("Uninstalling image credential provider...")
151-
if err := imagecredentialprovider.Uninstall(); err != nil {
177+
if err := imagecredentialprovider.Uninstall(u.Logger); err != nil {
152178
return err
153179
}
154180
}
@@ -167,8 +193,37 @@ func (u *Uninstaller) cleanup() error {
167193
return err
168194
}
169195

170-
if err := os.RemoveAll(eksConfigDir); err != nil {
196+
// Log and remove EKS config directory
197+
if _, err := os.Stat(eksConfigDir); os.IsNotExist(err) {
198+
u.Logger.Info("EKS config directory does not exist, skipping removal", zap.String("path", eksConfigDir))
199+
} else if err != nil {
200+
u.Logger.Error("Error checking EKS config directory status", zap.String("path", eksConfigDir), zap.Error(err))
171201
return err
202+
} else {
203+
204+
passwdFile := "/etc/passwd"
205+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
206+
u.Logger.Warn("Before /etc/passwd file does not exist", zap.String("path", passwdFile))
207+
} else if err != nil {
208+
u.Logger.Error("Before Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
209+
} else {
210+
u.Logger.Info("Before /etc/passwd file is present", zap.String("path", passwdFile))
211+
}
212+
213+
u.Logger.Info("Removing EKS config directory", zap.String("path", eksConfigDir))
214+
if err := os.RemoveAll(eksConfigDir); err != nil {
215+
u.Logger.Error("Failed to remove EKS config directory", zap.String("path", eksConfigDir), zap.Error(err))
216+
return err
217+
}
218+
u.Logger.Info("Successfully removed EKS config directory", zap.String("path", eksConfigDir))
219+
220+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
221+
u.Logger.Warn("After /etc/passwd file does not exist", zap.String("path", passwdFile))
222+
} else if err != nil {
223+
u.Logger.Error("After Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
224+
} else {
225+
u.Logger.Info("After /etc/passwd file is present", zap.String("path", passwdFile))
226+
}
172227
}
173228

174229
return nil

internal/iamauthenticator/install.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,31 @@ func downloadFileTo(ctx context.Context, opts InstallOptions) error {
8585
return nil
8686
}
8787

88-
func Uninstall() error {
89-
return os.RemoveAll(IAMAuthenticatorBinPath)
88+
func Uninstall(logger *zap.Logger) error {
89+
90+
passwdFile := "/etc/passwd"
91+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
92+
logger.Warn("Before /etc/passwd file does not exist", zap.String("path", passwdFile))
93+
} else if err != nil {
94+
logger.Error("Before Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
95+
} else {
96+
logger.Info("Before /etc/passwd file is present", zap.String("path", passwdFile))
97+
}
98+
99+
logger.Info("Uninstalling IAM authenticator", zap.String("path", IAMAuthenticatorBinPath))
100+
if err := os.RemoveAll(IAMAuthenticatorBinPath); err != nil {
101+
logger.Error("Failed to remove IAM authenticator binary", zap.String("path", IAMAuthenticatorBinPath), zap.Error(err))
102+
return err
103+
}
104+
logger.Info("Successfully removed IAM authenticator binary", zap.String("path", IAMAuthenticatorBinPath))
105+
if _, err := os.Stat(passwdFile); os.IsNotExist(err) {
106+
logger.Warn("After /etc/passwd file does not exist", zap.String("path", passwdFile))
107+
} else if err != nil {
108+
logger.Error("After Error checking /etc/passwd file status", zap.String("path", passwdFile), zap.Error(err))
109+
} else {
110+
logger.Info("After /etc/passwd file is present", zap.String("path", passwdFile))
111+
}
112+
return nil
90113
}
91114

92115
func Upgrade(ctx context.Context, src IAMAuthenticatorSource, log *zap.Logger) error {

0 commit comments

Comments
 (0)