Skip to content

Commit 719c5d7

Browse files
committed
Move data dir into position before creating CNI symlinks
Addresses issue where CNI bin symlinks were created with targets that were not yet valid, which caused external container runtimes that rely on filesystem notifications to detect changes to think that the bins did not exist. Signed-off-by: Brad Davidson <[email protected]> (cherry picked from commit b3ac144) Signed-off-by: Brad Davidson <[email protected]>
1 parent 85815e5 commit 719c5d7

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

cmd/k3s/main.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,25 @@ func extract(dataDir string) (string, error) {
308308
return "", err
309309
}
310310

311+
// Rotate 'current' symlink into 'previous', and create a new 'current' that points
312+
// at the new directory.
313+
currentSymLink := filepath.Join(dataDir, "data", "current")
314+
previousSymLink := filepath.Join(dataDir, "data", "previous")
315+
if _, err := os.Lstat(currentSymLink); err == nil {
316+
if err := os.Rename(currentSymLink, previousSymLink); err != nil {
317+
return "", err
318+
}
319+
}
320+
if err := os.Symlink(dir, currentSymLink); err != nil {
321+
return "", err
322+
}
323+
324+
// Rename the new directory into place after updating symlinks, so that the k3s binary check at the start
325+
// of this function only succeeds if everything else has been completed successfully.
326+
if err := os.Rename(tempDest, dir); err != nil {
327+
return "", err
328+
}
329+
311330
// Create a stable CNI bin dir and place it first in the path so that users have a
312331
// consistent location to drop their own CNI plugin binaries.
313332
cniPath := filepath.Join(dataDir, "data", "cni")
@@ -326,13 +345,13 @@ func extract(dataDir string) (string, error) {
326345
// Non-symlink plugins in the stable CNI bin dir will not be overwritten, to allow users to replace our
327346
// CNI plugins with their own versions if they want. Note that the cni multicall binary itself is always
328347
// symlinked into the stable bin dir and should not be replaced.
329-
ents, err := os.ReadDir(filepath.Join(tempDest, "bin"))
348+
ents, err := os.ReadDir(filepath.Join(dir, "bin"))
330349
if err != nil {
331350
return "", err
332351
}
333352
for _, ent := range ents {
334353
if info, err := ent.Info(); err == nil && info.Mode()&fs.ModeSymlink != 0 {
335-
if target, err := os.Readlink(filepath.Join(tempDest, "bin", ent.Name())); err == nil && target == "cni" {
354+
if target, err := os.Readlink(filepath.Join(dir, "bin", ent.Name())); err == nil && target == "cni" {
336355
src := filepath.Join(cniPath, ent.Name())
337356
// Check if plugin already exists in stable CNI bin dir
338357
if info, err := os.Lstat(src); err == nil {
@@ -353,25 +372,6 @@ func extract(dataDir string) (string, error) {
353372
}
354373
}
355374

356-
// Rotate 'current' symlink into 'previous', and create a new 'current' that points
357-
// at the new directory.
358-
currentSymLink := filepath.Join(dataDir, "data", "current")
359-
previousSymLink := filepath.Join(dataDir, "data", "previous")
360-
if _, err := os.Lstat(currentSymLink); err == nil {
361-
if err := os.Rename(currentSymLink, previousSymLink); err != nil {
362-
return "", err
363-
}
364-
}
365-
if err := os.Symlink(dir, currentSymLink); err != nil {
366-
return "", err
367-
}
368-
369-
// Rename the new directory into place after updating symlinks, so that the k3s binary check at the start
370-
// of this function only succeeds if everything else has been completed successfully.
371-
if err := os.Rename(tempDest, dir); err != nil {
372-
return "", err
373-
}
374-
375375
return dir, nil
376376
}
377377

0 commit comments

Comments
 (0)