probes

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 30, 2022 License: MIT Imports: 5 Imported by: 1

README

probes

probes is a simple and goroutine safe implementation for Kubernetes liveness and readiness probes. It starts a http.Server listening on a configurable address for the routes /live and /ready for the liveness and readiness probes respectively.

Installation

go get -u github.com/blacklane/go-libs/probes

Usage

	// Addr is set as the http.Server Addr.
	// See net.Dial for details of the address format.
    addr := ":4242"
    p := probes.New(addr)

	// Starting the probes on a goroutine as p.Start() is a blocking call
	go func() {
		if err := p.Start(); err != nil {
			log.Printf("probes shut down: %v\n", err)
		}
	}()

	// Passing it around in the Context
	ctx := p.WithContext(context.Background())
	// Retrieving it from a Context
	pp := probes.FromCtx(ctx)

	// Calls to /ready will fail with HTTP 500.
	// Kubernetes stops sending requests to the pod
	pp.ReadinessFail()
	// Calls to /ready will succeed with HTTP 200 - OK.
	// Kubernetes starts sending requests to the pod again
	pp.ReadinessSucceed()

	// Calls to /live will fail with HTTP 500.
	// Kubernetes will restart the pod
	pp.LivenessFail()
	// Calls to /live will succeed with HTTP 200 - OK.
	// Kubernetes will restart the pod
	pp.LivenessSucceed()

	// Gracefully shutting down the probes HTTP server
	if err := p.Shutdown(context.Background()); !errors.Is(err, http.ErrServerClosed) {
		log.Printf("failed to shutdown probes HTTP server: %v\n", err)
	}
  • There is also a noop implementation:
	noop := probes.NewNoop()
Full documentation:
# Install godoc
GO111MODULE=off go install golang.org/x/tools/cmd/godoc

# run godoc
godoc -http=localhost:6060

then head to:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Probe

type Probe interface {
	// Start starts the probes.
	Start() error
	// Shutdown shuts the probes down.
	Shutdown(ctx context.Context) error

	// WithContext returns a copy of parent associated with this Probe.
	WithContext(parent context.Context) context.Context

	// LivenessSucceed sets the liveness probe to respond success, 200 - OK.
	LivenessSucceed()
	// LivenessFail sets the liveness probe to return failure, 500 - Internal Server Error.
	LivenessFail()

	// ReadinessSucceed sets the liveness probe to respond success, 200 - OK.
	ReadinessSucceed()
	// ReadinessFail sets the liveness probe to return failure, 500 - Internal Server Error.
	ReadinessFail()
}

func FromCtx

func FromCtx(ctx context.Context) Probe

FromCtx returns the Probe associated with the context. A noop Probe is returned if there is no Probe associated with the context.

func New

func New(addr string) Probe

New creates an HTTP server to handle the kubernetes liveness and readiness probes on /live and /ready respectively. To start te server call Start(), to shut it down, Shutdown() addr is the server address check Addr property of http.Server for details.

func NewNoop

func NewNoop() Probe

NewNoop returns a noop Probe.

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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