github.com/looshlee/cilium@v1.6.12/plugins/cilium-cni/chaining/api/api.go (about) 1 // Copyright 2019 Authors of Cilium 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 api 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/cilium/cilium/pkg/client" 22 "github.com/cilium/cilium/pkg/lock" 23 "github.com/cilium/cilium/plugins/cilium-cni/types" 24 25 "github.com/containernetworking/cni/pkg/skel" 26 cniTypesVer "github.com/containernetworking/cni/pkg/types/current" 27 "github.com/sirupsen/logrus" 28 ) 29 30 var ( 31 chainingPlugins = map[string]ChainingPlugin{} 32 mutex lock.RWMutex 33 ) 34 35 const ( 36 // DefaultConfigName is the name used by default in the standard CNI 37 // configuration 38 DefaultConfigName = "cilium" 39 ) 40 41 // PluginContext is the context given to chaining plugins 42 type PluginContext struct { 43 Logger *logrus.Entry 44 Args *skel.CmdArgs 45 CniArgs types.ArgsSpec 46 NetConf *types.NetConf 47 Client *client.Client 48 } 49 50 // ChainingPlugin is the interface each chaining plugin must implement 51 type ChainingPlugin interface { 52 // Add is called on CNI ADD. It is given the plugin context from the 53 // previous plugin. It must return a CNI result or an error. 54 Add(ctx context.Context, pluginContext PluginContext) (res *cniTypesVer.Result, err error) 55 56 // ImplementsAdd returns true if the chaining plugin implements its own 57 // add logic 58 ImplementsAdd() bool 59 60 // Delete is called on CNI DELETE. It is given the plugin context from 61 // the previous plugin. 62 Delete(ctx context.Context, pluginContext PluginContext) (err error) 63 64 // ImplementsDelete returns true if the chaining plugin implements its 65 // own delete logic 66 ImplementsDelete() bool 67 } 68 69 // Register is called by chaining plugins to register themselves. After 70 // Register(), the plugin can be found with Lookup(). 71 func Register(name string, p ChainingPlugin) error { 72 mutex.Lock() 73 defer mutex.Unlock() 74 75 if name == DefaultConfigName { 76 return fmt.Errorf("invalid chain name. '%s' is reserved", DefaultConfigName) 77 } 78 79 if _, ok := chainingPlugins[name]; ok { 80 return fmt.Errorf("chaining plugin with name %s already exists", name) 81 } 82 83 chainingPlugins[name] = p 84 85 return nil 86 } 87 88 // Lookup searches for a chaining plugin with a given name and returns it 89 func Lookup(name string) ChainingPlugin { 90 mutex.RLock() 91 defer mutex.RUnlock() 92 93 return chainingPlugins[name] 94 }