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

Cherry-pick: Unbind container in containerRm (#6209) #6310

Merged
merged 1 commit into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/apiservers/engine/backends/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,16 @@ func (c *Container) ContainerRm(name string, config *types.ContainerRmConfig) er
if state.Status == "Starting" {
return derr.NewRequestConflictError(fmt.Errorf("The container is starting. To remove use -f"))
}

handle, err := c.Handle(id, name)
if err != nil {
return err
}

_, err = c.containerProxy.UnbindContainerFromNetwork(vc, handle)
if err != nil {
return err
}
}

//call the remove directly on the name. No need for using a handle.
Expand Down
38 changes: 25 additions & 13 deletions lib/apiservers/engine/backends/container_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ type VicContainerProxy interface {

GetContainerChanges(op trace.Operation, vc *viccontainer.VicContainer, data bool) (io.ReadCloser, error)

UnbindContainerFromNetwork(vc *viccontainer.VicContainer, handle string) (string, error)

Handle(id, name string) (string, error)
Client() *client.PortLayer
exitCode(vc *viccontainer.VicContainer) (string, error)
Expand Down Expand Up @@ -779,20 +781,9 @@ func (c *ContainerProxy) Stop(vc *viccontainer.VicContainer, name string, second
}

if unbound {
unbindParams := scopes.NewUnbindContainerParamsWithContext(ctx).WithHandle(handle)
ub, err := c.client.Scopes.UnbindContainer(unbindParams)
handle, err = c.UnbindContainerFromNetwork(vc, handle)
if err != nil {
switch err := err.(type) {
case *scopes.UnbindContainerNotFound:
// ignore error
log.Warnf("Container %s not found by network unbind", vc.ContainerID)
case *scopes.UnbindContainerInternalServerError:
return InternalServerError(err.Payload.Message)
default:
return InternalServerError(err.Error())
}
} else {
handle = ub.Payload.Handle
return err
}

// unmap ports
Expand Down Expand Up @@ -835,6 +826,27 @@ func (c *ContainerProxy) Stop(vc *viccontainer.VicContainer, name string, second
return nil
}

// UnbindContainerFromNetwork unbinds a container from the networks that it connects to
func (c *ContainerProxy) UnbindContainerFromNetwork(vc *viccontainer.VicContainer, handle string) (string, error) {
defer trace.End(trace.Begin(vc.ContainerID))

unbindParams := scopes.NewUnbindContainerParamsWithContext(ctx).WithHandle(handle)
ub, err := c.client.Scopes.UnbindContainer(unbindParams)
if err != nil {
switch err := err.(type) {
case *scopes.UnbindContainerNotFound:
// ignore error
log.Warnf("Container %s not found by network unbind", vc.ContainerID)
case *scopes.UnbindContainerInternalServerError:
return "", InternalServerError(err.Payload.Message)
default:
return "", InternalServerError(err.Error())
}
}

return ub.Payload.Handle, nil
}

// State returns container state
func (c *ContainerProxy) State(vc *viccontainer.VicContainer) (*types.ContainerState, error) {
defer trace.End(trace.Begin(""))
Expand Down
4 changes: 4 additions & 0 deletions lib/apiservers/engine/backends/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ func (m *MockContainerProxy) GetContainerChanges(op trace.Operation, vc *viccont
return nil, nil
}

func (m *MockContainerProxy) UnbindContainerFromNetwork(vc *viccontainer.VicContainer, handle string) (string, error) {
return "", nil
}

func (m *MockContainerProxy) exitCode(vc *viccontainer.VicContainer) (string, error) {
return "", nil
}
Expand Down