1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Do not update /etc/hosts for empty endpoints

There is no need to update the /etc/hosts files
of containers for endpoints which are created/deleted
in a network whose interface list is empty

Signed-off-by: Jana Radhakrishnan <mrjana@docker.com>
This commit is contained in:
Jana Radhakrishnan 2015-06-24 11:42:53 -07:00
parent e0e445434f
commit 28c2445dad
3 changed files with 47 additions and 0 deletions

View file

@ -68,6 +68,10 @@ func Build(path, IP, hostname, domainname string, extraContent []Record) error {
// Add adds an arbitrary number of Records to an already existing /etc/hosts file // Add adds an arbitrary number of Records to an already existing /etc/hosts file
func Add(path string, recs []Record) error { func Add(path string, recs []Record) error {
if len(recs) == 0 {
return nil
}
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return err return err
@ -91,6 +95,10 @@ func Add(path string, recs []Record) error {
// Delete deletes an arbitrary number of Records already existing in /etc/hosts file // Delete deletes an arbitrary number of Records already existing in /etc/hosts file
func Delete(path string, recs []Record) error { func Delete(path string, recs []Record) error {
if len(recs) == 0 {
return nil
}
old, err := ioutil.ReadFile(path) old, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return err return err

View file

@ -135,6 +135,23 @@ func TestUpdate(t *testing.T) {
} }
} }
func TestAddEmpty(t *testing.T) {
file, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(file.Name())
err = Build(file.Name(), "", "", "", nil)
if err != nil {
t.Fatal(err)
}
if err := Add(file.Name(), []Record{}); err != nil {
t.Fatal(err)
}
}
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
file, err := ioutil.TempFile("", "") file, err := ioutil.TempFile("", "")
if err != nil { if err != nil {
@ -166,6 +183,23 @@ func TestAdd(t *testing.T) {
} }
} }
func TestDeleteEmpty(t *testing.T) {
file, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(file.Name())
err = Build(file.Name(), "", "", "", nil)
if err != nil {
t.Fatal(err)
}
if err := Delete(file.Name(), []Record{}); err != nil {
t.Fatal(err)
}
}
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
file, err := ioutil.TempFile("", "") file, err := ioutil.TempFile("", "")
if err != nil { if err != nil {

View file

@ -416,6 +416,11 @@ func (n *network) updateSvcRecord(ep *endpoint, isAdd bool) {
} }
n.Unlock() n.Unlock()
// If there are no records to add or delete then simply return here
if len(recs) == 0 {
return
}
var epList []*endpoint var epList []*endpoint
n.WalkEndpoints(func(e Endpoint) bool { n.WalkEndpoints(func(e Endpoint) bool {
cEp := e.(*endpoint) cEp := e.(*endpoint)