Skip to content

Commit

Permalink
instantiate a default cns config if loading fails (#1829)
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Baker <[email protected]>
  • Loading branch information
rbtr authored Sep 8, 2023
1 parent 554055b commit d57afe3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 26 deletions.
35 changes: 17 additions & 18 deletions cns/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"os"
"path/filepath"
"strings"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/logger"
Expand Down Expand Up @@ -88,22 +89,22 @@ type KeyVaultSettings struct {
RefreshIntervalInHrs int
}

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

// Check if env set for config path otherwise use default path
configpath, found := os.LookupEnv(EnvCNSConfig)
if !found {
dir, err := common.GetExecutableDirectory()
if err != nil {
return "", errors.Wrap(err, "failed to discover exec dir for config")
}
configpath = filepath.Join(dir, defaultConfigName)
// If config path is set from env, return that.
if envPath := os.Getenv(EnvCNSConfig); strings.TrimSpace(envPath) != "" {
return envPath, nil
}
// otherwise compose the default config path and return that.
dir, err := common.GetExecutableDirectory()
if err != nil {
return "", errors.Wrap(err, "failed to discover exec dir for config")
}
return configpath, nil
defaultPath := filepath.Join(dir, defaultConfigName)
return defaultPath, nil
}

// ReadConfig returns a CNS config from file or an error.
Expand All @@ -112,18 +113,16 @@ func ReadConfig(cmdLineConfigPath string) (*CNSConfig, error) {
if err != nil {
return nil, err
}

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

logger.Printf("[Configuration] Using config path: %s", configpath)
return readConfigFromFile(configpath)
}

// readConfigFromFile attempts to read the file and unmarshal it in to a CNSConfig.
func readConfigFromFile(f string) (*CNSConfig, error) {
content, err := os.ReadFile(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to read config file %s", f)
}

var config CNSConfig
if err := json.Unmarshal(content, &config); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal config")
Expand Down
18 changes: 11 additions & 7 deletions cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/fs"
"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -42,7 +43,7 @@ import (
"github.com/Azure/azure-container-networking/crd/clustersubnetstate/api/v1alpha1"
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig"
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
"github.com/Azure/azure-container-networking/fs"
acnfs "github.com/Azure/azure-container-networking/internal/fs"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/nmagent"
"github.com/Azure/azure-container-networking/platform"
Expand Down Expand Up @@ -500,25 +501,28 @@ func main() {
logger.Errorf("[Azure CNS] Cannot disable telemetry via cmdline. Update cns_config.json to disable telemetry.")
}

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)
if errors.Is(err, fs.ErrNotExist) {
logger.Warnf("config file does not exist, using default")
cnsconfig = &configuration.CNSConfig{}
} else {
logger.Errorf("fatal: failed to read cns config: %v", err)
os.Exit(1)
}
}

configuration.SetCNSConfigDefaults(cnsconfig)
logger.Printf("[Azure CNS] Read config :%+v", cnsconfig)
logger.Printf("[Azure CNS] Using config: %+v", cnsconfig)

_, envEnableConflistGeneration := os.LookupEnv(envVarEnableCNIConflistGeneration)

var conflistGenerator restserver.CNIConflistGenerator
if cnsconfig.EnableCNIConflistGeneration || envEnableConflistGeneration {
conflistFilepath := cnsconfig.CNIConflistFilepath
if cniConflistFilepathArg != "" {
// allow the filepath to get overidden by command line arg
conflistFilepath = cniConflistFilepathArg
}
writer, newWriterErr := fs.NewAtomicWriter(conflistFilepath)
writer, newWriterErr := acnfs.NewAtomicWriter(conflistFilepath)
if newWriterErr != nil {
logger.Errorf("unable to create atomic writer to generate cni conflist: %v", newWriterErr)
os.Exit(1)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion fs/atomic_test.go → internal/fs/atomic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

"github.com/Azure/azure-container-networking/fs"
"github.com/Azure/azure-container-networking/internal/fs"
"github.com/stretchr/testify/require"
)

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit d57afe3

Please sign in to comment.