Skip to content

Commit

Permalink
cni add with retry
Browse files Browse the repository at this point in the history
  • Loading branch information
DuodenumL committed Sep 22, 2022
1 parent cb48fad commit c2f5e9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/cnicmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func runCNI(handler handler.Handler) func(*cli.Context) error {
}

log.Infof("[hook] docker-cni running: %+v", cniToolConfig)
log.Infof("[hook] state: %v", string(stateBuf))
err = cni.Run(cniToolConfig)
return errors.WithStack(err)
}
Expand Down
34 changes: 26 additions & 8 deletions cni/cnitool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"github.com/containernetworking/cni/libcni"
"github.com/containernetworking/cni/pkg/types"
log "github.com/sirupsen/logrus"
)

// Protocol parameters are passed to the plugins via OS environment variables.
const (
CmdAdd = "add"
CmdCheck = "check"
CmdDel = "del"
CmdAdd = "add"
CmdCheck = "check"
CmdDel = "del"
CmdAddMaxRetryTimes = 5
CmdAddRetryInterval = time.Second
)

// CNIToolConfig .
Expand Down Expand Up @@ -131,6 +136,23 @@ func LoadConfList(dir string, handler func([]byte) ([]byte, error)) (*libcni.Net
return libcni.ConfListFromConf(singleConf)
}

func AddNetworkListWithRetry(ctx context.Context, cniNet *libcni.CNIConfig, netconf *libcni.NetworkConfigList, rt *libcni.RuntimeConf) error {
var result types.Result
var err error
for i := 0; i < CmdAddMaxRetryTimes; i++ {
result, err = cniNet.AddNetworkList(ctx, netconf, rt)
if result != nil {
_ = result.Print()
}
if err == nil {
return nil
}
log.Errorf("[AddNetworkListWithRetry] add network list failed, retry after %v, err: %v", CmdAddRetryInterval, err)
time.Sleep(CmdAddRetryInterval)
}
return err
}

// Run .
func Run(config CNIToolConfig) error {
netconf, err := LoadConfList(config.NetConfPath, config.Handler)
Expand Down Expand Up @@ -165,11 +187,7 @@ func Run(config CNIToolConfig) error {

switch config.Cmd {
case CmdAdd:
result, err := cninet.AddNetworkList(context.TODO(), netconf, rt)
if result != nil {
_ = result.Print()
}
return err
return AddNetworkListWithRetry(context.Background(), cninet, netconf, rt)
case CmdCheck:
return cninet.CheckNetworkList(context.TODO(), netconf, rt)
case CmdDel:
Expand Down

0 comments on commit c2f5e9c

Please sign in to comment.