Compare commits

..

No commits in common. "aed7fec97de73eb3fa4b8088b40706d03383f4c1" and "ec53b2851fc40ca747ca236ce4d9674dc677c078" have entirely different histories.

4 changed files with 36 additions and 124 deletions

View File

@ -82,8 +82,8 @@ kp, _ := kubeprobes.New(
// ... // ...
) )
livenessHandler := kp.LivenessHandler() livenessHandler := kp.GetLivenessHandler()
readinessHandler := kp.ReadinessHandler() readinessHandler := kp.GetReadinessHandler()
``` ```
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.).

View File

@ -35,36 +35,44 @@ func New(options ...Option) (Kubeprobes, error) {
} }
func (kp *kubeprobes) validate() error { func (kp *kubeprobes) validate() error {
var err error errs := []error{}
if kp.pathLive == "" { if kp.pathLive == "" {
err = errors.Join(err, fmt.Errorf("liveness probe path must not be empty")) errs = append(
errs,
fmt.Errorf("liveness probe path must not be empty"),
)
} }
if kp.pathReady == "" { if kp.pathReady == "" {
err = errors.Join(err, fmt.Errorf("readiness probe path must not be empty")) errs = append(
errs,
fmt.Errorf("readiness probe path must not be empty"),
)
} }
if !strings.HasPrefix(kp.pathLive, "/") { if !strings.HasPrefix(kp.pathLive, "/") {
err = errors.Join(err, fmt.Errorf("liveness probe path must start with slash (current: %q)", kp.pathLive)) errs = append(
errs,
fmt.Errorf("liveness probe path must start with slash (current: %q)", kp.pathLive),
)
} }
if !strings.HasPrefix(kp.pathReady, "/") { if !strings.HasPrefix(kp.pathReady, "/") {
err = errors.Join(err, fmt.Errorf("readiness probe path must start with slash (current: %q)", kp.pathReady)) errs = append(
errs,
fmt.Errorf("readiness probe path must start with slash (current: %q)", kp.pathReady),
)
} }
if kp.pathLive == kp.pathReady { if kp.pathLive == kp.pathReady {
err = errors.Join(err, fmt.Errorf("liveness and readiness probes have the same values (both %q)", kp.pathLive)) errs = append(
errs,
fmt.Errorf("liveness and readiness probes have the same values (both %q)", kp.pathLive),
)
} }
if len(kp.livenessProbes) == 0 { return errors.Join(errs...)
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) {
@ -85,13 +93,13 @@ func (kp *kubeprobes) handleReadiness(w http.ResponseWriter, _ *http.Request) {
} }
} }
// LivenessHandler implements Kubeprobes. // GetLivenessHandler implements Kubeprobes.
func (kp *kubeprobes) LivenessHandler() http.Handler { func (kp *kubeprobes) GetLivenessHandler() http.Handler {
return http.HandlerFunc(kp.handleLiveness) return http.HandlerFunc(kp.handleLiveness)
} }
// ReadinessHandler implements Kubeprobes. // GetReadinessHandler implements Kubeprobes.
func (kp *kubeprobes) ReadinessHandler() http.Handler { func (kp *kubeprobes) GetReadinessHandler() http.Handler {
return http.HandlerFunc(kp.handleReadiness) return http.HandlerFunc(kp.handleReadiness)
} }

View File

@ -15,100 +15,7 @@ func getStatusFromEndpoint(t *testing.T, client *http.Client, endpoint string) i
return resp.StatusCode return resp.StatusCode
} }
func TestValidation(t *testing.T) { func TestKubeprobes(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 {
@ -143,13 +50,10 @@ func TestHandler(t *testing.T) {
}, },
} }
kp, err := New( kp, _ := 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()

View File

@ -6,10 +6,10 @@ import "net/http"
type Kubeprobes interface { type Kubeprobes interface {
http.Handler http.Handler
// LivenessHandler returns [http.Handler] for liveness probes. // GetLivenessHandler returns [http.Handler] for liveness probes.
LivenessHandler() http.Handler GetLivenessHandler() http.Handler
// ReadinessHandler returns [http.Handler] for readiness probes. // GetReadinessHandler returns [http.Handler] for readiness probes.
ReadinessHandler() http.Handler GetReadinessHandler() http.Handler
} }
// Option represents a [Kubeprobes] constructor option. // Option represents a [Kubeprobes] constructor option.