Skip to content

Commit 4850c5f

Browse files
committed
Avoid duplicate entries in /etc/hosts
Currently the local containers of a global scope network will get it's service records updated from both a local update and global update. There is no way to check if this is a local endpoint when a remote update comes in via watch because we add the endpoint to local endpoint list during join, while the remote update happens during createendpoint. The right thing to do is update the local endpoint list and start watching during createndpoint and remove the watch during delete endpoint. But this might result in the container getting it's own record in it's /etc/hosts. So added a filtering logic to filter out self records when updating the container's /etc/hosts Signed-off-by: Jana Radhakrishnan <[email protected]>
1 parent cd971b9 commit 4850c5f

2 files changed

Lines changed: 37 additions & 14 deletions

File tree

libnetwork/endpoint.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (ep *endpoint) sbJoin(sbox Sandbox, options ...EndpointOption) error {
345345
if ip := ep.getFirstInterfaceAddress(); ip != nil {
346346
address = ip.String()
347347
}
348-
if err = sb.updateHostsFile(address, network.getSvcRecords()); err != nil {
348+
if err = sb.updateHostsFile(address, network.getSvcRecords(ep)); err != nil {
349349
return err
350350
}
351351

@@ -467,9 +467,6 @@ func (ep *endpoint) sbLeave(sbox Sandbox, options ...EndpointOption) error {
467467
return err
468468
}
469469

470-
// unwatch for service records
471-
n.getController().unWatchSvcRecord(ep)
472-
473470
if sb.needDefaultGW() {
474471
ep := sb.getEPwithoutGateway()
475472
if ep == nil {
@@ -501,29 +498,32 @@ func (ep *endpoint) Delete() error {
501498
}
502499
ep.Unlock()
503500

504-
if err = n.getEpCnt().DecEndpointCnt(); err != nil {
501+
if err = n.getController().deleteFromStore(ep); err != nil {
505502
return err
506503
}
507504
defer func() {
508505
if err != nil {
509-
if e := n.getEpCnt().IncEndpointCnt(); e != nil {
510-
log.Warnf("failed to update network %s : %v", n.name, e)
506+
ep.dbExists = false
507+
if e := n.getController().updateToStore(ep); e != nil {
508+
log.Warnf("failed to recreate endpoint in store %s : %v", name, e)
511509
}
512510
}
513511
}()
514512

515-
if err = n.getController().deleteFromStore(ep); err != nil {
513+
if err = n.getEpCnt().DecEndpointCnt(); err != nil {
516514
return err
517515
}
518516
defer func() {
519517
if err != nil {
520-
ep.dbExists = false
521-
if e := n.getController().updateToStore(ep); e != nil {
522-
log.Warnf("failed to recreate endpoint in store %s : %v", name, e)
518+
if e := n.getEpCnt().IncEndpointCnt(); e != nil {
519+
log.Warnf("failed to update network %s : %v", n.name, e)
523520
}
524521
}
525522
}()
526523

524+
// unwatch for service records
525+
n.getController().unWatchSvcRecord(ep)
526+
527527
if err = ep.deleteEndpoint(); err != nil {
528528
return err
529529
}

libnetwork/network.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net"
77
"strconv"
8+
"strings"
89
"sync"
910

1011
log "github.com/Sirupsen/logrus"
@@ -685,6 +686,14 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi
685686
}
686687
}()
687688

689+
// Watch for service records
690+
n.getController().watchSvcRecord(ep)
691+
defer func() {
692+
if err != nil {
693+
n.getController().unWatchSvcRecord(ep)
694+
}
695+
}()
696+
688697
// Increment endpoint count to indicate completion of endpoint addition
689698
if err = n.getEpCnt().IncEndpointCnt(); err != nil {
690699
return nil, err
@@ -768,6 +777,12 @@ func (n *network) updateSvcRecord(ep *endpoint, localEps []*endpoint, isAdd bool
768777
var recs []etchosts.Record
769778
if iface := ep.Iface(); iface.Address() != nil {
770779
if isAdd {
780+
// If we already have this endpoint in service db just return
781+
if _, ok := sr[ep.Name()]; ok {
782+
n.Unlock()
783+
return
784+
}
785+
771786
sr[ep.Name()] = iface.Address().IP
772787
sr[ep.Name()+"."+n.name] = iface.Address().IP
773788
} else {
@@ -793,8 +808,12 @@ func (n *network) updateSvcRecord(ep *endpoint, localEps []*endpoint, isAdd bool
793808
}
794809

795810
var sbList []*sandbox
796-
for _, ep := range localEps {
797-
if sb, hasSandbox := ep.getSandbox(); hasSandbox {
811+
for _, lEp := range localEps {
812+
if ep.ID() == lEp.ID() {
813+
continue
814+
}
815+
816+
if sb, hasSandbox := lEp.getSandbox(); hasSandbox {
798817
sbList = append(sbList, sb)
799818
}
800819
}
@@ -808,14 +827,18 @@ func (n *network) updateSvcRecord(ep *endpoint, localEps []*endpoint, isAdd bool
808827
}
809828
}
810829

811-
func (n *network) getSvcRecords() []etchosts.Record {
830+
func (n *network) getSvcRecords(ep *endpoint) []etchosts.Record {
812831
n.Lock()
813832
defer n.Unlock()
814833

815834
var recs []etchosts.Record
816835
sr, _ := n.ctrlr.svcDb[n.id]
817836

818837
for h, ip := range sr {
838+
if ep != nil && strings.Split(h, ".")[0] == ep.Name() {
839+
continue
840+
}
841+
819842
recs = append(recs, etchosts.Record{
820843
Hosts: h,
821844
IP: ip.String(),

0 commit comments

Comments
 (0)