Files
Aditya Rajan 11fc0e5540 kube: Add support for podman pod logs
Following PR adds support for `kubectl` like `pod logs` to podman.
Usage `podman pod logs <podIDorName` gives a stream of logs for all
the containers within the pod with **containername** as a field.

Just like **`kubectl`** also supports `podman pod logs -c ctrIDorName podIDorName`
to limit the log stream to any of the specificied container which belongs to pod.

Signed-off-by: Aditya Rajan <arajan@redhat.com>
2021-09-05 16:24:49 +05:30

456 lines
9.7 KiB
Go

package entities
import (
"errors"
"strings"
"time"
"github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/pkg/specgen"
"github.com/containers/podman/v3/pkg/util"
"github.com/opencontainers/runtime-spec/specs-go"
)
type PodKillOptions struct {
All bool
Latest bool
Signal string
}
type PodKillReport struct {
Errs []error
Id string //nolint
}
type ListPodsReport struct {
Cgroup string
Containers []*ListPodContainer
Created time.Time
Id string //nolint
InfraId string //nolint
Name string
Namespace string
// Network names connected to infra container
Networks []string
Status string
Labels map[string]string
}
type ListPodContainer struct {
Id string //nolint
Names string
Status string
}
type PodPauseOptions struct {
All bool
Latest bool
}
type PodPauseReport struct {
Errs []error
Id string //nolint
}
type PodunpauseOptions struct {
All bool
Latest bool
}
type PodUnpauseReport struct {
Errs []error
Id string //nolint
}
type PodStopOptions struct {
All bool
Ignore bool
Latest bool
Timeout int
}
type PodStopReport struct {
Errs []error
Id string //nolint
}
type PodRestartOptions struct {
All bool
Latest bool
}
type PodRestartReport struct {
Errs []error
Id string //nolint
}
type PodStartOptions struct {
All bool
Latest bool
}
type PodStartReport struct {
Errs []error
Id string //nolint
}
type PodRmOptions struct {
All bool
Force bool
Ignore bool
Latest bool
}
type PodRmReport struct {
Err error
Id string //nolint
}
// PddSpec is an abstracted version of PodSpecGen designed to eventually accept options
// not meant to be in a specgen
type PodSpec struct {
PodSpecGen specgen.PodSpecGenerator
}
// PodCreateOptions provides all possible options for creating a pod and its infra container
// swagger:model PodCreateOptions
type PodCreateOptions struct {
CGroupParent string
CreateCommand []string
Hostname string
Infra bool
InfraImage string
InfraName string
InfraCommand string
InfraConmonPidFile string
Labels map[string]string
Name string
Net *NetOptions
Share []string
Pid string
Cpus float64
CpusetCpus string
Userns specgen.Namespace
}
// PodLogsOptions describes the options to extract pod logs.
type PodLogsOptions struct {
// Other fields are exactly same as ContainerLogOpts
ContainerLogsOptions
// If specified will only fetch the logs of specified container
ContainerName string
}
type ContainerCreateOptions struct {
Annotation []string
Attach []string
Authfile string
BlkIOWeight string
BlkIOWeightDevice []string
CapAdd []string
CapDrop []string
CgroupNS string
CGroupsMode string
CGroupParent string
CIDFile string
ConmonPIDFile string
CPUPeriod uint64
CPUQuota int64
CPURTPeriod uint64
CPURTRuntime int64
CPUShares uint64
CPUS float64
CPUSetCPUs string
CPUSetMems string
Devices []string
DeviceCGroupRule []string
DeviceReadBPs []string
DeviceReadIOPs []string
DeviceWriteBPs []string
DeviceWriteIOPs []string
Entrypoint *string
Env []string
EnvHost bool
EnvFile []string
Expose []string
GIDMap []string
GroupAdd []string
HealthCmd string
HealthInterval string
HealthRetries uint
HealthStartPeriod string
HealthTimeout string
Hostname string
HTTPProxy bool
ImageVolume string
Init bool
InitContainerType string
InitPath string
Interactive bool
IPC string
KernelMemory string
Label []string
LabelFile []string
LogDriver string
LogOptions []string
Memory string
MemoryReservation string
MemorySwap string
MemorySwappiness int64
Name string
NoHealthCheck bool
OOMKillDisable bool
OOMScoreAdj int
Arch string
OS string
Variant string
PID string
PIDsLimit *int64
Platform string
Pod string
PodIDFile string
Personality string
PreserveFDs uint
Privileged bool
PublishAll bool
Pull string
Quiet bool
ReadOnly bool
ReadOnlyTmpFS bool
Restart string
Replace bool
Requires []string
Rm bool
RootFS bool
Secrets []string
SecurityOpt []string
SdNotifyMode string
ShmSize string
SignaturePolicy string
StopSignal string
StopTimeout uint
StorageOpt []string
SubUIDName string
SubGIDName string
Sysctl []string
Systemd string
Timeout uint
TLSVerify bool
TmpFS []string
TTY bool
Timezone string
Umask string
UIDMap []string
Ulimit []string
User string
UserNS string
UTS string
Mount []string
Volume []string
VolumesFrom []string
Workdir string
SeccompPolicy string
PidFile string
IsInfra bool
Net *NetOptions
CgroupConf []string
}
type PodCreateReport struct {
Id string //nolint
}
func (p *PodCreateOptions) CPULimits() *specs.LinuxCPU {
cpu := &specs.LinuxCPU{}
hasLimits := false
if p.Cpus != 0 {
period, quota := util.CoresToPeriodAndQuota(p.Cpus)
cpu.Period = &period
cpu.Quota = &quota
hasLimits = true
}
if p.CpusetCpus != "" {
cpu.Cpus = p.CpusetCpus
hasLimits = true
}
if !hasLimits {
return cpu
}
return cpu
}
func ToPodSpecGen(s specgen.PodSpecGenerator, p *PodCreateOptions) (*specgen.PodSpecGenerator, error) {
// Basic Config
s.Name = p.Name
s.InfraName = p.InfraName
out, err := specgen.ParseNamespace(p.Pid)
if err != nil {
return nil, err
}
s.Pid = out
s.Hostname = p.Hostname
s.Labels = p.Labels
s.NoInfra = !p.Infra
if len(p.InfraCommand) > 0 {
s.InfraCommand = strings.Split(p.InfraCommand, " ")
}
if len(p.InfraConmonPidFile) > 0 {
s.InfraConmonPidFile = p.InfraConmonPidFile
}
s.InfraImage = p.InfraImage
s.SharedNamespaces = p.Share
s.PodCreateCommand = p.CreateCommand
// Networking config
if p.Net != nil {
s.NetNS = p.Net.Network
s.StaticIP = p.Net.StaticIP
s.StaticMAC = p.Net.StaticMAC
s.PortMappings = p.Net.PublishPorts
s.CNINetworks = p.Net.CNINetworks
s.NetworkOptions = p.Net.NetworkOptions
if p.Net.UseImageResolvConf {
s.NoManageResolvConf = true
}
s.DNSServer = p.Net.DNSServers
s.DNSSearch = p.Net.DNSSearch
s.DNSOption = p.Net.DNSOptions
s.NoManageHosts = p.Net.NoHosts
s.HostAdd = p.Net.AddHosts
}
// Cgroup
s.CgroupParent = p.CGroupParent
// Resource config
cpuDat := p.CPULimits()
if s.ResourceLimits == nil {
s.ResourceLimits = &specs.LinuxResources{}
s.ResourceLimits.CPU = &specs.LinuxCPU{}
}
if cpuDat != nil {
s.ResourceLimits.CPU = cpuDat
if p.Cpus != 0 {
s.CPUPeriod = *cpuDat.Period
s.CPUQuota = *cpuDat.Quota
}
}
s.Userns = p.Userns
return &s, nil
}
type PodPruneOptions struct {
Force bool `json:"force" schema:"force"`
}
type PodPruneReport struct {
Err error
Id string //nolint
}
type PodTopOptions struct {
// CLI flags.
ListDescriptors bool
Latest bool
// Options for the API.
Descriptors []string
NameOrID string
}
type PodPSOptions struct {
CtrNames bool
CtrIds bool
CtrStatus bool
Filters map[string][]string
Format string
Latest bool
Namespace bool
Quiet bool
Sort string
}
type PodInspectOptions struct {
Latest bool
// Options for the API.
NameOrID string
Format string
}
type PodInspectReport struct {
*define.InspectPodData
}
// PodStatsOptions are options for the pod stats command.
type PodStatsOptions struct {
// All - provide stats for all running pods.
All bool
// Latest - provide stats for the latest pod.
Latest bool
}
// PodStatsReport includes pod-resource statistics data.
type PodStatsReport struct {
CPU string
MemUsage string
MemUsageBytes string
Mem string
NetIO string
BlockIO string
PIDS string
Pod string
CID string
Name string
}
// ValidatePodStatsOptions validates the specified slice and options. Allows
// for sharing code in the front- and the back-end.
func ValidatePodStatsOptions(args []string, options *PodStatsOptions) error {
num := 0
if len(args) > 0 {
num++
}
if options.All {
num++
}
if options.Latest {
num++
}
switch num {
case 0:
// Podman v1 compat: if nothing's specified get all running
// pods.
options.All = true
return nil
case 1:
return nil
default:
return errors.New("--all, --latest and arguments cannot be used together")
}
}
// Converts PodLogOptions to ContainerLogOptions
func PodLogsOptionsToContainerLogsOptions(options PodLogsOptions) ContainerLogsOptions {
// PodLogsOptions are similar but contains few extra fields like ctrName
// So cast other values as is so we can re-use the code
containerLogsOpts := ContainerLogsOptions{
Details: options.Details,
Latest: options.Latest,
Follow: options.Follow,
Names: options.Names,
Since: options.Since,
Until: options.Until,
Tail: options.Tail,
Timestamps: options.Timestamps,
StdoutWriter: options.StdoutWriter,
StderrWriter: options.StderrWriter,
}
return containerLogsOpts
}