forked from rancher/rancher-cni-ipam
/
config.go
44 lines (36 loc) · 1.21 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"encoding/json"
"fmt"
"github.com/containernetworking/cni/pkg/types"
)
// IPAMConfig is used to load the options specified in the configuration file
type IPAMConfig struct {
types.CommonArgs
Type string `json:"type"`
LogToFile string `json:"logToFile"`
IsDebugLevel string `json:"isDebugLevel"`
SubnetPrefixSize string `json:"subnetPrefixSize"`
Routes []types.Route `json:"routes"`
RancherContainerUUID types.UnmarshallableString
IPAddress types.UnmarshallableString
}
// Net loads the options of the CNI network configuration file
type Net struct {
Name string `json:"name"`
IPAM *IPAMConfig `json:"ipam"`
}
// LoadIPAMConfig loads the IPAM configuration from the given bytes
func LoadIPAMConfig(bytes []byte, args string) (*IPAMConfig, error) {
n := Net{}
if err := json.Unmarshal(bytes, &n); err != nil {
return nil, fmt.Errorf("failed to load netconf: %v", err)
}
if n.IPAM == nil {
return nil, fmt.Errorf("IPAM config missing 'ipam' key")
}
if err := types.LoadArgs(args, n.IPAM); err != nil {
return nil, fmt.Errorf("failed to parse args %s: %v", args, err)
}
return n.IPAM, nil
}