Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions cns/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"

"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/common"
)

const (
Expand Down Expand Up @@ -46,17 +47,16 @@ func ReadConfig() (CNSConfig, error) {
// Check if env set for config path otherwise use default path
configpath, found := os.LookupEnv("CNS_CONFIGURATION_PATH")
if !found {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
dir, err := common.GetExecutableDirectory()
if err != nil {
logger.Errorf("[Configuration] Failed to find exe dir:%v", err)
return cnsConfig, err
}

configpath = filepath.Join(dir, defaultConfigName)
//dir + string(os.PathSeparator) + defaultConfigName
}

logger.Printf("Config path:%s", configpath)
logger.Printf("[Configuration] Config path:%s", configpath)

content, err := ioutil.ReadFile(configpath)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions cns/networkcontainers/networkcontainers_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"net"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"github.com/containernetworking/cni/libcni"
)
Expand Down Expand Up @@ -250,12 +250,13 @@ func getAzureNetworkContainerBinaryPath() (string, error) {
err error
)

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

binaryPath = path.Join(workingDir, binaryAzureNetworkContainer)
binaryPath = filepath.Join(workingDir, binaryAzureNetworkContainer)

if _, err = os.Stat(binaryPath); err != nil {
return binaryPath,
Expand Down
24 changes: 24 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -323,3 +324,26 @@ func GetAzureCloud(url string) (string, error) {

return strings.TrimSpace(string(bodyBytes)), nil
}

func GetExecutableDirectory() (string, error) {
var (
dir string
ex string
err error
)
ex, err = os.Executable()
if err == nil {
dir = filepath.Dir(ex)
} else {
var exReal string
// If a symlink was used to start the process, depending on the operating system,
// the result might be the symlink or the path it pointed to.
// filepath.EvalSymlinks returns stable results
exReal, err = filepath.EvalSymlinks(ex)
if err == nil {
dir = filepath.Dir(exReal)
}
}

return dir, err
}