mirror of
https://github.com/containers/podman.git
synced 2025-07-02 16:57:24 +08:00
Implement network disconnect for SQLite state
Signed-off-by: Matt Heon <mheon@redhat.com>
This commit is contained in:
@ -799,119 +799,18 @@ func (s *SQLiteState) GetNetworks(ctr *Container) (map[string]types.PerNetworkOp
|
|||||||
// NetworkConnect adds the given container to the given network. If aliases are
|
// NetworkConnect adds the given container to the given network. If aliases are
|
||||||
// specified, those will be added to the given network.
|
// specified, those will be added to the given network.
|
||||||
func (s *SQLiteState) NetworkConnect(ctr *Container, network string, opts types.PerNetworkOptions) error {
|
func (s *SQLiteState) NetworkConnect(ctr *Container, network string, opts types.PerNetworkOptions) error {
|
||||||
return s.networkModify(ctr, network, opts, true)
|
return s.networkModify(ctr, network, opts, true, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NetworkModify will allow you to set new options on an existing connected network
|
// NetworkModify will allow you to set new options on an existing connected network
|
||||||
func (s *SQLiteState) NetworkModify(ctr *Container, network string, opts types.PerNetworkOptions) error {
|
func (s *SQLiteState) NetworkModify(ctr *Container, network string, opts types.PerNetworkOptions) error {
|
||||||
return s.networkModify(ctr, network, opts, false)
|
return s.networkModify(ctr, network, opts, false, false)
|
||||||
}
|
|
||||||
|
|
||||||
// networkModify allows you to modify or add a new network, to add a new network use the new bool
|
|
||||||
func (s *SQLiteState) networkModify(ctr *Container, network string, opts types.PerNetworkOptions, new bool) error {
|
|
||||||
if !s.valid {
|
|
||||||
return define.ErrDBClosed
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ctr.valid {
|
|
||||||
return define.ErrCtrRemoved
|
|
||||||
}
|
|
||||||
|
|
||||||
if network == "" {
|
|
||||||
return fmt.Errorf("network names must not be empty: %w", define.ErrInvalidArg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Grab a fresh copy of the config, in case anything changed
|
|
||||||
newCfg, err := s.getCtrConfig(ctr.ID())
|
|
||||||
if err != nil && errors.Is(err, define.ErrNoSuchCtr) {
|
|
||||||
ctr.valid = false
|
|
||||||
return define.ErrNoSuchCtr
|
|
||||||
}
|
|
||||||
|
|
||||||
_, ok := newCfg.Networks[network]
|
|
||||||
if new && ok {
|
|
||||||
return fmt.Errorf("container %s is already connected to network %s: %w", ctr.ID(), network, define.ErrInvalidArg)
|
|
||||||
}
|
|
||||||
if !new && !ok {
|
|
||||||
return fmt.Errorf("container %s is not connected to network %s: %w", ctr.ID(), network, define.ErrInvalidArg)
|
|
||||||
}
|
|
||||||
|
|
||||||
newCfg.Networks[network] = opts
|
|
||||||
|
|
||||||
if err := s.rewriteContainerConfig(ctr, newCfg); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
ctr.config = newCfg
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NetworkDisconnect disconnects the container from the given network, also
|
// NetworkDisconnect disconnects the container from the given network, also
|
||||||
// removing any aliases in the network.
|
// removing any aliases in the network.
|
||||||
// TODO TODO TODO
|
|
||||||
func (s *SQLiteState) NetworkDisconnect(ctr *Container, network string) error {
|
func (s *SQLiteState) NetworkDisconnect(ctr *Container, network string) error {
|
||||||
if !s.valid {
|
return s.networkModify(ctr, network, types.PerNetworkOptions{}, false, true)
|
||||||
return define.ErrDBClosed
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ctr.valid {
|
|
||||||
return define.ErrCtrRemoved
|
|
||||||
}
|
|
||||||
|
|
||||||
if network == "" {
|
|
||||||
return fmt.Errorf("network names must not be empty: %w", define.ErrInvalidArg)
|
|
||||||
}
|
|
||||||
|
|
||||||
return define.ErrNotImplemented
|
|
||||||
|
|
||||||
// ctrID := []byte(ctr.ID())
|
|
||||||
|
|
||||||
// db, err := s.getDBCon()
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// defer s.deferredCloseDBCon(db)
|
|
||||||
|
|
||||||
// return db.Update(func(tx *bolt.Tx) error {
|
|
||||||
// ctrBucket, err := getCtrBucket(tx)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// dbCtr := ctrBucket.Bucket(ctrID)
|
|
||||||
// if dbCtr == nil {
|
|
||||||
// ctr.valid = false
|
|
||||||
// return fmt.Errorf("container %s does not exist in database: %w", ctr.ID(), define.ErrNoSuchCtr)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// ctrAliasesBkt := dbCtr.Bucket(aliasesBkt)
|
|
||||||
// ctrNetworksBkt := dbCtr.Bucket(networksBkt)
|
|
||||||
// if ctrNetworksBkt == nil {
|
|
||||||
// return fmt.Errorf("container %s is not connected to any networks, so cannot disconnect: %w", ctr.ID(), define.ErrNoSuchNetwork)
|
|
||||||
// }
|
|
||||||
// netConnected := ctrNetworksBkt.Get([]byte(network))
|
|
||||||
// if netConnected == nil {
|
|
||||||
// return fmt.Errorf("container %s is not connected to network %q: %w", ctr.ID(), network, define.ErrNoSuchNetwork)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if err := ctrNetworksBkt.Delete([]byte(network)); err != nil {
|
|
||||||
// return fmt.Errorf("removing container %s from network %s: %w", ctr.ID(), network, err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if ctrAliasesBkt != nil {
|
|
||||||
// bktExists := ctrAliasesBkt.Bucket([]byte(network))
|
|
||||||
// if bktExists == nil {
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if err := ctrAliasesBkt.DeleteBucket([]byte(network)); err != nil {
|
|
||||||
// return fmt.Errorf("removing container %s network aliases for network %s: %w", ctr.ID(), network, err)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return nil
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetContainerConfig returns a container config from the database by full ID
|
// GetContainerConfig returns a container config from the database by full ID
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/containers/common/libnetwork/types"
|
||||||
"github.com/containers/podman/v4/libpod/define"
|
"github.com/containers/podman/v4/libpod/define"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
@ -429,3 +430,51 @@ func (s *SQLiteState) removeContainer(ctr *Container) (defErr error) {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// networkModify allows you to modify or add a new network, to add a new network use the new bool
|
||||||
|
func (s *SQLiteState) networkModify(ctr *Container, network string, opts types.PerNetworkOptions, new, disconnect bool) error {
|
||||||
|
if !s.valid {
|
||||||
|
return define.ErrDBClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ctr.valid {
|
||||||
|
return define.ErrCtrRemoved
|
||||||
|
}
|
||||||
|
|
||||||
|
if network == "" {
|
||||||
|
return fmt.Errorf("network names must not be empty: %w", define.ErrInvalidArg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if new && disconnect {
|
||||||
|
return fmt.Errorf("new and disconnect are mutually exclusive: %w", define.ErrInvalidArg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Grab a fresh copy of the config, in case anything changed
|
||||||
|
newCfg, err := s.getCtrConfig(ctr.ID())
|
||||||
|
if err != nil && errors.Is(err, define.ErrNoSuchCtr) {
|
||||||
|
ctr.valid = false
|
||||||
|
return define.ErrNoSuchCtr
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ok := newCfg.Networks[network]
|
||||||
|
if new && ok {
|
||||||
|
return fmt.Errorf("container %s is already connected to network %s: %w", ctr.ID(), network, define.ErrNoSuchNetwork)
|
||||||
|
}
|
||||||
|
if !ok && (!new || disconnect) {
|
||||||
|
return fmt.Errorf("container %s is not connected to network %s: %w", ctr.ID(), network, define.ErrNoSuchNetwork)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !disconnect {
|
||||||
|
newCfg.Networks[network] = opts
|
||||||
|
} else {
|
||||||
|
delete(newCfg.Networks, network)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.rewriteContainerConfig(ctr, newCfg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ctr.config = newCfg
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user