Skip to content

Commit

Permalink
cnitool CNI 1.1.0 support
Browse files Browse the repository at this point in the history
This change make cnitool CNI v1.1.0 support. This also addresses
to fix error crash in case of GC with empty attachments.

Signed-off-by: Tomofumi Hayashi <[email protected]>
  • Loading branch information
s1061123 committed Apr 3, 2024
1 parent 83287db commit 0036b62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
Binary file added cnitool/cnitool
Binary file not shown.
23 changes: 16 additions & 7 deletions cnitool/cnitool.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ const (

DefaultNetDir = "/etc/cni/net.d"

CmdAdd = "add"
CmdCheck = "check"
CmdDel = "del"
CmdAdd = "add"
CmdCheck = "check"
CmdDel = "del"
CmdGC = "gc"
CmdStatus = "status"
)

func parseArgs(args string) ([][2]string, error) {
Expand Down Expand Up @@ -125,16 +127,23 @@ func main() {
exit(err)
case CmdDel:
exit(cninet.DelNetworkList(context.TODO(), netconf, rt))
case CmdGC:
// Currently just invoke GC without args, hence all network interface should be GC'ed!
exit(cninet.GCNetworkList(context.TODO(), netconf, nil))
case CmdStatus:
exit(cninet.GetStatusNetworkList(context.TODO(), netconf))
}
}

func usage() {
exe := filepath.Base(os.Args[0])

fmt.Fprintf(os.Stderr, "%s: Add, check, or remove network interfaces from a network namespace\n", exe)
fmt.Fprintf(os.Stderr, " %s add <net> <netns>\n", exe)
fmt.Fprintf(os.Stderr, " %s check <net> <netns>\n", exe)
fmt.Fprintf(os.Stderr, " %s del <net> <netns>\n", exe)
fmt.Fprintf(os.Stderr, "%s: Add, check, remove, gc or status network interfaces from a network namespace\n", exe)
fmt.Fprintf(os.Stderr, " %s add <net> <netns>\n", exe)
fmt.Fprintf(os.Stderr, " %s check <net> <netns>\n", exe)
fmt.Fprintf(os.Stderr, " %s del <net> <netns>\n", exe)
fmt.Fprintf(os.Stderr, " %s gc <net> <netns>\n", exe)
fmt.Fprintf(os.Stderr, " %s status <net> <netns>\n", exe)
os.Exit(1)
}

Expand Down
18 changes: 12 additions & 6 deletions libcni/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,12 @@ func (c *CNIConfig) GCNetworkList(ctx context.Context, list *NetworkConfigList,
return nil
}

validAttachments := make(map[types.GCAttachment]interface{}, len(args.ValidAttachments))
for _, a := range args.ValidAttachments {
validAttachments[a] = nil
var validAttachments map[types.GCAttachment]interface{}
if args != nil {
validAttachments = make(map[types.GCAttachment]interface{}, len(args.ValidAttachments))
for _, a := range args.ValidAttachments {
validAttachments[a] = nil
}
}

var errs []error
Expand Down Expand Up @@ -800,10 +803,13 @@ func (c *CNIConfig) GCNetworkList(ctx context.Context, list *NetworkConfigList,
// now, if the version supports it, issue a GC
if gt, _ := version.GreaterThanOrEqualTo(list.CNIVersion, "1.1.0"); gt {
inject := map[string]interface{}{
"name": list.Name,
"cniVersion": list.CNIVersion,
"cni.dev/valid-attachments": args.ValidAttachments,
"name": list.Name,
"cniVersion": list.CNIVersion,
}
if args != nil {
inject["cni.dev/valid-attachments"] = args.ValidAttachments
}

for _, plugin := range list.Plugins {
// build config here
pluginConfig, err := InjectConf(plugin, inject)
Expand Down

0 comments on commit 0036b62

Please sign in to comment.