Include infra container information in `pod inspect`
We had a field for this in the inspect data, but it was never being populated. Because of this, `podman pod inspect` stopped showing port bindings (and other infra container settings). Add code to populate the infra container inspect data, and add a test to ensure we don't regress again. Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
		
							parent
							
								
									210f1040d2
								
							
						
					
					
						commit
						41457b5a28
					
				| 
						 | 
				
			
			@ -612,22 +612,11 @@ func (c *Container) generateInspectContainerHostConfig(ctrSpec *spec.Spec, named
 | 
			
		|||
 | 
			
		||||
	// Port bindings.
 | 
			
		||||
	// Only populate if we're using CNI to configure the network.
 | 
			
		||||
	portBindings := make(map[string][]define.InspectHostPort)
 | 
			
		||||
	if c.config.CreateNetNS {
 | 
			
		||||
		for _, port := range c.config.PortMappings {
 | 
			
		||||
			key := fmt.Sprintf("%d/%s", port.ContainerPort, port.Protocol)
 | 
			
		||||
			hostPorts := portBindings[key]
 | 
			
		||||
			if hostPorts == nil {
 | 
			
		||||
				hostPorts = []define.InspectHostPort{}
 | 
			
		||||
			}
 | 
			
		||||
			hostPorts = append(hostPorts, define.InspectHostPort{
 | 
			
		||||
				HostIP:   port.HostIP,
 | 
			
		||||
				HostPort: fmt.Sprintf("%d", port.HostPort),
 | 
			
		||||
			})
 | 
			
		||||
			portBindings[key] = hostPorts
 | 
			
		||||
		}
 | 
			
		||||
		hostConfig.PortBindings = makeInspectPortBindings(c.config.PortMappings)
 | 
			
		||||
	} else {
 | 
			
		||||
		hostConfig.PortBindings = make(map[string][]define.InspectHostPort)
 | 
			
		||||
	}
 | 
			
		||||
	hostConfig.PortBindings = portBindings
 | 
			
		||||
 | 
			
		||||
	// Cap add and cap drop.
 | 
			
		||||
	// We need a default set of capabilities to compare against.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,8 +3,6 @@ package define
 | 
			
		|||
import (
 | 
			
		||||
	"net"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/cri-o/ocicni/pkg/ocicni"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// InspectPodData contains detailed information on a pod's configuration and
 | 
			
		||||
| 
						 | 
				
			
			@ -60,7 +58,7 @@ type InspectPodData struct {
 | 
			
		|||
type InspectPodInfraConfig struct {
 | 
			
		||||
	// PortBindings are ports that will be forwarded to the infra container
 | 
			
		||||
	// and then shared with the pod.
 | 
			
		||||
	PortBindings []ocicni.PortMapping
 | 
			
		||||
	PortBindings map[string][]InspectHostPort
 | 
			
		||||
	// HostNetwork is whether the infra container (and thus the whole pod)
 | 
			
		||||
	// will use the host's network and not create a network namespace.
 | 
			
		||||
	HostNetwork bool
 | 
			
		||||
| 
						 | 
				
			
			@ -89,6 +87,8 @@ type InspectPodInfraConfig struct {
 | 
			
		|||
	// HostAdd adds a number of hosts to the infra container's resolv.conf
 | 
			
		||||
	// which will be shared with the rest of the pod.
 | 
			
		||||
	HostAdd []string
 | 
			
		||||
	// Networks is a list of CNI networks te pod will join.
 | 
			
		||||
	Networks []string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// InspectPodContainerInfo contains information on a container in a pod.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -587,21 +587,7 @@ func getContainerNetIO(ctr *Container) (*netlink.LinkStatistics, error) {
 | 
			
		|||
// network.
 | 
			
		||||
func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, error) {
 | 
			
		||||
	settings := new(define.InspectNetworkSettings)
 | 
			
		||||
	settings.Ports = make(map[string][]define.InspectHostPort)
 | 
			
		||||
	if c.config.PortMappings != nil {
 | 
			
		||||
		for _, port := range c.config.PortMappings {
 | 
			
		||||
			key := fmt.Sprintf("%d/%s", port.ContainerPort, port.Protocol)
 | 
			
		||||
			mapping := settings.Ports[key]
 | 
			
		||||
			if mapping == nil {
 | 
			
		||||
				mapping = []define.InspectHostPort{}
 | 
			
		||||
			}
 | 
			
		||||
			mapping = append(mapping, define.InspectHostPort{
 | 
			
		||||
				HostIP:   port.HostIP,
 | 
			
		||||
				HostPort: fmt.Sprintf("%d", port.HostPort),
 | 
			
		||||
			})
 | 
			
		||||
			settings.Ports[key] = mapping
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	settings.Ports = makeInspectPortBindings(c.config.PortMappings)
 | 
			
		||||
 | 
			
		||||
	// We can't do more if the network is down.
 | 
			
		||||
	if c.state.NetNS == nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -481,6 +481,51 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Infra config contains detailed information on the pod's infra
 | 
			
		||||
	// container.
 | 
			
		||||
	var infraConfig *define.InspectPodInfraConfig
 | 
			
		||||
	if p.config.InfraContainer != nil && p.config.InfraContainer.HasInfraContainer {
 | 
			
		||||
		infraConfig = new(define.InspectPodInfraConfig)
 | 
			
		||||
		infraConfig.HostNetwork = p.config.InfraContainer.HostNetwork
 | 
			
		||||
		infraConfig.StaticIP = p.config.InfraContainer.StaticIP
 | 
			
		||||
		infraConfig.StaticMAC = p.config.InfraContainer.StaticMAC
 | 
			
		||||
		infraConfig.NoManageResolvConf = p.config.InfraContainer.UseImageResolvConf
 | 
			
		||||
		infraConfig.NoManageHosts = p.config.InfraContainer.UseImageHosts
 | 
			
		||||
 | 
			
		||||
		if len(p.config.InfraContainer.DNSServer) > 0 {
 | 
			
		||||
			infraConfig.DNSServer = make([]string, 0, len(p.config.InfraContainer.DNSServer))
 | 
			
		||||
			for _, i := range p.config.InfraContainer.DNSServer {
 | 
			
		||||
				infraConfig.DNSServer = append(infraConfig.DNSServer, i)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if len(p.config.InfraContainer.DNSSearch) > 0 {
 | 
			
		||||
			infraConfig.DNSSearch = make([]string, 0, len(p.config.InfraContainer.DNSSearch))
 | 
			
		||||
			for _, i := range p.config.InfraContainer.DNSSearch {
 | 
			
		||||
				infraConfig.DNSSearch = append(infraConfig.DNSSearch, i)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if len(p.config.InfraContainer.DNSOption) > 0 {
 | 
			
		||||
			infraConfig.DNSOption = make([]string, 0, len(p.config.InfraContainer.DNSOption))
 | 
			
		||||
			for _, i := range p.config.InfraContainer.DNSOption {
 | 
			
		||||
				infraConfig.DNSOption = append(infraConfig.DNSOption, i)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if len(p.config.InfraContainer.HostAdd) > 0 {
 | 
			
		||||
			infraConfig.HostAdd = make([]string, 0, len(p.config.InfraContainer.HostAdd))
 | 
			
		||||
			for _, i := range p.config.InfraContainer.HostAdd {
 | 
			
		||||
				infraConfig.HostAdd = append(infraConfig.HostAdd, i)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if len(p.config.InfraContainer.Networks) > 0 {
 | 
			
		||||
			infraConfig.Networks = make([]string, 0, len(p.config.InfraContainer.Networks))
 | 
			
		||||
			for _, i := range p.config.InfraContainer.Networks {
 | 
			
		||||
				infraConfig.Networks = append(infraConfig.Networks, i)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		infraConfig.PortBindings = makeInspectPortBindings(p.config.InfraContainer.PortBindings)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	inspectData := define.InspectPodData{
 | 
			
		||||
		ID:               p.ID(),
 | 
			
		||||
		Name:             p.Name(),
 | 
			
		||||
| 
						 | 
				
			
			@ -495,7 +540,7 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
 | 
			
		|||
		CgroupPath:       p.state.CgroupPath,
 | 
			
		||||
		CreateInfra:      false,
 | 
			
		||||
		InfraContainerID: p.state.InfraContainerID,
 | 
			
		||||
		InfraConfig:      nil,
 | 
			
		||||
		InfraConfig:      infraConfig,
 | 
			
		||||
		SharedNamespaces: sharesNS,
 | 
			
		||||
		NumContainers:    uint(len(containers)),
 | 
			
		||||
		Containers:       ctrs,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,6 +15,7 @@ import (
 | 
			
		|||
	"github.com/containers/common/pkg/config"
 | 
			
		||||
	"github.com/containers/libpod/v2/libpod/define"
 | 
			
		||||
	"github.com/containers/libpod/v2/utils"
 | 
			
		||||
	"github.com/cri-o/ocicni/pkg/ocicni"
 | 
			
		||||
	"github.com/fsnotify/fsnotify"
 | 
			
		||||
	spec "github.com/opencontainers/runtime-spec/specs-go"
 | 
			
		||||
	"github.com/pkg/errors"
 | 
			
		||||
| 
						 | 
				
			
			@ -254,3 +255,21 @@ func makeHTTPAttachHeader(stream byte, length uint32) []byte {
 | 
			
		|||
	binary.BigEndian.PutUint32(header[4:], length)
 | 
			
		||||
	return header
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Convert OCICNI port bindings into Inspect-formatted port bindings.
 | 
			
		||||
func makeInspectPortBindings(bindings []ocicni.PortMapping) map[string][]define.InspectHostPort {
 | 
			
		||||
	portBindings := make(map[string][]define.InspectHostPort)
 | 
			
		||||
	for _, port := range bindings {
 | 
			
		||||
		key := fmt.Sprintf("%d/%s", port.ContainerPort, port.Protocol)
 | 
			
		||||
		hostPorts := portBindings[key]
 | 
			
		||||
		if hostPorts == nil {
 | 
			
		||||
			hostPorts = []define.InspectHostPort{}
 | 
			
		||||
		}
 | 
			
		||||
		hostPorts = append(hostPorts, define.InspectHostPort{
 | 
			
		||||
			HostIP:   port.HostIP,
 | 
			
		||||
			HostPort: fmt.Sprintf("%d", port.HostPort),
 | 
			
		||||
		})
 | 
			
		||||
		portBindings[key] = hostPorts
 | 
			
		||||
	}
 | 
			
		||||
	return portBindings
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,8 @@ package integration
 | 
			
		|||
import (
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"github.com/containers/libpod/v2/libpod/define"
 | 
			
		||||
 | 
			
		||||
	. "github.com/containers/libpod/v2/test/utils"
 | 
			
		||||
	. "github.com/onsi/ginkgo"
 | 
			
		||||
	. "github.com/onsi/gomega"
 | 
			
		||||
| 
						 | 
				
			
			@ -79,4 +81,22 @@ var _ = Describe("Podman pod inspect", func() {
 | 
			
		|||
		index := len(inspectCreateCommand) - len(createCommand)
 | 
			
		||||
		Expect(inspectCreateCommand[index:]).To(Equal(createCommand))
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	It("podman pod inspect outputs port bindings", func() {
 | 
			
		||||
		podName := "testPod"
 | 
			
		||||
		create := podmanTest.Podman([]string{"pod", "create", "--name", testPod, "-p", "8080:80"})
 | 
			
		||||
		create.WaitWithDefaultTimeout()
 | 
			
		||||
		Expect(create.ExitCode()).To(Equal(0))
 | 
			
		||||
 | 
			
		||||
		inspectOut := podmanTest.Podman([]string{"pod", "inspect", podName})
 | 
			
		||||
		inspectOut.WaitWithDefaultTimeout()
 | 
			
		||||
		Expect(inspectOut.ExitCode()).To(Equal(0))
 | 
			
		||||
 | 
			
		||||
		inspectJSON := new(define.InspectPodData)
 | 
			
		||||
		err := json.Unmarshal(inspectOut.Out.Contents(), inspectJSON)
 | 
			
		||||
		Expect(err).To(BeNil())
 | 
			
		||||
		Expect(inspectJSON.InfraConfig).To(Not(BeNil()))
 | 
			
		||||
		Expect(len(inspectJSON.PortBindings["80/tcp"])).To(Equal(1))
 | 
			
		||||
		Expect(inspectJSON.PortBindings["80/tcp"].HostPort).To(Equal("8080"))
 | 
			
		||||
	})
 | 
			
		||||
})
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue