Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions cns/networkcontainers/networkcontainers_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
Expand All @@ -18,6 +20,10 @@ import (
"github.com/containernetworking/cni/libcni"
)

const (
binaryAzureNetworkContainer = "AzureNetworkContainer.exe"
)

var loopbackOperationLock = &sync.Mutex{}

func createOrUpdateInterface(createNetworkContainerRequest cns.CreateNetworkContainerRequest) error {
Expand Down Expand Up @@ -49,6 +55,11 @@ func updateInterface(createNetworkContainerRequest cns.CreateNetworkContainerReq
}

func setWeakHostOnInterface(ipAddress, ncID string) error {
acnBinaryPath, err := getAzureNetworkContainerBinaryPath()
if err != nil {
return err
}

interfaces, err := net.Interfaces()
if err != nil {
log.Printf("[Azure CNS] Unable to retrieve interfaces on machine. %+v", err)
Expand Down Expand Up @@ -85,7 +96,7 @@ func setWeakHostOnInterface(ipAddress, ncID string) error {

ethIndexString := strconv.Itoa(targetIface.Index)

args := []string{"/C", "AzureNetworkContainer.exe", "/logpath", log.GetLogDirectory(),
args := []string{"/C", acnBinaryPath, "/logpath", log.GetLogDirectory(),
"/index",
ethIndexString,
"/operation",
Expand Down Expand Up @@ -118,8 +129,9 @@ func createOrUpdateWithOperation(
setWeakHost bool,
primaryInterfaceIdentifier string,
operation string) error {
if _, err := os.Stat("./AzureNetworkContainer.exe"); err != nil {
return fmt.Errorf("[Azure CNS] Unable to find AzureNetworkContainer.exe. Cannot continue")
acnBinaryPath, err := getAzureNetworkContainerBinaryPath()
if err != nil {
return err
}

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

args := []string{"/C", "AzureNetworkContainer.exe", "/logpath", log.GetLogDirectory(),
args := []string{"/C", acnBinaryPath, "/logpath", log.GetLogDirectory(),
"/name",
adapterName,
"/operation",
Expand Down Expand Up @@ -172,15 +184,16 @@ func createOrUpdateWithOperation(
}

func deleteInterface(interfaceName string) error {
if _, err := os.Stat("./AzureNetworkContainer.exe"); err != nil {
return fmt.Errorf("[Azure CNS] Unable to find AzureNetworkContainer.exe. Cannot continue")
acnBinaryPath, err := getAzureNetworkContainerBinaryPath()
if err != nil {
return err
}

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

args := []string{"/C", "AzureNetworkContainer.exe", "/logpath", log.GetLogDirectory(),
args := []string{"/C", acnBinaryPath, "/logpath", log.GetLogDirectory(),
"/name",
interfaceName,
"/operation",
Expand Down Expand Up @@ -228,3 +241,25 @@ func configureNetworkContainerNetworking(operation, podName, podNamespace, docke

return err
}

func getAzureNetworkContainerBinaryPath() (string, error) {
var (
binaryPath string
workingDir string
err error
)

if workingDir, err = filepath.Abs(filepath.Dir(os.Args[0])); err != nil {
return binaryPath,
fmt.Errorf("[Azure CNS] Unable to find working directory. Error: %v. Cannot continue", err)
}

binaryPath = path.Join(workingDir, binaryAzureNetworkContainer)

if _, err = os.Stat(binaryPath); err != nil {
return binaryPath,
fmt.Errorf("[Azure CNS] Unable to find AzureNetworkContainer.exe. Error: %v. Cannot continue", err)
}

return binaryPath, nil
}