Compare commits

..

No commits in common. "v1.3.1-rc.1" and "v1.3.0" have entirely different histories.

10 changed files with 40 additions and 49 deletions

View File

@ -1,10 +1,3 @@
## [1.3.1-rc.1](https://git.ext.icikowski.pl/go/kubeprobes/compare/v1.3.0...v1.3.1-rc.1) (2024-03-03)
### Refactoring
* **probes:** rename `ProbeFunction` to `Probe` ([8dc7f27](https://git.ext.icikowski.pl/go/kubeprobes/commit/8dc7f27400075fabca9525f42eb20404736fb1cb))
## [1.3.0](https://git.ext.icikowski.pl/go/kubeprobes/compare/v1.2.0...v1.3.0) (2024-03-02)

View File

@ -21,22 +21,22 @@ Default paths can be overriden with options described below. Accessing any other
The `kubeprobes.New` function accepts following options as arguments:
- `kubeprobes.WithLivenessProbes(...)` - adds particular [probes](#probes) to the list of liveness probes;
- `kubeprobes.WithLivenessProbes(...)` - adds particular [probe functions](#probe-functions) to the list of liveness probes;
- `kubeprobes.WithLivenessPath("/some/liveness/path")` - sets liveness probe path to given path (default is `/live`);
- `kubeprobes.WithReadinessProbes(...)` - adds particular [probes](#probes) to the list of readiness probes;
- `kubeprobes.WithReadinessProbes(...)` - adds particular [probe functions](#probe-functions) to the list of readiness probes;
- `kubeprobes.WithReadinessPath("/some/readiness/path")` - sets readiness probe path to given path (default is `/ready`);
- `kubeprobes.WithVerboseOutput()` - enables verbose output by default (returns both failed and passed probes).
## Probes
In order to determine the state of particular element of application, probes need to be implemented either by creating [probes from functions](#standard-probes) or by using simple and thread-safe [manual probes](#manual-probes).
In order to determine the state of particular element of application, probes need to be implemented either by creating [status determining function](#probe-functions) or by using simple and thread-safe [manual probes](#manual-probes).
### Standard probes
### Probe functions
Probes (instances of `Probe` interface) are wrappers for functions that performs user defined logic with given interval of updates in order to determine whether the probe should be marked as healthy or not. Those functions should take no arguments and return error (if no error is returned, the probe is considered to be healthy; if error is returned, the probe is considered to be unhealthy). If given interval is less or equal zero, then function is only checked on probe creation and remains in determined state forever.
Probe functions (instances of `ProbeFunction` interface) are wrappers for functions that performs user defined logic with given interval of updates in order to determine whether the probe should be marked as healthy or not. Those functions should take no arguments and return error (if no error is returned, the probe is considered to be healthy; if error is returned, the probe is considered to be unhealthy). If given interval is less or equal zero, then function is only checked on probe creation and remains in determined state forever.
```go
someProbe := kubeprobes.NewProbe("live", func() error {
someProbe := kubeprobes.NewProbeFunction("live", func() error {
// Some logic here
if time.Now().Weekday() == time.Wednesday {
// Fail only on wednesday!
@ -45,7 +45,7 @@ someProbe := kubeprobes.NewProbe("live", func() error {
return nil
}, 1 * time.Hour)
someOtherProbe := kubeprobes.NewProbe("ready", func() error {
someOtherProbe := kubeprobes.NewProbeFunction("ready", func() error {
// Always healthy
return nil
}, 0) // This probe is checked once
@ -59,7 +59,7 @@ kp, _ := kubeprobes.New(
### Manual probes
Manual probes (instances of `ManualProbe` interface) are objects that can be marked either as healthy or unhealthy and implement `Probe` for easy integration. Those objects utilize `sync.RMutex` mechanism to ensure thread-safety.
Manual probes (instances of `ManualProbe` interface) are objects that can be marked either as healthy or unhealthy and implement `ProbeFunction` for easy integration. Those objects utilize `sync.RMutex` mechanism to ensure thread-safety.
Those probes can be changed by user with provided methods:
@ -78,9 +78,7 @@ kp, _ := kubeprobes.New(
kubeprobes.WithReadinessProbes(someOtherProbe),
)
// Can be later marked according to needs
someProbe.Pass()
someOtherProbe.FailWithCause(errors.New("I'm not doing anything!"))
// Can be later marked according
```
## Direct handler access
@ -107,9 +105,9 @@ appProbe := func() error {
return nil
}
// Create manual probes
live := kubeprobes.NewManualProbe()
ready := kubeprobes.NewManualProbe()
// Create stateful probes
live := kubeprobes.NewStatefulProbe()
ready := kubeprobes.NewStatefulProbe()
// Prepare handler
kp, err := kubeprobes.New(

View File

@ -19,8 +19,8 @@ type Kubeprobes interface {
}
type kubeprobes struct {
livenessProbes []Probe
readinessProbes []Probe
livenessProbes []ProbeFunction
readinessProbes []ProbeFunction
verbose bool
@ -31,8 +31,8 @@ type kubeprobes struct {
// New returns a new instance of a Kubernetes probes with given options.
func New(options ...Option) (Kubeprobes, error) {
kp := &kubeprobes{
livenessProbes: []Probe{},
readinessProbes: []Probe{},
livenessProbes: []ProbeFunction{},
readinessProbes: []ProbeFunction{},
pathLive: defaultLivenessPath,
pathReady: defaultReadinessPath,
}

View File

@ -12,7 +12,7 @@ func (o option) apply(kp *kubeprobes) {
}
// WithLivenessProbes adds given probe functions to the set of liveness probes.
func WithLivenessProbes(probes ...Probe) Option {
func WithLivenessProbes(probes ...ProbeFunction) Option {
return option(func(kp *kubeprobes) {
kp.livenessProbes = append(kp.livenessProbes, probes...)
})
@ -26,7 +26,7 @@ func WithLivenessPath(path string) Option {
}
// WithReadinessProbes adds given probe functions to the set of readiness probes.
func WithReadinessProbes(probes ...Probe) Option {
func WithReadinessProbes(probes ...ProbeFunction) Option {
return option(func(kp *kubeprobes) {
kp.readinessProbes = append(kp.readinessProbes, probes...)
})

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "kubeprobes",
"version": "1.3.1-rc.1",
"version": "1.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "kubeprobes",
"version": "1.3.1-rc.1",
"version": "1.3.0",
"devDependencies": {
"@saithodev/semantic-release-gitea": "^2.1.0",
"@semantic-release/changelog": "^6.0.3",

View File

@ -1,7 +1,7 @@
{
"private": true,
"name": "kubeprobes",
"version": "1.3.1-rc.1",
"version": "1.3.0",
"scripts": {
"release": "./node_modules/.bin/semantic-release"
},

View File

@ -5,15 +5,15 @@ import (
"time"
)
// Probe is a wrapper for a function that determines whether
// ProbeFunction is a wrapper for a function that determines whether
// the given metric may be marked as correctly functioning.
// It not, the error should be returned.
type Probe interface {
type ProbeFunction interface {
name() string
status() error
}
type probe struct {
type probeFunction struct {
probeName string
probeFunc func() error
refreshInterval time.Duration
@ -22,20 +22,20 @@ type probe struct {
err error
}
// NewProbe returns new instance of [Probe].
// NewProbeFunction returns new instance of [ProbeFunction].
//
// If update interval is less or equal zero then probe is updated only
// on its creation and remains in the same state forever.
func NewProbe(
func NewProbeFunction(
name string,
fn func() error,
updateInterval time.Duration,
) (Probe, error) {
) (ProbeFunction, error) {
if name == "" {
return nil, errProbeNameEmpty
}
pf := &probe{
pf := &probeFunction{
probeName: name,
probeFunc: fn,
refreshInterval: updateInterval,
@ -47,25 +47,25 @@ func NewProbe(
}
// name implements ProbeFunction.
func (pf *probe) name() string {
func (pf *probeFunction) name() string {
return pf.probeName
}
// status implements ProbeFunction.
func (pf *probe) status() error {
func (pf *probeFunction) status() error {
pf.mux.RLock()
defer pf.mux.RUnlock()
return pf.err
}
func (pf *probe) update() {
func (pf *probeFunction) update() {
err := pf.probeFunc()
pf.mux.Lock()
pf.err = err
pf.mux.Unlock()
}
func (pf *probe) autoUpdate() {
func (pf *probeFunction) autoUpdate() {
pf.update()
if pf.refreshInterval <= 0 {
return

View File

@ -7,7 +7,7 @@ import (
// ManualProbe represents the simple probe that can be either
// marked as "up" (healthy) or "down" (unhealthy).
type ManualProbe interface {
Probe
ProbeFunction
// Pass marks the probe as healthy.
Pass()

View File

@ -20,7 +20,7 @@ func (sq *statusQuery) wait() {
sq.wg.Wait()
}
func newStatusQuery(probes []Probe) *statusQuery {
func newStatusQuery(probes []ProbeFunction) *statusQuery {
sq := &statusQuery{
ok: true,
passed: make([]statusEntry, 0, len(probes)),

View File

@ -6,28 +6,28 @@ import (
func TestStatusQueryIsAllGreen(t *testing.T) {
var (
probePassing, _ = NewProbe("pass", func() error {
probePassing, _ = NewProbeFunction("pass", func() error {
return nil
}, 0)
probeFailing, _ = NewProbe("fail", func() error {
probeFailing, _ = NewProbeFunction("fail", func() error {
return errProbeFailed
}, 0)
)
tests := map[string]struct {
probes []Probe
probes []ProbeFunction
expectedStatus bool
}{
"all green": {
probes: []Probe{probePassing},
probes: []ProbeFunction{probePassing},
expectedStatus: true,
},
"some failed": {
probes: []Probe{probePassing, probeFailing},
probes: []ProbeFunction{probePassing, probeFailing},
expectedStatus: false,
},
"all failed": {
probes: []Probe{probeFailing},
probes: []ProbeFunction{probeFailing},
expectedStatus: false,
},
}