go.ligato.io/vpp-agent/v3@v3.5.0/examples/tutorials/101_custom-vppagent/main.go (about) 1 package main 2 3 import ( 4 "go.ligato.io/cn-infra/v2/agent" 5 "go.ligato.io/cn-infra/v2/datasync" 6 "go.ligato.io/cn-infra/v2/datasync/kvdbsync" 7 "go.ligato.io/cn-infra/v2/datasync/kvdbsync/local" 8 "go.ligato.io/cn-infra/v2/datasync/resync" 9 "go.ligato.io/cn-infra/v2/db/keyval/etcd" 10 "go.ligato.io/cn-infra/v2/logging" 11 "go.ligato.io/cn-infra/v2/logging/logmanager" 12 13 "go.ligato.io/vpp-agent/v3/cmd/vpp-agent/app" 14 "go.ligato.io/vpp-agent/v3/plugins/orchestrator" 15 ) 16 17 func main() { 18 // Create an instance of our custom agent. 19 p := NewCustomAgent() 20 21 // Create new agent with our plugin instance. 22 a := agent.NewAgent(agent.AllPlugins(p)) 23 24 // Run starts the agent with plugins, waits until shutdown 25 // and then stops the agent and plugins. 26 if err := a.Run(); err != nil { 27 logging.DefaultLogger.Fatalln(err) 28 } 29 } 30 31 // CustomAgent represents our plugin. 32 type CustomAgent struct { 33 LogManager *logmanager.Plugin 34 35 app.VPP 36 app.Linux 37 38 Orchestrator *orchestrator.Plugin 39 KVDBSync *kvdbsync.Plugin 40 Resync *resync.Plugin 41 } 42 43 // NewCustomAgent returns new CustomAgent instance. 44 func NewCustomAgent() *CustomAgent { 45 p := &CustomAgent{ 46 LogManager: &logmanager.DefaultPlugin, 47 } 48 49 etcdDataSync := kvdbsync.NewPlugin(kvdbsync.UseKV(&etcd.DefaultPlugin)) 50 p.KVDBSync = etcdDataSync 51 p.Resync = &resync.DefaultPlugin 52 53 p.VPP = app.DefaultVPP() 54 p.Linux = app.DefaultLinux() 55 56 // connect IfPlugins for Linux & VPP 57 p.Linux.IfPlugin.VppIfPlugin = p.VPP.IfPlugin 58 p.VPP.IfPlugin.LinuxIfPlugin = p.Linux.IfPlugin 59 p.VPP.IfPlugin.NsPlugin = p.Linux.NSPlugin 60 61 // Set watcher for KVScheduler. 62 watchers := datasync.KVProtoWatchers{ 63 local.DefaultRegistry, 64 etcdDataSync, 65 } 66 orch := &orchestrator.DefaultPlugin 67 orch.Watcher = watchers 68 p.Orchestrator = orch 69 70 return p 71 } 72 73 // String is used to identify the plugin by giving it name. 74 func (p *CustomAgent) String() string { 75 return "CustomAgent" 76 } 77 78 // Init is executed on agent initialization. 79 func (p *CustomAgent) Init() error { 80 logging.DefaultLogger.Info("Initializing CustomAgent") 81 return nil 82 } 83 84 // AfterInit is executed after initialization of all plugins. It's optional 85 // and used for executing operations that require plugins to be initalized. 86 func (p *CustomAgent) AfterInit() error { 87 p.Resync.DoResync() 88 logging.DefaultLogger.Info("CustomAgent is Ready") 89 return nil 90 } 91 92 // Close is executed on agent shutdown. 93 func (p *CustomAgent) Close() error { 94 logging.DefaultLogger.Info("Shutting down CustomAgent") 95 return nil 96 }