libpod: add c.ConfigWithNetworks()

Reading the networks requires an extra db operation. Most c.Config() callers
do not need them so create a new function which returns the config with
networks.

[NO NEW TESTS NEEDED]

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
Paul Holzinger 2022-05-06 13:49:07 +02:00
parent 5d5cb402cb
commit ed8c1dfb4c
No known key found for this signature in database
GPG Key ID: EB145DD938A3CAF2
2 changed files with 22 additions and 13 deletions

View File

@ -281,22 +281,30 @@ type ContainerNetworkDescriptions map[string]int
// Config accessors // Config accessors
// Unlocked // Unlocked
// Config returns the configuration used to create the container // Config returns the configuration used to create the container.
// Note that the returned config does not include the actual networks.
// Use ConfigWithNetworks() if you need them.
func (c *Container) Config() *ContainerConfig { func (c *Container) Config() *ContainerConfig {
returnConfig := new(ContainerConfig) returnConfig := new(ContainerConfig)
if err := JSONDeepCopy(c.config, returnConfig); err != nil { if err := JSONDeepCopy(c.config, returnConfig); err != nil {
return nil return nil
} }
return returnConfig
}
if c != nil { // Config returns the configuration used to create the container.
networks, err := c.networks() func (c *Container) ConfigWithNetworks() *ContainerConfig {
if err != nil { returnConfig := c.Config()
return nil if returnConfig == nil {
} return nil
returnConfig.Networks = networks
} }
networks, err := c.networks()
if err != nil {
return nil
}
returnConfig.Networks = networks
return returnConfig return returnConfig
} }
@ -1269,10 +1277,7 @@ func (c *Container) NetworkMode() string {
// Unlocked accessor for networks // Unlocked accessor for networks
func (c *Container) networks() (map[string]types.PerNetworkOptions, error) { func (c *Container) networks() (map[string]types.PerNetworkOptions, error) {
if c != nil && c.runtime != nil && c.runtime.state != nil { // can fail if c.networks is called from the tests return c.runtime.state.GetNetworks(c)
return c.runtime.state.GetNetworks(c)
}
return nil, nil
} }
// getInterfaceByName returns a formatted interface name for a given // getInterfaceByName returns a formatted interface name for a given

View File

@ -3,6 +3,7 @@ package generate
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"strings" "strings"
"time" "time"
@ -352,7 +353,10 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, contaierID s
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
conf := c.Config() conf := c.ConfigWithNetworks()
if conf == nil {
return nil, nil, fmt.Errorf("failed to get config for container %s", c.ID())
}
tmpSystemd := conf.Systemd tmpSystemd := conf.Systemd
tmpMounts := conf.Mounts tmpMounts := conf.Mounts