github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/domain/entities/pods.go (about)

     1  package entities
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"time"
     7  
     8  	commonFlag "github.com/containers/common/pkg/flag"
     9  	"github.com/hanks177/podman/v4/libpod/define"
    10  	"github.com/hanks177/podman/v4/pkg/specgen"
    11  	"github.com/hanks177/podman/v4/pkg/util"
    12  	"github.com/opencontainers/runtime-spec/specs-go"
    13  )
    14  
    15  type PodKillOptions struct {
    16  	All    bool
    17  	Latest bool
    18  	Signal string
    19  }
    20  
    21  type PodKillReport struct {
    22  	Errs []error
    23  	Id   string // nolint
    24  }
    25  
    26  type ListPodsReport struct {
    27  	Cgroup     string
    28  	Containers []*ListPodContainer
    29  	Created    time.Time
    30  	Id         string // nolint
    31  	InfraId    string // nolint
    32  	Name       string
    33  	Namespace  string
    34  	// Network names connected to infra container
    35  	Networks []string
    36  	Status   string
    37  	Labels   map[string]string
    38  }
    39  
    40  type ListPodContainer struct {
    41  	Id     string // nolint
    42  	Names  string
    43  	Status string
    44  }
    45  
    46  type PodPauseOptions struct {
    47  	All    bool
    48  	Latest bool
    49  }
    50  
    51  type PodPauseReport struct {
    52  	Errs []error
    53  	Id   string // nolint
    54  }
    55  
    56  type PodunpauseOptions struct {
    57  	All    bool
    58  	Latest bool
    59  }
    60  
    61  type PodUnpauseReport struct {
    62  	Errs []error
    63  	Id   string // nolint
    64  }
    65  
    66  type PodStopOptions struct {
    67  	All     bool
    68  	Ignore  bool
    69  	Latest  bool
    70  	Timeout int
    71  }
    72  
    73  type PodStopReport struct {
    74  	Errs []error
    75  	Id   string // nolint
    76  }
    77  
    78  type PodRestartOptions struct {
    79  	All    bool
    80  	Latest bool
    81  }
    82  
    83  type PodRestartReport struct {
    84  	Errs []error
    85  	Id   string // nolint
    86  }
    87  
    88  type PodStartOptions struct {
    89  	All    bool
    90  	Latest bool
    91  }
    92  
    93  type PodStartReport struct {
    94  	Errs []error
    95  	Id   string // nolint
    96  }
    97  
    98  type PodRmOptions struct {
    99  	All     bool
   100  	Force   bool
   101  	Ignore  bool
   102  	Latest  bool
   103  	Timeout *uint
   104  }
   105  
   106  type PodRmReport struct {
   107  	Err error
   108  	Id  string // nolint
   109  }
   110  
   111  // PddSpec is an abstracted version of PodSpecGen designed to eventually accept options
   112  // not meant to be in a specgen
   113  type PodSpec struct {
   114  	PodSpecGen specgen.PodSpecGenerator
   115  }
   116  
   117  // PodCreateOptions provides all possible options for creating a pod and its infra container.
   118  // The JSON tags below are made to match the respective field in ContainerCreateOptions for the purpose of mapping.
   119  // swagger:model PodCreateOptions
   120  type PodCreateOptions struct {
   121  	CgroupParent       string            `json:"cgroup_parent,omitempty"`
   122  	CreateCommand      []string          `json:"create_command,omitempty"`
   123  	Devices            []string          `json:"devices,omitempty"`
   124  	DeviceReadBPs      []string          `json:"device_read_bps,omitempty"`
   125  	ExitPolicy         string            `json:"exit_policy,omitempty"`
   126  	Hostname           string            `json:"hostname,omitempty"`
   127  	Infra              bool              `json:"infra,omitempty"`
   128  	InfraImage         string            `json:"infra_image,omitempty"`
   129  	InfraName          string            `json:"container_name,omitempty"`
   130  	InfraCommand       *string           `json:"container_command,omitempty"`
   131  	InfraConmonPidFile string            `json:"container_conmon_pidfile,omitempty"`
   132  	Labels             map[string]string `json:"labels,omitempty"`
   133  	Name               string            `json:"name,omitempty"`
   134  	Net                *NetOptions       `json:"net,omitempty"`
   135  	Share              []string          `json:"share,omitempty"`
   136  	ShareParent        *bool             `json:"share_parent,omitempty"`
   137  	Pid                string            `json:"pid,omitempty"`
   138  	Cpus               float64           `json:"cpus,omitempty"`
   139  	CpusetCpus         string            `json:"cpuset_cpus,omitempty"`
   140  	Userns             specgen.Namespace `json:"-"`
   141  	Volume             []string          `json:"volume,omitempty"`
   142  	VolumesFrom        []string          `json:"volumes_from,omitempty"`
   143  	SecurityOpt        []string          `json:"security_opt,omitempty"`
   144  	Sysctl             []string          `json:"sysctl,omitempty"`
   145  }
   146  
   147  // PodLogsOptions describes the options to extract pod logs.
   148  type PodLogsOptions struct {
   149  	// Other fields are exactly same as ContainerLogOpts
   150  	ContainerLogsOptions
   151  	// If specified will only fetch the logs of specified container
   152  	ContainerName string
   153  	// Show different colors in the logs.
   154  	Color bool
   155  }
   156  
   157  type ContainerCreateOptions struct {
   158  	Annotation        []string
   159  	Attach            []string
   160  	Authfile          string
   161  	BlkIOWeight       string
   162  	BlkIOWeightDevice []string
   163  	CapAdd            []string
   164  	CapDrop           []string
   165  	CgroupNS          string
   166  	CgroupsMode       string
   167  	CgroupParent      string `json:"cgroup_parent,omitempty"`
   168  	CIDFile           string
   169  	ConmonPIDFile     string `json:"container_conmon_pidfile,omitempty"`
   170  	CPUPeriod         uint64
   171  	CPUQuota          int64
   172  	CPURTPeriod       uint64
   173  	CPURTRuntime      int64
   174  	CPUShares         uint64
   175  	CPUS              float64 `json:"cpus,omitempty"`
   176  	CPUSetCPUs        string  `json:"cpuset_cpus,omitempty"`
   177  	CPUSetMems        string
   178  	Devices           []string `json:"devices,omitempty"`
   179  	DeviceCgroupRule  []string
   180  	DeviceReadBPs     []string `json:"device_read_bps,omitempty"`
   181  	DeviceReadIOPs    []string
   182  	DeviceWriteBPs    []string
   183  	DeviceWriteIOPs   []string
   184  	Entrypoint        *string `json:"container_command,omitempty"`
   185  	Env               []string
   186  	EnvHost           bool
   187  	EnvFile           []string
   188  	Expose            []string
   189  	GIDMap            []string
   190  	GroupAdd          []string
   191  	HealthCmd         string
   192  	HealthInterval    string
   193  	HealthRetries     uint
   194  	HealthStartPeriod string
   195  	HealthTimeout     string
   196  	Hostname          string `json:"hostname,omitempty"`
   197  	HTTPProxy         bool
   198  	HostUsers         []string
   199  	ImageVolume       string
   200  	Init              bool
   201  	InitContainerType string
   202  	InitPath          string
   203  	Interactive       bool
   204  	IPC               string
   205  	Label             []string
   206  	LabelFile         []string
   207  	LogDriver         string
   208  	LogOptions        []string
   209  	Memory            string
   210  	MemoryReservation string
   211  	MemorySwap        string
   212  	MemorySwappiness  int64
   213  	Name              string `json:"container_name"`
   214  	NoHealthCheck     bool
   215  	OOMKillDisable    bool
   216  	OOMScoreAdj       *int
   217  	Arch              string
   218  	OS                string
   219  	Variant           string
   220  	PID               string `json:"pid,omitempty"`
   221  	PIDsLimit         *int64
   222  	Platform          string
   223  	Pod               string
   224  	PodIDFile         string
   225  	Personality       string
   226  	PreserveFDs       uint
   227  	Privileged        bool
   228  	PublishAll        bool
   229  	Pull              string
   230  	Quiet             bool
   231  	ReadOnly          bool
   232  	ReadOnlyTmpFS     bool
   233  	Restart           string
   234  	Replace           bool
   235  	Requires          []string
   236  	Rm                bool
   237  	RootFS            bool
   238  	Secrets           []string
   239  	SecurityOpt       []string `json:"security_opt,omitempty"`
   240  	SdNotifyMode      string
   241  	ShmSize           string
   242  	SignaturePolicy   string
   243  	StopSignal        string
   244  	StopTimeout       uint
   245  	StorageOpts       []string
   246  	SubUIDName        string
   247  	SubGIDName        string
   248  	Sysctl            []string `json:"sysctl,omitempty"`
   249  	Systemd           string
   250  	Timeout           uint
   251  	TLSVerify         commonFlag.OptionalBool
   252  	TmpFS             []string
   253  	TTY               bool
   254  	Timezone          string
   255  	Umask             string
   256  	UnsetEnv          []string
   257  	UnsetEnvAll       bool
   258  	UIDMap            []string
   259  	Ulimit            []string
   260  	User              string
   261  	UserNS            string `json:"-"`
   262  	UTS               string
   263  	Mount             []string
   264  	Volume            []string `json:"volume,omitempty"`
   265  	VolumesFrom       []string `json:"volumes_from,omitempty"`
   266  	Workdir           string
   267  	SeccompPolicy     string
   268  	PidFile           string
   269  	ChrootDirs        []string
   270  	IsInfra           bool
   271  	IsClone           bool
   272  
   273  	Net *NetOptions `json:"net,omitempty"`
   274  
   275  	CgroupConf []string
   276  
   277  	PasswdEntry string
   278  }
   279  
   280  func NewInfraContainerCreateOptions() ContainerCreateOptions {
   281  	options := ContainerCreateOptions{
   282  		IsInfra:          true,
   283  		ImageVolume:      "bind",
   284  		MemorySwappiness: -1,
   285  	}
   286  	return options
   287  }
   288  
   289  type PodCreateReport struct {
   290  	Id string // nolint
   291  }
   292  
   293  func (p *PodCreateOptions) CPULimits() *specs.LinuxCPU {
   294  	cpu := &specs.LinuxCPU{}
   295  	hasLimits := false
   296  
   297  	if p.Cpus != 0 {
   298  		period, quota := util.CoresToPeriodAndQuota(p.Cpus)
   299  		cpu.Period = &period
   300  		cpu.Quota = &quota
   301  		hasLimits = true
   302  	}
   303  	if p.CpusetCpus != "" {
   304  		cpu.Cpus = p.CpusetCpus
   305  		hasLimits = true
   306  	}
   307  	if !hasLimits {
   308  		return cpu
   309  	}
   310  	return cpu
   311  }
   312  
   313  func ToPodSpecGen(s specgen.PodSpecGenerator, p *PodCreateOptions) (*specgen.PodSpecGenerator, error) {
   314  	// Basic Config
   315  	s.Name = p.Name
   316  	s.InfraName = p.InfraName
   317  	out, err := specgen.ParseNamespace(p.Pid)
   318  	if err != nil {
   319  		return nil, err
   320  	}
   321  	s.Pid = out
   322  	s.Hostname = p.Hostname
   323  	s.ExitPolicy = p.ExitPolicy
   324  	s.Labels = p.Labels
   325  	s.Devices = p.Devices
   326  	s.SecurityOpt = p.SecurityOpt
   327  	s.NoInfra = !p.Infra
   328  	if p.InfraCommand != nil && len(*p.InfraCommand) > 0 {
   329  		s.InfraCommand = strings.Split(*p.InfraCommand, " ")
   330  	}
   331  	if len(p.InfraConmonPidFile) > 0 {
   332  		s.InfraConmonPidFile = p.InfraConmonPidFile
   333  	}
   334  	s.InfraImage = p.InfraImage
   335  	s.SharedNamespaces = p.Share
   336  	s.ShareParent = p.ShareParent
   337  	s.PodCreateCommand = p.CreateCommand
   338  	s.VolumesFrom = p.VolumesFrom
   339  
   340  	// Networking config
   341  
   342  	if p.Net != nil {
   343  		s.NetNS = p.Net.Network
   344  		s.PortMappings = p.Net.PublishPorts
   345  		s.Networks = p.Net.Networks
   346  		s.NetworkOptions = p.Net.NetworkOptions
   347  		if p.Net.UseImageResolvConf {
   348  			s.NoManageResolvConf = true
   349  		}
   350  		s.DNSServer = p.Net.DNSServers
   351  		s.DNSSearch = p.Net.DNSSearch
   352  		s.DNSOption = p.Net.DNSOptions
   353  		s.NoManageHosts = p.Net.NoHosts
   354  		s.HostAdd = p.Net.AddHosts
   355  	}
   356  
   357  	// Cgroup
   358  	s.CgroupParent = p.CgroupParent
   359  
   360  	// Resource config
   361  	cpuDat := p.CPULimits()
   362  	if s.ResourceLimits == nil {
   363  		s.ResourceLimits = &specs.LinuxResources{}
   364  		s.ResourceLimits.CPU = &specs.LinuxCPU{}
   365  	}
   366  	if cpuDat != nil {
   367  		s.ResourceLimits.CPU = cpuDat
   368  		if p.Cpus != 0 {
   369  			s.CPUPeriod = *cpuDat.Period
   370  			s.CPUQuota = *cpuDat.Quota
   371  		}
   372  	}
   373  	s.Userns = p.Userns
   374  	sysctl := map[string]string{}
   375  	if ctl := p.Sysctl; len(ctl) > 0 {
   376  		sysctl, err = util.ValidateSysctls(ctl)
   377  		if err != nil {
   378  			return nil, err
   379  		}
   380  	}
   381  	s.Sysctl = sysctl
   382  
   383  	return &s, nil
   384  }
   385  
   386  type PodPruneOptions struct {
   387  	Force bool `json:"force" schema:"force"`
   388  }
   389  
   390  type PodPruneReport struct {
   391  	Err error
   392  	Id  string // nolint
   393  }
   394  
   395  type PodTopOptions struct {
   396  	// CLI flags.
   397  	ListDescriptors bool
   398  	Latest          bool
   399  
   400  	// Options for the API.
   401  	Descriptors []string
   402  	NameOrID    string
   403  }
   404  
   405  type PodPSOptions struct {
   406  	CtrNames  bool
   407  	CtrIds    bool
   408  	CtrStatus bool
   409  	Filters   map[string][]string
   410  	Format    string
   411  	Latest    bool
   412  	Namespace bool
   413  	Quiet     bool
   414  	Sort      string
   415  }
   416  
   417  type PodInspectOptions struct {
   418  	Latest bool
   419  
   420  	// Options for the API.
   421  	NameOrID string
   422  
   423  	Format string
   424  }
   425  
   426  type PodInspectReport struct {
   427  	*define.InspectPodData
   428  }
   429  
   430  // PodStatsOptions are options for the pod stats command.
   431  type PodStatsOptions struct {
   432  	// All - provide stats for all running pods.
   433  	All bool
   434  	// Latest - provide stats for the latest pod.
   435  	Latest bool
   436  }
   437  
   438  // PodStatsReport includes pod-resource statistics data.
   439  type PodStatsReport struct {
   440  	// Percentage of CPU utilized by pod
   441  	// example: 75.5%
   442  	CPU string
   443  	// Humanized Memory usage and maximum
   444  	// example: 12mb / 24mb
   445  	MemUsage string
   446  	// Memory usage and maximum in bytes
   447  	// example: 1,000,000 / 4,000,000
   448  	MemUsageBytes string
   449  	// Percentage of Memory utilized by pod
   450  	// example: 50.5%
   451  	Mem string
   452  	// Network usage inbound + outbound
   453  	NetIO string
   454  	// Humanized disk usage read + write
   455  	BlockIO string
   456  	// Container PID
   457  	PIDS string
   458  	// Pod ID
   459  	// example: 62310217a19e
   460  	Pod string
   461  	// Container ID
   462  	// example: e43534f89a7d
   463  	CID string
   464  	// Pod Name
   465  	// example: elastic_pascal
   466  	Name string
   467  }
   468  
   469  // ValidatePodStatsOptions validates the specified slice and options. Allows
   470  // for sharing code in the front- and the back-end.
   471  func ValidatePodStatsOptions(args []string, options *PodStatsOptions) error {
   472  	num := 0
   473  	if len(args) > 0 {
   474  		num++
   475  	}
   476  	if options.All {
   477  		num++
   478  	}
   479  	if options.Latest {
   480  		num++
   481  	}
   482  	switch num {
   483  	case 0:
   484  		// Podman v1 compat: if nothing's specified get all running
   485  		// pods.
   486  		options.All = true
   487  		return nil
   488  	case 1:
   489  		return nil
   490  	default:
   491  		return errors.New("--all, --latest and arguments cannot be used together")
   492  	}
   493  }
   494  
   495  // PodLogsOptionsToContainerLogsOptions converts PodLogOptions to ContainerLogOptions
   496  func PodLogsOptionsToContainerLogsOptions(options PodLogsOptions) ContainerLogsOptions {
   497  	// PodLogsOptions are similar but contains few extra fields like ctrName
   498  	// So cast other values as is so we can re-use the code
   499  	containerLogsOpts := ContainerLogsOptions{
   500  		Details:      options.Details,
   501  		Latest:       options.Latest,
   502  		Follow:       options.Follow,
   503  		Names:        options.Names,
   504  		Since:        options.Since,
   505  		Until:        options.Until,
   506  		Tail:         options.Tail,
   507  		Timestamps:   options.Timestamps,
   508  		Colors:       options.Colors,
   509  		StdoutWriter: options.StdoutWriter,
   510  		StderrWriter: options.StderrWriter,
   511  	}
   512  	return containerLogsOpts
   513  }