Skip to content

Commit

Permalink
named checks if verbose is used
Browse files Browse the repository at this point in the history
Signed-off-by: Imran Pochi <imranpochi@microsoft.com>
  • Loading branch information
ipochi committed Aug 4, 2023
1 parent 7eb8d62 commit f5170c1
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
36 changes: 31 additions & 5 deletions cmd/agent/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package app

import (
"bytes"
"context"
"crypto/tls"
"fmt"
Expand All @@ -26,6 +27,7 @@ import (
"runtime"
runpprof "runtime/pprof"
"strconv"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -107,14 +109,38 @@ func (a *Agent) runHealthServer(o *options.GrpcProxyAgentOptions, cs agent.Readi
livenessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
})

checks := []agent.HealthChecker{agent.PingHealthz, agent.NewServerConnectedHealthz(cs)}
readinessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cs.Ready() {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "ok")
} else {
var failedChecks []string
var individualCheckOutput bytes.Buffer
for _, check := range checks {
if err := check.Check(r); err != nil {
fmt.Fprintf(&individualCheckOutput, "[-]%s failed: %v\n", check.Name(), err)
failedChecks = append(failedChecks, check.Name())
} else {
fmt.Fprintf(&individualCheckOutput, "[+]%s ok\n", check.Name())
}
}

// Always be verbose if the check has failed
if len(failedChecks) > 0 {
klog.V(0).Infoln("%s check failed: \n%v", strings.Join(failedChecks, ","), individualCheckOutput.String())
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "not ready")
fmt.Fprintf(w, individualCheckOutput.String())
return
}

if _, found := r.URL.Query()["verbose"]; !found {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "ok")
return
}

fmt.Fprintf(&individualCheckOutput, "check passed\n")

w.WriteHeader(http.StatusOK)
fmt.Fprint(w, individualCheckOutput.String())
})

muxHandler := http.NewServeMux()
Expand Down
65 changes: 65 additions & 0 deletions pkg/agent/healthz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
copyright 2023 the kubernetes 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 agent

import (
"fmt"
"net/http"
)

type HealthChecker interface {
Name() string
Check(req *http.Request) error
}

// ping implements the simplest possible healthz checker.
type ping struct{}

var PingHealthz HealthChecker = &ping{}

func (p *ping) Name() string {
return "ping"
}

// PingHealthz is a health check that returns true.
func (p *ping) Check(_ *http.Request) error {
return nil
}

type serverConnected struct {
rm ReadinessManager
}

var ServerConnectedHealthz HealthChecker = &serverConnected{}

func NewServerConnectedHealthz(cs ReadinessManager) HealthChecker {
return &serverConnected{
rm: cs,
}
}

func (s *serverConnected) Name() string {
return "server-connected"
}

func (s *serverConnected) Check(_ *http.Request) error {
if s.rm.Ready() {
return nil
}

return fmt.Errorf("no servers connected")
}

0 comments on commit f5170c1

Please sign in to comment.