github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/libpod/define/containerstate.go (about)

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