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
9 changes: 3 additions & 6 deletions cni/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ import (
"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/cni/api"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/platform"
semver "github.com/hashicorp/go-version"
utilexec "k8s.io/utils/exec"
)

const (
azureVnetExecutable = "/opt/cni/bin/azure-vnet"
)

type Client interface {
GetEndpointState() (*api.AzureCNIState, error)
}
Expand All @@ -32,7 +29,7 @@ func New(exec utilexec.Interface) *client {
}

func (c *client) GetEndpointState() (*api.AzureCNIState, error) {
cmd := c.exec.Command(azureVnetExecutable)
cmd := c.exec.Command(platform.CNIBinaryPath)

envs := os.Environ()
cmdenv := fmt.Sprintf("%s=%s", cni.Cmd, cni.CmdGetEndpointsState)
Expand All @@ -54,7 +51,7 @@ func (c *client) GetEndpointState() (*api.AzureCNIState, error) {
}

func (c *client) GetVersion() (*semver.Version, error) {
cmd := c.exec.Command(azureVnetExecutable, "-v")
cmd := c.exec.Command(platform.CNIBinaryPath, "-v")

output, err := cmd.CombinedOutput()
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cns/cnireconciler/statefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

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

// WriteObjectToCNIStatefile checks for a file at the CNI statefile path,
Expand All @@ -18,7 +19,7 @@ import (
// empty file on the host filesystem, crashing older CNI because it doesn't know
// how to handle empty statefiles.
func WriteObjectToCNIStatefile() error {
filename := "/var/run/azure-vnet.json"
filename := platform.CNIStateFilePath
return writeObjectToFile(filename)
}

Expand Down
11 changes: 8 additions & 3 deletions cns/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ type ManagedSettings struct {
NodeSyncIntervalInSeconds int
}

func getConfigFilePath() (string, error) {
func getConfigFilePath(cmdLineConfigPath string) (string, error) {
// If config path is set from cmd line, return that
if cmdLineConfigPath != "" {
return cmdLineConfigPath, nil
}

// Check if env set for config path otherwise use default path
configpath, found := os.LookupEnv(EnvCNSConfig)
if !found {
Expand All @@ -80,8 +85,8 @@ func getConfigFilePath() (string, error) {
}

// ReadConfig returns a CNS config from file or an error.
func ReadConfig() (*CNSConfig, error) {
configpath, err := getConfigFilePath()
func ReadConfig(cmdLineConfigPath string) (*CNSConfig, error) {
configpath, err := getConfigFilePath(cmdLineConfigPath)
if err != nil {
return nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions cns/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ func TestGetConfigFilePath(t *testing.T) {
execpath, _ := common.GetExecutableDirectory()

// env unset
f, err := getConfigFilePath()
f, err := getConfigFilePath("")
assert.NoError(t, err)
assert.Equal(t, filepath.Join(execpath, defaultConfigName), f)

// env set
os.Setenv(EnvCNSConfig, "test.cfg")
f, err = getConfigFilePath()
f, err = getConfigFilePath("")
assert.NoError(t, err)
assert.Equal(t, "test.cfg", f)

// test with cmdline config path
f, err = getConfigFilePath("/var/lib/cns_config.json")
assert.NoError(t, err)
assert.Equal(t, "/var/lib/cns_config.json", f)
}

func TestReadConfigFromFile(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions cns/restserver/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ func (service *HTTPRestService) restoreNetworkState() error {
return nil
}

if !service.store.Exists() {
logger.Printf("[Azure CNS] Store does not exist, nothing to restore for network state.")
return nil
}

rebooted := false
modTime, err := service.store.GetModificationTime()

Expand Down
11 changes: 10 additions & 1 deletion cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ var args = acn.ArgumentList{
Type: "string",
DefaultValue: "",
},
{
Name: acn.OptCNSConfigPath,
Shorthand: acn.OptCNSConfigPathAlias,
Description: "Path to cns config file",
Type: "string",
DefaultValue: "",
},
}

// init() is executed before main() whenever this package is imported
Expand Down Expand Up @@ -385,6 +392,7 @@ func main() {
nodeID := acn.GetArg(acn.OptNodeID).(string)
clientDebugCmd := acn.GetArg(acn.OptDebugCmd).(string)
clientDebugArg := acn.GetArg(acn.OptDebugArg).(string)
cmdLineConfigPath := acn.GetArg(acn.OptCNSConfigPath).(string)

if vers {
printVersion()
Expand Down Expand Up @@ -418,7 +426,8 @@ func main() {
logger.Errorf("[Azure CNS] Cannot disable telemetry via cmdline. Update cns_config.json to disable telemetry.")
}

cnsconfig, err := configuration.ReadConfig()
logger.Printf("[Azure CNS] cmdLineConfigPath: %s", cmdLineConfigPath)
cnsconfig, err := configuration.ReadConfig(cmdLineConfigPath)
if err != nil {
logger.Errorf("[Azure CNS] Error reading cns config: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,8 @@ const (
// Client mode, args for cmd
OptDebugArg = "debugarg"
OptDebugArgAlias = "darg"

// CNS config path
OptCNSConfigPath = "config-path"
OptCNSConfigPathAlias = "cp"
)
4 changes: 4 additions & 0 deletions platform/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const (
CNIRuntimePath = "/var/run/"
// CNILockPath is the path where CNI lock files are stored.
CNILockPath = "/var/run/azure-vnet/"
// CNIStateFilePath is the path to the CNI state file
CNIStateFilePath = "/var/run/azure-vnet.json"
// CNIBinaryPath is the path to the CNI binary
CNIBinaryPath = "/opt/cni/bin/azure-vnet"
// CNSRuntimePath is the path where CNS state files are stored.
CNSRuntimePath = "/var/run/"
// CNI runtime path on a Kubernetes cluster
Expand Down
6 changes: 6 additions & 0 deletions platform/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const (
// CNILockPath is the path where CNI lock files are stored.
CNILockPath = ""

// CNIStateFilePath is the path to the CNI state file
CNIStateFilePath = "C:\\k\\azure-vnet.json"

// CNIBinaryPath is the path to the CNI binary
CNIBinaryPath = "C:\\k\\azurecni\\bin\\azure-vnet.exe"

// CNI runtime path on a Kubernetes cluster
K8SCNIRuntimePath = "C:\\k\\azurecni\\bin"

Expand Down
7 changes: 7 additions & 0 deletions store/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ func NewJsonFileStore(fileName string, lockclient processlock.Interface) (KeyVal
return kvs, nil
}

func (kvs *jsonFileStore) Exists() bool {
if _, err := os.Stat(kvs.fileName); err != nil {
return false
}
return true
}

// Read restores the value for the given key from persistent store.
func (kvs *jsonFileStore) Read(key string, value interface{}) error {
kvs.Mutex.Lock()
Expand Down
4 changes: 4 additions & 0 deletions store/mockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func NewMockStore(lockFilePath string) KeyValueStore {
}
}

func (ms *mockStore) Exists() bool {
return false
}

// Read restores the value for the given key from persistent store.
func (ms *mockStore) Read(key string, value interface{}) error {
return nil
Expand Down
1 change: 1 addition & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

// KeyValueStore represents a persistent store of (key,value) pairs.
type KeyValueStore interface {
Exists() bool
Read(key string, value interface{}) error
Write(key string, value interface{}) error
Flush() error
Expand Down
5 changes: 5 additions & 0 deletions testutils/store_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var _ store.KeyValueStore = (*KeyValueStoreMock)(nil)

type KeyValueStoreMock struct {
ExistsBool bool
ReadError error
WriteError error
FlushError error
Expand All @@ -21,6 +22,10 @@ type KeyValueStoreMock struct {
GetModificationTimeError error
}

func (mockst *KeyValueStoreMock) Exists() bool {
return mockst.ExistsBool
}

func (mockst *KeyValueStoreMock) Read(key string, value interface{}) error {
return mockst.ReadError
}
Expand Down