From dfde26a1c97f9afd34a37b292da1939ad78834d5 Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Mon, 9 Jan 2023 13:00:15 +0200 Subject: [PATCH 1/2] cnitool: add extra checks for the existence of the NETCONFPATH directory. Signed-off-by: Nashwan Azhari --- cnitool/cnitool.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cnitool/cnitool.go b/cnitool/cnitool.go index 3ac2ba37..3cedacbf 100644 --- a/cnitool/cnitool.go +++ b/cnitool/cnitool.go @@ -68,6 +68,23 @@ func main() { if netdir == "" { netdir = DefaultNetDir } + + if !filepath.IsAbs(netdir) { + var err error + netdir, err = filepath.Abs(netdir) + if err != nil { + exit(fmt.Errorf("error converting the provided CNI config path ($%s) %q to an absolute path: %w", EnvNetDir, netdir, err)) + } + } + + if stat, err := os.Stat(netdir); err == nil { + if !stat.IsDir() { + exit(fmt.Errorf("the provided CNI config path ($%s) is not a directory: %q", EnvNetDir, netdir)) + } + } else { + exit(fmt.Errorf("the provided CNI config path ($%s) does not exist: %q", EnvNetDir, netdir)) + } + netconf, err := libcni.LoadNetworkConf(netdir, os.Args[2]) if err != nil { exit(err) From 2cd00e3eabed9babeefcc6ed874e1da92aca9591 Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Mon, 9 Jan 2023 12:59:43 +0200 Subject: [PATCH 2/2] cnitool: define default NETCONFPATH constant on Windows. Signed-off-by: Nashwan Azhari --- cnitool/cnitool.go | 2 -- cnitool/constants_unix.go | 22 ++++++++++++++++++++++ cnitool/constants_windows.go | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 cnitool/constants_unix.go create mode 100644 cnitool/constants_windows.go diff --git a/cnitool/cnitool.go b/cnitool/cnitool.go index 3cedacbf..53cdd727 100644 --- a/cnitool/cnitool.go +++ b/cnitool/cnitool.go @@ -34,8 +34,6 @@ const ( EnvCNIArgs = "CNI_ARGS" EnvCNIIfname = "CNI_IFNAME" - DefaultNetDir = "/etc/cni/net.d" - CmdAdd = "add" CmdCheck = "check" CmdDel = "del" diff --git a/cnitool/constants_unix.go b/cnitool/constants_unix.go new file mode 100644 index 00000000..dda0a500 --- /dev/null +++ b/cnitool/constants_unix.go @@ -0,0 +1,22 @@ +// Copyright 2023 CNI authors +// +// 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. + +//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris +// +build darwin dragonfly freebsd linux netbsd openbsd solaris + +package main + +const ( + DefaultNetDir = "/etc/cni/net.d" +) diff --git a/cnitool/constants_windows.go b/cnitool/constants_windows.go new file mode 100644 index 00000000..d14d3e14 --- /dev/null +++ b/cnitool/constants_windows.go @@ -0,0 +1,22 @@ +// Copyright 2023 CNI authors +// +// 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 main + +const ( + // NOTE: this is the most reasonable default as most CNI setups on Windows + // will have been made using the helper script in the containerd repo. + // https://github.com/containerd/containerd/blob/main/script/setup/install-cni-windows + DefaultNetDir = "C:\\Program Files\\containerd\\cni\\conf" +)