Skip to content

Commit 33890dd

Browse files
jaer-tsunJaeryn
andauthored
fix: skip reserved IPs (#2060)
Co-authored-by: Jaeryn <[email protected]>
1 parent dc37cef commit 33890dd

File tree

4 files changed

+125
-77
lines changed

4 files changed

+125
-77
lines changed

cns/kubecontroller/nodenetworkconfig/conversion_linux_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,63 @@ var validOverlayRequest = &cns.CreateNetworkContainerRequest{
3535
},
3636
},
3737
}
38+
39+
var validVNETBlockRequest = &cns.CreateNetworkContainerRequest{
40+
Version: strconv.FormatInt(version, 10),
41+
IPConfiguration: cns.IPConfiguration{
42+
GatewayIPAddress: vnetBlockDefaultGateway,
43+
IPSubnet: cns.IPSubnet{
44+
PrefixLength: uint8(vnetBlockSubnetPrefixLen),
45+
IPAddress: vnetBlockPrimaryIP,
46+
},
47+
},
48+
NetworkContainerid: ncID,
49+
NetworkContainerType: cns.Docker,
50+
// Ignore first IP in first CIDR Block, i.e. 10.224.0.4
51+
SecondaryIPConfigs: map[string]cns.SecondaryIPConfig{
52+
"10.224.0.5": {
53+
IPAddress: "10.224.0.5",
54+
NCVersion: version,
55+
},
56+
"10.224.0.6": {
57+
IPAddress: "10.224.0.6",
58+
NCVersion: version,
59+
},
60+
"10.224.0.7": {
61+
IPAddress: "10.224.0.7",
62+
NCVersion: version,
63+
},
64+
"10.224.0.8": {
65+
IPAddress: "10.224.0.8",
66+
NCVersion: version,
67+
},
68+
"10.224.0.9": {
69+
IPAddress: "10.224.0.9",
70+
NCVersion: version,
71+
},
72+
"10.224.0.10": {
73+
IPAddress: "10.224.0.10",
74+
NCVersion: version,
75+
},
76+
"10.224.0.11": {
77+
IPAddress: "10.224.0.11",
78+
NCVersion: version,
79+
},
80+
"10.224.0.12": {
81+
IPAddress: "10.224.0.12",
82+
NCVersion: version,
83+
},
84+
"10.224.0.13": {
85+
IPAddress: "10.224.0.13",
86+
NCVersion: version,
87+
},
88+
"10.224.0.14": {
89+
IPAddress: "10.224.0.14",
90+
NCVersion: version,
91+
},
92+
"10.224.0.15": {
93+
IPAddress: "10.224.0.15",
94+
NCVersion: version,
95+
},
96+
},
97+
}

cns/kubecontroller/nodenetworkconfig/conversion_test.go

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -117,66 +117,6 @@ var validVNETBlockNC = v1alpha.NetworkContainer{
117117
Version: version,
118118
}
119119

120-
var validVNETBlockRequest = &cns.CreateNetworkContainerRequest{
121-
Version: strconv.FormatInt(version, 10),
122-
IPConfiguration: cns.IPConfiguration{
123-
GatewayIPAddress: vnetBlockDefaultGateway,
124-
IPSubnet: cns.IPSubnet{
125-
PrefixLength: uint8(vnetBlockSubnetPrefixLen),
126-
IPAddress: vnetBlockPrimaryIP,
127-
},
128-
},
129-
NetworkContainerid: ncID,
130-
NetworkContainerType: cns.Docker,
131-
// Ignore first IP in first CIDR Block, i.e. 10.224.0.4
132-
SecondaryIPConfigs: map[string]cns.SecondaryIPConfig{
133-
"10.224.0.5": {
134-
IPAddress: "10.224.0.5",
135-
NCVersion: version,
136-
},
137-
"10.224.0.6": {
138-
IPAddress: "10.224.0.6",
139-
NCVersion: version,
140-
},
141-
"10.224.0.7": {
142-
IPAddress: "10.224.0.7",
143-
NCVersion: version,
144-
},
145-
"10.224.0.8": {
146-
IPAddress: "10.224.0.8",
147-
NCVersion: version,
148-
},
149-
"10.224.0.9": {
150-
IPAddress: "10.224.0.9",
151-
NCVersion: version,
152-
},
153-
"10.224.0.10": {
154-
IPAddress: "10.224.0.10",
155-
NCVersion: version,
156-
},
157-
"10.224.0.11": {
158-
IPAddress: "10.224.0.11",
159-
NCVersion: version,
160-
},
161-
"10.224.0.12": {
162-
IPAddress: "10.224.0.12",
163-
NCVersion: version,
164-
},
165-
"10.224.0.13": {
166-
IPAddress: "10.224.0.13",
167-
NCVersion: version,
168-
},
169-
"10.224.0.14": {
170-
IPAddress: "10.224.0.14",
171-
NCVersion: version,
172-
},
173-
"10.224.0.15": {
174-
IPAddress: "10.224.0.15",
175-
NCVersion: version,
176-
},
177-
},
178-
}
179-
180120
func TestCreateNCRequestFromDynamicNC(t *testing.T) {
181121
tests := []struct {
182122
name string

cns/kubecontroller/nodenetworkconfig/conversion_windows.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,19 @@ import (
1010
)
1111

1212
// createNCRequestFromStaticNCHelper generates a CreateNetworkContainerRequest from a static NetworkContainer.
13-
// If the NC's DefaultGateway is empty, it will set the 0th IP as the gateway IP and all remaining IPs as
14-
// secondary IPs. If the gateway is not empty, it will not reserve the 0th IP and add it as a secondary IP.
13+
// If the NC's DefaultGateway is empty and nc type is overlay, it will set the 2nd IP (*.1) as the gateway IP and all remaining IPs as
14+
// secondary IPs. If the gateway is not empty, it will not reserve the 2nd IP and add it as a secondary IP.
1515
//
1616
//nolint:gocritic //ignore hugeparam
1717
func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPrefix netip.Prefix, subnet cns.IPSubnet) (*cns.CreateNetworkContainerRequest, error) {
1818
secondaryIPConfigs := map[string]cns.SecondaryIPConfig{}
19-
20-
// if NC DefaultGateway is empty, set the 0th IP to the gateway and add the rest of the IPs
21-
// as secondary IPs
22-
startingAddr := primaryIPPrefix.Masked().Addr() // the masked address is the 0th IP in the subnet
19+
// the masked address is the 0th IP in the subnet and startingAddr is the 2nd IP (*.1)
20+
startingAddr := primaryIPPrefix.Masked().Addr().Next()
21+
lastAddr := startingAddr
22+
// if NC DefaultGateway is empty, set the 2nd IP (*.1) to the gateway and add the rest of the IPs as secondary IPs
2323
if nc.DefaultGateway == "" && nc.Type == v1alpha.Overlay {
24-
// assign 0th IP to the default gateway
2524
nc.DefaultGateway = startingAddr.String()
2625
startingAddr = startingAddr.Next()
27-
} else if nc.Type == v1alpha.VNETBlock {
28-
// skipping 0th IP for the Primary IP of NC
29-
startingAddr = startingAddr.Next()
3026
}
3127

3228
// iterate through all IP addresses in the subnet described by primaryPrefix and
@@ -36,6 +32,7 @@ func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPre
3632
IPAddress: addr.String(),
3733
NCVersion: int(nc.Version),
3834
}
35+
lastAddr = addr
3936
}
4037

