Skip to content

Commit 1181792

Browse files
authored
Map CNI config 'type' to correct HCN NetworkType constant. (#99)
* CNI: Map CNI config 'type' to HCN Network Type. This patch switches the internal structures from internal constants into the string network type constants in the `hcsshim/hcn`, as well as adding a mechanism for automatically mapping the CNI config 'type' value to said constants before making any HCN requests. Signed-off-by: Nashwan Azhari <nazhari@cloudbasesolutions.com> * Testing: map binary names to HCN types. Signed-off-by: Nashwan Azhari <nazhari@cloudbasesolutions.com> --------- Signed-off-by: Nashwan Azhari <nazhari@cloudbasesolutions.com>
1 parent a0463d8 commit 1181792

File tree

7 files changed

+113
-42
lines changed

7 files changed

+113
-42
lines changed

cni/cni.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ const (
3030
// Supported CNI versions.
3131
var VersionsSupported = []string{"0.2.0", "0.3.0", "0.4.0", "1.0.0"}
3232

33+
// Aliases for the Windows CNI plugin binary names:
34+
const (
35+
NatPluginName = "nat"
36+
SdnBridgePluginName = "sdnbridge"
37+
SdnOverlayPluginName = "sdnoverlay"
38+
// SdnTunnelPluginName = "sdntunnel"
39+
)
40+
41+
// NOTE(aznashwan): this mapping between the names of binary releases of the WinCNI
42+
// plugins and the underlying HCN network types is required for
43+
// backwards-compatibility with callers which rely on the old plugin naming schemes.
44+
// See: https://github.com/microsoft/windows-container-networking/issues/57
45+
var CniPluginNameToHcnTypeMap map[string]hcn.NetworkType = map[string]hcn.NetworkType{
46+
NatPluginName: hcn.NAT,
47+
SdnBridgePluginName: hcn.L2Bridge,
48+
SdnOverlayPluginName: hcn.Overlay,
49+
// SdnTunnelPluginName: network.L2Tunnel,
50+
}
51+
3352
type KVP struct {
3453
Name string `json:"name"`
3554
Value json.RawMessage `json:"value"`
@@ -67,8 +86,8 @@ type IpamConfig struct {
6786
// Defined as per https://github.com/containernetworking/cni/blob/master/SPEC.md
6887
type NetworkConfig struct {
6988
CniVersion string `json:"cniVersion"`
70-
Name string `json:"name"` // Name is the Network Name. We would also use this as the Type of HNS Network
71-
Type string `json:"type"` // As per SPEC, Type is Name of the Binary
89+
Name string `json:"name"` // Name is the Network Name.
90+
Type hcn.NetworkType `json:"type"` // As per SPEC, Type is Name of the Binary
7291
Ipam IpamConfig `json:"ipam"`
7392
DNS cniTypes.DNS `json:"dns"`
7493
OptionalFlags OptionalFlags `json:"optionalFlags"`
@@ -180,6 +199,12 @@ func ParseNetworkConfig(b []byte) (*NetworkConfig, error) {
180199
return nil, err
181200
}
182201

202+
hcnType, err := MapCniTypeToHcnType(config.Type)
203+
if err != nil {
204+
return nil, err
205+
}
206+
config.Type = hcnType
207+
183208
return &config, nil
184209
}
185210

@@ -252,7 +277,7 @@ func (config *NetworkConfig) GetNetworkInfo(podNamespace string) (ninfo *network
252277
ninfo = &network.NetworkInfo{
253278
ID: config.Name,
254279
Name: config.Name,
255-
Type: network.NetworkType(config.Type),
280+
Type: config.Type,
256281
Subnets: subnets,
257282
InterfaceName: "",
258283
DNS: dnsSettings,
@@ -494,3 +519,23 @@ func GetInterface(endpoint *network.EndpointInfo) Interface {
494519
Sandbox: "",
495520
}
496521
}
522+
523+
// Maps the given CNI network config "type" field into the correct HNS network type.
524+
func MapCniTypeToHcnType(typeArg hcn.NetworkType) (hcn.NetworkType, error) {
525+
var mappedType *hcn.NetworkType = nil
526+
for binaryName, netType := range CniPluginNameToHcnTypeMap {
527+
if string(typeArg) == binaryName || typeArg == netType {
528+
mappedType = &netType
529+
break
530+
}
531+
}
532+
533+
if mappedType == nil {
534+
return hcn.NetworkType(""), fmt.Errorf("Unsupported CNI config 'type' %q. Options are the keys/values from: %v", typeArg, CniPluginNameToHcnTypeMap)
535+
}
536+
if *mappedType != typeArg {
537+
logrus.Warnf("Provided CNI config 'type' %q will be mapped to %q", typeArg, *mappedType)
538+
}
539+
540+
return *mappedType, nil
541+
}

common/core/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (plugin *netPlugin) Add(args *cniSkel.CmdArgs) (resultError error) {
152152
return err
153153
}
154154

155-
if nwConfig.Type != network.L2Bridge {
155+
if nwConfig.Type != hcn.L2Bridge {
156156
logrus.Errorf("[cni-net] Dual stack can only be specified with l2bridge network: [%v].", nwConfig.Type)
157157
return errors.New("Dual stack specified with non l2bridge network")
158158
}

network/network.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ import (
1111
"github.com/Microsoft/hcsshim/hcn"
1212
)
1313

14-
type NetworkType string
15-
16-
const (
17-
NAT NetworkType = "NAT"
18-
Overlay NetworkType = "Overlay"
19-
Transparent NetworkType = "Transparent"
20-
L2Tunnel NetworkType = "L2Tunnel"
21-
L2Bridge NetworkType = "L2Bridge"
22-
)
23-
2414
type DNSInfo struct {
2515
Nameservers []string
2616
Domain string
@@ -32,7 +22,7 @@ type DNSInfo struct {
3222
type NetworkInfo struct {
3323
ID string
3424
Name string
35-
Type NetworkType
25+
Type hcn.NetworkType
3626
InterfaceName string
3727
Subnets []SubnetInfo
3828
DNS DNSInfo
@@ -99,7 +89,7 @@ func GetNetworkInfoFromHostComputeNetwork(hcnNetwork *hcn.HostComputeNetwork) *N
9989
return &NetworkInfo{
10090
ID: hcnNetwork.Id,
10191
Name: hcnNetwork.Name,
102-
Type: NetworkType(hcnNetwork.Type),
92+
Type: hcnNetwork.Type,
10393
// Note: HostComputeNetwork has NetAdapterNameNetworkPolicySetting instead of a NetworkAdapterName/InterfaceName field.
10494
InterfaceName: GetNetAdapterNameNetworkPolicySetting(hcnNetwork.Policies),
10595
Subnets: subnets,

plugins/nat/nat_windows_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@ import (
55
"testing"
66

77
"github.com/Microsoft/hcsshim/hcn"
8+
"github.com/Microsoft/windows-container-networking/cni"
89
util "github.com/Microsoft/windows-container-networking/test/utilities"
910
)
1011

1112
var testDualStack bool
1213
var imageToUse string
1314

14-
func CreateNatTestNetwork() *hcn.HostComputeNetwork {
15+
func CreateNatTestNetwork(t *testing.T) *hcn.HostComputeNetwork {
1516
ipams := util.GetDefaultIpams()
16-
return util.CreateTestNetwork("natNet", "Nat", ipams, false)
17+
return util.CreateTestNetwork(t, "natNet", cni.NatPluginName, ipams, false)
1718
}
1819

1920
func TestNatCmdAdd(t *testing.T) {
2021
// t.Skip("Nat test is disabled for now.")
2122
testDualStack = (os.Getenv("TestDualStack") == "1")
2223
imageToUse = os.Getenv("ImageToUse")
23-
testNetwork := CreateNatTestNetwork()
24-
pt := util.MakeTestStruct(t, testNetwork, "nat", false, false, "", testDualStack, imageToUse)
24+
testNetwork := CreateNatTestNetwork(t)
25+
pt := util.MakeTestStruct(t, testNetwork, false, false, "", testDualStack, imageToUse)
2526
pt.RunAll(t)
2627
}

plugins/sdnbridge/sdnbridge_windows_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/Microsoft/hcsshim/hcn"
7+
"github.com/Microsoft/windows-container-networking/cni"
78
util "github.com/Microsoft/windows-container-networking/test/utilities"
89

910
"os"
@@ -12,20 +13,20 @@ import (
1213
var testDualStack bool
1314
var imageToUse string
1415

15-
func CreateBridgeTestNetwork() *hcn.HostComputeNetwork {
16+
func CreateBridgeTestNetwork(t *testing.T) *hcn.HostComputeNetwork {
1617
ipams := util.GetDefaultIpams()
1718
if testDualStack {
1819
ipams = append(ipams, util.GetDefaultIpv6Ipams()...)
1920
}
20-
return util.CreateTestNetwork("bridgeNet", "L2Bridge", ipams, true)
21+
return util.CreateTestNetwork(t, "bridgeNet", cni.SdnBridgePluginName, ipams, true)
2122
}
2223

2324
func TestBridgeCmdAdd(t *testing.T) {
2425
// t.Skip("Bridge test is disabled for now.")
2526
testDualStack = (os.Getenv("TestDualStack") == "1")
2627
imageToUse = os.Getenv("ImageToUse")
27-
testNetwork := CreateBridgeTestNetwork()
28-
pt := util.MakeTestStruct(t, testNetwork, "L2Bridge", true, true, "", testDualStack, imageToUse)
28+
testNetwork := CreateBridgeTestNetwork(t)
29+
pt := util.MakeTestStruct(t, testNetwork, true, true, "", testDualStack, imageToUse)
2930
pt.Ipv6Url = os.Getenv("Ipv6UrlToUse")
3031
pt.RunAll(t)
3132
}

plugins/sdnoverlay/sdnoverlay_windows_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/Microsoft/hcsshim/hcn"
9+
"github.com/Microsoft/windows-container-networking/cni"
910
util "github.com/Microsoft/windows-container-networking/test/utilities"
1011
)
1112

@@ -28,17 +29,17 @@ func GetVsidPol() []json.RawMessage {
2829
return []json.RawMessage{vsidPolRaw}
2930
}
3031

31-
func CreateOverlayTestNetwork() *hcn.HostComputeNetwork {
32+
func CreateOverlayTestNetwork(t *testing.T) *hcn.HostComputeNetwork {
3233
ipams := util.GetDefaultIpams()
3334
ipams[0].Subnets[0].Policies = GetVsidPol()
34-
return util.CreateTestNetwork("overlayNet", "Overlay", ipams, true)
35+
return util.CreateTestNetwork(t, "overlayNet", cni.SdnOverlayPluginName, ipams, true)
3536
}
3637

3738
func TestOverlayCmdAdd(t *testing.T) {
3839
// t.Skip("Overlay test is disabled for now.")
3940
testDualStack = (os.Getenv("TestDualStack") == "1")
4041
imageToUse = os.Getenv("ImageToUse")
41-
testNetwork := CreateOverlayTestNetwork()
42-
pt := util.MakeTestStruct(t, testNetwork, "Overlay", true, false, "", testDualStack, imageToUse)
42+
testNetwork := CreateOverlayTestNetwork(t)
43+
pt := util.MakeTestStruct(t, testNetwork, true, false, "", testDualStack, imageToUse)
4344
pt.RunAll(t)
4445
}

test/utilities/connectivity_testing.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,52 @@ func CreateNetConfIpam(cidr string) cni.IpamConfig {
8888
return testIpam
8989
}
9090

91-
func CreateNetworkConf(cniVersion string, name string, pluginType string,
92-
dns *cniTypes.DNS, addArgs []cni.KVP, gatewayPrefix string) *cni.NetworkConfig {
91+
func CreateNetworkConf(t *testing.T, cniVersion string, name string, pluginType hcn.NetworkType,
92+
dns *cniTypes.DNS, addArgs []cni.KVP, gatewayPrefix string) (*cni.NetworkConfig, error) {
93+
94+
mappedType, err := cni.MapCniTypeToHcnType(pluginType)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
if pluginType != mappedType {
100+
t.Logf("WARN: supplied network plugin type %q was mapped to HCN network type %q", pluginType, mappedType)
101+
}
102+
93103
netConf := cni.NetworkConfig{
94104
CniVersion: cniVersion,
95105
Name: name,
96106
Ipam: CreateNetConfIpam(gatewayPrefix),
97-
Type: pluginType,
107+
Type: mappedType,
98108
DNS: *dns,
99109
AdditionalArgs: addArgs,
100110
}
101-
return &netConf
111+
return &netConf, nil
102112
}
103113

104114
func CreateDualStackNetworkConf(
115+
t *testing.T,
105116
cniVersion string,
106117
name string,
107-
pluginType string,
118+
pluginType hcn.NetworkType,
108119
dns *cniTypes.DNS,
109120
addArgs []cni.KVP,
110121
gatewayPrefixv4 string,
111-
gatewayPrefixv6 string) *cni.NetworkConfig {
122+
gatewayPrefixv6 string) (*cni.NetworkConfig, error) {
123+
124+
mappedType, err := cni.MapCniTypeToHcnType(pluginType)
125+
if err != nil {
126+
return nil, err
127+
}
128+
129+
if pluginType != mappedType {
130+
t.Logf("WARN: supplied network plugin type %q was mapped to HCN network type %q", pluginType, mappedType)
131+
}
112132

113133
netConf := cni.NetworkConfig{
114134
CniVersion: cniVersion,
115135
Name: name,
116-
Type: pluginType,
136+
Type: mappedType,
117137
DNS: *dns,
118138
AdditionalArgs: addArgs,
119139
}
@@ -139,7 +159,7 @@ func CreateDualStackNetworkConf(
139159

140160
netConf.AdditionalRoutes = append(netConf.AdditionalRoutes, testRoute)
141161

142-
return &netConf
162+
return &netConf, nil
143163
}
144164

145165
func GetDefaultIpams() []hcn.Ipam {
@@ -289,14 +309,24 @@ func CreateGatewayEp(t *testing.T, networkId string, ipAddress string, ipv6Adres
289309
return nil
290310
}
291311

292-
func CreateTestNetwork(name string, netType string, ipams []hcn.Ipam, tryGetNetAdapter bool) *hcn.HostComputeNetwork {
312+
func CreateTestNetwork(t *testing.T, name string, netType string, ipams []hcn.Ipam, tryGetNetAdapter bool) *hcn.HostComputeNetwork {
313+
hcnNetType := hcn.NetworkType(netType)
314+
mappedType, err := cni.MapCniTypeToHcnType(hcnNetType)
315+
if err != nil {
316+
t.Errorf("Failed to map testing network type %q to HCN network type: %v", netType, err)
317+
}
318+
319+
if mappedType != hcnNetType {
320+
t.Logf("WARN: testing network type %q was mapped to HCN network type %q", netType, mappedType)
321+
}
322+
293323
network := &hcn.HostComputeNetwork{
294324
SchemaVersion: hcn.SchemaVersion{
295325
Major: 2,
296326
Minor: 0,
297327
},
298328
Name: name,
299-
Type: hcn.NetworkType(netType),
329+
Type: mappedType,
300330
Ipams: ipams,
301331
}
302332

@@ -312,7 +342,6 @@ func CreateTestNetwork(name string, netType string, ipams []hcn.Ipam, tryGetNetA
312342
func MakeTestStruct(
313343
t *testing.T,
314344
testNetwork *hcn.HostComputeNetwork,
315-
pluginType string,
316345
epPols bool,
317346
needGW bool,
318347
cid string,
@@ -352,11 +381,15 @@ func MakeTestStruct(
352381
var netConf *cni.NetworkConfig
353382

354383
if !testDualStack {
355-
netConf = CreateNetworkConf(defaultCniVersion, testNetwork.Name, pluginType, dns, addArgs, netConfPrefix)
384+
if netConf, err = CreateNetworkConf(t, defaultCniVersion, testNetwork.Name, testNetwork.Type, dns, addArgs, netConfPrefix); err != nil {
385+
t.Errorf("Failed to create testing network config: %v", err)
386+
return nil
387+
}
356388
} else {
357-
358389
netConfPrefixv6 := "fd00::101/64"
359-
netConf = CreateDualStackNetworkConf(defaultCniVersion, testNetwork.Name, pluginType, dns, addArgs, netConfPrefix, netConfPrefixv6)
390+
if netConf, err = CreateDualStackNetworkConf(t, defaultCniVersion, testNetwork.Name, testNetwork.Type, dns, addArgs, netConfPrefix, netConfPrefixv6); err != nil {
391+
return nil
392+
}
360393

361394
}
362395
netJson, _ := json.Marshal(netConf)

0 commit comments

Comments
 (0)