From d3e794bda39167b15c5dc14d83333d1306316b11 Mon Sep 17 00:00:00 2001 From: baude Date: Wed, 11 Nov 2020 09:45:07 -0600 Subject: add network connect|disconnect compat endpoints this enables the ability to connect and disconnect a container from a given network. it is only for the compatibility layer. some code had to be refactored to avoid circular imports. additionally, tests are being deferred temporarily due to some incompatibility/bug in either docker-py or our stack. Signed-off-by: baude --- pkg/api/handlers/compat/networks.go | 63 ++++++++++++++----------------------- 1 file changed, 23 insertions(+), 40 deletions(-) (limited to 'pkg/api/handlers/compat') diff --git a/pkg/api/handlers/compat/networks.go b/pkg/api/handlers/compat/networks.go index abbb6d2c0..64ddebf9c 100644 --- a/pkg/api/handlers/compat/networks.go +++ b/pkg/api/handlers/compat/networks.go @@ -312,48 +312,40 @@ func RemoveNetwork(w http.ResponseWriter, r *http.Request) { } // Connect adds a container to a network -// TODO: For now this func is a no-op that checks the container name, network name, and -// responds with a 200. This allows the call to remain intact. We need to decide how -// we make this work with CNI networking and setup/teardown. func Connect(w http.ResponseWriter, r *http.Request) { runtime := r.Context().Value("runtime").(*libpod.Runtime) - var netConnect types.NetworkConnect + var ( + aliases []string + netConnect types.NetworkConnect + ) if err := json.NewDecoder(r.Body).Decode(&netConnect); err != nil { utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()")) return } - config, err := runtime.GetConfig() - if err != nil { - utils.InternalServerError(w, err) - return - } name := utils.GetName(r) - exists, err := network.Exists(config, name) - if err != nil { - utils.InternalServerError(w, err) - return - } - if !exists { - utils.Error(w, "network not found", http.StatusNotFound, define.ErrNoSuchNetwork) - return + if netConnect.EndpointConfig != nil { + if netConnect.EndpointConfig.Aliases != nil { + aliases = netConnect.EndpointConfig.Aliases + } } - if _, err = runtime.LookupContainer(netConnect.Container); err != nil { + err := runtime.ConnectContainerToNetwork(netConnect.Container, name, aliases) + if err != nil { if errors.Cause(err) == define.ErrNoSuchCtr { utils.ContainerNotFound(w, netConnect.Container, err) return } - utils.Error(w, "unable to lookup container", http.StatusInternalServerError, err) + if errors.Cause(err) == define.ErrNoSuchNetwork { + utils.Error(w, "network not found", http.StatusNotFound, err) + return + } + utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err) return } - logrus.Warnf("network connect endpoint is not fully implemented - tried to connect container %s to network %s", netConnect.Container, name) utils.WriteResponse(w, http.StatusOK, "OK") } // Disconnect removes a container from a network -// TODO: For now this func is a no-op that checks the container name, network name, and -// responds with a 200. This allows the call to remain intact. We need to decide how -// we make this work with CNI networking and setup/teardown. func Disconnect(w http.ResponseWriter, r *http.Request) { runtime := r.Context().Value("runtime").(*libpod.Runtime) @@ -362,29 +354,20 @@ func Disconnect(w http.ResponseWriter, r *http.Request) { utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()")) return } - config, err := runtime.GetConfig() - if err != nil { - utils.InternalServerError(w, err) - return - } + name := utils.GetName(r) - exists, err := network.Exists(config, name) + err := runtime.DisconnectContainerFromNetwork(netDisconnect.Container, name, netDisconnect.Force) if err != nil { - utils.InternalServerError(w, err) - return - } - if !exists { - utils.Error(w, "network not found", http.StatusNotFound, define.ErrNoSuchNetwork) - return - } - if _, err = runtime.LookupContainer(netDisconnect.Container); err != nil { if errors.Cause(err) == define.ErrNoSuchCtr { - utils.ContainerNotFound(w, netDisconnect.Container, err) + utils.Error(w, "container not found", http.StatusNotFound, err) + return + } + if errors.Cause(err) == define.ErrNoSuchNetwork { + utils.Error(w, "network not found", http.StatusNotFound, err) return } - utils.Error(w, "unable to lookup container", http.StatusInternalServerError, err) + utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err) return } - logrus.Warnf("network disconnect endpoint is not fully implemented - tried to connect container %s to network %s", netDisconnect.Container, name) utils.WriteResponse(w, http.StatusOK, "OK") } -- cgit v1.2.3-54-g00ecf