Skip to content

Commit

Permalink
add interface name in cni response
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Jun 11, 2021
1 parent 14c8ad7 commit 8c16932
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
7 changes: 7 additions & 0 deletions cmd/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,21 @@ func cmdAdd(args *skel.CmdArgs) error {
func generateCNIResult(cniVersion string, cniResponse *request.CniResponse) current.Result {
result := current.Result{CNIVersion: cniVersion}
_, mask, _ := net.ParseCIDR(cniResponse.CIDR)
podIface := current.Interface{
Name: cniResponse.PodNicName,
Mac: cniResponse.MacAddress,
}
switch cniResponse.Protocol {
case kubeovnv1.ProtocolIPv4:
ip, route := assignV4Address(cniResponse.IpAddress, cniResponse.Gateway, mask)
result.IPs = []*current.IPConfig{&ip}
result.Routes = []*types.Route{&route}
result.Interfaces = []*current.Interface{&podIface}
case kubeovnv1.ProtocolIPv6:
ip, route := assignV6Address(cniResponse.IpAddress, cniResponse.Gateway, mask)
result.IPs = []*current.IPConfig{&ip}
result.Routes = []*types.Route{&route}
result.Interfaces = []*current.Interface{&podIface}
case kubeovnv1.ProtocolDual:
var netMask *net.IPNet
for _, cidrBlock := range strings.Split(cniResponse.CIDR, ",") {
Expand All @@ -99,6 +105,7 @@ func generateCNIResult(cniVersion string, cniResponse *request.CniResponse) curr
result.Routes = append(result.Routes, &route)
}
}
result.Interfaces = []*current.Interface{&podIface}
}

return result
Expand Down
7 changes: 4 additions & 3 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
}

klog.Infof("add port request %v", podRequest)
var macAddr, ip, ipAddr, cidr, gw, subnet, ingress, egress, vlanID, ifName, nicType, netns string
var macAddr, ip, ipAddr, cidr, gw, subnet, ingress, egress, vlanID, ifName, nicType, netns, podNicName string
var pod *v1.Pod
var err error
for i := 0; i < 15; i++ {
Expand Down Expand Up @@ -136,8 +136,9 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
nsArray := strings.Split(netns, "/")
podNetns := nsArray[len(nsArray)-1]
if nicType == util.InternalType {
err = csh.configureNicWithInternalPort(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, ifName, macAddr, ipAddr, gw, ingress, egress, vlanID, podRequest.DeviceID, nicType, podNetns)
podNicName, err = csh.configureNicWithInternalPort(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, ifName, macAddr, ipAddr, gw, ingress, egress, vlanID, podRequest.DeviceID, nicType, podNetns)
} else {
podNicName = ifName
err = csh.configureNic(podRequest.PodName, podRequest.PodNamespace, podRequest.Provider, podRequest.NetNs, podRequest.ContainerID, ifName, macAddr, ipAddr, gw, ingress, egress, vlanID, podRequest.DeviceID, nicType, podNetns)
}
if err != nil {
Expand All @@ -159,7 +160,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
}
}

if err := resp.WriteHeaderAndEntity(http.StatusOK, request.CniResponse{Protocol: util.CheckProtocol(cidr), IpAddress: ip, MacAddress: macAddr, CIDR: cidr, Gateway: gw}); err != nil {
if err := resp.WriteHeaderAndEntity(http.StatusOK, request.CniResponse{Protocol: util.CheckProtocol(cidr), IpAddress: ip, MacAddress: macAddr, CIDR: cidr, Gateway: gw, PodNicName: podNicName}); err != nil {
klog.Errorf("failed to write response, %v", err)
}
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/daemon/ovs.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ func renameLink(curName, newName string) error {
return nil
}

func (csh cniServerHandler) configureNicWithInternalPort(podName, podNamespace, provider, netns, containerID, ifName, mac, ip, gateway, ingress, egress, vlanID, DeviceID, nicType, podNetns string) error {
func (csh cniServerHandler) configureNicWithInternalPort(podName, podNamespace, provider, netns, containerID, ifName, mac, ip, gateway, ingress, egress, vlanID, DeviceID, nicType, podNetns string) (string, error) {
var err error

_, containerNicName := generateNicName(containerID, ifName)
Expand All @@ -712,27 +712,27 @@ func (csh cniServerHandler) configureNicWithInternalPort(podName, podNamespace,
fmt.Sprintf("external_ids:ip=%s", ipStr),
fmt.Sprintf("external_ids:pod_netns=%s", podNetns))
if err != nil {
return fmt.Errorf("add nic to ovs failed %v: %q", err, output)
return containerNicName, fmt.Errorf("add nic to ovs failed %v: %q", err, output)
}

// container nic must use same mac address from pod annotation, otherwise ovn will reject these packets by default
macAddr, err := net.ParseMAC(mac)
if err != nil {
return fmt.Errorf("failed to parse mac %s %v", macAddr, err)
return containerNicName, fmt.Errorf("failed to parse mac %s %v", macAddr, err)
}

if err = ovs.SetInterfaceBandwidth(podName, podNamespace, ifaceID, ingress, egress); err != nil {
return err
return containerNicName, err
}

podNS, err := ns.GetNS(netns)
if err != nil {
return fmt.Errorf("failed to open netns %q: %v", netns, err)
return containerNicName, fmt.Errorf("failed to open netns %q: %v", netns, err)
}
if err = configureContainerNic(containerNicName, ifName, ip, gateway, macAddr, podNS, csh.Config.MTU, nicType); err != nil {
return err
return containerNicName, err
}
return nil
return containerNicName, nil
}

// https://github.com/antrea-io/antrea/issues/1691
Expand Down
1 change: 1 addition & 0 deletions pkg/request/cniserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type CniResponse struct {
CIDR string `json:"cidr"`
Gateway string `json:"gateway"`
Mtu int `json:"mtu"`
PodNicName string `json:"nicname"`
Err string `json:"error"`
}

Expand Down

0 comments on commit 8c16932

Please sign in to comment.