Browse code

Merge pull request #30265 from allencloud/add-CheckDuplicate-details-and-logic

add CheckDuplicate docs and logics in network

Sebastiaan van Stijn authored on 2017/03/03 22:18:52
Showing 8 changed files
... ...
@@ -181,6 +181,16 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
181 181
 
182 182
 	nw, err := n.backend.CreateNetwork(create)
183 183
 	if err != nil {
184
+		var warning string
185
+		if _, ok := err.(libnetwork.NetworkNameError); ok {
186
+			// check if user defined CheckDuplicate, if set true, return err
187
+			// otherwise prepare a warning message
188
+			if create.CheckDuplicate {
189
+				return libnetwork.NetworkNameError(create.Name)
190
+			}
191
+			warning = libnetwork.NetworkNameError(create.Name).Error()
192
+		}
193
+
184 194
 		if _, ok := err.(libnetwork.ManagerRedirectError); !ok {
185 195
 			return err
186 196
 		}
... ...
@@ -188,7 +198,10 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
188 188
 		if err != nil {
189 189
 			return err
190 190
 		}
191
-		nw = &types.NetworkCreateResponse{ID: id}
191
+		nw = &types.NetworkCreateResponse{
192
+			ID:      id,
193
+			Warning: warning,
194
+		}
192 195
 	}
193 196
 
194 197
 	return httputils.WriteJSON(w, http.StatusCreated, nw)
... ...
@@ -6288,7 +6288,7 @@ paths:
6288 6288
                 description: "The network's name."
6289 6289
                 type: "string"
6290 6290
               CheckDuplicate:
6291
-                description: "Check for networks with duplicate names."
6291
+                description: "Check for networks with duplicate names. Since Network is primarily keyed based on a random ID and not on the name, and network name is strictly a user-friendly alias to the network which is uniquely identified using ID, there is no guaranteed way to check for duplicates. CheckDuplicate is there to provide a best effort checking of any networks which has the same name but it is not guaranteed to catch all name collisions."
6292 6292
                 type: "boolean"
6293 6293
               Driver:
6294 6294
                 description: "Name of the network driver plugin to use."
... ...
@@ -416,6 +416,13 @@ type EndpointResource struct {
416 416
 
417 417
 // NetworkCreate is the expected body of the "create network" http request message
418 418
 type NetworkCreate struct {
419
+	// Check for networks with duplicate names.
420
+	// Network is primarily keyed based on a random ID and not on the name.
421
+	// Network name is strictly a user-friendly alias to the network
422
+	// which is uniquely identified using ID.
423
+	// And there is no guaranteed way to check for duplicates.
424
+	// Option CheckDuplicate is there to provide a best effort checking of any networks
425
+	// which has the same name but it is not guaranteed to catch all name collisions.
419 426
 	CheckDuplicate bool
420 427
 	Driver         string
421 428
 	EnableIPv6     bool
... ...
@@ -248,6 +248,8 @@ func (daemon *Daemon) createNetwork(create types.NetworkCreateRequest, id string
248 248
 		}
249 249
 	}
250 250
 	if nw != nil {
251
+		// check if user defined CheckDuplicate, if set true, return err
252
+		// otherwise prepare a warning message
251 253
 		if create.CheckDuplicate {
252 254
 			return nil, libnetwork.NetworkNameError(create.Name)
253 255
 		}
... ...
@@ -2838,7 +2838,12 @@ Content-Type: application/json
2838 2838
 **JSON parameters**:
2839 2839
 
2840 2840
 - **Name** - The new network's name. this is a mandatory field
2841
-- **CheckDuplicate** - Requests daemon to check for networks with same name. Defaults to `false`
2841
+- **CheckDuplicate** - Requests daemon to check for networks with same name. Defaults to `false`.
2842
+    Since Network is primarily keyed based on a random ID and not on the name, 
2843
+    and network name is strictly a user-friendly alias to the network which is uniquely identified using ID, 
2844
+    there is no guaranteed way to check for duplicates across a cluster of docker hosts. 
2845
+    This parameter CheckDuplicate is there to provide a best effort checking of any networks 
2846
+    which has the same name but it is not guaranteed to catch all name collisions.
2842 2847
 - **Driver** - Name of the network driver plugin to use. Defaults to `bridge` driver
2843 2848
 - **IPAM** - Optional custom IP scheme for the network
2844 2849
   - **Driver** - Name of the IPAM driver to use. Defaults to `default` driver
... ...
@@ -3167,7 +3167,12 @@ Content-Type: application/json
3167 3167
 **JSON parameters**:
3168 3168
 
3169 3169
 - **Name** - The new network's name. this is a mandatory field
3170
-- **CheckDuplicate** - Requests daemon to check for networks with same name. Defaults to `false`
3170
+- **CheckDuplicate** - Requests daemon to check for networks with same name. Defaults to `false`.
3171
+    Since Network is primarily keyed based on a random ID and not on the name, 
3172
+    and network name is strictly a user-friendly alias to the network 
3173
+    which is uniquely identified using ID, there is no guaranteed way to check for duplicates. 
3174
+    This parameter CheckDuplicate is there to provide a best effort checking of any networks 
3175
+    which has the same name but it is not guaranteed to catch all name collisions.
3171 3176
 - **Driver** - Name of the network driver plugin to use. Defaults to `bridge` driver
3172 3177
 - **IPAM** - Optional custom IP scheme for the network
3173 3178
   - **Driver** - Name of the IPAM driver to use. Defaults to `default` driver
... ...
@@ -3281,7 +3281,12 @@ Content-Type: application/json
3281 3281
 **JSON parameters**:
3282 3282
 
3283 3283
 - **Name** - The new network's name. this is a mandatory field
3284
-- **CheckDuplicate** - Requests daemon to check for networks with same name. Defaults to `false`
3284
+- **CheckDuplicate** - Requests daemon to check for networks with same name. Defaults to `false`.
3285
+    Since Network is primarily keyed based on a random ID and not on the name, 
3286
+    and network name is strictly a user-friendly alias to the network 
3287
+    which is uniquely identified using ID, there is no guaranteed way to check for duplicates. 
3288
+    This parameter CheckDuplicate is there to provide a best effort checking of any networks 
3289
+    which has the same name but it is not guaranteed to catch all name collisions.
3285 3290
 - **Driver** - Name of the network driver plugin to use. Defaults to `bridge` driver
3286 3291
 - **Internal** - Restrict external access to the network
3287 3292
 - **IPAM** - Optional custom IP scheme for the network
... ...
@@ -3346,7 +3346,12 @@ Content-Type: application/json
3346 3346
 **JSON parameters**:
3347 3347
 
3348 3348
 - **Name** - The new network's name. this is a mandatory field
3349
-- **CheckDuplicate** - Requests daemon to check for networks with same name. Defaults to `false`
3349
+- **CheckDuplicate** - Requests daemon to check for networks with same name. Defaults to `false`.
3350
+    Since Network is primarily keyed based on a random ID and not on the name, 
3351
+    and network name is strictly a user-friendly alias to the network 
3352
+    which is uniquely identified using ID, there is no guaranteed way to check for duplicates. 
3353
+    This parameter CheckDuplicate is there to provide a best effort checking of any networks 
3354
+    which has the same name but it is not guaranteed to catch all name collisions.
3350 3355
 - **Driver** - Name of the network driver plugin to use. Defaults to `bridge` driver
3351 3356
 - **Internal** - Restrict external access to the network
3352 3357
 - **IPAM** - Optional custom IP scheme for the network