diff --git a/go.mod b/go.mod index 02cff30d0d1..85809b7989d 100644 --- a/go.mod +++ b/go.mod @@ -42,9 +42,9 @@ require ( k8s.io/apiserver v0.18.12 k8s.io/client-go v11.0.1-0.20190805182717-6502b5e7b1b5+incompatible k8s.io/utils v0.0.0-20200603063816-c1c6865ac451 - knative.dev/hack v0.0.0-20201214230143-4ed1ecb8db24 - knative.dev/pkg v0.0.0-20210107022335-51c72e24c179 - knative.dev/reconciler-test v0.0.0-20210108100436-db4d65735605 + knative.dev/hack v0.0.0-20210112093330-d946d2557383 + knative.dev/pkg v0.0.0-20210112143930-acbf2af596cf + knative.dev/reconciler-test v0.0.0-20210111135237-9b251467ccb6 sigs.k8s.io/yaml v1.2.0 ) diff --git a/go.sum b/go.sum index c54af7f5039..34216ed8273 100644 --- a/go.sum +++ b/go.sum @@ -1098,10 +1098,14 @@ k8s.io/utils v0.0.0-20200603063816-c1c6865ac451 h1:v8ud2Up6QK1lNOKFgiIVrZdMg7Mpm k8s.io/utils v0.0.0-20200603063816-c1c6865ac451/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= knative.dev/hack v0.0.0-20201214230143-4ed1ecb8db24 h1:kIztWfvnIFV8Lhlea02K3YO2mIzcDyQNzrBLn0Oq9sA= knative.dev/hack v0.0.0-20201214230143-4ed1ecb8db24/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI= +knative.dev/hack v0.0.0-20210112093330-d946d2557383 h1:YDYKfHaplhMelgOVP8eRvHlYf5elgZveo36GBDFfDqA= +knative.dev/hack v0.0.0-20210112093330-d946d2557383/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI= knative.dev/pkg v0.0.0-20210107022335-51c72e24c179 h1:lkrgrv69iUk2qhOG9symy15kJUaJZmMybSloi7C3gIw= knative.dev/pkg v0.0.0-20210107022335-51c72e24c179/go.mod h1:hckgW978SdzPA2H5EDvRPY8xsnPuDZLJLbPf8Jte7Q0= -knative.dev/reconciler-test v0.0.0-20210108100436-db4d65735605 h1:gTcj4/ULCzgXEtW+sSd08C5LE3dcPGHU+6/wLT+PVMU= -knative.dev/reconciler-test v0.0.0-20210108100436-db4d65735605/go.mod h1:rmQpZseeqDpg6/ToFzIeV5hTRkOJujaXBCK7iYL7M4E= +knative.dev/pkg v0.0.0-20210112143930-acbf2af596cf h1:6/VyHMk6pa395xWho0KLfVj+7NNrmlO7Ki9jLbVjoaI= +knative.dev/pkg v0.0.0-20210112143930-acbf2af596cf/go.mod h1:hckgW978SdzPA2H5EDvRPY8xsnPuDZLJLbPf8Jte7Q0= +knative.dev/reconciler-test v0.0.0-20210111135237-9b251467ccb6 h1:VE3bjgOLAekEujKZizi9dtl40TsOloowOjy/fCR77LA= +knative.dev/reconciler-test v0.0.0-20210111135237-9b251467ccb6/go.mod h1:rmQpZseeqDpg6/ToFzIeV5hTRkOJujaXBCK7iYL7M4E= pgregory.net/rapid v0.3.3 h1:jCjBsY4ln4Atz78QoBWxUEvAHaFyNDQg9+WU62aCn1U= pgregory.net/rapid v0.3.3/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/pkg/kncloudevents/message_receiver.go b/pkg/kncloudevents/message_receiver.go index af59e5a3470..4ca1c2d0843 100644 --- a/pkg/kncloudevents/message_receiver.go +++ b/pkg/kncloudevents/message_receiver.go @@ -37,6 +37,8 @@ type HTTPMessageReceiver struct { server *http.Server listener net.Listener + + checker http.HandlerFunc } // HTTPMessageReceiverOption enables further configuration of a HTTPMessageReceiver. @@ -52,6 +54,16 @@ func NewHTTPMessageReceiver(port int, o ...HTTPMessageReceiverOption) *HTTPMessa return h } +// WithChecker takes a handler func which will run as an additional health check in Drainer. +// kncloudevents HTTPMessageReceiver uses Drainer to perform health check. +// By default, Drainer directly writes StatusOK to kubelet probe if the Pod is not draining. +// Users can configure customized liveness and readiness check logic by defining checker here. +func WithChecker(checker http.HandlerFunc) HTTPMessageReceiverOption { + return func(h *HTTPMessageReceiver) { + h.checker = checker + } +} + // Blocking func (recv *HTTPMessageReceiver) StartListen(ctx context.Context, handler http.Handler) error { var err error @@ -60,7 +72,8 @@ func (recv *HTTPMessageReceiver) StartListen(ctx context.Context, handler http.H } drainer := &handlers.Drainer{ - Inner: CreateHandler(handler), + Inner: CreateHandler(handler), + HealthCheck: recv.checker, } recv.server = &http.Server{ Addr: recv.listener.Addr().String(), diff --git a/vendor/knative.dev/pkg/network/handlers/drain.go b/vendor/knative.dev/pkg/network/handlers/drain.go index 75919578c97..0ed37ba6dd1 100644 --- a/vendor/knative.dev/pkg/network/handlers/drain.go +++ b/vendor/knative.dev/pkg/network/handlers/drain.go @@ -47,7 +47,8 @@ var newTimer = func(d time.Duration) timer { } // Drainer wraps an inner http.Handler to support responding to kubelet -// probes and KProbes with a "200 OK" until the handler is told to Drain. +// probes and KProbes with a "200 OK" until the handler is told to Drain, +// or Drainer will optionally run the HealthCheck if it is defined. // When the Drainer is told to Drain, it will immediately start to fail // probes with a "500 shutting down", and the call will block until no // requests have been received for QuietPeriod (defaults to @@ -56,6 +57,10 @@ type Drainer struct { // Mutex guards the initialization and resets of the timer sync.RWMutex + // HealthCheck is an optional health check that is performed until the drain signal is received. + // When unspecified, a "200 OK" is returned, otherwise this function is invoked. + HealthCheck http.HandlerFunc + // Inner is the http.Handler to which we delegate actual requests. Inner http.Handler @@ -78,6 +83,8 @@ func (d *Drainer) ServeHTTP(w http.ResponseWriter, r *http.Request) { if network.IsKubeletProbe(r) { // Respond to probes regardless of path. if d.draining() { http.Error(w, "shutting down", http.StatusServiceUnavailable) + } else if d.HealthCheck != nil { + d.HealthCheck(w, r) } else { w.WriteHeader(http.StatusOK) } diff --git a/vendor/modules.txt b/vendor/modules.txt index 5b4b4f0b866..de9b4a9d0b2 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -966,11 +966,11 @@ k8s.io/utils/buffer k8s.io/utils/integer k8s.io/utils/pointer k8s.io/utils/trace -# knative.dev/hack v0.0.0-20201214230143-4ed1ecb8db24 +# knative.dev/hack v0.0.0-20210112093330-d946d2557383 ## explicit knative.dev/hack knative.dev/hack/shell -# knative.dev/pkg v0.0.0-20210107022335-51c72e24c179 +# knative.dev/pkg v0.0.0-20210112143930-acbf2af596cf ## explicit knative.dev/pkg/apiextensions/storageversion knative.dev/pkg/apiextensions/storageversion/cmd/migrate @@ -1097,7 +1097,7 @@ knative.dev/pkg/webhook/resourcesemantics knative.dev/pkg/webhook/resourcesemantics/conversion knative.dev/pkg/webhook/resourcesemantics/defaulting knative.dev/pkg/webhook/resourcesemantics/validation -# knative.dev/reconciler-test v0.0.0-20210108100436-db4d65735605 +# knative.dev/reconciler-test v0.0.0-20210111135237-9b251467ccb6 ## explicit knative.dev/reconciler-test/cmd/eventshub knative.dev/reconciler-test/pkg/environment