Skip to content

Commit 0036b62

Browse files
committed
cnitool CNI 1.1.0 support
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]>
1 parent 83287db commit 0036b62

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

cnitool/cnitool

3.45 MB
Binary file not shown.

cnitool/cnitool.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ const (
3636

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

39-
CmdAdd = "add"
40-
CmdCheck = "check"
41-
CmdDel = "del"
39+
CmdAdd = "add"
40+
CmdCheck = "check"
41+
CmdDel = "del"
42+
CmdGC = "gc"
43+
CmdStatus = "status"
4244
)
4345

4446
func parseArgs(args string) ([][2]string, error) {
@@ -125,16 +127,23 @@ func main() {
125127
exit(err)
126128
case CmdDel:
127129
exit(cninet.DelNetworkList(context.TODO(), netconf, rt))
130+
case CmdGC:
131+
// Currently just invoke GC without args, hence all network interface should be GC'ed!
132+
exit(cninet.GCNetworkList(context.TODO(), netconf, nil))
133+
case CmdStatus:
134+
exit(cninet.GetStatusNetworkList(context.TODO(), netconf))
128135
}
129136
}
130137

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

134-
fmt.Fprintf(os.Stderr, "%s: Add, check, or remove network interfaces from a network namespace\n", exe)
135-
fmt.Fprintf(os.Stderr, " %s add <net> <netns>\n", exe)
136-
fmt.Fprintf(os.Stderr, " %s check <net> <netns>\n", exe)
137-
fmt.Fprintf(os.Stderr, " %s del <net> <netns>\n", exe)
141+
fmt.Fprintf(os.Stderr, "%s: Add, check, remove, gc or status network interfaces from a network namespace\n", exe)
142+
fmt.Fprintf(os.Stderr, " %s add <net> <netns>\n", exe)
143+
fmt.Fprintf(os.Stderr, " %s check <net> <netns>\n", exe)
144+
fmt.Fprintf(os.Stderr, " %s del <net> <netns>\n", exe)
145+
fmt.Fprintf(os.Stderr, " %s gc <net> <netns>\n", exe)
146+
fmt.Fprintf(os.Stderr, " %s status <net> <netns>\n", exe)
138147
os.Exit(1)
139148
}
140149

libcni/api.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,12 @@ func (c *CNIConfig) GCNetworkList(ctx context.Context, list *NetworkConfigList,
765765
return nil
766766
}
767767

768-
validAttachments := make(map[types.GCAttachment]interface{}, len(args.ValidAttachments))
769-
for _, a := range args.ValidAttachments {
770-
validAttachments[a] = nil
768+
var validAttachments map[types.GCAttachment]interface{}
769+
if args != nil {
770+
validAttachments = make(map[types.GCAttachment]interface{}, len(args.ValidAttachments))
771+
for _, a := range args.ValidAttachments {
772+
validAttachments[a] = nil
773+
}
771774
}
772775

773776
var errs []error
@@ -800,10 +803,13 @@ func (c *CNIConfig) GCNetworkList(ctx context.Context, list *NetworkConfigList,
800803
// now, if the version supports it, issue a GC
801804
if gt, _ := version.GreaterThanOrEqualTo(list.CNIVersion, "1.1.0"); gt {
802805
inject := map[string]interface{}{
803-
"name": list.Name,
804-
"cniVersion": list.CNIVersion,
805-
"cni.dev/valid-attachments": args.ValidAttachments,
806+
"name": list.Name,
807+
"cniVersion": list.CNIVersion,
808+
}
809+
if args != nil {
810+
inject["cni.dev/valid-attachments"] = args.ValidAttachments
806811
}
812+
807813
for _, plugin := range list.Plugins {
808814
// build config here
809815
pluginConfig, err := InjectConf(plugin, inject)

0 commit comments

Comments
 (0)