@@ -60,10 +60,14 @@ func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo
6060 // empty parent and --internal are handled the same. Set here to update k/v
6161 config .Internal = true
6262 }
63- err = d .createNetwork (config )
63+ foundExisting , err : = d .createNetwork (config )
6464 if err != nil {
6565 return err
6666 }
67+
68+ if foundExisting {
69+ return types .InternalMaskableErrorf ("restoring existing network %s" , config .ID )
70+ }
6771 // update persistent db, rollback on fail
6872 err = d .storeUpdate (config )
6973 if err != nil {
@@ -76,22 +80,29 @@ func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo
7680}
7781
7882// createNetwork is used by new network callbacks and persistent network cache
79- func (d * driver ) createNetwork (config * configuration ) error {
83+ func (d * driver ) createNetwork (config * configuration ) (bool , error ) {
84+ foundExisting := false
8085 networkList := d .getNetworks ()
8186 for _ , nw := range networkList {
8287 if config .Parent == nw .config .Parent {
83- return fmt .Errorf ("network %s is already using parent interface %s" ,
84- getDummyName (stringid .TruncateID (nw .config .ID )), config .Parent )
88+ if config .ID != nw .config .ID {
89+ return false , fmt .Errorf ("network %s is already using parent interface %s" ,
90+ getDummyName (stringid .TruncateID (nw .config .ID )), config .Parent )
91+ }
92+ logrus .Debugf ("Create Network for the same ID %s\n " , config .ID )
93+ foundExisting = true
94+ break
8595 }
8696 }
8797 if ! parentExists (config .Parent ) {
8898 // if the --internal flag is set, create a dummy link
8999 if config .Internal {
90100 err := createDummyLink (config .Parent , getDummyName (stringid .TruncateID (config .ID )))
91101 if err != nil {
92- return err
102+ return false , err
93103 }
94104 config .CreatedSlaveLink = true
105+
95106 // notify the user in logs they have limited communications
96107 if config .Parent == getDummyName (stringid .TruncateID (config .ID )) {
97108 logrus .Debugf ("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s" ,
@@ -102,22 +113,24 @@ func (d *driver) createNetwork(config *configuration) error {
102113 // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
103114 err := createVlanLink (config .Parent )
104115 if err != nil {
105- return err
116+ return false , err
106117 }
107118 // if driver created the networks slave link, record it for future deletion
108119 config .CreatedSlaveLink = true
109120 }
110121 }
111- n := & network {
112- id : config .ID ,
113- driver : d ,
114- endpoints : endpointTable {},
115- config : config ,
122+ if ! foundExisting {
123+ n := & network {
124+ id : config .ID ,
125+ driver : d ,
126+ endpoints : endpointTable {},
127+ config : config ,
128+ }
129+ // add the network
130+ d .addNetwork (n )
116131 }
117- // add the *network
118- d .addNetwork (n )
119132
120- return nil
133+ return foundExisting , nil
121134}
122135
123136// DeleteNetwork the network for the specified driver type
0 commit comments