mirror of
https://github.com/containers/podman.git
synced 2025-05-21 17:16:22 +08:00
libpod: do not call (*container).Config()
Access the container's config field directly inside of libpod instead of calling `Config()` which in turn creates expensive JSON deep copies. Accessing the field directly drops memory consumption of a simple `podman run --rm busybox true` from 1245kB to 410kB. [NO TESTS NEEDED] Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
@ -174,7 +174,7 @@ func (c *Container) copyToArchive(ctx context.Context, path string, writer io.Wr
|
|||||||
|
|
||||||
// getContainerUser returns the specs.User and ID mappings of the container.
|
// getContainerUser returns the specs.User and ID mappings of the container.
|
||||||
func getContainerUser(container *Container, mountPoint string) (specs.User, error) {
|
func getContainerUser(container *Container, mountPoint string) (specs.User, error) {
|
||||||
userspec := container.Config().User
|
userspec := container.config.User
|
||||||
|
|
||||||
uid, gid, _, err := chrootuser.GetUser(mountPoint, userspec)
|
uid, gid, _, err := chrootuser.GetUser(mountPoint, userspec)
|
||||||
u := specs.User{
|
u := specs.User{
|
||||||
|
@ -923,12 +923,11 @@ func (c *Container) checkDependenciesRunning() ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check the status
|
// Check the status
|
||||||
conf := depCtr.Config()
|
|
||||||
state, err := depCtr.State()
|
state, err := depCtr.State()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error retrieving state of dependency %s of container %s", dep, c.ID())
|
return nil, errors.Wrapf(err, "error retrieving state of dependency %s of container %s", dep, c.ID())
|
||||||
}
|
}
|
||||||
if state != define.ContainerStateRunning && !conf.IsInfra {
|
if state != define.ContainerStateRunning && !depCtr.config.IsInfra {
|
||||||
notRunning = append(notRunning, dep)
|
notRunning = append(notRunning, dep)
|
||||||
}
|
}
|
||||||
depCtrs[dep] = depCtr
|
depCtrs[dep] = depCtr
|
||||||
@ -1003,7 +1002,7 @@ func (c *Container) cniHosts() string {
|
|||||||
for _, status := range c.getNetworkStatus() {
|
for _, status := range c.getNetworkStatus() {
|
||||||
for _, netInt := range status.Interfaces {
|
for _, netInt := range status.Interfaces {
|
||||||
for _, netAddress := range netInt.Networks {
|
for _, netAddress := range netInt.Networks {
|
||||||
hosts += fmt.Sprintf("%s\t%s %s\n", netAddress.Subnet.IP.String(), c.Hostname(), c.Config().Name)
|
hosts += fmt.Sprintf("%s\t%s %s\n", netAddress.Subnet.IP.String(), c.Hostname(), c.config.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2106,7 +2105,7 @@ func (c *Container) canWithPrevious() error {
|
|||||||
// JSON files for later export
|
// JSON files for later export
|
||||||
func (c *Container) prepareCheckpointExport() error {
|
func (c *Container) prepareCheckpointExport() error {
|
||||||
// save live config
|
// save live config
|
||||||
if _, err := metadata.WriteJSONFile(c.Config(), c.bundlePath(), metadata.ConfigDumpFile); err != nil {
|
if _, err := metadata.WriteJSONFile(c.config, c.bundlePath(), metadata.ConfigDumpFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ func (c *Container) resolvePath(mountPoint string, containerPath string) (string
|
|||||||
func findVolume(c *Container, containerPath string) (*Volume, error) {
|
func findVolume(c *Container, containerPath string) (*Volume, error) {
|
||||||
runtime := c.Runtime()
|
runtime := c.Runtime()
|
||||||
cleanedContainerPath := filepath.Clean(containerPath)
|
cleanedContainerPath := filepath.Clean(containerPath)
|
||||||
for _, vol := range c.Config().NamedVolumes {
|
for _, vol := range c.config.NamedVolumes {
|
||||||
if cleanedContainerPath == filepath.Clean(vol.Dest) {
|
if cleanedContainerPath == filepath.Clean(vol.Dest) {
|
||||||
return runtime.GetVolume(vol.Name)
|
return runtime.GetVolume(vol.Name)
|
||||||
}
|
}
|
||||||
@ -124,7 +124,7 @@ func findVolume(c *Container, containerPath string) (*Volume, error) {
|
|||||||
// Volume's destination.
|
// Volume's destination.
|
||||||
func isPathOnVolume(c *Container, containerPath string) bool {
|
func isPathOnVolume(c *Container, containerPath string) bool {
|
||||||
cleanedContainerPath := filepath.Clean(containerPath)
|
cleanedContainerPath := filepath.Clean(containerPath)
|
||||||
for _, vol := range c.Config().NamedVolumes {
|
for _, vol := range c.config.NamedVolumes {
|
||||||
if cleanedContainerPath == filepath.Clean(vol.Dest) {
|
if cleanedContainerPath == filepath.Clean(vol.Dest) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ func isPathOnVolume(c *Container, containerPath string) bool {
|
|||||||
// path of a Mount. Returns a matching Mount or nil.
|
// path of a Mount. Returns a matching Mount or nil.
|
||||||
func findBindMount(c *Container, containerPath string) *specs.Mount {
|
func findBindMount(c *Container, containerPath string) *specs.Mount {
|
||||||
cleanedPath := filepath.Clean(containerPath)
|
cleanedPath := filepath.Clean(containerPath)
|
||||||
for _, m := range c.Config().Spec.Mounts {
|
for _, m := range c.config.Spec.Mounts {
|
||||||
if m.Type != "bind" {
|
if m.Type != "bind" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -157,7 +157,7 @@ func findBindMount(c *Container, containerPath string) *specs.Mount {
|
|||||||
// Mount's destination.
|
// Mount's destination.
|
||||||
func isPathOnBindMount(c *Container, containerPath string) bool {
|
func isPathOnBindMount(c *Container, containerPath string) bool {
|
||||||
cleanedContainerPath := filepath.Clean(containerPath)
|
cleanedContainerPath := filepath.Clean(containerPath)
|
||||||
for _, m := range c.Config().Spec.Mounts {
|
for _, m := range c.config.Spec.Mounts {
|
||||||
if cleanedContainerPath == filepath.Clean(m.Destination) {
|
if cleanedContainerPath == filepath.Clean(m.Destination) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ func (p *Pod) GenerateForKube(ctx context.Context) (*v1.Pod, []v1.ServicePort, e
|
|||||||
// so set it at here
|
// so set it at here
|
||||||
for _, ctr := range allContainers {
|
for _, ctr := range allContainers {
|
||||||
if !ctr.IsInfra() {
|
if !ctr.IsInfra() {
|
||||||
switch ctr.Config().RestartPolicy {
|
switch ctr.config.RestartPolicy {
|
||||||
case define.RestartPolicyAlways:
|
case define.RestartPolicyAlways:
|
||||||
pod.Spec.RestartPolicy = v1.RestartPolicyAlways
|
pod.Spec.RestartPolicy = v1.RestartPolicyAlways
|
||||||
case define.RestartPolicyOnFailure:
|
case define.RestartPolicyOnFailure:
|
||||||
|
@ -222,7 +222,7 @@ func (r *Runtime) setupSlirp4netns(ctr *Container) error {
|
|||||||
defer errorhandling.CloseQuiet(syncR)
|
defer errorhandling.CloseQuiet(syncR)
|
||||||
defer errorhandling.CloseQuiet(syncW)
|
defer errorhandling.CloseQuiet(syncW)
|
||||||
|
|
||||||
havePortMapping := len(ctr.Config().PortMappings) > 0
|
havePortMapping := len(ctr.config.PortMappings) > 0
|
||||||
logPath := filepath.Join(ctr.runtime.config.Engine.TmpDir, fmt.Sprintf("slirp4netns-%s.log", ctr.config.ID))
|
logPath := filepath.Join(ctr.runtime.config.Engine.TmpDir, fmt.Sprintf("slirp4netns-%s.log", ctr.config.ID))
|
||||||
|
|
||||||
ctrNetworkSlipOpts := []string{}
|
ctrNetworkSlipOpts := []string{}
|
||||||
|
@ -1150,7 +1150,7 @@ func (r *ConmonOCIRuntime) createOCIContainer(ctr *Container, restoreOptions *Co
|
|||||||
|
|
||||||
if ctr.config.NetMode.IsSlirp4netns() || rootless.IsRootless() {
|
if ctr.config.NetMode.IsSlirp4netns() || rootless.IsRootless() {
|
||||||
if ctr.config.PostConfigureNetNS {
|
if ctr.config.PostConfigureNetNS {
|
||||||
havePortMapping := len(ctr.Config().PortMappings) > 0
|
havePortMapping := len(ctr.config.PortMappings) > 0
|
||||||
if havePortMapping {
|
if havePortMapping {
|
||||||
ctr.rootlessPortSyncR, ctr.rootlessPortSyncW, err = os.Pipe()
|
ctr.rootlessPortSyncR, ctr.rootlessPortSyncW, err = os.Pipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -164,8 +164,7 @@ func (p *Pod) PidMode() string {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
conf := infra.Config()
|
ctrSpec := infra.config.Spec
|
||||||
ctrSpec := conf.Spec
|
|
||||||
if ctrSpec != nil && ctrSpec.Linux != nil {
|
if ctrSpec != nil && ctrSpec.Linux != nil {
|
||||||
for _, ns := range ctrSpec.Linux.Namespaces {
|
for _, ns := range ctrSpec.Linux.Namespaces {
|
||||||
if ns.Type == specs.PIDNamespace {
|
if ns.Type == specs.PIDNamespace {
|
||||||
@ -186,8 +185,7 @@ func (p *Pod) UserNSMode() string {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
conf := infra.Config()
|
ctrSpec := infra.config.Spec
|
||||||
ctrSpec := conf.Spec
|
|
||||||
if ctrSpec != nil && ctrSpec.Linux != nil {
|
if ctrSpec != nil && ctrSpec.Linux != nil {
|
||||||
for _, ns := range ctrSpec.Linux.Namespaces {
|
for _, ns := range ctrSpec.Linux.Namespaces {
|
||||||
if ns.Type == specs.UserNamespace {
|
if ns.Type == specs.UserNamespace {
|
||||||
|
@ -34,7 +34,7 @@ func (p *Pod) startInitContainers(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
// If the container is a once init container, we need to remove it
|
// If the container is a once init container, we need to remove it
|
||||||
// after it runs
|
// after it runs
|
||||||
if initCon.Config().InitContainerType == define.OneShotInitContainer {
|
if initCon.config.InitContainerType == define.OneShotInitContainer {
|
||||||
icLock := initCon.lock
|
icLock := initCon.lock
|
||||||
icLock.Lock()
|
icLock.Lock()
|
||||||
if err := p.runtime.removeContainer(ctx, initCon, false, false, true); err != nil {
|
if err := p.runtime.removeContainer(ctx, initCon, false, false, true); err != nil {
|
||||||
@ -590,16 +590,16 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
infraConfig = new(define.InspectPodInfraConfig)
|
infraConfig = new(define.InspectPodInfraConfig)
|
||||||
infraConfig.HostNetwork = !infra.Config().ContainerNetworkConfig.UseImageHosts
|
infraConfig.HostNetwork = !infra.config.ContainerNetworkConfig.UseImageHosts
|
||||||
infraConfig.StaticIP = infra.Config().ContainerNetworkConfig.StaticIP
|
infraConfig.StaticIP = infra.config.ContainerNetworkConfig.StaticIP
|
||||||
infraConfig.NoManageResolvConf = infra.Config().UseImageResolvConf
|
infraConfig.NoManageResolvConf = infra.config.UseImageResolvConf
|
||||||
infraConfig.NoManageHosts = infra.Config().UseImageHosts
|
infraConfig.NoManageHosts = infra.config.UseImageHosts
|
||||||
infraConfig.CPUPeriod = p.CPUPeriod()
|
infraConfig.CPUPeriod = p.CPUPeriod()
|
||||||
infraConfig.CPUQuota = p.CPUQuota()
|
infraConfig.CPUQuota = p.CPUQuota()
|
||||||
infraConfig.CPUSetCPUs = p.ResourceLim().CPU.Cpus
|
infraConfig.CPUSetCPUs = p.ResourceLim().CPU.Cpus
|
||||||
infraConfig.PidNS = p.PidMode()
|
infraConfig.PidNS = p.PidMode()
|
||||||
infraConfig.UserNS = p.UserNSMode()
|
infraConfig.UserNS = p.UserNSMode()
|
||||||
namedVolumes, mounts := infra.sortUserVolumes(infra.Config().Spec)
|
namedVolumes, mounts := infra.sortUserVolumes(infra.config.Spec)
|
||||||
inspectMounts, err = infra.GetInspectMounts(namedVolumes, infra.config.ImageVolumes, mounts)
|
inspectMounts, err = infra.GetInspectMounts(namedVolumes, infra.config.ImageVolumes, mounts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -611,30 +611,30 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(infra.Config().ContainerNetworkConfig.DNSServer) > 0 {
|
if len(infra.config.ContainerNetworkConfig.DNSServer) > 0 {
|
||||||
infraConfig.DNSServer = make([]string, 0, len(infra.Config().ContainerNetworkConfig.DNSServer))
|
infraConfig.DNSServer = make([]string, 0, len(infra.config.ContainerNetworkConfig.DNSServer))
|
||||||
for _, entry := range infra.Config().ContainerNetworkConfig.DNSServer {
|
for _, entry := range infra.config.ContainerNetworkConfig.DNSServer {
|
||||||
infraConfig.DNSServer = append(infraConfig.DNSServer, entry.String())
|
infraConfig.DNSServer = append(infraConfig.DNSServer, entry.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(infra.Config().ContainerNetworkConfig.DNSSearch) > 0 {
|
if len(infra.config.ContainerNetworkConfig.DNSSearch) > 0 {
|
||||||
infraConfig.DNSSearch = make([]string, 0, len(infra.Config().ContainerNetworkConfig.DNSSearch))
|
infraConfig.DNSSearch = make([]string, 0, len(infra.config.ContainerNetworkConfig.DNSSearch))
|
||||||
infraConfig.DNSSearch = append(infraConfig.DNSSearch, infra.Config().ContainerNetworkConfig.DNSSearch...)
|
infraConfig.DNSSearch = append(infraConfig.DNSSearch, infra.config.ContainerNetworkConfig.DNSSearch...)
|
||||||
}
|
}
|
||||||
if len(infra.Config().ContainerNetworkConfig.DNSOption) > 0 {
|
if len(infra.config.ContainerNetworkConfig.DNSOption) > 0 {
|
||||||
infraConfig.DNSOption = make([]string, 0, len(infra.Config().ContainerNetworkConfig.DNSOption))
|
infraConfig.DNSOption = make([]string, 0, len(infra.config.ContainerNetworkConfig.DNSOption))
|
||||||
infraConfig.DNSOption = append(infraConfig.DNSOption, infra.Config().ContainerNetworkConfig.DNSOption...)
|
infraConfig.DNSOption = append(infraConfig.DNSOption, infra.config.ContainerNetworkConfig.DNSOption...)
|
||||||
}
|
}
|
||||||
if len(infra.Config().HostAdd) > 0 {
|
if len(infra.config.HostAdd) > 0 {
|
||||||
infraConfig.HostAdd = make([]string, 0, len(infra.Config().HostAdd))
|
infraConfig.HostAdd = make([]string, 0, len(infra.config.HostAdd))
|
||||||
infraConfig.HostAdd = append(infraConfig.HostAdd, infra.Config().HostAdd...)
|
infraConfig.HostAdd = append(infraConfig.HostAdd, infra.config.HostAdd...)
|
||||||
}
|
}
|
||||||
if len(infra.Config().ContainerNetworkConfig.Networks) > 0 {
|
if len(infra.config.ContainerNetworkConfig.Networks) > 0 {
|
||||||
infraConfig.Networks = make([]string, 0, len(infra.Config().ContainerNetworkConfig.Networks))
|
infraConfig.Networks = make([]string, 0, len(infra.config.ContainerNetworkConfig.Networks))
|
||||||
infraConfig.Networks = append(infraConfig.Networks, infra.Config().ContainerNetworkConfig.Networks...)
|
infraConfig.Networks = append(infraConfig.Networks, infra.config.ContainerNetworkConfig.Networks...)
|
||||||
}
|
}
|
||||||
infraConfig.NetworkOptions = infra.Config().ContainerNetworkConfig.NetworkOptions
|
infraConfig.NetworkOptions = infra.config.ContainerNetworkConfig.NetworkOptions
|
||||||
infraConfig.PortBindings = makeInspectPortBindings(infra.Config().ContainerNetworkConfig.PortMappings, nil)
|
infraConfig.PortBindings = makeInspectPortBindings(infra.config.ContainerNetworkConfig.PortMappings, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
inspectData := define.InspectPodData{
|
inspectData := define.InspectPodData{
|
||||||
|
Reference in New Issue
Block a user