/
cni.go
286 lines (244 loc) · 7.81 KB
/
cni.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright Microsoft Corp.
// All rights reserved.
package cni
import (
"encoding/json"
"fmt"
"net"
"strings"
cniSkel "github.com/containernetworking/cni/pkg/skel"
cniTypes "github.com/containernetworking/cni/pkg/types"
cniTypes020 "github.com/containernetworking/cni/pkg/types/020"
"github.com/sirupsen/logrus"
network "visualstudio.com/containernetworking/cni/network"
)
const (
// Supported CNI versions.
Version = "0.2.0"
// CNI commands.
CmdAdd = "ADD"
CmdDel = "DEL"
Internal = "internal"
)
type KVP struct {
Name string `json:"name"`
Value json.RawMessage `json:"value"`
}
type PortMapping struct {
HostPort int `json:"hostPort"`
ContainerPort int `json:"containerPort"`
Protocol string `json:"protocol"`
HostIp string `json:"hostIP,omitempty"`
}
type RuntimeConfig struct {
PortMappings []PortMapping `json:"portMappings,omitempty"`
}
// NetworkConfig represents the Windows CNI plugin's network configuration.
// Defined as per https://github.com/containernetworking/cni/blob/master/SPEC.md
type NetworkConfig struct {
CniVersion string `json:"cniVersion"`
Name string `json:"name"` // Name is the Network Name. We would also use this as the Type of HNS Network
Type string `json:"type"` // As per SPEC, Type is Name of the Binary
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"`
Routes []cniTypes.Route `json:"routes,omitempty"`
}
DNS cniTypes.DNS `json:"dns"`
RuntimeConfig RuntimeConfig `json:"runtimeConfig"`
AdditionalArgs []KVP
}
type Interface struct {
Name string `json:"name"`
MacAddress net.HardwareAddr `json:"mac"`
Sandbox string `json:"sandbox"`
}
type IP struct {
Version string `json:"version"` // 4 or 6
Address cniTypes.IPNet `json:"address"`
Gateway net.IP `json:"gateway"`
InterfaceIndex int `json:"interface"` // Numeric index into 'interfaces' list
}
type Result struct {
CniVersion string `json:"cniVersion"`
Interfaces []Interface `json:"interfaces"`
IP []IP `json:"ip"`
DNS cniTypes.DNS `json:"dns"`
Routes []cniTypes.Route `json:"routes,omitempty"`
}
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"`
}
func (r *Result) Print() {
fmt.Printf(r.String())
}
func (r *Result) String() string {
json, _ := json.Marshal(r)
return string(json)
}
// CNI contract.
type PluginApi interface {
Add(args *cniSkel.CmdArgs) error
Delete(args *cniSkel.CmdArgs) error
}
// CallPlugin calls the given CNI plugin through the internal interface.
func CallPlugin(plugin PluginApi, cmd string, args *cniSkel.CmdArgs, config *NetworkConfig) (*cniTypes.Result, error) {
var err error
savedType := config.Ipam.Type
config.Ipam.Type = Internal
args.StdinData = config.Serialize()
// Call the plugin's internal interface.
if cmd == CmdAdd {
err = plugin.Add(args)
} else {
err = plugin.Delete(args)
}
config.Ipam.Type = savedType
if err != nil {
return nil, err
}
// Read back the result.
var result cniTypes.Result
err = json.Unmarshal(args.StdinData, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// ParseNetworkConfig unmarshals network configuration from bytes.
func ParseNetworkConfig(b []byte) (*NetworkConfig, error) {
config := NetworkConfig{}
err := json.Unmarshal(b, &config)
if err != nil {
return nil, err
}
return &config, nil
}
// ParseCniArgs
func ParseCniArgs(args string) (*K8SPodEnvArgs, error) {
podConfig := K8SPodEnvArgs{}
err := cniTypes.LoadArgs(args, &podConfig)
if err != nil {
return nil, err
}
return &podConfig, nil
}
// Serialize marshals a network configuration to bytes.
func (config *NetworkConfig) Serialize() []byte {
bytes, _ := json.Marshal(config)
return bytes
}
// Get NetworkInfo from the NetworkConfig
func (config *NetworkConfig) GetNetworkInfo() *network.NetworkInfo {
var subnet = network.SubnetInfo{}
if config.Ipam.Subnet != "" {
ip, s, _ := net.ParseCIDR(config.Ipam.Subnet)
gatewayIP := ip.To4()
gatewayIP[3]++
if config.Ipam.Routes != nil && len(config.Ipam.Routes) > 0 && config.Ipam.Routes[0].GW != nil {
gatewayIP = config.Ipam.Routes[0].GW
}
subnet = network.SubnetInfo{
AddressPrefix: *s,
GatewayAddress: gatewayIP,
}
}
ninfo := &network.NetworkInfo{
ID: config.Name,
Name: config.Name,
Type: network.NetworkType(config.Name),
Subnets: []network.SubnetInfo{subnet},
InterfaceName: "",
DNS: network.DNSInfo{
Servers: config.DNS.Nameservers,
Suffix: strings.Join(config.DNS.Search, ","),
},
}
if config.AdditionalArgs != nil {
for _, kvp := range config.AdditionalArgs {
if strings.Contains(kvp.Name, "Policy") {
npolicy := network.Policy{Type: network.CNIPolicyType(kvp.Name), Data: kvp.Value}
ninfo.Policies = append(ninfo.Policies, npolicy)
}
}
}
return ninfo
}
// GetEndpointInfo constructs endpoint info using endpoint id, containerid and netns
func (config *NetworkConfig) GetEndpointInfo(
networkinfo *network.NetworkInfo,
containerID string, netNs string, podK8sNamespace string) *network.EndpointInfo {
containerIDToUse := containerID
if netNs != "" {
splits := strings.Split(netNs, ":")
if len(splits) == 2 {
containerIDToUse = splits[1]
}
}
epInfo := &network.EndpointInfo{
Name: containerIDToUse + "_" + networkinfo.ID,
NetworkID: networkinfo.ID,
ContainerID: containerID,
}
epInfo.DNS = network.DNSInfo{
Servers: networkinfo.DNS.Servers,
Suffix: podK8sNamespace + "." + networkinfo.DNS.Suffix,
}
epInfo.Subnet = networkinfo.Subnets[0].AddressPrefix
epInfo.Gateway = networkinfo.Subnets[0].GatewayAddress
runtimeConf := config.RuntimeConfig
logrus.Debugf("Parsing port mappings from %+v", runtimeConf.PortMappings)
for _, mapping := range runtimeConf.PortMappings {
natPolicy := network.GetHNSNatPolicy(mapping.HostPort, mapping.ContainerPort, mapping.Protocol)
logrus.Debugf("Created raw policy from mapping: %+v --- %+v", mapping, natPolicy)
epInfo.Policies = append(epInfo.Policies, natPolicy)
}
return epInfo
}
// GetResult gets the result object
func GetResult(network *network.NetworkInfo, endpoint *network.EndpointInfo) Result {
var iFace = GetInterface(endpoint)
var ip = GetIP(network, endpoint)
return Result{
CniVersion: Version,
Interfaces: []Interface{iFace},
IP: []IP{ip},
}
}
// GetResult020 gets the v020 result object
func GetResult020(network *network.NetworkInfo, endpoint *network.EndpointInfo) cniTypes020.Result {
var ip = GetIP(network, endpoint)
var ip4 = &cniTypes020.IPConfig{
IP: net.IPNet(ip.Address),
Gateway: ip.Gateway,
}
return cniTypes020.Result{
IP4: ip4,
}
}
// GetIP returns the IP for the corresponding endpoint
func GetIP(network *network.NetworkInfo, endpoint *network.EndpointInfo) IP {
address := network.Subnets[0].AddressPrefix
address.IP = endpoint.IPAddress
return IP{
Version: "4",
Address: cniTypes.IPNet(address),
Gateway: endpoint.Gateway,
InterfaceIndex: 0,
}
}
// GetInterface returns the interface for endpoint
func GetInterface(endpoint *network.EndpointInfo) Interface {
return Interface{
Name: endpoint.Name,
MacAddress: endpoint.MacAddress,
Sandbox: "",
}
}