Skip to content

Commit

Permalink
All functional failures corrected related to MACVLAN handling, defaul…
Browse files Browse the repository at this point in the history
…t IP route, and policy-based IP route provsioning.
  • Loading branch information
Levovar committed Jan 25, 2019
1 parent 81332a1 commit 8572e9c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 60 deletions.
2 changes: 2 additions & 0 deletions pkg/cnidel/cniconfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cnidel

import (
"errors"
"log"
"io/ioutil"
"encoding/json"
"github.com/nokia/danm/pkg/danmep"
Expand Down Expand Up @@ -80,6 +81,7 @@ func getMacvlanCniConfig(netInfo *danmtypes.DanmNet, ipamOptions danmtypes.IpamC
MTU: 1500,
Ipam: ipamOptions,
}
log.Printf("LOFASZ MACVLAN CONFIG %v/n",macvlanConfig)
rawConfig, err := json.Marshal(macvlanConfig)
if err != nil {
return nil, errors.New("Error putting together CNI config for MACVLAN plugin: " + err.Error())
Expand Down
51 changes: 22 additions & 29 deletions pkg/cnidel/cnidel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cnidel
import (
"errors"
"log"
"net"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -31,8 +30,8 @@ var (
// Decision is made based on the NetworkType parameter of the DanmNet object
func IsDelegationRequired(danmClient danmclientset.Interface, nid, namespace string) (bool,*danmtypes.DanmNet,error) {
netInfo, err := danmClient.DanmV1().DanmNets(namespace).Get(nid, meta_v1.GetOptions{})
if err != nil {
return false, nil, err
if err != nil || netInfo.ObjectMeta.Name == ""{
return false, nil, errors.New("NID:" + nid + " in namespace:" + namespace + " cannot be GET from K8s API server!")
}
neType := netInfo.Spec.NetworkType
if neType == "ipvlan" || neType == "" {
Expand All @@ -43,7 +42,7 @@ func IsDelegationRequired(danmClient danmclientset.Interface, nid, namespace str

// DelegateInterfaceSetup delegates K8s Pod network interface setup task to the input 3rd party CNI plugin
// Returns the CNI compatible result object, or an error if interface creation was unsuccessful, or if the 3rd party CNI config could not be loaded
func DelegateInterfaceSetup(danmClient danmclientset.Interface, netInfo *danmtypes.DanmNet, ep *danmtypes.DanmEp) (types.Result,error) {
func DelegateInterfaceSetup(danmClient danmclientset.Interface, netInfo *danmtypes.DanmNet, ep *danmtypes.DanmEp) (*current.Result,error) {
var (
err error
ipamOptions danmtypes.IpamConfig
Expand All @@ -66,12 +65,16 @@ func DelegateInterfaceSetup(danmClient danmclientset.Interface, netInfo *danmtyp
freeDelegatedIps(danmClient, netInfo, ep.Spec.Iface.Address, ep.Spec.Iface.AddressIPv6)
return nil, errors.New("Error delegating ADD to CNI plugin:" + cniType + " because:" + err.Error())
}
delegatedResult := ConvertCniResult(cniResult)
if delegatedResult != nil {
setEpIfaceAddress(delegatedResult, &ep.Spec.Iface)
}
err = danmep.CreateRoutesInNetNs(*ep, netInfo)
if err != nil {
freeDelegatedIps(danmClient, netInfo, ep.Spec.Iface.Address, ep.Spec.Iface.AddressIPv6)
return nil, errors.New("Could not create IP routes for CNI:" + cniType + " because:" + err.Error())
// We don't consider this serious error, so we only log a warning about the issue.
log.Println("WARNING: Could not create IP routes for CNI:" + cniType + " because:" + err.Error())
}
return cniResult, nil
return delegatedResult, nil
}

func isIpamNeeded(cniType string) bool {
Expand All @@ -83,27 +86,21 @@ func isIpamNeeded(cniType string) bool {
return false
}

func getCniIpamConfig(options danmtypes.DanmNetOption, ip4 string, ip6 string) danmtypes.IpamConfig {
func getCniIpamConfig(options danmtypes.DanmNetOption, ip4, ip6 string) danmtypes.IpamConfig {
var (
subnet string
routes []danmtypes.IpamRoute
defaultGw string
ip string
)
if options.Cidr != "" {
ip = ip4
subnet = options.Cidr
routes, defaultGw = parseRoutes(options.Routes, subnet)
} else {
ip = ip6
subnet = options.Net6
routes, defaultGw = parseRoutes(options.Routes6, subnet)
}
return danmtypes.IpamConfig {
Type: ipamType,
Subnet: subnet,
Routes: routes,
DefaultGw: defaultGw,
Ip: strings.Split(ip, "/")[0],
}
}
Expand All @@ -118,21 +115,6 @@ func getCniPluginConfig(netInfo *danmtypes.DanmNet, ipamOptions danmtypes.IpamCo
return readCniConfigFile(netInfo)
}

func parseRoutes(rawRoutes map[string]string, netCidr string) ([]danmtypes.IpamRoute, string) {
defaultGw := ""
routes := []danmtypes.IpamRoute{}
for dst, gw := range rawRoutes {
routes = append(routes, danmtypes.IpamRoute{
Dst: dst,
Gw: gw,
})
if _, sn, _ := net.ParseCIDR(dst); sn.String() == netCidr {
defaultGw = gw
}
}
return routes, defaultGw
}

func execCniPlugin(cniType string, netInfo *danmtypes.DanmNet, rawConfig []byte, ep *danmtypes.DanmEp) (types.Result,error) {
cniPath, cniArgs, err := getExecCniParams(cniType, netInfo, ep)
if err != nil {
Expand Down Expand Up @@ -168,6 +150,17 @@ func getExecCniParams(cniType string, netInfo *danmtypes.DanmNet, ep *danmtypes.
return cniPath, cniArgs, nil
}

func setEpIfaceAddress(cniResult *current.Result, epIface *danmtypes.DanmEpIface) error {
for _, ip := range cniResult.IPs {
if ip.Version == "4" {
epIface.Address = ip.Address.String()
} else {
epIface.AddressIPv6 = ip.Address.String()
}
}
return nil
}

// DelegateInterfaceDelete delegates Ks8 Pod network interface delete task to the input 3rd party CNI plugin
// Returns an error if interface creation was unsuccessful, or if the 3rd party CNI config could not be loaded
func DelegateInterfaceDelete(danmClient danmclientset.Interface, netInfo *danmtypes.DanmNet, ep *danmtypes.DanmEp) error {
Expand Down
17 changes: 1 addition & 16 deletions pkg/danm/danm.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,32 +233,17 @@ func createDelegatedInterface(danmClient danmclientset.Interface, iface danmtype
if err != nil {
return nil, errors.New("DanmEp object could not be created due to error:" + err.Error())
}
delegateResult,err := cnidel.DelegateInterfaceSetup(danmClient, netInfo, &ep)
delegatedResult,err := cnidel.DelegateInterfaceSetup(danmClient, netInfo, &ep)
if err != nil {
return nil, err
}
delegatedResult := cnidel.ConvertCniResult(delegateResult)
if delegatedResult != nil {
setEpIfaceAddress(delegatedResult, &ep.Spec.Iface)
}
err = putDanmEp(args, ep)
if err != nil {
return nil, errors.New("DanmEp object could not be PUT to K8s due to error:" + err.Error())
}
return delegatedResult, nil
}

func setEpIfaceAddress(cniResult *current.Result, epIface *danmtypes.DanmEpIface) error {
for _, ip := range cniResult.IPs {
if ip.Version == "4" {
epIface.Address = ip.Address.String()
} else {
epIface.AddressIPv6 = ip.Address.String()
}
}
return nil
}

func createDanmInterface(danmClient danmclientset.Interface, iface danmtypes.Interface, netInfo *danmtypes.DanmNet, args *cniArgs) (*current.Result,error) {
netId := netInfo.Spec.NetworkID
ip4, ip6, macAddr, err := ipam.Reserve(danmClient, *netInfo, iface.Ip, iface.Ip6)
Expand Down
24 changes: 11 additions & 13 deletions pkg/danmep/ep.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func createContainerIface(ep danmtypes.DanmEp, dnet *danmtypes.DanmNet, device s
log.Println("Could not switch back to default ns during IPVLAN interface creation:" + err.Error())
}
}()
//cns,_ := ns.GetCurrentNS()
cpath := origns.Path()
log.Println("EP NS BASE PATH:" + cpath)
iface, err := netlink.LinkByName(device)
if err != nil {
return errors.New("cannot find host device because:" + err.Error())
Expand Down Expand Up @@ -164,19 +167,11 @@ func addIpRoutes(ep danmtypes.DanmEp, dnet *danmtypes.DanmNet) error {
if err != nil {
return err
}
err = addRulesForPolicyRoutes(dnet.Spec.Options.RTables, ep.Spec.Iface.Address)
err = addPolicyRoute(dnet.Spec.Options.RTables, ep.Spec.Iface.Address, ep.Spec.Iface.Proutes)
if err != nil {
return err
}
err = addRoutes(ep.Spec.Iface.Proutes, dnet.Spec.Options.RTables)
if err != nil {
return err
}
err = addRulesForPolicyRoutes(dnet.Spec.Options.RTables, ep.Spec.Iface.AddressIPv6)
if err != nil {
return err
}
err = addRoutes(ep.Spec.Iface.Proutes6, dnet.Spec.Options.RTables)
err = addPolicyRoute(dnet.Spec.Options.RTables, ep.Spec.Iface.AddressIPv6, ep.Spec.Iface.Proutes6)
if err != nil {
return err
}
Expand Down Expand Up @@ -215,8 +210,8 @@ func addRoutes(routes map[string]string, rtable int) error {
return nil
}

func addRulesForPolicyRoutes(rtable int, cidr string) error {
if rtable == 0 || cidr == "" {
func addPolicyRoute(rtable int, cidr string, proutes map[string]string) error {
if rtable == 0 || cidr == "" || proutes == nil {
return nil
}
srcIp, srcNet, _ := net.ParseCIDR(cidr)
Expand All @@ -228,10 +223,13 @@ func addRulesForPolicyRoutes(rtable int, cidr string) error {
if err != nil {
return errors.New("cannot add rule for policy-based IP routes because:" + err.Error())
}
err = addRoutes(proutes, rtable)
if err != nil {
return err
}
return nil
}

// TODO: Refactor this, as cyclomatic complexity is 15
func deleteContainerIface(ep danmtypes.DanmEp) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
Expand Down
4 changes: 2 additions & 2 deletions pkg/glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8572e9c

Please sign in to comment.