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:
parent
e0e445434f
commit
28c2445dad
3 changed files with 47 additions and 0 deletions
|
@ -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
|
||||
func Add(path string, recs []Record) error {
|
||||
if len(recs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
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
|
||||
func Delete(path string, recs []Record) error {
|
||||
if len(recs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
old, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -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) {
|
||||
file, err := ioutil.TempFile("", "")
|
||||
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) {
|
||||
file, err := ioutil.TempFile("", "")
|
||||
if err != nil {
|
||||
|
|
|
@ -416,6 +416,11 @@ func (n *network) updateSvcRecord(ep *endpoint, isAdd bool) {
|
|||
}
|
||||
n.Unlock()
|
||||
|
||||
// If there are no records to add or delete then simply return here
|
||||
if len(recs) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var epList []*endpoint
|
||||
n.WalkEndpoints(func(e Endpoint) bool {
|
||||
cEp := e.(*endpoint)
|
||||
|
|
Loading…
Reference in a new issue