-
Notifications
You must be signed in to change notification settings - Fork 332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add health checks #2671
Add health checks #2671
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2022 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package network | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"net/http" | ||
"os" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// ServeHealthProbes sets up liveness and readiness probes. | ||
func ServeHealthProbes(ctx context.Context) { | ||
port := os.Getenv("KNATIVE_HEALTH_PROBES_PORT") | ||
if port == "" { | ||
port = "8080" | ||
} | ||
handler := healthHandler{HealthCheck: newHealthCheck(ctx)} | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", handler.handle) | ||
mux.HandleFunc("/health", handler.handle) | ||
mux.HandleFunc("/readiness", handler.handle) | ||
|
||
server := http.Server{ReadHeaderTimeout: time.Minute, Handler: mux, Addr: ":" + port} | ||
|
||
go func() { | ||
go func() { | ||
<-ctx.Done() | ||
_ = server.Shutdown(ctx) | ||
}() | ||
|
||
// start the web server on port and accept requests | ||
log.Printf("Probes server listening on port %s", port) | ||
|
||
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
log.Fatal(err) | ||
} | ||
}() | ||
} | ||
|
||
func newHealthCheck(sigCtx context.Context) func() error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any thoughts about how we might let users customize this later? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could refactor this to allow pass a func and propagate it from ServeHealthProbes(ctx context.Context) or use context to register another callback there and get it with some func such as healthCheckFromContext. WDYTH? |
||
once := sync.Once{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Activator has the same approach too for its healthcheck. So since the probe handler could be part of a handler chain (we might want to make it generic) we want the msg to be printed once. Of course I can simplify it for now if we expect SIGTERM to be managed once. |
||
return func() error { | ||
select { | ||
// When we get SIGTERM (sigCtx done), let readiness probes start failing. | ||
case <-sigCtx.Done(): | ||
once.Do(func() { | ||
log.Println("Signal context canceled") | ||
}) | ||
return errors.New("received SIGTERM from kubelet") | ||
default: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// healthHandler handles responding to kubelet probes with a provided health check. | ||
type healthHandler struct { | ||
HealthCheck func() error | ||
} | ||
|
||
func (h *healthHandler) handle(w http.ResponseWriter, r *http.Request) { | ||
if IsKubeletProbe(r) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking I know we may not need them today, but I'm wondering if we should be passing the request to the health checking function rather than doing this check here. i.e. |
||
if err := h.HealthCheck(); err != nil { | ||
log.Println("Healthcheck failed: ", err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this extract the logger from the context and use that? |
||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} else { | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
return | ||
} | ||
http.Error(w, "Unexpected request", http.StatusBadRequest) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be an exported method, or can it be internal-only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be private.