Skip to content

Commit

Permalink
Merge pull request #20365 from p-fruck/fix/api-compat-network-connected
Browse files Browse the repository at this point in the history
fix(API): Catch ErrNetworkConnected for compat
  • Loading branch information
openshift-ci[bot] authored Oct 18, 2023
2 parents 074a0de + ad53190 commit a1982c0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libpod/networking_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func (c *Container) NetworkConnect(nameOrID, netName string, netOpts types.PerNe

if err := c.runtime.state.NetworkConnect(c, netName, netOpts); err != nil {
// Docker compat: treat requests to attach already attached networks as a no-op, ignoring opts
if errors.Is(err, define.ErrNetworkConnected) && c.ensureState(define.ContainerStateConfigured) {
if errors.Is(err, define.ErrNetworkConnected) && !c.ensureState(define.ContainerStateRunning, define.ContainerStateCreated) {
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/api/handlers/compat/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ func Connect(w http.ResponseWriter, r *http.Request) {
utils.Error(w, http.StatusNotFound, err)
return
}
if errors.Is(err, define.ErrNetworkConnected) {
utils.Error(w, http.StatusForbidden, err)
return
}
utils.Error(w, http.StatusInternalServerError, err)
return
}
Expand Down
22 changes: 22 additions & 0 deletions test/apiv2/35-networks.at
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,26 @@ t GET networks/$nid 200 .Name="network5" \
# clean the network
podman network rm -f network5

#
# Test connecting container to network when connection already exists
#
podman network create netcon
podman create --network bridge --name c1 $IMAGE top

# connect c1 to the network for the first time.
t POST networks/netcon/connect Container=c1 200 OK
# connect c1 to netcon again should not error
t POST networks/netcon/connect Container=c1 200 OK

# connect c1 to netcon here should error as the container is running and it is already connected
podman start c1
t POST networks/netcon/connect Container=c1 403 .cause="network is already connected"

# connect c1 to netcon here should not error as container was stopped
podman stop c1
t POST networks/netcon/connect Container=c1 200 OK

# cleanup
podman network rm -f netcon

# vim: filetype=sh

0 comments on commit a1982c0

Please sign in to comment.