Debian Codegraph @

plugins/sample/main.go

raw symbols SCIP
1 // Copyright 2017 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // This is a sample chained plugin that supports multiple CNI versions. It
16 // parses prevResult according to the cniVersion
17 package main
18
19 import (
20 "encoding/json"
21 "fmt"
22
23 "github.com/containernetworking/cni/pkg/skel"
24 "github.com/containernetworking/cni/pkg/types"
25 current "github.com/containernetworking/cni/pkg/types/100"
26 "github.com/containernetworking/cni/pkg/version"
27 "github.com/containernetworking/plugins/pkg/ipam"
28 bv "github.com/containernetworking/plugins/pkg/utils/buildversion"
29 )
30
31 // PluginConf is whatever you expect your configuration json to be. This is whatever
32 // is passed in on stdin. Your plugin may wish to expose its functionality via
33 // runtime args, see CONVENTIONS.md in the CNI spec.
34 type PluginConf struct {
35 // This embeds the standard NetConf structure which allows your plugin
36 // to more easily parse standard fields like Name, Type, CNIVersion,
37 // and PrevResult.
38 types.NetConf
39
40 RuntimeConfig *struct {
41 SampleConfig map[string]interface{} `json:"sample"`
42 } `json:"runtimeConfig"`
43
44 // Add plugin-specifc flags here
45 MyAwesomeFlag bool `json:"myAwesomeFlag"`
46 AnotherAwesomeArg string `json:"anotherAwesomeArg"`
47 }
48
49 // parseConfig parses the supplied configuration (and prevResult) from stdin.
50 func parseConfig(stdin []byte) (*PluginConf, error) {
51 conf := PluginConf{}
52
53 if err := json.Unmarshal(stdin, &conf); err != nil {
54 return nil, fmt.Errorf("failed to parse network configuration: %v", err)
55 }
56
57 // Parse previous result. This will parse, validate, and place the
58 // previous result object into conf.PrevResult. If you need to modify
59 // or inspect the PrevResult you will need to convert it to a concrete
60 // versioned Result struct.
61 if err := version.ParsePrevResult(&conf.NetConf); err != nil {
62 return nil, fmt.Errorf("could not parse prevResult: %v", err)
63 }
64 // End previous result parsing
65
66 // Do any validation here
67 if conf.AnotherAwesomeArg == "" {
68 return nil, fmt.Errorf("anotherAwesomeArg must be specified")
69 }
70
71 return &conf, nil
72 }
73
74 // cmdAdd is called for ADD requests
75 func cmdAdd(args *skel.CmdArgs) error {
76 conf, err := parseConfig(args.StdinData)
77 if err != nil {
78 return err
79 }
80
81 // A plugin can be either an "originating" plugin or a "chained" plugin.
82 // Originating plugins perform initial sandbox setup and do not require
83 // any result from a previous plugin in the chain. A chained plugin
84 // modifies sandbox configuration that was previously set up by an
85 // originating plugin and may optionally require a PrevResult from
86 // earlier plugins in the chain.
87
88 // START chained plugin code
89 if conf.PrevResult == nil {
90 return fmt.Errorf("must be called as chained plugin")
91 }
92
93 // Convert the PrevResult to a concrete Result type that can be modified.
94 prevResult, err := current.GetResult(conf.PrevResult)
95 if err != nil {
96 return fmt.Errorf("failed to convert prevResult: %v", err)
97 }
98
99 if len(prevResult.IPs) == 0 {
100 return fmt.Errorf("got no container IPs")
101 }
102
103 // Pass the prevResult through this plugin to the next one
104 result := prevResult
105
106 // END chained plugin code
107
108 // START originating plugin code
109 // if conf.PrevResult != nil {
110 // return fmt.Errorf("must be called as the first plugin")
111 // }
112
113 // Generate some fake container IPs and add to the result
114 // result := &current.Result{CNIVersion: current.ImplementedSpecVersion}
115 // result.Interfaces = []*current.Interface{
116 // {
117 // Name: "intf0",
118 // Sandbox: args.Netns,
119 // Mac: "00:11:22:33:44:55",
120 // },
121 // }
122 // result.IPs = []*current.IPConfig{
123 // {
124 // Address: "1.2.3.4/24",
125 // Gateway: "1.2.3.1",
126 // // Interface is an index into the Interfaces array
127 // // of the Interface element this IP applies to
128 // Interface: current.Int(0),
129 // }
130 // }
131 // END originating plugin code
132
133 // Implement your plugin here
134
135 // Pass through the result for the next plugin
136 return types.PrintResult(result, conf.CNIVersion)
137 }
138
139 // cmdDel is called for DELETE requests
140 func cmdDel(args *skel.CmdArgs) error {
141 conf, err := parseConfig(args.StdinData)
142 if err != nil {
143 return err
144 }
145 _ = conf
146
147 // Do your delete here
148
149 return nil
150 }
151
152 func main() {
153 // replace TODO with your plugin name
154 skel.PluginMainFuncs(skel.CNIFuncs{
155 Add: cmdAdd,
156 Check: cmdCheck,
157 Del: cmdDel,
158 Status: cmdStatus,
159 /* FIXME GC */
160 }, version.All, bv.BuildString("TODO"))
161 }
162
163 func cmdCheck(_ *skel.CmdArgs) error {
164 // TODO: implement
165 return fmt.Errorf("not implemented")
166 }
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 }