Skip to content

Commit

Permalink
Add overlay configuration via config file
Browse files Browse the repository at this point in the history
This allows to provide per node default
configuraiton for nv-ipam cni plugin

Signed-off-by: adrianc <adrianc@nvidia.com>
  • Loading branch information
adrianchiris committed May 3, 2023
1 parent 94c439d commit 5dc9a3a
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions pkg/cni/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@ import (
)

const (
// DefaultConfDir is the default dir where configurations are found
DefaultConfDir = "/etc/cni/net.d/nv-ipam.d"
// DefaultDataDir is the default dir where cni stores data and binaries
DefaultDataDir = "/var/lib/cni/nv-ipam"
// DefaultLogFile default log file path to be used for logging
DefaultLogFile = "/var/log/nv-ipam-cni.log"

HostLocalDataDir = "state/host-local"
K8sNodeNameFile = "k8s-node-name"
// HostLocalDataDir is the relative path within the data dir for host-local state data
HostLocalDataDir = "state/host-local"
// K8sNodeNameFile is the file name containing k8s node name
K8sNodeNameFile = "k8s-node-name"
// DefaultKubeConfigFileName is the default name of kubeconfig file
DefaultKubeConfigFileName = "nv-ipam.kubeconfig"
// ConfFileName is the name of CNI configuration file found in conf dir
ConfFileName = "nv-ipam.conf"
)

// IPAMConf is the configuration supported by our CNI plugin
type IPAMConf struct {
types.IPAM

Expand All @@ -52,12 +61,14 @@ type IPAMConf struct {
K8sClient *kubernetes.Clientset
}

// NetConf is CNI network config
type NetConf struct {
Name string `json:"name"`
CNIVersion string `json:"cniVersion"`
IPAM *IPAMConf `json:"ipam"`
}

// Load NetConf from json string provided as []byte
func LoadConf(bytes []byte) (*NetConf, error) {
n := &NetConf{}

Expand All @@ -73,6 +84,10 @@ func LoadConf(bytes []byte) (*NetConf, error) {
n.IPAM.ConfDir = DefaultConfDir
}

// NOTE: ignore errors, we treat this as best effort. we might fail here if file does not exist or
// its malformed. in both cases we can ignore.
_ = LoadFromConfFile(n.IPAM, filepath.Join(n.IPAM.ConfDir, ConfFileName))

if n.IPAM.DataDir == "" {
n.IPAM.DataDir = DefaultDataDir
}
Expand Down Expand Up @@ -106,3 +121,37 @@ func LoadConf(bytes []byte) (*NetConf, error) {

return n, nil
}

// LoadFromConfFile updates IPAMConf with values from config file located in filePath
// for empty entries.
// if error occurs no updates will occur and error will be returned
func LoadFromConfFile(conf *IPAMConf, filePath string) error {
data, err := os.ReadFile(filePath)
if err != nil {
return err
}

confFromFile := IPAMConf{}
err = json.Unmarshal(data, &confFromFile)
if err != nil {
return err
}

if confFromFile.DataDir != "" {
conf.DataDir = confFromFile.DataDir
}

if confFromFile.Kubeconfig != "" {
conf.Kubeconfig = confFromFile.Kubeconfig
}

if confFromFile.LogFile != "" {
conf.LogFile = confFromFile.LogFile
}

if confFromFile.LogLevel != "" {
conf.LogLevel = confFromFile.LogLevel
}

return nil
}

0 comments on commit 5dc9a3a

Please sign in to comment.