Skip to content

Commit e5af9cf

Browse files
committed
cnitool: refactor, add GC, convert to workspace
This refactors cnitool to be a modern cobra-style program. It also adds STATUS and GC support. Finally it uses go workspaces to allow for separate `go.mod` files. Signed-off-by: Casey Callendrello <[email protected]>
1 parent 951d8d0 commit e5af9cf

File tree

11 files changed

+420
-147
lines changed

11 files changed

+420
-147
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ gopath/
44
*.sw[ponm]
55
.vagrant
66
release-*
7+
cnitool/cnitool

cnitool/LICENSE

Whitespace-only changes.

cnitool/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,11 @@ And clean up:
7575
sudo CNI_PATH=./bin cnitool del myptp /var/run/netns/testing
7676
sudo ip netns del testing
7777
```
78+
79+
cnitool also supports the GC command, which takes a list of still-valid attachments
80+
and removes all others. Since cnitool generates the container ID based on the
81+
path to the network namespace, this is the parameter
82+
83+
```bash
84+
sudo CNI_PATH=./bin cnitool gc myptp /var/run/netns/keep-1 /var/run/netns/keep-2
85+
```

cnitool/cmd/add.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// addCmd represents the add command
14+
var addCmd = &cobra.Command{
15+
Use: "add",
16+
Short: "A brief description of your command",
17+
Long: `A longer description that spans multiple lines and likely contains examples
18+
and usage of using your command. For example:
19+
20+
Cobra is a CLI library for Go that empowers applications.
21+
This application is a tool to generate the needed files
22+
to quickly create a Cobra application.`,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
fmt.Println("add called")
25+
},
26+
}
27+
28+
func init() {
29+
rootCmd.AddCommand(addCmd)
30+
31+
// Here you will define your flags and configuration settings.
32+
33+
// Cobra supports Persistent Flags which will work for this command
34+
// and all subcommands, e.g.:
35+
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
36+
37+
// Cobra supports local flags which will only run when this command
38+
// is called directly, e.g.:
39+
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
40+
}

cnitool/cmd/root.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"os"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
14+
15+
// rootCmd represents the base command when called without any subcommands
16+
var rootCmd = &cobra.Command{
17+
Use: "cnitool",
18+
Short: "A brief description of your application",
19+
Long: `A longer description that spans multiple lines and likely contains
20+
examples and usage of using your application. For example:
21+
22+
Cobra is a CLI library for Go that empowers applications.
23+
This application is a tool to generate the needed files
24+
to quickly create a Cobra application.`,
25+
// Uncomment the following line if your bare application
26+
// has an action associated with it:
27+
// Run: func(cmd *cobra.Command, args []string) { },
28+
}
29+
30+
// Execute adds all child commands to the root command and sets flags appropriately.
31+
// This is called by main.main(). It only needs to happen once to the rootCmd.
32+
func Execute() {
33+
err := rootCmd.Execute()
34+
if err != nil {
35+
os.Exit(1)
36+
}
37+
}
38+
39+
func init() {
40+
// Here you will define your flags and configuration settings.
41+
// Cobra supports persistent flags, which, if defined here,
42+
// will be global for your application.
43+
44+
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cnitool.yaml)")
45+
46+
// Cobra also supports local flags, which will only run
47+
// when this action is called directly.
48+
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
49+
}
50+
51+

cnitool/cnitool.go

Lines changed: 0 additions & 147 deletions
This file was deleted.

cnitool/go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/containernetworking/cni/cnitool
2+
3+
go 1.20
4+
5+
require (
6+
github.com/containernetworking/cni v0.0.0-00010101000000-000000000000
7+
github.com/spf13/cobra v1.8.0
8+
)
9+
10+
require (
11+
github.com/Masterminds/semver/v3 v3.2.1 // indirect
12+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
13+
github.com/spf13/pflag v1.0.5 // indirect
14+
)
15+
16+
replace github.com/containernetworking/cni => ../

cnitool/go.sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
2+
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
3+
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
4+
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
5+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
6+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
7+
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
8+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
9+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
10+
github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs=
11+
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
12+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
13+
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
14+
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
15+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
16+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
17+
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
18+
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
19+
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
20+
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
21+
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
22+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
23+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
24+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)