cni配置存储成json格式,由kubelet读取json配置并在调用cni插件时传入,其对应的结构体:
// PluginConf is whatever you expect your configuration json to be. This is whatever
// is passed in on stdin. Your plugin may wish to expose its functionality via
// runtime args, see CONVENTIONS.md in the CNI spec.
type PluginConf struct {
// This embeds the standard NetConf structure which allows your plugin
// to more easily parse standard fields like Name, Type, CNIVersion,
// and PrevResult.
types.NetConf
RuntimeConfig *struct {
SampleConfig map[string]interface{} `json:"sample"`
} `json:"runtimeConfig"`
// 插件自定义的属性
// Add plugin-specific flags here
MyAwesomeFlag bool `json:"myAwesomeFlag"`
AnotherAwesomeArg string `json:"anotherAwesomeArg"`
}
cni规范规定插件要实现cmdAdd,cmdDel,cmdCheck语义:
- cmdAdd
// cmdAdd 用于分配网络资源
// cmdAdd is called for ADD requests
func cmdAdd(args *skel.CmdArgs) error {
// 从参数中获取配置PluginConf结构体
// func parseConfig(stdin []byte)功能是将字节数组转换为PluginConf结构体
conf, err := parseConfig(args.StdinData)
if err != nil {
return err
}
// A plugin can be either an "originating" plugin or a "chained" plugin.
// Originating plugins perform initial sandbox setup and do not require
// any result from a previous plugin in the chain. A chained plugin
// modifies sandbox configuration that was previously set up by an
// originating plugin and may optionally require a PrevResult from
// earlier plugins in the chain.
// 插件可以链式调用,即给一个容器分配网络资源可以调用一揽子插件,比如pluginA -> pluginB -> pluginC
// pluginA 叫做originating plugin; pluginC 叫做chained plugin
// pluginC 需要将pluginA的输出结果当作输入,即conf.PrevResult
// START chained plugin code
if conf.PrevResult == nil {
return fmt.Errorf("must be called as chained plugin")
}
// Convert the PrevResult to a concrete Result type that can be modified.
prevResult, err := current.GetResult(conf.PrevResult)
if err != nil {
return fmt.Errorf("failed to convert prevResult: %v", err)
}
if len(prevResult.IPs) == 0 {
return fmt.Errorf("got no container IPs")
}
// Pass the prevResult through this plugin to the next one
result := prevResult
// END chained plugin code
// START originating plugin code
// if conf.PrevResult != nil {
// return fmt.Errorf("must be called as the first plugin")
// }
// Generate some fake container IPs and add to the result
// result := ¤t.Result{CNIVersion: current.ImplementedSpecVersion}
// result.Interfaces = []*current.Interface{
// {
// Name: "intf0",
// Sandbox: args.Netns,
// Mac: "00:11:22:33:44:55",
// },
// }
// result.IPs = []*current.IPConfig{
// {
// Address: "1.2.3.4/24",
// Gateway: "1.2.3.1",
// // Interface is an index into the Interfaces array
// // of the Interface element this IP applies to
// Interface: current.Int(0),
// }
// }
// END originating plugin code
// 这里实现cmdAdd自定义逻辑
// ....
// Pass through the result for the next plugin
// 将结果输出到标准stdout
return types.PrintResult(result, conf.CNIVersion)
}
- cmdDel
cmdDel类似,用于回收网络资源;默认无输出即成功,出错返回错误
// cmdDel is called for DELETE requests
func cmdDel(args *skel.CmdArgs) error {
conf, err := parseConfig(args.StdinData)
if err != nil {
return err
}
_ = conf
// Do your delete here
return nil
}
- cmdCheck
cmdCheck用于检查cmdAdd分配的网络资源操作是否成功
func cmdCheck(args *skel.CmdArgs) error {
// TODO: implement
return fmt.Errorf("not implemented")
}
更多参考CNI Specification


![kubernetes CNI实现规范 [source code] kubernetes CNI实现规范 [source code]](http://www.mshxw.com/aiimages/31/946924.png)
