Skip to content

Commit d55e395

Browse files
committed
refactor: Update apipa nic as separate entry in podIPInfo
This PR updates both CNS and CNI code to construct apipa nic as separate entry in podIpInfo if either of allowhostonc or allownctohost set. This allows CNI to treat this as separate endpoint and align with current cni design/model of 1 nic per endpoint info. CNI then iterates through endpoint info and creates one nic at a time. Signed-off-by: Tamilmani <tamanoha@microsoft.com>
1 parent 2e10059 commit d55e395

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

cni/network/invoker_cns.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
192192
if err := addBackendNICToResult(&info, &addResult, key); err != nil {
193193
return IPAMAddResult{}, err
194194
}
195+
case cns.ApipaNIC:
196+
if err := configureApipaAddResult(&info, &addResult, &response.PodIPInfo[i].PodIPConfig, key); err != nil {
197+
return IPAMAddResult{}, err
198+
}
199+
195200
case cns.InfraNIC, "":
196201
// if we change from legacy cns, the nicType will be empty, so we assume it is infra nic
197202
info.nicType = cns.InfraNIC
@@ -508,6 +513,29 @@ func configureSecondaryAddResult(info *IPResultInfo, addResult *IPAMAddResult, p
508513
return nil
509514
}
510515

516+
func configureApipaAddResult(info *IPResultInfo, addResult *IPAMAddResult, podIPConfig *cns.IPSubnet, key string) error {
517+
ip, ipnet, err := podIPConfig.GetIPNet()
518+
if ip == nil {
519+
return errors.Wrap(err, "Unable to parse IP from response: "+info.podIPAddress+" with err %w")
520+
}
521+
522+
addResult.interfaceInfo[key] = network.InterfaceInfo{
523+
IPConfigs: []*network.IPConfig{
524+
{
525+
Address: net.IPNet{
526+
IP: ip,
527+
Mask: ipnet.Mask,
528+
},
529+
Gateway: net.ParseIP(info.ncGatewayIPAddress),
530+
},
531+
},
532+
NICType: info.nicType,
533+
SkipDefaultRoutes: true,
534+
}
535+
536+
return nil
537+
}
538+
511539
func addBackendNICToResult(info *IPResultInfo, addResult *IPAMAddResult, key string) error {
512540
macAddress, err := net.ParseMAC(info.macAddress)
513541
if err != nil {

cns/NetworkContainerContract.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ const (
9393
NodeNetworkInterfaceFrontendNIC NICType = "FrontendNIC"
9494
// NodeNetworkInterfaceBackendNIC is the new name for BackendNIC
9595
NodeNetworkInterfaceBackendNIC NICType = "BackendNIC"
96+
97+
// ApipaNIC is used for internal communication between host and container
98+
ApipaNIC NICType = "ApipaNIC"
9699
)
97100

98101
// ChannelMode :- CNS channel modes
@@ -516,6 +519,10 @@ type PodIpInfo struct {
516519
PnPID string
517520
// Default Deny ACL's to configure on HNS endpoints for Swiftv2 window nodes
518521
EndpointPolicies []policy.Policy
522+
// This flag is in effect only if nic type is apipa. This allows connection originating from host to container via apipa nic and not other way.
523+
AllowHostToNCCommunication bool
524+
// This flag is in effect only if nic type is apipa. This allows connection originating from container to host via apipa nic and not other way.
525+
AllowNCToHostCommunication bool
519526
}
520527

521528
type HostIPInfo struct {

cns/restserver/ipam.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func (service *HTTPRestService) requestIPConfigHandlerHelperStandalone(ctx conte
149149

150150
// assign NICType and MAC Address for SwiftV2. we assume that there won't be any SwiftV1 NCs here
151151
podIPInfoList := make([]cns.PodIpInfo, 0, len(resp))
152+
apipaIndex := -1
152153
for i := range resp {
153154
podIPInfo := cns.PodIpInfo{
154155
PodIPConfig: resp[i].IPConfiguration.IPSubnet,
@@ -157,6 +158,21 @@ func (service *HTTPRestService) requestIPConfigHandlerHelperStandalone(ctx conte
157158
NetworkContainerPrimaryIPConfig: resp[i].IPConfiguration,
158159
}
159160
podIPInfoList = append(podIPInfoList, podIPInfo)
161+
if resp[i].AllowHostToNCCommunication || resp[i].AllowNCToHostCommunication {
162+
apipaIndex = i
163+
}
164+
}
165+
166+
if apipaIndex != -1 {
167+
apipaPodIPInfo := cns.PodIpInfo{
168+
PodIPConfig: resp[apipaIndex].LocalIPConfiguration.IPSubnet,
169+
NICType: cns.ApipaNIC,
170+
NetworkContainerPrimaryIPConfig: resp[apipaIndex].LocalIPConfiguration,
171+
SkipDefaultRoutes: true,
172+
AllowHostToNCCommunication: resp[apipaIndex].AllowHostToNCCommunication,
173+
AllowNCToHostCommunication: resp[apipaIndex].AllowNCToHostCommunication,
174+
}
175+
podIPInfoList = append(podIPInfoList, apipaPodIPInfo)
160176
}
161177

162178
ipConfigsResp := &cns.IPConfigsResponse{

network/network_windows.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ func (nm *networkManager) addIPv6DefaultRoute() error {
342342
// newNetworkImplHnsV2 creates a new container network for HNSv2.
343343
func (nm *networkManager) newNetworkImplHnsV2(nwInfo *EndpointInfo, extIf *externalInterface) (*network, error) {
344344
// network creation is not required for IB
345-
if nwInfo.NICType == cns.BackendNIC {
345+
// For apipa nic, we create network as part of endpoint creation
346+
if nwInfo.NICType == cns.BackendNIC || nwInfo.NICType == cns.ApipaNIC {
346347
return &network{Endpoints: make(map[string]*endpoint)}, nil
347348
}
348349

0 commit comments

Comments
 (0)