go.ligato.io/vpp-agent/v3@v3.5.0/examples/localclient_with_etcd/main.go (about) 1 // Copyright (c) 2019 Cisco and/or its affiliates. 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 main 16 17 import ( 18 "fmt" 19 "log" 20 21 "go.ligato.io/cn-infra/v2/agent" 22 "go.ligato.io/cn-infra/v2/datasync" 23 "go.ligato.io/cn-infra/v2/datasync/kvdbsync" 24 "go.ligato.io/cn-infra/v2/datasync/kvdbsync/local" 25 "go.ligato.io/cn-infra/v2/datasync/resync" 26 "go.ligato.io/cn-infra/v2/db/keyval/etcd" 27 "go.ligato.io/cn-infra/v2/health/statuscheck" 28 29 "go.ligato.io/vpp-agent/v3/client" 30 legacyclient "go.ligato.io/vpp-agent/v3/clientv2/linux/localclient" 31 "go.ligato.io/vpp-agent/v3/plugins/orchestrator" 32 vpp_ifplugin "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin" 33 vpp_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces" 34 ) 35 36 func main() { 37 ep := &ExamplePlugin{ 38 VPPIfPlugin: &vpp_ifplugin.DefaultPlugin, 39 Orchestrator: &orchestrator.DefaultPlugin, 40 ETCDDataSync: kvdbsync.NewPlugin(kvdbsync.UseKV(&etcd.DefaultPlugin)), 41 } 42 43 writers := datasync.KVProtoWriters{ 44 ep.ETCDDataSync, 45 } 46 statuscheck.DefaultPlugin.Transport = writers 47 48 watchers := datasync.KVProtoWatchers{ 49 local.DefaultRegistry, 50 ep.ETCDDataSync, 51 } 52 orchestrator.DefaultPlugin.Watcher = watchers 53 54 a := agent.NewAgent( 55 agent.AllPlugins(ep), 56 ) 57 if err := a.Run(); err != nil { 58 log.Fatal(err) 59 } 60 } 61 62 // ExamplePlugin is the main plugin which 63 // handles resync and changes in this example. 64 type ExamplePlugin struct { 65 VPPIfPlugin *vpp_ifplugin.IfPlugin 66 Orchestrator *orchestrator.Plugin 67 ETCDDataSync *kvdbsync.Plugin 68 } 69 70 // String returns plugin name 71 func (p *ExamplePlugin) String() string { 72 return "example" 73 } 74 75 // Init handles initialization phase. 76 func (p *ExamplePlugin) Init() error { 77 return nil 78 } 79 80 // AfterInit first triggers localclient-based resync, then resync against etcd. 81 func (p *ExamplePlugin) AfterInit() error { 82 // local client resync 83 resyncLocalClient() 84 85 // demonstrate also legacy localclient - it should not trigger any additional 86 // changes since the same configuration was already applied by resyncLocalClient(). 87 resyncLegacyLocalClient() 88 89 // etcd resync 90 fmt.Println("=== ETCD RESYNC ===") 91 resync.DefaultPlugin.DoResync() 92 return nil 93 } 94 95 // Close cleans up the resources. 96 func (p *ExamplePlugin) Close() error { 97 return nil 98 } 99 100 // resyncLegacyLocalClient demonstrates resync of the local client (from "client" 101 // package) 102 func resyncLocalClient() { 103 fmt.Println("=== LOCALCLIENT RESYNC ===") 104 105 err := client.LocalClient.ResyncConfig(myMemif) 106 if err != nil { 107 fmt.Println(err) 108 return 109 } 110 } 111 112 // resyncLegacyLocalClient demonstrates resync of (legacy) local client 113 // ("clientv2" package). It is recommended to use the client from "client" 114 // package instead, simply because it is extensible beyond the vpp-agent core 115 // plugins and also it provides additional methods to obtain the configuration 116 // state. 117 func resyncLegacyLocalClient() { 118 fmt.Println("=== LEGACY LOCALCLIENT RESYNC ===") 119 120 txn := legacyclient.DataResyncRequest("example") 121 err := txn. 122 VppInterface(myMemif). 123 Send().ReceiveReply() 124 if err != nil { 125 fmt.Println(err) 126 return 127 } 128 } 129 130 var ( 131 myMemif = &vpp_interfaces.Interface{ 132 Name: "my-memif", 133 Type: vpp_interfaces.Interface_MEMIF, 134 Enabled: true, 135 IpAddresses: []string{"192.168.1.1/24"}, 136 Link: &vpp_interfaces.Interface_Memif{ 137 Memif: &vpp_interfaces.MemifLink{ 138 Mode: vpp_interfaces.MemifLink_ETHERNET, 139 Master: true, 140 Id: 0, 141 RxQueues: 4, 142 TxQueues: 4, 143 }, 144 }, 145 } 146 )