Skip to content
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

Merged

Conversation

HusterWan
Copy link
Contributor

@HusterWan HusterWan commented Apr 20, 2018

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

root@osboxes:pouch (zr/network-disconnect) -> pouch inspect -f {{.NetworkSettings.Networks.bridge}} test
{[] map[] bfdbbdd9fb28258c270eed0a93b90da4172eee62336b35ddcf9f58e1375237f7 172.17.0.1  0 <nil> 172.17.0.2 24  [] 02:42:ac:11:00:02 89f1c37aac4c6cd86848290c54a85f9af37b4c7ce18b64358cc4dc49d4c89f74}

check network config inside container:

root@osboxes:pouch (zr/network-disconnect) -> nsenter -t 14414 -p -n -m ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
9: eth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/24 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link
       valid_lft forever preferred_lft forever

check network config on host:

root@osboxes:pouch (zr/network-disconnect) -> brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.0242fbc4e8bd	no
p0		8000.1ad80237a756	no		veth9489e6d
virbr0		8000.525400edda3f	yes		virbr0-nic

disconnect container from network

root@osboxes:pouch (zr/network-disconnect) -> pouch network disconnect bridge test

check network config again:

root@osboxes:pouch (zr/network-disconnect) -> brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.0242fbc4e8bd	no
p0		8000.000000000000	no
virbr0		8000.525400edda3f	yes		virbr0-nic

endpoint has been removed

Ⅴ. Special notes for reviews

@codecov-io
Copy link

codecov-io commented Apr 20, 2018

Codecov Report

Merging #1172 into master will decrease coverage by 0.03%.
The diff coverage is 10.14%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
apis/server/network_bridge.go 0% <0%> (ø) ⬆️
ctrd/container.go 0% <0%> (ø) ⬆️
daemon/mgr/container.go 0% <0%> (ø) ⬆️
cli/network.go 0% <0%> (ø) ⬆️
apis/server/router.go 0% <0%> (ø) ⬆️
client/network_disconnect.go 100% <100%> (ø)

@HusterWan HusterWan changed the title feature: add network disconnect interface [WIP]feature: add network disconnect interface Apr 20, 2018
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

name := mux.Vars(req)["name"]
Copy link
Collaborator

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:
Copy link
Collaborator

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 {
Copy link
Collaborator

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.

// 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)
Copy link
Collaborator

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) ?

return fmt.Errorf("failed to disconnect container %s from %s: get network failed: %v", c.Name(), networkName, err)
}

if c.meta.NetworkSettings != nil {
Copy link
Collaborator

@allencloud allencloud Apr 20, 2018

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree !

@HusterWan HusterWan force-pushed the zr/network-disconnect branch 3 times, most recently from 1f2c7ca to 026dd89 Compare April 20, 2018 14:33
}

networkSettings := c.meta.NetworkSettings
if networkSettings == nil || networkSettings.Networks == nil {
Copy link
Collaborator

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.

@HusterWan
Copy link
Contributor Author

@allencloud Thanks a lot for your review, Updated, PTAL

@rudyfly Please review this PR, thanks

@HusterWan HusterWan changed the title [WIP]feature: add network disconnect interface feature: add network disconnect interface Apr 21, 2018
Signed-off-by: Michael Wan <zirenwan@gmail.com>
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)
Copy link
Collaborator

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)
Copy link
Collaborator

@allencloud allencloud Apr 22, 2018

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@allencloud
Copy link
Collaborator

LGTM

@pouchrobot pouchrobot added the LGTM one maintainer or community participant agrees to merge the pull reuqest. label Apr 22, 2018
@allencloud allencloud merged commit 76bf1cf into AliyunContainerService:master Apr 22, 2018
@@ -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)
Copy link
Collaborator

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
areas/network kind/feature LGTM one maintainer or community participant agrees to merge the pull reuqest. size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants