Skip to content

Commit 8d074e9

Browse files
Fix the logic to get the AzureNetworkContainer binary path (#440)
AzureNetworkContainer.exe is in the same location as azure-cns.exe but if CNS is invoked from a process which is in a different working directory, os.stat called in CNS points to the directory of parent process. This change fixes the logic to point to the correct working directory.
1 parent 43d2c68 commit 8d074e9

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

cns/networkcontainers/networkcontainers_windows.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"net"
1010
"os"
1111
"os/exec"
12+
"path"
13+
"path/filepath"
1214
"strconv"
1315
"strings"
1416
"sync"
@@ -18,6 +20,10 @@ import (
1820
"github.com/containernetworking/cni/libcni"
1921
)
2022

23+
const (
24+
binaryAzureNetworkContainer = "AzureNetworkContainer.exe"
25+
)
26+
2127
var loopbackOperationLock = &sync.Mutex{}
2228

2329
func createOrUpdateInterface(createNetworkContainerRequest cns.CreateNetworkContainerRequest) error {
@@ -49,6 +55,11 @@ func updateInterface(createNetworkContainerRequest cns.CreateNetworkContainerReq
4955
}
5056

5157
func setWeakHostOnInterface(ipAddress, ncID string) error {
58+
acnBinaryPath, err := getAzureNetworkContainerBinaryPath()
59+
if err != nil {
60+
return err
61+
}
62+
5263
interfaces, err := net.Interfaces()
5364
if err != nil {
5465
log.Printf("[Azure CNS] Unable to retrieve interfaces on machine. %+v", err)
@@ -85,7 +96,7 @@ func setWeakHostOnInterface(ipAddress, ncID string) error {
8596

8697
ethIndexString := strconv.Itoa(targetIface.Index)
8798

88-
args := []string{"/C", "AzureNetworkContainer.exe", "/logpath", log.GetLogDirectory(),
99+
args := []string{"/C", acnBinaryPath, "/logpath", log.GetLogDirectory(),
89100
"/index",
90101
ethIndexString,
91102
"/operation",
@@ -118,8 +129,9 @@ func createOrUpdateWithOperation(
118129
setWeakHost bool,
119130
primaryInterfaceIdentifier string,
120131
operation string) error {
121-
if _, err := os.Stat("./AzureNetworkContainer.exe"); err != nil {
122-
return fmt.Errorf("[Azure CNS] Unable to find AzureNetworkContainer.exe. Cannot continue")
132+
acnBinaryPath, err := getAzureNetworkContainerBinaryPath()
133+
if err != nil {
134+
return err
123135
}
124136

125137
if ipConfig.IPSubnet.IPAddress == "" {
@@ -134,7 +146,7 @@ func createOrUpdateWithOperation(
134146
ipv4NetStr := fmt.Sprintf("%d.%d.%d.%d", ipv4NetInt[0], ipv4NetInt[1], ipv4NetInt[2], ipv4NetInt[3])
135147
log.Printf("[Azure CNS] Created netmask in string format %v", ipv4NetStr)
136148

137-
args := []string{"/C", "AzureNetworkContainer.exe", "/logpath", log.GetLogDirectory(),
149+
args := []string{"/C", acnBinaryPath, "/logpath", log.GetLogDirectory(),
138150
"/name",
139151
adapterName,
140152
"/operation",
@@ -172,15 +184,16 @@ func createOrUpdateWithOperation(
172184
}
173185

174186
func deleteInterface(interfaceName string) error {
175-
if _, err := os.Stat("./AzureNetworkContainer.exe"); err != nil {
176-
return fmt.Errorf("[Azure CNS] Unable to find AzureNetworkContainer.exe. Cannot continue")
187+
acnBinaryPath, err := getAzureNetworkContainerBinaryPath()
188+
if err != nil {
189+
return err
177190
}
178191

179192
if interfaceName == "" {
180193
return fmt.Errorf("[Azure CNS] Interface name is nil")
181194
}
182195

183-
args := []string{"/C", "AzureNetworkContainer.exe", "/logpath", log.GetLogDirectory(),
196+
args := []string{"/C", acnBinaryPath, "/logpath", log.GetLogDirectory(),
184197
"/name",
185198
interfaceName,
186199
"/operation",
@@ -228,3 +241,25 @@ func configureNetworkContainerNetworking(operation, podName, podNamespace, docke
228241

229242
return err
230243
}
244+
245+
func getAzureNetworkContainerBinaryPath() (string, error) {
246+
var (
247+
binaryPath string
248+
workingDir string
249+
err error
250+
)
251+
252+
if workingDir, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
253+
return binaryPath,
254+
fmt.Errorf("[Azure CNS] Unable to find working directory. Error: %v. Cannot continue", err)
255+
}
256+
257+
binaryPath = path.Join(workingDir, binaryAzureNetworkContainer)
258+
259+
if _, err = os.Stat(binaryPath); err != nil {
260+
return binaryPath,
261+
fmt.Errorf("[Azure CNS] Unable to find AzureNetworkContainer.exe. Error: %v. Cannot continue", err)
262+
}
263+
264+
return binaryPath, nil
265+
}

0 commit comments

Comments
 (0)