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

     1  package define
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/containers/image/v5/manifest"
     8  )
     9  
    10  const (
    11  	// HealthCheckHealthy describes a healthy container
    12  	HealthCheckHealthy string = "healthy"
    13  	// HealthCheckUnhealthy describes an unhealthy container
    14  	HealthCheckUnhealthy string = "unhealthy"
    15  	// HealthCheckStarting describes the time between when the container starts
    16  	// and the start-period (time allowed for the container to start and application
    17  	// to be running) expires.
    18  	HealthCheckStarting string = "starting"
    19  )
    20  
    21  // HealthCheckStatus represents the current state of a container
    22  type HealthCheckStatus int
    23  
    24  const (
    25  	// HealthCheckSuccess means the health worked
    26  	HealthCheckSuccess HealthCheckStatus = iota
    27  	// HealthCheckFailure means the health ran and failed
    28  	HealthCheckFailure HealthCheckStatus = iota
    29  	// HealthCheckContainerStopped means the health check cannot
    30  	// be run because the container is stopped
    31  	HealthCheckContainerStopped HealthCheckStatus = iota
    32  	// HealthCheckContainerNotFound means the container could
    33  	// not be found in local store
    34  	HealthCheckContainerNotFound HealthCheckStatus = iota
    35  	// HealthCheckNotDefined means the container has no health
    36  	// check defined in it
    37  	HealthCheckNotDefined HealthCheckStatus = iota
    38  	// HealthCheckInternalError means some something failed obtaining or running
    39  	// a given health check
    40  	HealthCheckInternalError HealthCheckStatus = iota
    41  	// HealthCheckDefined means the healthcheck was found on the container
    42  	HealthCheckDefined HealthCheckStatus = iota
    43  	// HealthCheckStartup means the healthcheck was unhealthy, but is still
    44  	// either within the startup HC or the startup period of the healthcheck
    45  	HealthCheckStartup HealthCheckStatus = iota
    46  )
    47  
    48  // Healthcheck defaults.  These are used both in the cli as well in
    49  // libpod and were moved from cmd/podman/common
    50  const (
    51  	// DefaultHealthCheckInterval default value
    52  	DefaultHealthCheckInterval = "30s"
    53  	// DefaultHealthCheckRetries default value
    54  	DefaultHealthCheckRetries uint = 3
    55  	// DefaultHealthCheckStartPeriod default value
    56  	DefaultHealthCheckStartPeriod = "0s"
    57  	// DefaultHealthCheckTimeout default value
    58  	DefaultHealthCheckTimeout = "30s"
    59  )
    60  
    61  // HealthConfig.Test options
    62  const (
    63  	// HealthConfigTestNone disables healthcheck
    64  	HealthConfigTestNone = "NONE"
    65  	// HealthConfigTestCmd execs arguments directly
    66  	HealthConfigTestCmd = "CMD"
    67  	// HealthConfigTestCmdShell runs commands with the system's default shell
    68  	HealthConfigTestCmdShell = "CMD-SHELL"
    69  )
    70  
    71  // HealthCheckOnFailureAction defines how Podman reacts when a container's health
    72  // status turns unhealthy.
    73  type HealthCheckOnFailureAction int
    74  
    75  // Healthcheck on-failure actions.
    76  const (
    77  	// HealthCheckOnFailureActionNonce instructs Podman to not react on an unhealthy status.
    78  	HealthCheckOnFailureActionNone = iota // Must be first iota for backwards compatibility
    79  	// HealthCheckOnFailureActionInvalid denotes an invalid on-failure policy.
    80  	HealthCheckOnFailureActionInvalid = iota
    81  	// HealthCheckOnFailureActionNonce instructs Podman to kill the container on an unhealthy status.
    82  	HealthCheckOnFailureActionKill = iota
    83  	// HealthCheckOnFailureActionNonce instructs Podman to restart the container on an unhealthy status.
    84  	HealthCheckOnFailureActionRestart = iota
    85  	// HealthCheckOnFailureActionNonce instructs Podman to stop the container on an unhealthy status.
    86  	HealthCheckOnFailureActionStop = iota
    87  )
    88  
    89  // String representations for on-failure actions.
    90  const (
    91  	strHealthCheckOnFailureActionNone    = "none"
    92  	strHealthCheckOnFailureActionInvalid = "invalid"
    93  	strHealthCheckOnFailureActionKill    = "kill"
    94  	strHealthCheckOnFailureActionRestart = "restart"
    95  	strHealthCheckOnFailureActionStop    = "stop"
    96  )
    97  
    98  // SupportedHealthCheckOnFailureActions lists all supported healthcheck restart policies.
    99  var SupportedHealthCheckOnFailureActions = []string{
   100  	strHealthCheckOnFailureActionNone,
   101  	strHealthCheckOnFailureActionKill,
   102  	strHealthCheckOnFailureActionRestart,
   103  	strHealthCheckOnFailureActionStop,
   104  }
   105  
   106  // String returns the string representation of the HealthCheckOnFailureAction.
   107  func (h HealthCheckOnFailureAction) String() string {
   108  	switch h {
   109  	case HealthCheckOnFailureActionNone:
   110  		return strHealthCheckOnFailureActionNone
   111  	case HealthCheckOnFailureActionKill:
   112  		return strHealthCheckOnFailureActionKill
   113  	case HealthCheckOnFailureActionRestart:
   114  		return strHealthCheckOnFailureActionRestart
   115  	case HealthCheckOnFailureActionStop:
   116  		return strHealthCheckOnFailureActionStop
   117  	default:
   118  		return strHealthCheckOnFailureActionInvalid
   119  	}
   120  }
   121  
   122  // ParseHealthCheckOnFailureAction parses the specified string into a HealthCheckOnFailureAction.
   123  // An error is returned for an invalid input.
   124  func ParseHealthCheckOnFailureAction(s string) (HealthCheckOnFailureAction, error) {
   125  	switch s {
   126  	case "", strHealthCheckOnFailureActionNone:
   127  		return HealthCheckOnFailureActionNone, nil
   128  	case strHealthCheckOnFailureActionKill:
   129  		return HealthCheckOnFailureActionKill, nil
   130  	case strHealthCheckOnFailureActionRestart:
   131  		return HealthCheckOnFailureActionRestart, nil
   132  	case strHealthCheckOnFailureActionStop:
   133  		return HealthCheckOnFailureActionStop, nil
   134  	default:
   135  		err := fmt.Errorf("invalid on-failure action %q for health check: supported actions are %s", s, strings.Join(SupportedHealthCheckOnFailureActions, ","))
   136  		return HealthCheckOnFailureActionInvalid, err
   137  	}
   138  }
   139  
   140  // StartupHealthCheck is the configuration of a startup healthcheck.
   141  type StartupHealthCheck struct {
   142  	manifest.Schema2HealthConfig
   143  	// Successes are the number of successes required to mark the startup HC
   144  	// as passed.
   145  	// If set to 0, a single success will mark the HC as passed.
   146  	Successes int `json:",omitempty"`
   147  }