Skip to content

Commit

Permalink
continue to delete veth after ipam del error, logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Jul 18, 2022
1 parent 2191fea commit db58647
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions plugins/ecs-bridge/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,44 @@ import (
"github.com/containernetworking/cni/pkg/types/current"
)

func detailLogMsg(msg string, args *skel.CmdArgs, conf *types.NetConf, hostVethName string) string {
var ipamType string
var bridgeName string
if conf != nil {
ipamType = conf.IPAM.Type
bridgeName = conf.BridgeName
}
msg = fmt.Sprintf("msg=\"%s\" netns=%s ifname=%s containerID=%s",
msg, args.Netns, args.IfName, args.ContainerID)
if bridgeName != "" {
msg = msg + " bridgeName=" + bridgeName
}
if ipamType != "" {
msg = msg + " ipamType=" + ipamType
}
if hostVethName != "" {
msg = msg + " hostVethName=" + hostVethName
}
return msg
}

func detailLogError(msg string, args *skel.CmdArgs, conf *types.NetConf, hostVethName string) {
msg = detailLogMsg(msg, args, conf, hostVethName)
log.Error(msg)
}

func detailLogInfo(msg string, args *skel.CmdArgs, conf *types.NetConf, hostVethName string) {
msg = detailLogMsg(msg, args, conf, hostVethName)
log.Info(msg)
}

// Add invokes the command to create the bridge add the veth pair to
// connect container's namespace with the bridge
func Add(args *skel.CmdArgs) error {
defer log.Flush()
err := add(args, engine.New())
if err != nil {
log.Errorf("Error executing ADD command: %v", err)
detailLogError("Error executing ADD command: "+err.Error(), args, nil, "")
}

return err
Expand All @@ -42,54 +73,38 @@ func Del(args *skel.CmdArgs) error {
defer log.Flush()
err := del(args, engine.New())
if err != nil {
log.Errorf("Error executing DEL command: %v", err)
detailLogError("Error executing DEL command: "+err.Error(), args, nil, "")
}

return err
}

func detailLog(msg string, args *skel.CmdArgs, conf *types.NetConf, hostVethName string) {
var ipamType string
if conf != nil {
ipamType = conf.IPAM.Type
}
msg = fmt.Sprintf("msg=\"%s\" netns=%s ifname=%s bridgeName=%s containerID=%s",
msg, args.Netns, args.IfName, conf.BridgeName, args.ContainerID)
if ipamType != "" {
msg = msg + " ipamType=" + ipamType
}
if hostVethName != "" {
msg = msg + " hostVethName=" + hostVethName
}
log.Infof(msg)
}

func add(args *skel.CmdArgs, engine engine.Engine) error {
conf, err := types.NewConf(args)
if err != nil {
return err
}

detailLog("Creating the bridge", args, conf, "")
detailLogInfo("Creating the bridge", args, conf, "")
bridge, err := engine.CreateBridge(conf.BridgeName, conf.MTU)
if err != nil {
return err
}

detailLog("Creating veth pair for namespace", args, conf, "")
detailLogInfo("Creating veth pair for namespace", args, conf, "")
containerVethInterface, hostVethName, err := engine.CreateVethPair(
args.Netns, conf.MTU, args.IfName)
if err != nil {
return err
}

detailLog("Attaching veth pair to bridge", args, conf, hostVethName)
detailLogInfo("Attaching veth pair to bridge", args, conf, hostVethName)
hostVethInterface, err := engine.AttachHostVethInterfaceToBridge(hostVethName, bridge)
if err != nil {
return err
}

detailLog("Running IPAM plugin ADD", args, conf, hostVethName)
detailLogInfo("Running IPAM plugin ADD", args, conf, hostVethName)
result, err := engine.RunIPAMPluginAdd(conf.IPAM.Type, args.StdinData)
if err != nil {
return err
Expand All @@ -116,13 +131,13 @@ func add(args *skel.CmdArgs, engine engine.Engine) error {
// needs to know which interface should be used when adding routes
result.IPs[0].Interface = 2

detailLog("Configuring container's interface", args, conf, hostVethName)
detailLogInfo("Configuring container's interface", args, conf, hostVethName)
err = engine.ConfigureContainerVethInterface(args.Netns, result, args.IfName)
if err != nil {
return err
}

detailLog("Configuring bridge", args, conf, hostVethName)
detailLogInfo("Configuring bridge", args, conf, hostVethName)
err = engine.ConfigureBridge(result, bridge)
if err != nil {
return err
Expand All @@ -135,18 +150,18 @@ func del(args *skel.CmdArgs, engine engine.Engine) error {
if err != nil {
return err
}
detailLog("Deleting veth interface", args, conf, "")
detailLogInfo("Deleting veth interface", args, conf, "")

if utils.ZeroOrNil(conf.IPAM) {
detailLog("IPAM configuration not found, skip DEL for IPAM", args, conf, "")
detailLogInfo("IPAM configuration not found, skip DEL for IPAM", args, conf, "")
} else {
detailLog("Running IPAM plugin DEL", args, conf, "")
detailLogInfo("Running IPAM plugin DEL", args, conf, "")
err = engine.RunIPAMPluginDel(conf.IPAM.Type, args.StdinData)
if err != nil {
return err
detailLogError("Error running IPAM plugin DEL: "+err.Error(), args, conf, "")
}
}

detailLog("Deleting container interface", args, conf, "")
detailLogInfo("Deleting container interface", args, conf, "")
return engine.DeleteVeth(args.Netns, args.IfName)
}

0 comments on commit db58647

Please sign in to comment.