Skip to content

Commit ed14f7f

Browse files
committed
Use static CNI bin dir
Maintain a separate dir for CNI binaries so that additional plugins can be installed in a predictable location that does not change every time k3s is upgraded. Signed-off-by: Brad Davidson <[email protected]>
1 parent ea5add3 commit ed14f7f

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

cmd/k3s/main.go

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"io"
7+
"io/fs"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -187,13 +188,24 @@ func stageAndRun(dataDir, cmd string, args []string, calledAsInternal bool) erro
187188
}
188189
logrus.Debugf("Asset dir %s", dir)
189190

190-
var pathEnv string
191+
pathList := []string{
192+
filepath.Clean(filepath.Join(dir, "..", "cni")),
193+
filepath.Join(dir, "bin"),
194+
}
191195
if findPreferBundledBin(args) {
192-
pathEnv = filepath.Join(dir, "bin") + string(os.PathListSeparator) + filepath.Join(dir, "bin/aux") + string(os.PathListSeparator) + os.Getenv("PATH")
196+
pathList = append(
197+
pathList,
198+
filepath.Join(dir, "bin", "aux"),
199+
os.Getenv("PATH"),
200+
)
193201
} else {
194-
pathEnv = filepath.Join(dir, "bin") + string(os.PathListSeparator) + os.Getenv("PATH") + string(os.PathListSeparator) + filepath.Join(dir, "bin/aux")
202+
pathList = append(
203+
pathList,
204+
os.Getenv("PATH"),
205+
filepath.Join(dir, "bin", "aux"),
206+
)
195207
}
196-
if err := os.Setenv("PATH", pathEnv); err != nil {
208+
if err := os.Setenv("PATH", strings.Join(pathList, string(os.PathListSeparator))); err != nil {
197209
return err
198210
}
199211

@@ -268,6 +280,39 @@ func extract(dataDir string) (string, error) {
268280
return "", err
269281
}
270282

283+
// Rename the new directory into place, before updating symlinks
284+
if err := os.Rename(tempDest, dir); err != nil {
285+
return "", err
286+
}
287+
288+
// Create a stable CNI bin dir and place it first in the path so that users have a
289+
// consistent location to drop their own CNI plugin binaries.
290+
cniPath := filepath.Join(dataDir, "data", "cni")
291+
cniBin := filepath.Join(dir, "bin", "cni")
292+
if err := os.MkdirAll(cniPath, 0755); err != nil {
293+
return "", err
294+
}
295+
if err := os.Symlink(cniBin, filepath.Join(cniPath, "cni")); err != nil {
296+
return "", err
297+
}
298+
299+
// Find symlinks that point to the cni multicall binary, and clone them in the stable CNI bin dir.
300+
ents, err := os.ReadDir(filepath.Join(dir, "bin"))
301+
if err != nil {
302+
return "", err
303+
}
304+
for _, ent := range ents {
305+
if info, err := ent.Info(); err == nil && info.Mode()&fs.ModeSymlink != 0 {
306+
if target, err := os.Readlink(filepath.Join(dir, "bin", ent.Name())); err == nil && target == "cni" {
307+
if err := os.Symlink(cniBin, filepath.Join(cniPath, ent.Name())); err != nil {
308+
return "", err
309+
}
310+
}
311+
}
312+
}
313+
314+
// Rotate 'current' symlink into 'previous', and create a new 'current' that points
315+
// at the new directory.
271316
currentSymLink := filepath.Join(dataDir, "data", "current")
272317
previousSymLink := filepath.Join(dataDir, "data", "previous")
273318
if _, err := os.Lstat(currentSymLink); err == nil {
@@ -278,7 +323,8 @@ func extract(dataDir string) (string, error) {
278323
if err := os.Symlink(dir, currentSymLink); err != nil {
279324
return "", err
280325
}
281-
return dir, os.Rename(tempDest, dir)
326+
327+
return dir, nil
282328
}
283329

284330
// findCriConfig returns the path to crictl.yaml

0 commit comments

Comments
 (0)