Compare commits
6 Commits
v1.2.1-rc.
...
v1.3.0-rc.
Author | SHA1 | Date | |
---|---|---|---|
|
71f7e9e7df | ||
aed7fec97d | |||
ab8b7e84e2 | |||
82b6034625 | |||
ea92b2979c | |||
373c803c00 |
13
CHANGELOG.md
13
CHANGELOG.md
@ -1,3 +1,16 @@
|
|||||||
|
## [1.3.0-rc.1](https://git.ext.icikowski.pl/go/kubeprobes/compare/v1.2.1-rc.1...v1.3.0-rc.1) (2024-03-02)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **validation:** add empty probe lists validation ([82b6034](https://git.ext.icikowski.pl/go/kubeprobes/commit/82b6034625089450d3de734edd8eb95ea35aff2f))
|
||||||
|
|
||||||
|
|
||||||
|
### Refactoring
|
||||||
|
|
||||||
|
* **names:** refactor handler fetching methods' names ([373c803](https://git.ext.icikowski.pl/go/kubeprobes/commit/373c803c00499a7e607cdd0ad99315dcd6e2919d))
|
||||||
|
* **validation:** improve errors joining ([ea92b29](https://git.ext.icikowski.pl/go/kubeprobes/commit/ea92b2979cbdb67507d934725434f21b0456be03))
|
||||||
|
|
||||||
## [1.2.1-rc.1](https://git.ext.icikowski.pl/go/kubeprobes/compare/v1.2.0...v1.2.1-rc.1) (2024-03-02)
|
## [1.2.1-rc.1](https://git.ext.icikowski.pl/go/kubeprobes/compare/v1.2.0...v1.2.1-rc.1) (2024-03-02)
|
||||||
|
|
||||||
|
|
||||||
|
@ -82,8 +82,8 @@ kp, _ := kubeprobes.New(
|
|||||||
// ...
|
// ...
|
||||||
)
|
)
|
||||||
|
|
||||||
livenessHandler := kp.GetLivenessHandler()
|
livenessHandler := kp.LivenessHandler()
|
||||||
readinessHandler := kp.GetReadinessHandler()
|
readinessHandler := kp.ReadinessHandler()
|
||||||
```
|
```
|
||||||
|
|
||||||
Those handler can be used for manually mounting them on other servers/routers/muxes (eg. `go-chi/chi`, `gorilla/mux`, `http`'s `ServeMux` etc.).
|
Those handler can be used for manually mounting them on other servers/routers/muxes (eg. `go-chi/chi`, `gorilla/mux`, `http`'s `ServeMux` etc.).
|
||||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "kubeprobes",
|
"name": "kubeprobes",
|
||||||
"version": "1.2.1-rc.1",
|
"version": "1.3.0-rc.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "kubeprobes",
|
"name": "kubeprobes",
|
||||||
"version": "1.2.1-rc.1",
|
"version": "1.3.0-rc.1",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@saithodev/semantic-release-gitea": "^2.1.0",
|
"@saithodev/semantic-release-gitea": "^2.1.0",
|
||||||
"@semantic-release/changelog": "^6.0.3",
|
"@semantic-release/changelog": "^6.0.3",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"private": true,
|
"private": true,
|
||||||
"name": "kubeprobes",
|
"name": "kubeprobes",
|
||||||
"version": "1.2.1-rc.1",
|
"version": "1.3.0-rc.1",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"release": "./node_modules/.bin/semantic-release"
|
"release": "./node_modules/.bin/semantic-release"
|
||||||
},
|
},
|
||||||
|
46
probes.go
46
probes.go
@ -35,44 +35,36 @@ func New(options ...Option) (Kubeprobes, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (kp *kubeprobes) validate() error {
|
func (kp *kubeprobes) validate() error {
|
||||||
errs := []error{}
|
var err error
|
||||||
|
|
||||||
if kp.pathLive == "" {
|
if kp.pathLive == "" {
|
||||||
errs = append(
|
err = errors.Join(err, fmt.Errorf("liveness probe path must not be empty"))
|
||||||
errs,
|
|
||||||
fmt.Errorf("liveness probe path must not be empty"),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if kp.pathReady == "" {
|
if kp.pathReady == "" {
|
||||||
errs = append(
|
err = errors.Join(err, fmt.Errorf("readiness probe path must not be empty"))
|
||||||
errs,
|
|
||||||
fmt.Errorf("readiness probe path must not be empty"),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(kp.pathLive, "/") {
|
if !strings.HasPrefix(kp.pathLive, "/") {
|
||||||
errs = append(
|
err = errors.Join(err, fmt.Errorf("liveness probe path must start with slash (current: %q)", kp.pathLive))
|
||||||
errs,
|
|
||||||
fmt.Errorf("liveness probe path must start with slash (current: %q)", kp.pathLive),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(kp.pathReady, "/") {
|
if !strings.HasPrefix(kp.pathReady, "/") {
|
||||||
errs = append(
|
err = errors.Join(err, fmt.Errorf("readiness probe path must start with slash (current: %q)", kp.pathReady))
|
||||||
errs,
|
|
||||||
fmt.Errorf("readiness probe path must start with slash (current: %q)", kp.pathReady),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if kp.pathLive == kp.pathReady {
|
if kp.pathLive == kp.pathReady {
|
||||||
errs = append(
|
err = errors.Join(err, fmt.Errorf("liveness and readiness probes have the same values (both %q)", kp.pathLive))
|
||||||
errs,
|
|
||||||
fmt.Errorf("liveness and readiness probes have the same values (both %q)", kp.pathLive),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.Join(errs...)
|
if len(kp.livenessProbes) == 0 {
|
||||||
|
err = errors.Join(err, fmt.Errorf("no liveness probes defined"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(kp.readinessProbes) == 0 {
|
||||||
|
err = errors.Join(err, fmt.Errorf("no readiness probes defined"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kp *kubeprobes) handleLiveness(w http.ResponseWriter, _ *http.Request) {
|
func (kp *kubeprobes) handleLiveness(w http.ResponseWriter, _ *http.Request) {
|
||||||
@ -93,13 +85,13 @@ func (kp *kubeprobes) handleReadiness(w http.ResponseWriter, _ *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLivenessHandler implements Kubeprobes.
|
// LivenessHandler implements Kubeprobes.
|
||||||
func (kp *kubeprobes) GetLivenessHandler() http.Handler {
|
func (kp *kubeprobes) LivenessHandler() http.Handler {
|
||||||
return http.HandlerFunc(kp.handleLiveness)
|
return http.HandlerFunc(kp.handleLiveness)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetReadinessHandler implements Kubeprobes.
|
// ReadinessHandler implements Kubeprobes.
|
||||||
func (kp *kubeprobes) GetReadinessHandler() http.Handler {
|
func (kp *kubeprobes) ReadinessHandler() http.Handler {
|
||||||
return http.HandlerFunc(kp.handleReadiness)
|
return http.HandlerFunc(kp.handleReadiness)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
100
probes_test.go
100
probes_test.go
@ -15,7 +15,100 @@ func getStatusFromEndpoint(t *testing.T, client *http.Client, endpoint string) i
|
|||||||
return resp.StatusCode
|
return resp.StatusCode
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKubeprobes(t *testing.T) {
|
func TestValidation(t *testing.T) {
|
||||||
|
var (
|
||||||
|
live = NewStatefulProbe()
|
||||||
|
ready = NewStatefulProbe()
|
||||||
|
)
|
||||||
|
|
||||||
|
tests := map[string]struct {
|
||||||
|
opts []Option
|
||||||
|
expectedError bool
|
||||||
|
}{
|
||||||
|
"no modifications and no error": {
|
||||||
|
opts: []Option{
|
||||||
|
WithLivenessStatefulProbes(live),
|
||||||
|
WithReadinessStatefulProbes(ready),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"modifications and no error": {
|
||||||
|
opts: []Option{
|
||||||
|
WithLivenessStatefulProbes(live),
|
||||||
|
WithReadinessStatefulProbes(ready),
|
||||||
|
WithLivenessPath("/livez"),
|
||||||
|
WithReadinessPath("/readyz"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"missing liveness probes": {
|
||||||
|
opts: []Option{
|
||||||
|
WithReadinessStatefulProbes(ready),
|
||||||
|
},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
"missing readiness probes": {
|
||||||
|
opts: []Option{
|
||||||
|
WithLivenessStatefulProbes(live),
|
||||||
|
},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
"liveness probe path empty": {
|
||||||
|
opts: []Option{
|
||||||
|
WithLivenessStatefulProbes(live),
|
||||||
|
WithReadinessStatefulProbes(ready),
|
||||||
|
WithLivenessPath(""),
|
||||||
|
},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
"readiness probe path empty": {
|
||||||
|
opts: []Option{
|
||||||
|
WithLivenessStatefulProbes(live),
|
||||||
|
WithReadinessStatefulProbes(ready),
|
||||||
|
WithReadinessPath(""),
|
||||||
|
},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
"liveness probe path does not start with slash": {
|
||||||
|
opts: []Option{
|
||||||
|
WithLivenessStatefulProbes(live),
|
||||||
|
WithReadinessStatefulProbes(ready),
|
||||||
|
WithLivenessPath("livez"),
|
||||||
|
},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
"readiness probe path does not start with slash": {
|
||||||
|
opts: []Option{
|
||||||
|
WithLivenessStatefulProbes(live),
|
||||||
|
WithReadinessStatefulProbes(ready),
|
||||||
|
WithReadinessPath("readyz"),
|
||||||
|
},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
"liveness and readiness probe paths are equal": {
|
||||||
|
opts: []Option{
|
||||||
|
WithLivenessStatefulProbes(live),
|
||||||
|
WithReadinessStatefulProbes(ready),
|
||||||
|
WithLivenessPath("/check"),
|
||||||
|
WithReadinessPath("/check"),
|
||||||
|
},
|
||||||
|
expectedError: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, tc := range tests {
|
||||||
|
name, tc := name, tc
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
_, err := New(tc.opts...)
|
||||||
|
switch {
|
||||||
|
case err == nil && tc.expectedError:
|
||||||
|
t.Error("expected error, but no error was returned")
|
||||||
|
case err != nil && !tc.expectedError:
|
||||||
|
t.Errorf("expected no error but got %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandler(t *testing.T) {
|
||||||
live, ready := NewStatefulProbe(), NewStatefulProbe()
|
live, ready := NewStatefulProbe(), NewStatefulProbe()
|
||||||
|
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
@ -50,10 +143,13 @@ func TestKubeprobes(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
kp, _ := New(
|
kp, err := New(
|
||||||
WithLivenessStatefulProbes(live),
|
WithLivenessStatefulProbes(live),
|
||||||
WithReadinessStatefulProbes(ready),
|
WithReadinessStatefulProbes(ready),
|
||||||
)
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected no error, got %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
srv := httptest.NewServer(kp)
|
srv := httptest.NewServer(kp)
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
8
types.go
8
types.go
@ -6,10 +6,10 @@ import "net/http"
|
|||||||
type Kubeprobes interface {
|
type Kubeprobes interface {
|
||||||
http.Handler
|
http.Handler
|
||||||
|
|
||||||
// GetLivenessHandler returns [http.Handler] for liveness probes.
|
// LivenessHandler returns [http.Handler] for liveness probes.
|
||||||
GetLivenessHandler() http.Handler
|
LivenessHandler() http.Handler
|
||||||
// GetReadinessHandler returns [http.Handler] for readiness probes.
|
// ReadinessHandler returns [http.Handler] for readiness probes.
|
||||||
GetReadinessHandler() http.Handler
|
ReadinessHandler() http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option represents a [Kubeprobes] constructor option.
|
// Option represents a [Kubeprobes] constructor option.
|
||||||
|
Loading…
Reference in New Issue
Block a user