healthcheck

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2023 License: Apache-2.0, MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Checker

type Checker func() (ok bool, msg string, err error)

Checker is a function that checks whether a precondition is met. It returns whether the check succeeded, an optional message to present to the user, and error in case the check logic itself failed.

(true, *, nil) => HealthcheckStatusOK
(false, *, nil) => HealthcheckStatusFailed
(false, *, not-nil) => HealthcheckStatusAborted
checker doesn't run => HealthcheckStatusOmitted (e.g. dependent checks where the upstream failed)

func All

func All(checkers ...Checker) Checker

All returns a Checker that succeeds when all provided Checkers succeed. If a Checker fails, it short-circuits and returns the first failure.

func Any

func Any(checkers ...Checker) Checker

Any returns a Checker that succeeds if any of the provided Checkers succeed. If none do, it fails.

func CheckCommandStatus

func CheckCommandStatus(ctx context.Context, cmd string, args ...string) Checker

CheckCommandStatus returns a checker which executes a command and returns successfully or unsuccessfully depending on the exit status of the command.

func CheckContainerStarted

func CheckContainerStarted(ctx context.Context, ow *rpc.OutputWriter, cli *client.Client, name string) Checker

CheckContainerStarted returns a Checker that succeeds if a container is started, and fails otherwise.

func CheckDirectoryExists

func CheckDirectoryExists(path string) Checker

CheckDirectoryExists returns a Checker that checks whether the specified directory exists. It succeeds if the directory exists, and fails if the Go runtime returns a ErrNotExist error. All other errors are propagated back, which presumably will mark this check as aborted.

func CheckK8sPods

func CheckK8sPods(ctx context.Context, client *kubernetes.Clientset, label string, namespace string, count int) Checker

CheckK8sPods returns a checker which verifies the number of pods found matches the number expected. If Listing the pods returns an error, the error is returned. The boolean value returned by the check follows whether the number of pods observed in the list matches the expected count.

func CheckNetwork

func CheckNetwork(ctx context.Context, ow *rpc.OutputWriter, cli *client.Client, networkID string) Checker

CheckNetwork returns a Checker that succeeds if the specified network exists, and fails otherwise.

func CheckRedisPort added in v0.5.3

func CheckRedisPort(ctx context.Context, ow *rpc.OutputWriter, cli *client.Client) Checker

CheckRedisPort returns a checker which verifies if the default port of redis (6379) is already binded on localhost. If it is, it fails. If not, it succeeds.

func DialableChecker

func DialableChecker(protocol string, address string) Checker

DialableChecker returns a Checker that checks whether a remote endpoint is dialable. For TCP sockets, a failure could mean the network is unreachable, or that the remote TCP socket is closed. For UDP sockets, being connectionless, may return a false positive even if there is no listening process, but at least it will try to find a route to the host.

func Not

func Not(ckr Checker) Checker

Not negates a Checker. To make it clear to the user that the check is negated, the message is re-formatted using "NOT(<msg>)" notation.

Any error encountered when the check is executed gets included in the message value.

This is useful if there is a checker which does exactly the opposite of what you want to check for i.e. you want to check that a directory does NOT exist, etc. or you want to check for combinations using Any() or All() with some negated predicates.

type Fixer

type Fixer func() (msg string, err error)

Fixer is a function that will be called to attempt to fix a failing check. It returns an optional message to present to the user, and error in case the fix failed.

func And

func And(fixers ...Fixer) Fixer

And returns a Fixer that executes all fixes sequentially, short-circuiting at the first error.

func BuildImage

func BuildImage(ctx context.Context, ow *rpc.OutputWriter, cli *client.Client, opts *docker.BuildImageOpts) Fixer

BuildImage returns a Fixer that builds the provided image if it doesn't exist yet.

func CreateDirectory

func CreateDirectory(path string) Fixer

CreateDirectory returns a Fixer that creates the specified directory and any parent directories as appropriate.

func CreateNetwork

func CreateNetwork(ctx context.Context, ow *rpc.OutputWriter, cli *client.Client, networkID string, netcfg network.IPAMConfig) Fixer

CreateNetwork returns a Fixer that creates a Docker bridge network with supplied ID and characteristics.

func NotImplemented

func NotImplemented() Fixer

NotImplemented is a placeholder Fixer which always returns successfully.

func Or

func Or(fixers ...Fixer) Fixer

Or returns a Fixer that short-circuits on the first success, or errors if all fixes failed.

func RequiresManualFixing added in v0.5.3

func RequiresManualFixing() Fixer

RequiresManualFixing is a placeholder Fixer which always returns unsuccessfully.

func StartCommand

func StartCommand(ctx context.Context, cmd string, args ...string) Fixer

StartCommand returns a Fixer that starts the given process, under the supplied context, via os/exec.CommandContext.

func StartContainer

func StartContainer(ctx context.Context, ow *rpc.OutputWriter, cli *client.Client, opts *docker.EnsureContainerOpts) Fixer

StartContainer returns a Fixer that starts the specified container if it exists, potentially acquiring the image first via the supplied image strategy, if the image itself is absent.

type Helper

type Helper struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Helper is a utility that facilitates the execution of healthchecks.

Healthchecks are registered via the Enlist() method, which takes a name, a Checker, and a Fixer function. Common Checker and Fixer functions can be found in this package.

To run the healthchecks and obtain an api.HealthcheckReport, call RunChecks.

Healthchecks are run sequentially, in the same order they were listed.

For each item, the Checker runs first. If it results in a "failed" status, and an associated Fixer is registered, we run the Fixer, if and only if "fix" mode is requested when calling RunChecks.

func (*Helper) Enlist

func (h *Helper) Enlist(name string, c Checker, f Fixer)

Enlist registers a new healthcheck, supplying its name, a compulsory Checker, and an optional Fixer.

func (*Helper) RunChecks

func (h *Helper) RunChecks(_ context.Context, fix bool) (*api.HealthcheckReport, error)

RunChecks runs the checks and returns an api.HealthcheckReport, or a non-nil error if an internal error occured. See godocs on the Helper type for additional information.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL