Merge pull request #20365 from p-fruck/fix/api-compat-network-connected

fix(API): Catch ErrNetworkConnected for compat
This commit is contained in:
openshift-ci[bot]
2023-10-18 08:55:31 +00:00
committed by GitHub
3 changed files with 27 additions and 1 deletions

View File

@ -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 { 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 // 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 return nil
} }

View File

@ -448,6 +448,10 @@ func Connect(w http.ResponseWriter, r *http.Request) {
utils.Error(w, http.StatusNotFound, err) utils.Error(w, http.StatusNotFound, err)
return return
} }
if errors.Is(err, define.ErrNetworkConnected) {
utils.Error(w, http.StatusForbidden, err)
return
}
utils.Error(w, http.StatusInternalServerError, err) utils.Error(w, http.StatusInternalServerError, err)
return return
} }

View File

@ -218,4 +218,26 @@ t GET networks/$nid 200 .Name="network5" \
# clean the network # clean the network
podman network rm -f network5 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 # vim: filetype=sh