1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
package main
import (
"context"
"fmt"
"google.golang.org/grpc"
"io"
kexec "k8s.io/utils/exec"
"os"
"os/signal"
pb "ovn4nfv-k8s-plugin/internal/pkg/nfnNotify/proto"
"ovn4nfv-k8s-plugin/internal/pkg/ovn"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"strings"
"syscall"
"time"
//"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/status"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
var log = logf.Log.WithName("nfn-agent")
var errorChannel chan string
var inSync bool
var pnCreateStore []*pb.Notification_ProviderNwCreate
// subscribe Notifications
func subscribeNotif(client pb.NfnNotifyClient) error {
log.Info("Subscribe Notification from server")
ctx := context.Background()
var n pb.SubscribeContext
n.NodeName = os.Getenv("NFN_NODE_NAME")
for {
stream, err := client.Subscribe(ctx, &n, grpc.WaitForReady(true))
if err != nil {
log.Error(err, "Subscribe", "client", client, "status", status.Code(err))
continue
}
log.Info("Subscribe Notification success")
for {
in, err := stream.Recv()
if err == io.EOF {
// read done.
shutDownAgent("Stream closed")
return err
}
if err != nil {
log.Error(err, "Stream closed from server")
shutDownAgent("Stream closed from server")
return err
}
log.Info("Got message", "msg", in)
handleNotif(in)
}
}
}
func createVlanProvidernetwork(payload *pb.Notification_ProviderNwCreate) error {
var err error
vlanID := payload.ProviderNwCreate.GetVlan().GetVlanId()
ln := payload.ProviderNwCreate.GetVlan().GetLogicalIntf()
pn := payload.ProviderNwCreate.GetVlan().GetProviderIntf()
name := payload.ProviderNwCreate.GetProviderNwName()
if ln == "" {
ln = name + "." + vlanID
}
err = ovn.CreateVlan(vlanID, pn, ln)
if err != nil {
log.Error(err, "Unable to create VLAN", "vlan", ln)
return err
}
err = ovn.CreatePnBridge("nw_"+name, "br-"+name, ln)
if err != nil {
log.Error(err, "Unable to create vlan direct bridge", "vlan", pn)
return err
}
return nil
}
func createDirectProvidernetwork(payload *pb.Notification_ProviderNwCreate) error {
var err error
pn := payload.ProviderNwCreate.GetDirect().GetProviderIntf()
name := payload.ProviderNwCreate.GetProviderNwName()
err = ovn.CreatePnBridge("nw_"+name, "br-"+name, pn)
if err != nil {
log.Error(err, "Unable to create direct bridge", "direct", pn)
return err
}
return nil
}
func deleteVlanProvidernetwork(payload *pb.Notification_ProviderNwRemove) {
ln := payload.ProviderNwRemove.GetVlanLogicalIntf()
name := payload.ProviderNwRemove.GetProviderNwName()
ovn.DeleteVlan(ln)
ovn.DeletePnBridge("nw_"+name, "br-"+name)
}
func deleteDirectProvidernetwork(payload *pb.Notification_ProviderNwRemove) {
ln := payload.ProviderNwRemove.GetVlanLogicalIntf()
name := payload.ProviderNwRemove.GetProviderNwName()
ovn.DeleteVlan(ln)
ovn.DeletePnBridge("nw_"+name, "br-"+name)
}
func inSyncVlanProvidernetwork() {
var err error
// Read config from node
vlanList := ovn.GetVlan()
pnBridgeList := ovn.GetPnBridge("nfn")
diffVlan := make(map[string]bool)
diffPnBridge := make(map[string]bool)
VLAN:
for _, pn := range pnCreateStore {
if pn.ProviderNwCreate.GetVlan() != nil {
continue
}
id := pn.ProviderNwCreate.GetVlan().GetVlanId()
ln := pn.ProviderNwCreate.GetVlan().GetLogicalIntf()
pn := pn.ProviderNwCreate.GetVlan().GetProviderIntf()
if ln == "" {
ln = pn + "." + id
}
for _, vlan := range vlanList {
if vlan == ln {
// VLAN already present
diffVlan[vlan] = true
continue VLAN
}
}
// Vlan not found
err = ovn.CreateVlan(id, pn, ln)
if err != nil {
log.Error(err, "Unable to create VLAN", "vlan", ln)
return
}
}
PRNETWORK:
for _, pn := range pnCreateStore {
if pn.ProviderNwCreate.GetVlan() != nil {
continue
}
ln := pn.ProviderNwCreate.GetVlan().GetLogicalIntf()
name := pn.ProviderNwCreate.GetProviderNwName()
for _, br := range pnBridgeList {
pnName := strings.Replace(br, "br-", "", -1)
if name == pnName {
diffPnBridge[br] = true
continue PRNETWORK
}
}
// Provider Network not found
ovn.CreatePnBridge("nw_"+name, "br-"+name, ln)
}
// Delete VLAN not in the list
for _, vlan := range vlanList {
if diffVlan[vlan] == false {
ovn.DeleteVlan(vlan)
}
}
// Delete Provider Bridge not in the list
for _, br := range pnBridgeList {
if diffPnBridge[br] == false {
name := strings.Replace(br, "br-", "", -1)
ovn.DeletePnBridge("nw_"+name, "br-"+name)
}
}
}
func inSyncDirectProvidernetwork() {
// Read config from node
pnBridgeList := ovn.GetPnBridge("nfn")
diffPnBridge := make(map[string]bool)
DIRECTPRNETWORK:
for _, pn := range pnCreateStore {
if pn.ProviderNwCreate.GetDirect() != nil {
continue
}
pr := pn.ProviderNwCreate.GetDirect().GetProviderIntf()
name := pn.ProviderNwCreate.GetProviderNwName()
for _, br := range pnBridgeList {
pnName := strings.Replace(br, "br-", "", -1)
if name == pnName {
diffPnBridge[br] = true
continue DIRECTPRNETWORK
}
}
// Provider Network not found
ovn.CreatePnBridge("nw_"+name, "br-"+name, pr)
}
// Delete Provider Bridge not in the list
for _, br := range pnBridgeList {
if diffPnBridge[br] == false {
name := strings.Replace(br, "br-", "", -1)
ovn.DeletePnBridge("nw_"+name, "br-"+name)
}
}
}
func handleNotif(msg *pb.Notification) {
switch msg.GetCniType() {
case "ovn4nfv":
switch payload := msg.Payload.(type) {
case *pb.Notification_ProviderNwCreate:
if !inSync {
// Store Msgs
pnCreateStore = append(pnCreateStore, payload)
return
}
if payload.ProviderNwCreate.GetVlan() != nil {
err := createVlanProvidernetwork(payload)
if err != nil {
return
}
}
if payload.ProviderNwCreate.GetDirect() != nil {
err := createDirectProvidernetwork(payload)
if err != nil {
return
}
}
case *pb.Notification_ProviderNwRemove:
if !inSync {
// Unexpected Remove message
return
}
if payload.ProviderNwRemove.GetVlanLogicalIntf() != "" {
deleteVlanProvidernetwork(payload)
}
if payload.ProviderNwRemove.GetDirectProviderIntf() != "" {
deleteDirectProvidernetwork(payload)
}
case *pb.Notification_InSync:
inSyncVlanProvidernetwork()
inSyncDirectProvidernetwork()
pnCreateStore = nil
inSync = true
}
// Add other Types here
default:
log.Info("Not supported cni type", "cni type", msg.GetCniType())
}
}
func shutdownHandler(errorChannel <-chan string) {
// Register to receive term/int signal.
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM)
signal.Notify(signalChan, syscall.SIGINT)
signal.Notify(signalChan, syscall.SIGHUP)
var reason string
select {
case sig := <-signalChan:
if sig == syscall.SIGHUP {
log.Info("Received a SIGHUP")
}
reason = fmt.Sprintf("Received OS signal %v", sig)
case reason = <-errorChannel:
log.Info("Error", "reason", reason)
}
log.Info("nfn-agent is shutting down", "reason", reason)
}
func shutDownAgent(reason string) {
// Send a failure message and give few seconds complete shutdown.
log.Info("shutDownAgent recieved")
errorChannel <- reason
time.Sleep(10 * time.Second)
// The graceful shutdown failed, terminate the process.
panic("Shutdown failed. Panicking.")
}
func main() {
logf.SetLogger(zap.Logger(true))
log.Info("nfn-agent Started")
serverAddr := os.Getenv("NFN_OPERATOR_SERVICE_HOST") + ":" + os.Getenv("NFN_OPERATOR_SERVICE_PORT")
// Setup ovn utilities
exec := kexec.New()
err := ovn.SetExec(exec)
if err != nil {
log.Error(err, "Unable to setup OVN Utils")
return
}
conn, err := grpc.Dial(serverAddr, grpc.WithInsecure())
if err != nil {
log.Error(err, "fail to dial")
return
}
defer conn.Close()
client := pb.NewNfnNotifyClient(conn)
errorChannel = make(chan string)
// Run client in background
go subscribeNotif(client)
shutdownHandler(errorChannel)
}
|