|
| 1 | +/* |
| 2 | +Copyright 2014 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package healthz |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "net/http" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "k8s.io/apimachinery/pkg/util/sets" |
| 26 | +) |
| 27 | + |
| 28 | +// HealthzChecker is a named healthz checker. |
| 29 | +type Checker interface { |
| 30 | + Name() string |
| 31 | + Check(req *http.Request) error |
| 32 | +} |
| 33 | + |
| 34 | +// PingHealthz returns true automatically when checked |
| 35 | +var PingHealthz Checker = ping{} |
| 36 | + |
| 37 | +// ping implements the simplest possible healthz checker. |
| 38 | +type ping struct{} |
| 39 | + |
| 40 | +func (ping) Name() string { |
| 41 | + return "ping" |
| 42 | +} |
| 43 | + |
| 44 | +// PingHealthz is a health check that returns true. |
| 45 | +func (ping) Check(_ *http.Request) error { |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// NamedCheck returns a healthz checker for the given name and function. |
| 50 | +func NamedCheck(name string, check func(r *http.Request) error) Checker { |
| 51 | + return &healthzCheck{name, check} |
| 52 | +} |
| 53 | + |
| 54 | +// InstallPathHandler registers handlers for health checking on |
| 55 | +// a specific path to mux. *All handlers* for the path must be |
| 56 | +// specified in exactly one call to InstallPathHandler. Calling |
| 57 | +// InstallPathHandler more than once for the same path and mux will |
| 58 | +// result in a panic. |
| 59 | +func InstallPathHandler(mux mux, path string, checks ...Checker) { |
| 60 | + if len(checks) == 0 { |
| 61 | + log.V(1).Info("No default health checks specified. Installing the ping handler.") |
| 62 | + checks = []Checker{PingHealthz} |
| 63 | + } |
| 64 | + |
| 65 | + mux.Handle(path, handleRootHealthz(checks...)) |
| 66 | + for _, check := range checks { |
| 67 | + log.V(1).Info("installing healthz checker", "checker", check.Name()) |
| 68 | + mux.Handle(fmt.Sprintf("%s/%s", path, check.Name()), adaptCheckToHandler(check.Check)) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// mux is an interface describing the methods InstallHandler requires. |
| 73 | +type mux interface { |
| 74 | + Handle(pattern string, handler http.Handler) |
| 75 | +} |
| 76 | + |
| 77 | +// healthzCheck implements HealthzChecker on an arbitrary name and check function. |
| 78 | +type healthzCheck struct { |
| 79 | + name string |
| 80 | + check func(r *http.Request) error |
| 81 | +} |
| 82 | + |
| 83 | +var _ Checker = &healthzCheck{} |
| 84 | + |
| 85 | +func (c *healthzCheck) Name() string { |
| 86 | + return c.name |
| 87 | +} |
| 88 | + |
| 89 | +func (c *healthzCheck) Check(r *http.Request) error { |
| 90 | + return c.check(r) |
| 91 | +} |
| 92 | + |
| 93 | +// getExcludedChecks extracts the health check names to be excluded from the query param |
| 94 | +func getExcludedChecks(r *http.Request) sets.String { |
| 95 | + checks, found := r.URL.Query()["exclude"] |
| 96 | + if found { |
| 97 | + return sets.NewString(checks...) |
| 98 | + } |
| 99 | + return sets.NewString() |
| 100 | +} |
| 101 | + |
| 102 | +// handleRootHealthz returns an http.HandlerFunc that serves the provided checks. |
| 103 | +func handleRootHealthz(checks ...Checker) http.HandlerFunc { |
| 104 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 105 | + failed := false |
| 106 | + excluded := getExcludedChecks(r) |
| 107 | + var verboseOut bytes.Buffer |
| 108 | + for _, check := range checks { |
| 109 | + // no-op the check if we've specified we want to exclude the check |
| 110 | + if excluded.Has(check.Name()) { |
| 111 | + excluded.Delete(check.Name()) |
| 112 | + fmt.Fprintf(&verboseOut, "[+]%s excluded: ok\n", check.Name()) |
| 113 | + continue |
| 114 | + } |
| 115 | + if err := check.Check(r); err != nil { |
| 116 | + // don't include the error since this endpoint is public. If someone wants more detail |
| 117 | + // they should have explicit permission to the detailed checks. |
| 118 | + log.V(1).Info("healthz check failed", "checker", check.Name(), "error", err) |
| 119 | + fmt.Fprintf(&verboseOut, "[-]%s failed: reason withheld\n", check.Name()) |
| 120 | + failed = true |
| 121 | + } else { |
| 122 | + fmt.Fprintf(&verboseOut, "[+]%s ok\n", check.Name()) |
| 123 | + } |
| 124 | + } |
| 125 | + if excluded.Len() > 0 { |
| 126 | + fmt.Fprintf(&verboseOut, "warn: some health checks cannot be excluded: no matches for %s\n", formatQuoted(excluded.List()...)) |
| 127 | + for _, c := range excluded.List() { |
| 128 | + log.Info("cannot exclude health check, no matches for it", "checker", c) |
| 129 | + } |
| 130 | + } |
| 131 | + // always be verbose on failure |
| 132 | + if failed { |
| 133 | + log.V(1).Info("healthz check failed", "message", verboseOut.String()) |
| 134 | + http.Error(w, fmt.Sprintf("%shealthz check failed", verboseOut.String()), http.StatusInternalServerError) |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + w.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 139 | + w.Header().Set("X-Content-Type-Options", "nosniff") |
| 140 | + if _, found := r.URL.Query()["verbose"]; !found { |
| 141 | + fmt.Fprint(w, "ok") |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + _, err := verboseOut.WriteTo(w) |
| 146 | + if err != nil { |
| 147 | + log.V(1).Info("healthz check failed", "message", verboseOut.String()) |
| 148 | + http.Error(w, fmt.Sprintf("%shealthz check failed", verboseOut.String()), http.StatusInternalServerError) |
| 149 | + return |
| 150 | + } |
| 151 | + fmt.Fprint(w, "healthz check passed\n") |
| 152 | + }) |
| 153 | +} |
| 154 | + |
| 155 | +// adaptCheckToHandler returns an http.HandlerFunc that serves the provided checks. |
| 156 | +func adaptCheckToHandler(c func(r *http.Request) error) http.HandlerFunc { |
| 157 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 158 | + err := c(r) |
| 159 | + if err != nil { |
| 160 | + http.Error(w, fmt.Sprintf("internal server error: %v", err), http.StatusInternalServerError) |
| 161 | + } else { |
| 162 | + fmt.Fprint(w, "ok") |
| 163 | + } |
| 164 | + }) |
| 165 | +} |
| 166 | + |
| 167 | +// formatQuoted returns a formatted string of the health check names, |
| 168 | +// preserving the order passed in. |
| 169 | +func formatQuoted(names ...string) string { |
| 170 | + quoted := make([]string, 0, len(names)) |
| 171 | + for _, name := range names { |
| 172 | + quoted = append(quoted, fmt.Sprintf("%q", name)) |
| 173 | + } |
| 174 | + return strings.Join(quoted, ",") |
| 175 | +} |
0 commit comments