-
Notifications
You must be signed in to change notification settings - Fork 949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: add network disconnect interface #1172
feature: add network disconnect interface #1172
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1172 +/- ##
==========================================
- Coverage 16.01% 15.97% -0.04%
==========================================
Files 175 176 +1
Lines 10062 10130 +68
==========================================
+ Hits 1611 1618 +7
- Misses 8333 8394 +61
Partials 118 118
|
b1f889d
to
dcc6b41
Compare
apis/server/network_bridge.go
Outdated
return httputils.NewHTTPError(err, http.StatusBadRequest) | ||
} | ||
|
||
name := mux.Vars(req)["name"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sugget using network
with the naming, since it will be some kind of clear. 😃 Not blocking
apis/swagger.yml
Outdated
responses: | ||
200: | ||
description: "No error" | ||
404: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You returned return httputils.NewHTTPError(err, http.StatusBadRequest)
. So I am afraid we need to add status code of 400.
cli/network.go
Outdated
|
||
ctx := context.Background() | ||
apiClient := nd.cli.Client() | ||
if err := apiClient.NetworkDisconnect(ctx, nd.network, nd.container, nd.force); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do not wrapper the error in the return statement, you can use return apiClient.NetworkDisconnect(ctx, nd.network, nd.container, nd.force)
directly.
daemon/mgr/container.go
Outdated
// Get network | ||
network, err := mgr.NetworkMgr.Get(ctx, networkName) | ||
if err != nil { | ||
return fmt.Errorf("failed to disconnect container %s from %s: get network failed: %v", c.Name(), networkName, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fmt.Errorf("failed to get network %s when disconnecting container %s from it: %v", c.Name(), network, err)
?
daemon/mgr/container.go
Outdated
return fmt.Errorf("failed to disconnect container %s from %s: get network failed: %v", c.Name(), networkName, err) | ||
} | ||
|
||
if c.meta.NetworkSettings != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not know if the logic is correct. While I insist the code could be simplified, like
if c.meta.NetworkSettings == nil {
return nil
}
epConfig, ok := c.meta.NetworkSettings.Networks[network.Name]
if !ok {
return fmt.Errorf("failed to disconnect container from network: container %s not attach to %s", c.Name(), networkName)
}
...........
Using return fast to reduce ident of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree !
1f2c7ca
to
026dd89
Compare
daemon/mgr/container.go
Outdated
} | ||
|
||
networkSettings := c.meta.NetworkSettings | ||
if networkSettings == nil || networkSettings.Networks == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested that there is no need to check if networkSettings.Networks equals to nil, if we checked epConfig, ok := networkSettings.Networks[network.Name]
. Since we could get a value by the following way if it is nil:
value, exist := networkSettings.Networks[network.Name]
even if networkSettings.Networks
is nil.
026dd89
to
adf0361
Compare
@allencloud Thanks a lot for your review, Updated, PTAL @rudyfly Please review this PR, thanks |
Signed-off-by: Michael Wan <zirenwan@gmail.com>
adf0361
to
fc27d0e
Compare
inspectInfo := command.PouchRun("inspect", name).Stdout() | ||
metaJSON := []types.ContainerJSON{} | ||
if err := json.Unmarshal([]byte(inspectInfo), &metaJSON); err != nil { | ||
c.Errorf("failed to decode inspect output: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use c.Assert here? I am not very sure. @Letty5411
// remove old container from cache | ||
mgr.cache.Remove(c.ID()) | ||
// add new container to cache | ||
mgr.cache.Put(c.ID(), c) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing it and adding it again is our current way to update an instance.
I think it is kind of redundant. I wish that we could add an Update function in package satemap
. Also it will be much better to encapsulate this two function into a single one.
Not block this. Just record this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Continue to add a comment to this. I am afraid we do not need to function Update, because we can use Put
function directly to achieve this. @HusterWan
Since the code is as following:
// Put stores a key-value pair into inner map safely.
func (m *SafeMap) Put(k string, v interface{}) {
m.Lock()
defer m.Unlock()
if m.inner == nil {
return
}
m.inner[k] = v
}
So no need to remove, just put is OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i will create a new pr to refactor this part of code.
LGTM |
@@ -72,6 +72,7 @@ func initRoute(s *Server) http.Handler { | |||
s.addRoute(r, http.MethodPost, "/networks/create", s.createNetwork) | |||
s.addRoute(r, http.MethodGet, "/networks/{name:.*}", s.getNetwork) | |||
s.addRoute(r, http.MethodDelete, "/networks/{name:.*}", s.deleteNetwork) | |||
s.addRoute(r, http.MethodPost, "/networks/{name:.*}/disconnect", s.disconnectNetwork) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change "name" to "id", and make it consistent with moby api.
|
||
// DisconnectContainerFromNetwork disconnects the given container from | ||
// given network | ||
DisconnectContainerFromNetwork(ctx context.Context, containerName, networkName string, force bool) error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/DisconnectContainerFromNetwork/Disconnect?
it looks complicated.
Signed-off-by: Michael Wan zirenwan@gmail.com
Ⅰ. Describe what this PR did
support network disconnect restful api
Ⅱ. Does this pull request fix one issue?
fixes part of #868
Ⅲ. Describe how you did it
Ⅳ. Describe how to verify it
show container network information
check network config inside container:
check network config on host:
disconnect container from network
check network config again:
endpoint has been removed
Ⅴ. Special notes for reviews