github.com/containers/podman/v4@v4.9.4/libpod/define/containerstate.go (about)

     1  package define
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  // ContainerStatus represents the current state of a container
     9  type ContainerStatus int
    10  
    11  const (
    12  	// ContainerStateUnknown indicates that the container is in an error
    13  	// state where information about it cannot be retrieved
    14  	ContainerStateUnknown ContainerStatus = iota
    15  	// ContainerStateConfigured indicates that the container has had its
    16  	// storage configured but it has not been created in the OCI runtime
    17  	ContainerStateConfigured ContainerStatus = iota
    18  	// ContainerStateCreated indicates the container has been created in
    19  	// the OCI runtime but not started
    20  	ContainerStateCreated ContainerStatus = iota
    21  	// ContainerStateRunning indicates the container is currently executing
    22  	ContainerStateRunning ContainerStatus = iota
    23  	// ContainerStateStopped indicates that the container was running but has
    24  	// exited
    25  	ContainerStateStopped ContainerStatus = iota
    26  	// ContainerStatePaused indicates that the container has been paused
    27  	ContainerStatePaused ContainerStatus = iota
    28  	// ContainerStateExited indicates the container has stopped and been
    29  	// cleaned up
    30  	ContainerStateExited ContainerStatus = iota
    31  	// ContainerStateRemoving indicates the container is in the process of
    32  	// being removed.
    33  	ContainerStateRemoving ContainerStatus = iota
    34  	// ContainerStateStopping indicates the container is in the process of
    35  	// being stopped.
    36  	ContainerStateStopping ContainerStatus = iota
    37  )
    38  
    39  // ContainerStatus returns a string representation for users of a container
    40  // state. All results should match Docker's versions (from `docker ps`) as
    41  // closely as possible, given the different set of states we support.
    42  func (t ContainerStatus) String() string {
    43  	switch t {
    44  	case ContainerStateUnknown:
    45  		return "unknown"
    46  	case ContainerStateConfigured:
    47  		// The naming here is confusing, but it's necessary for Docker
    48  		// compatibility - their Created state is our Configured state.
    49  		return "created"
    50  	case ContainerStateCreated:
    51  		// Docker does not have an equivalent to this state, so give it
    52  		// a clear name. Most of the time this is a purely transitory
    53  		// state between Configured and Running so we don't expect to
    54  		// see it much anyways.
    55  		return "initialized"
    56  	case ContainerStateRunning:
    57  		return "running"
    58  	case ContainerStateStopped:
    59  		return "stopped"
    60  	case ContainerStatePaused:
    61  		return "paused"
    62  	case ContainerStateExited:
    63  		return "exited"
    64  	case ContainerStateRemoving:
    65  		return "removing"
    66  	case ContainerStateStopping:
    67  		return "stopping"
    68  	}
    69  	return "bad state"
    70  }
    71  
    72  // StringToContainerStatus converts a string representation of a container's
    73  // status into an actual container status type
    74  func StringToContainerStatus(status string) (ContainerStatus, error) {
    75  	switch status {
    76  	case ContainerStateUnknown.String():
    77  		return ContainerStateUnknown, nil
    78  	case ContainerStateConfigured.String():
    79  		return ContainerStateConfigured, nil
    80  	case ContainerStateCreated.String():
    81  		return ContainerStateCreated, nil
    82  	case ContainerStateRunning.String():
    83  		return ContainerStateRunning, nil
    84  	case ContainerStateStopped.String():
    85  		return ContainerStateStopped, nil
    86  	case ContainerStatePaused.String():
    87  		return ContainerStatePaused, nil
    88  	case ContainerStateExited.String():
    89  		return ContainerStateExited, nil
    90  	case ContainerStateRemoving.String():
    91  		return ContainerStateRemoving, nil
    92  	default:
    93  		return ContainerStateUnknown, fmt.Errorf("unknown container state: %s: %w", status, ErrInvalidArg)
    94  	}
    95  }
    96  
    97  // ContainerExecStatus is the status of an exec session within a container.
    98  type ContainerExecStatus int
    99  
   100  const (
   101  	// ExecStateUnknown indicates that the state of the exec session is not
   102  	// known.
   103  	ExecStateUnknown ContainerExecStatus = iota
   104  	// ExecStateCreated indicates that the exec session has been created but
   105  	// not yet started
   106  	ExecStateCreated ContainerExecStatus = iota
   107  	// ExecStateRunning indicates that the exec session has been started but
   108  	// has not yet exited.
   109  	ExecStateRunning ContainerExecStatus = iota
   110  	// ExecStateStopped indicates that the exec session has stopped and is
   111  	// no longer running.
   112  	ExecStateStopped ContainerExecStatus = iota
   113  )
   114  
   115  // String returns a string representation of a given exec state.
   116  func (s ContainerExecStatus) String() string {
   117  	switch s {
   118  	case ExecStateUnknown:
   119  		return "unknown"
   120  	case ExecStateCreated:
   121  		return "created"
   122  	case ExecStateRunning:
   123  		return "running"
   124  	case ExecStateStopped:
   125  		return "stopped"
   126  	default:
   127  		return "bad state"
   128  	}
   129  }
   130  
   131  // ContainerStats contains the statistics information for a running container
   132  type ContainerStats struct {
   133  	AvgCPU        float64
   134  	ContainerID   string
   135  	Name          string
   136  	PerCPU        []uint64
   137  	CPU           float64
   138  	CPUNano       uint64
   139  	CPUSystemNano uint64
   140  	SystemNano    uint64
   141  	MemUsage      uint64
   142  	MemLimit      uint64
   143  	MemPerc       float64
   144  	NetInput      uint64
   145  	NetOutput     uint64
   146  	BlockInput    uint64
   147  	BlockOutput   uint64
   148  	PIDs          uint64
   149  	UpTime        time.Duration
   150  	Duration      uint64
   151  }