mirror of
https://github.com/containers/podman.git
synced 2025-06-20 00:51:16 +08:00
Merge pull request #8355 from baude/compatnetworkconnectdisconnect
add network connect|disconnect compat endpoints
This commit is contained in:
@ -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 netConnect.EndpointConfig != nil {
|
||||
if netConnect.EndpointConfig.Aliases != nil {
|
||||
aliases = netConnect.EndpointConfig.Aliases
|
||||
}
|
||||
}
|
||||
err := runtime.ConnectContainerToNetwork(netConnect.Container, name, aliases)
|
||||
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(netConnect.Container); 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
|
||||
}
|
||||
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 disconnect endpoint is not fully implemented - tried to connect container %s to network %s", netDisconnect.Container, name)
|
||||
utils.WriteResponse(w, http.StatusOK, "OK")
|
||||
}
|
||||
|
@ -110,7 +110,11 @@ func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, o
|
||||
}
|
||||
|
||||
func (ic *ContainerEngine) NetworkCreate(ctx context.Context, name string, options entities.NetworkCreateOptions) (*entities.NetworkCreateReport, error) {
|
||||
return network.Create(name, options, ic.Libpod)
|
||||
runtimeConfig, err := ic.Libpod.GetConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return network.Create(name, options, runtimeConfig)
|
||||
}
|
||||
|
||||
func ifPassesFilterTest(netconf *libcni.NetworkConfigList, filter []string) bool {
|
||||
|
@ -6,8 +6,10 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
|
||||
"github.com/containers/podman/v2/cmd/podman/utils"
|
||||
"github.com/containers/podman/v2/libpod"
|
||||
"github.com/containers/podman/v2/pkg/cgroups"
|
||||
"github.com/containers/podman/v2/pkg/domain/entities"
|
||||
@ -16,6 +18,7 @@ import (
|
||||
"github.com/containers/storage"
|
||||
"github.com/containers/storage/pkg/idtools"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
@ -348,3 +351,24 @@ func ParseIDMapping(mode namespaces.UsernsMode, uidMapSlice, gidMapSlice []strin
|
||||
}
|
||||
return &options, nil
|
||||
}
|
||||
|
||||
// StartWatcher starts a new SIGHUP go routine for the current config.
|
||||
func StartWatcher(rt *libpod.Runtime) {
|
||||
// Setup the signal notifier
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, utils.SIGHUP)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
// Block until the signal is received
|
||||
logrus.Debugf("waiting for SIGHUP to reload configuration")
|
||||
<-ch
|
||||
if err := rt.Reload(); err != nil {
|
||||
logrus.Errorf("unable to reload configuration: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
logrus.Debugf("registered SIGHUP watcher for config")
|
||||
}
|
||||
|
Reference in New Issue
Block a user