Skip to content

Commit b65b051

Browse files
authored
[cni]: dualstack overlay hostIP issue hotfix (#2290)
* dualstack overlay hostIP issue
1 parent 54154ab commit b65b051

File tree

5 files changed

+68
-44
lines changed

5 files changed

+68
-44
lines changed

.pipelines/singletenancy/dualstack-overlay/dualstackoverlay-e2e-job-template.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ stages:
8181
dns: true
8282
portforward: true
8383
service: true
84+
hostport: true
8485
hybridWin: true
8586

8687
- job: failedE2ELogs

cni/network/network.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,12 @@ func (plugin *NetPlugin) createEndpointInternal(opt *createEndpointInternalOpt)
782782
}
783783

784784
isIPv6Enabled := opt.resultV6 != nil
785-
epPolicies := getPoliciesFromRuntimeCfg(opt.nwCfg, isIPv6Enabled)
785+
epPolicies, err := getPoliciesFromRuntimeCfg(opt.nwCfg, isIPv6Enabled)
786+
if err != nil {
787+
logger.Error("failed to get policies from runtime configurations", zap.Error(err))
788+
return epInfo, plugin.Errorf(err.Error())
789+
}
790+
786791
epInfo.Policies = append(epInfo.Policies, epPolicies...)
787792

788793
// Populate addresses.

