forked from Azure/azure-container-networking
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetconfig.go
101 lines (84 loc) · 2.61 KB
/
netconfig.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package cni
import (
"encoding/json"
"strings"
"github.com/Azure/azure-container-networking/network/policy"
cniTypes "github.com/containernetworking/cni/pkg/types"
)
const (
PolicyStr string = "Policy"
)
// KVPair represents a K-V pair of a json object.
type KVPair struct {
Name string `json:"name"`
Value json.RawMessage `json:"value"`
}
// NetworkConfig represents Azure CNI plugin network configuration.
type NetworkConfig struct {
CNIVersion string `json:"cniVersion"`
Name string `json:"name"`
Type string `json:"type"`
Mode string `json:"mode"`
Master string `json:"master"`
Bridge string `json:"bridge,omitempty"`
LogLevel string `json:"logLevel,omitempty"`
LogTarget string `json:"logTarget,omitempty"`
Ipam struct {
Type string `json:"type"`
Environment string `json:"environment,omitempty"`
AddrSpace string `json:"addressSpace,omitempty"`
Subnet string `json:"subnet,omitempty"`
Address string `json:"ipAddress,omitempty"`
QueryInterval string `json:"queryInterval,omitempty"`
}
DNS cniTypes.DNS `json:"dns"`
AdditionalArgs []KVPair
}
type K8SPodEnvArgs struct {
cniTypes.CommonArgs
K8S_POD_NAMESPACE cniTypes.UnmarshallableString `json:"K8S_POD_NAMESPACE,omitempty"`
K8S_POD_NAME cniTypes.UnmarshallableString `json:"K8S_POD_NAME,omitempty"`
K8S_POD_INFRA_CONTAINER_ID cniTypes.UnmarshallableString `json:"K8S_POD_INFRA_CONTAINER_ID,omitempty"`
}
// ParseCniArgs unmarshals cni arguments.
func ParseCniArgs(args string) (*K8SPodEnvArgs, error) {
podCfg := K8SPodEnvArgs{}
err := cniTypes.LoadArgs(args, &podCfg)
if err != nil {
return nil, err
}
return &podCfg, nil
}
// ParseNetworkConfig unmarshals network configuration from bytes.
func ParseNetworkConfig(b []byte) (*NetworkConfig, error) {
nwCfg := NetworkConfig{}
err := json.Unmarshal(b, &nwCfg)
if err != nil {
return nil, err
}
if nwCfg.CNIVersion == "" {
nwCfg.CNIVersion = defaultVersion
}
return &nwCfg, nil
}
// GetPoliciesFromNwCfg returns network policies from network config.
func GetPoliciesFromNwCfg(kvp []KVPair) []policy.Policy {
var policies []policy.Policy
for _, pair := range kvp {
if strings.Contains(pair.Name, PolicyStr) {
policy := policy.Policy{
Type: policy.CNIPolicyType(pair.Name),
Data: pair.Value,
}
policies = append(policies, policy)
}
}
return policies
}
// Serialize marshals a network configuration to bytes.
func (nwcfg *NetworkConfig) Serialize() []byte {
bytes, _ := json.Marshal(nwcfg)
return bytes
}