-
Notifications
You must be signed in to change notification settings - Fork 241
/
crdtranslator.go
78 lines (67 loc) · 2.87 KB
/
crdtranslator.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
package kubecontroller
import (
"fmt"
"net"
"strconv"
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/logger"
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
)
// CRDStatusToNCRequest translates a crd status to createnetworkcontainer request
func CRDStatusToNCRequest(crdStatus v1alpha.NodeNetworkConfigStatus) (cns.CreateNetworkContainerRequest, error) {
var (
ncRequest cns.CreateNetworkContainerRequest
nc v1alpha.NetworkContainer
secondaryIPConfig cns.SecondaryIPConfig
ipSubnet cns.IPSubnet
ipAssignment v1alpha.IPAssignment
err error
ip net.IP
ipNet *net.IPNet
size int
numNCsSupported int
numNCs int
)
numNCsSupported = 1
numNCs = len(crdStatus.NetworkContainers)
// Right now we're only supporing one NC per node, but in the future we will support multiple NCs per node
if numNCs > numNCsSupported {
return ncRequest, fmt.Errorf("Number of network containers is not supported. Got %v number of ncs, supports %v", numNCs, numNCsSupported)
}
for _, nc = range crdStatus.NetworkContainers {
ncRequest.SecondaryIPConfigs = make(map[string]cns.SecondaryIPConfig)
ncRequest.NetworkContainerid = nc.ID
ncRequest.NetworkContainerType = cns.Docker
ncRequest.Version = strconv.FormatInt(nc.Version, 10)
if ip = net.ParseIP(nc.PrimaryIP); ip == nil {
return ncRequest, fmt.Errorf("Invalid PrimaryIP %s:", nc.PrimaryIP)
}
if _, ipNet, err = net.ParseCIDR(nc.SubnetAddressSpace); err != nil {
return ncRequest, fmt.Errorf("Invalid SubnetAddressSpace %s:, err:%s", nc.SubnetAddressSpace, err)
}
size, _ = ipNet.Mask.Size()
ipSubnet.IPAddress = ip.String()
ipSubnet.PrefixLength = uint8(size)
ncRequest.IPConfiguration.IPSubnet = ipSubnet
ncRequest.IPConfiguration.GatewayIPAddress = nc.DefaultGateway
var ncVersion int
if ncVersion, err = strconv.Atoi(ncRequest.Version); err != nil {
return ncRequest, fmt.Errorf("Invalid ncRequest.Version is %s in CRD, err:%s", ncRequest.Version, err)
}
for _, ipAssignment = range nc.IPAssignments {
if ip = net.ParseIP(ipAssignment.IP); ip == nil {
return ncRequest, fmt.Errorf("Invalid SecondaryIP %s:", ipAssignment.IP)
}
secondaryIPConfig = cns.SecondaryIPConfig{
IPAddress: ip.String(),
NCVersion: ncVersion,
}
ncRequest.SecondaryIPConfigs[ipAssignment.Name] = secondaryIPConfig
logger.Debugf("Seconday IP Configs got set, name is %s, config is %v", ipAssignment.Name, secondaryIPConfig)
}
logger.Printf("Set NC request info with NetworkContainerid %s, NetworkContainerType %s, NC Version %s",
ncRequest.NetworkContainerid, ncRequest.NetworkContainerType, ncRequest.Version)
}
// Only returning the first network container for now, later we will return a list
return ncRequest, nil
}