cni/network/network_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ func getEndpointPolicies(PolicyArgs) ([]policy.Policy, error) {
123123

124124
// getPoliciesFromRuntimeCfg returns network policies from network config.
125125
// getPoliciesFromRuntimeCfg is a dummy function for Linux platform.
126-
func getPoliciesFromRuntimeCfg(_ *cni.NetworkConfig, _ bool) []policy.Policy {
127-
return nil
126+
func getPoliciesFromRuntimeCfg(_ *cni.NetworkConfig, _ bool) ([]policy.Policy, error) {
127+
return nil, nil
128128
}
129129

130130
func addIPV6EndpointPolicy(nwInfo network.NetworkInfo) (policy.Policy, error) {

cni/network/network_windows.go

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ func getEndpointDNSSettings(nwCfg *cni.NetworkConfig, result *cniTypesCurr.Resul
248248
}
249249

250250
// getPoliciesFromRuntimeCfg returns network policies from network config.
251-
func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig, isIPv6Enabled bool) []policy.Policy {
252-
logger.Info("Runtime Info",
253-
zap.Any("config", nwCfg.RuntimeConfig))
251+
func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig, isIPv6Enabled bool) ([]policy.Policy, error) {
252+
logger.Info("Runtime Info", zap.Any("config", nwCfg.RuntimeConfig))
254253
var policies []policy.Policy
255254
var protocol uint32
256255

@@ -266,57 +265,57 @@ func getPoliciesFromRuntimeCfg(nwCfg *cni.NetworkConfig, isIPv6Enabled bool) []p
266265

267266
// To support hostport policy mapping
268267
// uint32 NatFlagsLocalRoutedVip = 1
269-
rawPolicy, _ := json.Marshal(&hnsv2.PortMappingPolicySetting{
268+
// To support hostport policy mapping for ipv6 in dualstack overlay mode
269+
// uint32 NatFlagsIPv6 = 2
270+
271+
flag := hnsv2.NatFlagsLocalRoutedVip
272+
if mapping.HostIp != "" {
273+
hostIP, err := netip.ParseAddr(mapping.HostIp)
274+
if err != nil {
275+
return nil, errors.Wrapf(err, "failed to parse hostIP %v", hostIP)
276+
}
277+
278+
if hostIP.Is6() && isIPv6Enabled {
279+
flag = hnsv2.NatFlagsIPv6
280+
}
281+
282+
if hostIP.Is6() && !isIPv6Enabled {
283+
logger.Info("Do not use ipv6 hostIP to create windows pod on ipv4 cluster")
284+
}
285+
}
286+
287+
rawPolicy, err := json.Marshal(&hnsv2.PortMappingPolicySetting{
270288
ExternalPort: uint16(mapping.HostPort),
271289
InternalPort: uint16(mapping.ContainerPort),
272290
VIP: mapping.HostIp,
273291
Protocol: protocol,
274-
Flags: hnsv2.NatFlagsLocalRoutedVip,
292+
Flags: flag,
275293
})
276294

277-
hnsv2Policy, _ := json.Marshal(&hnsv2.EndpointPolicy{
295+
if err != nil {
296+
return nil, errors.Wrap(err, "failed to marshal HNS portMappingPolicySetting")
297+
}
298+
299+
hnsv2Policy, err := json.Marshal(&hnsv2.EndpointPolicy{
278300
Type: hnsv2.PortMapping,
279301
Settings: rawPolicy,
280302
})
281303

282-
policyv4 := policy.Policy{
304+
if err != nil {
305+
return nil, errors.Wrap(err, "failed to marshal HNS endpointPolicy")
306+
}
307+
308+
hnsPolicy := policy.Policy{
283309
Type: policy.EndpointPolicy,
284310
Data: hnsv2Policy,
285311
}
286312

287-
logger.Info("Creating port mapping policyv4",
288-
zap.Any("policy", policyv4))
289-
policies = append(policies, policyv4)
290-
291-
// add port mapping policy for v6 if we have IPV6 enabled
292-
if isIPv6Enabled {
293-
// To support hostport policy mapping for ipv6 in dualstack overlay mode
294-
// uint32 NatFlagsIPv6 = 2
295-
rawPolicyv6, _ := json.Marshal(&hnsv2.PortMappingPolicySetting{ // nolint
296-
ExternalPort: uint16(mapping.HostPort),
297-
InternalPort: uint16(mapping.ContainerPort),
298-
VIP: mapping.HostIp,
299-
Protocol: protocol,
300-
Flags: hnsv2.NatFlagsIPv6,
301-
})
302-
303-
hnsv2Policyv6, _ := json.Marshal(&hnsv2.EndpointPolicy{ // nolint
304-
Type: hnsv2.PortMapping,
305-
Settings: rawPolicyv6,
306-
})
307-
308-
policyv6 := policy.Policy{
309-
Type: policy.EndpointPolicy,
310-
Data: hnsv2Policyv6,
311-
}
313+
logger.Info("Creating port mapping policy", zap.Any("policy", hnsPolicy))
312314

313-
logger.Info("Creating port mapping policyv6",
314-
zap.Any("policy", policyv6))
315-
policies = append(policies, policyv6)
316-
}
315+
policies = append(policies, hnsPolicy)
317316
}
318317

319-
return policies
318+
return policies, nil
320319
}
321320

322321
func getEndpointPolicies(args PolicyArgs) ([]policy.Policy, error) {

cni/network/network_windows_test.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ func TestSetEndpointOptions(t *testing.T) {
217217

218218
func TestSetPoliciesFromNwCfg(t *testing.T) {
219219
tests := []struct {
220-
name string
221-
nwCfg cni.NetworkConfig
220+
name string
221+
nwCfg cni.NetworkConfig
222+
isIPv6Enabled bool
222223
}{
223224
{
224225
name: "Runtime network polices",
@@ -234,6 +235,7 @@ func TestSetPoliciesFromNwCfg(t *testing.T) {
234235
},
235236
},
236237
},
238+
isIPv6Enabled: false,
237239
},
238240
{
239241
name: "Runtime hostPort mapping polices",
@@ -248,13 +250,30 @@ func TestSetPoliciesFromNwCfg(t *testing.T) {
248250
},
249251
},
250252
},
253+
isIPv6Enabled: false,
254+
},
255+
{
256+
name: "Runtime hostPort mapping polices with ipv6 hostIP",
257+
nwCfg: cni.NetworkConfig{
258+
RuntimeConfig: cni.RuntimeConfig{
259+
PortMappings: []cni.PortMapping{
260+
{
261+
Protocol: "tcp",
262+
HostPort: 44000,
263+
ContainerPort: 80,
264+
HostIp: "2001:2002:2003::1",
265+
},
266+
},
267+
},
268+
},
269+
isIPv6Enabled: true,
251270
},
252271
}
253272
for _, tt := range tests {
254273
tt := tt
255274
t.Run(tt.name, func(t *testing.T) {
256-
isIPv6Enabled := false
257-
policies := getPoliciesFromRuntimeCfg(&tt.nwCfg, isIPv6Enabled)
275+
policies, err := getPoliciesFromRuntimeCfg(&tt.nwCfg, tt.isIPv6Enabled)
276+
require.NoError(t, err)
258277
require.Condition(t, assert.Comparison(func() bool {
259278
return len(policies) > 0 && policies[0].Type == policy.EndpointPolicy
260279
}))

0 commit comments

Comments
 (0)