Debian Codegraph @

plugins/meta/vrf/main.go

raw symbols SCIP
1 // Copyright 2020 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 package mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/
16
17 import (
18 "encoding/jsonscip-go gomod github.com/golang/go/src go1.24.2 `encoding/json`/"
19 "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/"
20
21 "github.com/vishvananda/netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/"
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/nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/"
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 // VRFNetConf represents the vrf configuration.
32 type VRFNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf# struct {
33 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/meta/vrf`/VRFNetConf#NetConf.
34
35 // VRFName is the name of the vrf to add the interface to.
36 VRFNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#VRFName. string `json:"vrfname"`
37 // Table is the optional name of the routing table set for the vrf
38 Tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#Table. uint32 `json:"table"`
39 }
40
41 func mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/main().() {
42 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#{
43 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/meta/vrf`/cmdAdd().,
44 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/meta/vrf`/cmdCheck().,
45 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/meta/vrf`/cmdDel().,
46 /* FIXME GC */
47 /* FIXME Status */
48 }, versionscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/.VersionsStartingFromscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/version`/VersionsStartingFrom().("0.3.1"), 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().("vrf"))
49 }
50
51 func cmdAddscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/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 {
52 conf, result, err := parseConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/parseConf().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.)
53 if err != nil {
54 return err
55 }
56
57 if conf.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult. == nil {
58 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().("missing prevResult from earlier plugin")
59 }
60
61 err = nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.WithNetNSPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/WithNetNSPath().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns., func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error {
62 vrf, err := findVRFscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/findVRF().(conf.VRFNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#VRFName.)
63
64 // If the user set a tableid and the vrf is already in the namespace
65 // we check if the tableid is the same one already assigned to the vrf.
66 if err == nil && conf.Tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#Table. != 0 && vrf.Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#Table. != conf.Tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#Table. {
67 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().("VRF %s already exist with different routing table %d", conf.VRFNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#VRFName., vrf.Tablescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Vrf#Table.)
68 }
69
70 if _, ok := err.(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkNotFoundErrorscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkNotFoundError#); ok {
71 vrf, err = createVRFscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/createVRF().(conf.VRFNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#VRFName., conf.Tablescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#Table.)
72 }
73
74 if err != nil {
75 return err
76 }
77
78 err = addInterfacescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/addInterface().(vrf, args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.)
79 if err != nil {
80 return err
81 }
82 return nil
83 })
84 if err != nil {
85 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().("cmdAdd failed: %v", err)
86 }
87
88 if result == nil {
89 result = &currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#{}
90 }
91
92 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.)
93 }
94
95 func cmdDelscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/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 {
96 conf, _, err := parseConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/parseConf().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.)
97 if err != nil {
98 return err
99 }
100 err = nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.WithNetNSPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/WithNetNSPath().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns., func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error {
101 vrf, err := findVRFscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/findVRF().(conf.VRFNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#VRFName.)
102 if _, ok := err.(netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkNotFoundErrorscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkNotFoundError#); ok {
103 return nil
104 }
105
106 if err != nil {
107 return err
108 }
109
110 err = resetMasterscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/resetMaster().(args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName.)
111 if err != nil {
112 return err
113 }
114
115 interfaces, err := assignedInterfacesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/assignedInterfaces().(vrf)
116 if err != nil {
117 return err
118 }
119
120 // Meaning, we are deleting the last interface assigned to the VRF
121 if len(interfaces) == 0 {
122 err = netlinkscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/.LinkDelscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkDel().(vrf)
123 if err != nil {
124 return err
125 }
126 }
127 return nil
128 })
129 if err != nil {
130 // if NetNs is passed down by the Cloud Orchestration Engine, or if it called multiple times
131 // so don't return an error if the device is already removed.
132 // https://github.com/kubernetes/kubernetes/issues/43014#issuecomment-287164444
133 _, ok := err.(nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NSPathNotExistErrscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NSPathNotExistErr#)
134 if ok {
135 return nil
136 }
137 return err
138 }
139
140 if err != nil {
141 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().("cmdDel failed: %v", err)
142 }
143 return nil
144 }
145
146 func cmdCheckscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/cmdCheck().(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 {
147 conf, _, err := parseConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/parseConf().(args.StdinDatascip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#StdinData.)
148 if err != nil {
149 return err
150 }
151
152 // Ensure we have previous result.
153 if conf.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult. == nil {
154 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().("missing prevResult from earlier plugin")
155 }
156
157 err = nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.WithNetNSPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/WithNetNSPath().(args.Netnsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#Netns., func(_ nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) error {
158 vrf, err := findVRFscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/findVRF().(conf.VRFNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#VRFName.)
159 if err != nil {
160 return err
161 }
162 vrfInterfaces, err := assignedInterfacesscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/assignedInterfaces().(vrf)
163 if err != nil {
164 return err
165 }
166
167 found := false
168 for _, intf := range vrfInterfaces {
169 if intf.Attrsscip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/Link#Attrs().().Namescip-go gomod github.com/vishvananda/netlink v1.3.1 `github.com/vishvananda/netlink`/LinkAttrs#Name. == args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName. {
170 found = true
171 break
172 }
173 }
174 if !found {
175 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 find %s associated to vrf %s", args.IfNamescip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/skel`/CmdArgs#IfName., conf.VRFNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#VRFName.)
176 }
177 return nil
178 })
179 if err != nil {
180 return err
181 }
182
183 return nil
184 }
185
186 func parseConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/parseConf().(data []byte) (*VRFNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#, *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#, error) {
187 conf := VRFNetConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#{}
188 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().(data, &conf); err != nil {
189 return nil, 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 load netconf: %v", err)
190 }
191
192 if conf.VRFNamescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/vrf`/VRFNetConf#VRFName. == "" {
193 return nil, 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().("configuration is expected to have a valid vrf name")
194 }
195
196 if conf.RawPrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#RawPrevResult. == nil {
197 // return early if there was no previous result, which is allowed for DEL calls
198 return &conf, &currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#{}, nil
199 }
200
201 // Parse previous result.
202 var result *currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.Resultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/Result#
203 var err error
204 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/meta/vrf`/VRFNetConf#NetConf.); err != nil {
205 return nil, 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)
206 }
207
208 result, err = currentscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/.NewResultFromResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types/100`/NewResultFromResult().(conf.PrevResultscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/PluginConf#PrevResult.)
209 if err != nil {
210 return nil, 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 convert result to current version: %v", err)
211 }
212
213 return &conf, result, nil
214 }