4138
if nc.Type == v1alpha.VNETBlock {
@@ -53,10 +50,13 @@ func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPre
5350
IPAddress: addr.String(),
5451
NCVersion: int(nc.Version),
5552
}
53+
lastAddr = addr
5654
}
5755
}
5856
}
5957

58+
delete(secondaryIPConfigs, lastAddr.String())
59+
6060
return &cns.CreateNetworkContainerRequest{
6161
SecondaryIPConfigs: secondaryIPConfigs,
6262
NetworkContainerid: nc.ID,

cns/kubecontroller/nodenetworkconfig/conversion_windows_test.go

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,69 @@ var validOverlayRequest = &cns.CreateNetworkContainerRequest{
1313
PrefixLength: uint8(subnetPrefixLen),
1414
IPAddress: primaryIP,
1515
},
16-
GatewayIPAddress: "10.0.0.0",
16+
GatewayIPAddress: "10.0.0.1",
1717
},
1818
NetworkContainerid: ncID,
1919
NetworkContainerType: cns.Docker,
2020
SecondaryIPConfigs: map[string]cns.SecondaryIPConfig{
21-
"10.0.0.1": {
22-
IPAddress: "10.0.0.1",
23-
NCVersion: version,
24-
},
2521
"10.0.0.2": {
2622
IPAddress: "10.0.0.2",
23+
NCVersion: 0,
24+
},
25+
},
26+
}
27+
28+
var validVNETBlockRequest = &cns.CreateNetworkContainerRequest{
29+
Version: strconv.FormatInt(version, 10),
30+
IPConfiguration: cns.IPConfiguration{
31+
GatewayIPAddress: vnetBlockDefaultGateway,
32+
IPSubnet: cns.IPSubnet{
33+
PrefixLength: uint8(vnetBlockSubnetPrefixLen),
34+
IPAddress: vnetBlockPrimaryIP,
35+
},
36+
},
37+
NetworkContainerid: ncID,
38+
NetworkContainerType: cns.Docker,
39+
// Ignore first IP in first CIDR Block, i.e. 10.224.0.4
40+
SecondaryIPConfigs: map[string]cns.SecondaryIPConfig{
41+
"10.224.0.5": {
42+
IPAddress: "10.224.0.5",
43+
NCVersion: version,
44+
},
45+
"10.224.0.6": {
46+
IPAddress: "10.224.0.6",
47+
NCVersion: version,
48+
},
49+
"10.224.0.7": {
50+
IPAddress: "10.224.0.7",
51+
NCVersion: version,
52+
},
53+
"10.224.0.8": {
54+
IPAddress: "10.224.0.8",
55+
NCVersion: version,
56+
},
57+
"10.224.0.9": {
58+
IPAddress: "10.224.0.9",
59+
NCVersion: version,
60+
},
61+
"10.224.0.10": {
62+
IPAddress: "10.224.0.10",
63+
NCVersion: version,
64+
},
65+
"10.224.0.11": {
66+
IPAddress: "10.224.0.11",
67+
NCVersion: version,
68+
},
69+
"10.224.0.12": {
70+
IPAddress: "10.224.0.12",
71+
NCVersion: version,
72+
},
73+
"10.224.0.13": {
74+
IPAddress: "10.224.0.13",
2775
NCVersion: version,
2876
},
29-
"10.0.0.3": {
30-
IPAddress: "10.0.0.3",
77+
"10.224.0.14": {
78+
IPAddress: "10.224.0.14",
3179
NCVersion: version,
3280
},
3381
},

0 commit comments

Comments
 (0)