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 mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/
18
19 import (
20 "encoding/jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/"
21 "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/"
22
23 "github.com/containernetworking/cni/pkg/skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/"
24 "github.com/containernetworking/cni/pkg/typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/"
25 currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/ "github.com/containernetworking/cni/pkg/types/100scip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/"
26 "github.com/containernetworking/cni/pkg/versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/"
27 "github.com/containernetworking/plugins/pkg/ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/"
28 bvscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/ "github.com/containernetworking/plugins/pkg/utils/buildversionscip-go gomod github.com/containernetworking/plugins . `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 PluginConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/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 typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#NetConf.
39
40 RuntimeConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#RuntimeConfig. *struct {
41 SampleConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#$anon_ce9ae4257df5756d#SampleConfig. map[string]interface{} `json:"sample"`
42 } `json:"runtimeConfig"`
43
44 // Add plugin-specifc flags here
45 MyAwesomeFlagscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#MyAwesomeFlag. bool `json:"myAwesomeFlag"`
46 AnotherAwesomeArgscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#AnotherAwesomeArg. string `json:"anotherAwesomeArg"`
47 }
48
49 // parseConfig parses the supplied configuration (and prevResult) from stdin.
50 func parseConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/parseConfig().(stdin []byte) (*PluginConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#, error) {
51 conf := PluginConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#{}
52
53 if err := jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/Unmarshal().(stdin, &conf); err != nil {
54 return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 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 := versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/.ParsePrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/ParsePrevResult().(&conf.NetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#NetConf.); err != nil {
62 return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("could not parse prevResult: %v", err)
63 }
64 // End previous result parsing
65
66 // Do any validation here
67 if conf.AnotherAwesomeArgscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/PluginConf#AnotherAwesomeArg. == "" {
68 return nil, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("anotherAwesomeArg must be specified")
69 }
70
71 return &conf, nil
72 }
73
74 // cmdAdd is called for ADD requests
75 func cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdAdd().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error {
76 conf, err := parseConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/parseConfig().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#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.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult. == nil {
90 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 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 := currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.GetResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/GetResult().(conf.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult.)
95 if err != nil {
96 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to convert prevResult: %v", err)
97 }
98
99 if len(prevResult.IPsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#IPs.) == 0 {
100 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 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 typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.PrintResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PrintResult().(result, conf.CNIVersionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#CNIVersion.)
137 }
138
139 // cmdDel is called for DELETE requests
140 func cmdDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdDel().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error {
141 conf, err := parseConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/parseConfig().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#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 mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/main().() {
153 // replace TODO with your plugin name
154 skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.PluginMainFuncsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/PluginMainFuncs().(skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CNIFuncsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#{
155 Addscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Add.: cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdAdd().,
156 Checkscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Check.: cmdCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdCheck().,
157 Delscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Del.: cmdDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdDel().,
158 Statusscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CNIFuncs#Status.: cmdStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdStatus().,
159 /* FIXME GC */
160 }, versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/.Allscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/All., bvscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/.BuildStringscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/utils/buildversion`/BuildString().("TODO"))
161 }
162
163 func cmdCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdCheck().(_ *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error {
164 // TODO: implement
165 return fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 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 cmdStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/cmdStatus().(args *skelscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/.CmdArgsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#) error {
175 conf, err := parseConfigscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/sample`/parseConfig().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#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 := ipamscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/.ExecStatusscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ipam`/ExecStatus().(conf.IPAMscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#IPAM..Typescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/IPAM#Type., args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#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 }