Skip to content

Commit 94403f0

Browse files
committed
Convert endpoint gossip to use protobuf
Endpoint gossip will use protobuf so that we can make changes in a backward compatible way. Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
1 parent a66d8ed commit 94403f0

3 files changed

Lines changed: 546 additions & 13 deletions

File tree

agent.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package libnetwork
22

3+
//go:generate protoc -I.:Godeps/_workspace/src/github.com/gogo/protobuf --gogo_out=import_path=github.com/docker/libnetwork,Mgogoproto/gogo.proto=github.com/gogo/protobuf/gogoproto:. agent.proto
4+
35
import (
46
"fmt"
57
"net"
68
"os"
7-
"strings"
89

910
"github.com/Sirupsen/logrus"
1011
"github.com/docker/go-events"
1112
"github.com/docker/libnetwork/datastore"
1213
"github.com/docker/libnetwork/discoverapi"
1314
"github.com/docker/libnetwork/driverapi"
1415
"github.com/docker/libnetwork/networkdb"
16+
"github.com/gogo/protobuf/proto"
1517
)
1618

1719
type agent struct {
@@ -169,8 +171,18 @@ func (ep *endpoint) addToCluster() error {
169171
return err
170172
}
171173

172-
if err := c.agent.networkDB.CreateEntry("endpoint_table", n.ID(), ep.ID(), []byte(fmt.Sprintf("%s,%s,%s,%s", ep.Name(), ep.svcName,
173-
ep.svcID, ep.Iface().Address().IP))); err != nil {
174+
buf, err := proto.Marshal(&EndpointRecord{
175+
Name: ep.Name(),
176+
ServiceName: ep.svcName,
177+
ServiceID: ep.svcID,
178+
EndpointIP: ep.Iface().Address().IP.String(),
179+
})
180+
181+
if err != nil {
182+
return err
183+
}
184+
185+
if err := c.agent.networkDB.CreateEntry("endpoint_table", n.ID(), ep.ID(), buf); err != nil {
174186
return err
175187
}
176188
}
@@ -310,20 +322,21 @@ func (c *controller) handleEpTableEvent(ev events.Event) {
310322
var (
311323
nid string
312324
eid string
313-
value string
325+
value []byte
314326
isAdd bool
327+
epRec EndpointRecord
315328
)
316329

317330
switch event := ev.(type) {
318331
case networkdb.CreateEvent:
319332
nid = event.NetworkID
320333
eid = event.Key
321-
value = string(event.Value)
334+
value = event.Value
322335
isAdd = true
323336
case networkdb.DeleteEvent:
324337
nid = event.NetworkID
325338
eid = event.Key
326-
value = string(event.Value)
339+
value = event.Value
327340
case networkdb.UpdateEvent:
328341
logrus.Errorf("Unexpected update service table event = %#v", event)
329342
}
@@ -335,16 +348,16 @@ func (c *controller) handleEpTableEvent(ev events.Event) {
335348
}
336349
n := nw.(*network)
337350

338-
vals := strings.Split(value, ",")
339-
if len(vals) < 4 {
340-
logrus.Errorf("Incorrect service table value = %s", value)
351+
err = proto.Unmarshal(value, &epRec)
352+
if err != nil {
353+
logrus.Errorf("Failed to unmarshal service table value: %v", err)
341354
return
342355
}
343356

344-
name := vals[0]
345-
svcName := vals[1]
346-
svcID := vals[2]
347-
ip := net.ParseIP(vals[3])
357+
name := epRec.Name
358+
svcName := epRec.ServiceName
359+
svcID := epRec.ServiceID
360+
ip := net.ParseIP(epRec.EndpointIP)
348361

349362
if name == "" || ip == nil {
350363
logrus.Errorf("Invalid endpoint name/ip received while handling service table event %s", value)

0 commit comments

Comments
 (0)