@@ -64,10 +64,15 @@ func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo
6464 // empty parent and --internal are handled the same. Set here to update k/v
6565 config .Internal = true
6666 }
67- err = d .createNetwork (config )
67+ foundExisting , err : = d .createNetwork (config )
6868 if err != nil {
6969 return err
7070 }
71+
72+ if foundExisting {
73+ return types .InternalMaskableErrorf ("restoring existing network %s" , config .ID )
74+ }
75+
7176 // update persistent db, rollback on fail
7277 err = d .storeUpdate (config )
7378 if err != nil {
@@ -80,22 +85,32 @@ func (d *driver) CreateNetwork(nid string, option map[string]interface{}, nInfo
8085}
8186
8287// createNetwork is used by new network callbacks and persistent network cache
83- func (d * driver ) createNetwork (config * configuration ) error {
88+ func (d * driver ) createNetwork (config * configuration ) (bool , error ) {
89+ foundExisting := false
8490 networkList := d .getNetworks ()
8591 for _ , nw := range networkList {
8692 if config .Parent == nw .config .Parent {
87- return fmt .Errorf ("network %s is already using parent interface %s" ,
88- getDummyName (stringid .TruncateID (nw .config .ID )), config .Parent )
93+ if config .ID != nw .config .ID {
94+ return false , fmt .Errorf ("network %s is already using parent interface %s" ,
95+ getDummyName (stringid .TruncateID (nw .config .ID )), config .Parent )
96+ }
97+ logrus .Debugf ("Create Network for the same ID %s\n " , config .ID )
98+ foundExisting = true
99+ break
89100 }
90101 }
91102 if ! parentExists (config .Parent ) {
92103 // if the --internal flag is set, create a dummy link
93104 if config .Internal {
94- err := createDummyLink (config .Parent , getDummyName (stringid .TruncateID (config .ID )))
95- if err != nil {
96- return err
105+ if ! dummyLinkExists (getDummyName (stringid .TruncateID (config .ID ))) {
106+ err := createDummyLink (config .Parent , getDummyName (stringid .TruncateID (config .ID )))
107+ if err != nil {
108+ return false , err
109+ }
110+ config .CreatedSlaveLink = true
111+ } else {
112+ logrus .Debugf ("Dummy Link %s for Mac Vlan already exists" , getDummyName (stringid .TruncateID (config .ID )))
97113 }
98- config .CreatedSlaveLink = true
99114 // notify the user in logs they have limited communications
100115 if config .Parent == getDummyName (stringid .TruncateID (config .ID )) {
101116 logrus .Debugf ("Empty -o parent= and --internal flags limit communications to other containers inside of network: %s" ,
@@ -104,24 +119,33 @@ func (d *driver) createNetwork(config *configuration) error {
104119 } else {
105120 // if the subinterface parent_iface.vlan_id checks do not pass, return err.
106121 // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
107- err := createVlanLink (config .Parent )
108- if err != nil {
109- return err
122+
123+ if ! vlanLinkExists (config .Parent ) {
124+ // if the subinterface parent_iface.vlan_id checks do not pass, return err.
125+ // a valid example is 'eth0.10' for a parent iface 'eth0' with a vlan id '10'
126+ err := createVlanLink (config .Parent )
127+ if err != nil {
128+ return false , err
129+ }
130+ // if driver created the networks slave link, record it for future deletion
131+ config .CreatedSlaveLink = true
132+ } else {
133+ logrus .Debugf ("Parent Sub Interface %s already Exists NetID %s" , config .Parent , config .ID )
110134 }
111- // if driver created the networks slave link, record it for future deletion
112- config .CreatedSlaveLink = true
113135 }
114136 }
115- n := & network {
116- id : config .ID ,
117- driver : d ,
118- endpoints : endpointTable {},
119- config : config ,
137+ if ! foundExisting {
138+ n := & network {
139+ id : config .ID ,
140+ driver : d ,
141+ endpoints : endpointTable {},
142+ config : config ,
143+ }
144+ // add the network
145+ d .addNetwork (n )
120146 }
121- // add the *network
122- d .addNetwork (n )
123147
124- return nil
148+ return foundExisting , nil
125149}
126150
127151// DeleteNetwork deletes the network for the specified driver type
0 commit comments