Skip to content

Commit cef22f9

Browse files
committed
sample: add STATUS and GC impls
Signed-off-by: Casey Callendrello <[email protected]>
1 parent a3263ad commit cef22f9

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

plugins/sample/main.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/containernetworking/cni/pkg/types"
2525
current "github.com/containernetworking/cni/pkg/types/100"
2626
"github.com/containernetworking/cni/pkg/version"
27+
"github.com/containernetworking/plugins/pkg/ipam"
2728
bv "github.com/containernetworking/plugins/pkg/utils/buildversion"
2829
)
2930

@@ -150,10 +151,65 @@ func cmdDel(args *skel.CmdArgs) error {
150151

151152
func main() {
152153
// replace TODO with your plugin name
153-
skel.PluginMain(cmdAdd, cmdCheck, cmdDel, version.All, bv.BuildString("TODO"))
154+
skel.PluginMainFuncs(skel.CNIFuncs{
155+
Add: cmdAdd,
156+
Del: cmdDel,
157+
Check: cmdCheck,
158+
Status: cmdStatus,
159+
GC: cmdGc,
160+
}, version.All, bv.BuildString("a sample plugin"))
154161
}
155162

156163
func cmdCheck(_ *skel.CmdArgs) error {
157164
// TODO: implement
158165
return fmt.Errorf("not implemented")
159166
}
167+
168+
// cmdStatus implements the STATUS command, which indicates whether or not
169+
// this plugin is able to accept ADD requests.
170+
//
171+
// If the plugin has external dependencies, such as a daemon
172+
// or chained ipam plugin, it should determine their status. If all is well,
173+
// and an ADD can be successfully processed, return nil
174+
func cmdStatus(args *skel.CmdArgs) error {
175+
conf, err := parseConfig(args.StdinData)
176+
if err != nil {
177+
return err
178+
}
179+
_ = conf
180+
181+
// If this plugins delegates IPAM, ensure that IPAM is also running
182+
if err := ipam.ExecStatus(conf.IPAM.Type, args.StdinData); err != nil {
183+
return err
184+
}
185+
186+
// TODO: implement STATUS here
187+
// e.g. querying an external deamon, or delegating STATUS to an IPAM plugin
188+
189+
return nil
190+
}
191+
192+
// cmdGc implements the GC command, which the runtime uses to indicate
193+
// the currently valid set of attachments for a given configuration; any
194+
// resources not owned by these containers may be deleted.
195+
//
196+
// The set of valid attachments is provided in the variable 'cni.dev/valid-attachments'.
197+
// All other attachments should be considered invalid.
198+
func cmdGc(args *skel.CmdArgs) error {
199+
conf, err := parseConfig(args.StdinData)
200+
if err != nil {
201+
return err
202+
}
203+
_ = conf
204+
205+
// If this plugin delegates IPAM, then GC must be passed
206+
if err := ipam.ExecGC(conf.IPAM.Type, args.StdinData); err != nil {
207+
return err
208+
}
209+
210+
// TODO: implement GC here
211+
// e.g clean up any stale resources, such as iptables rules.
212+
// You can assume that anything attached to a network namespace is gone.
213+
214+
return nil
215+
}

0 commit comments

Comments
 (0)