Skip to content

Commit

Permalink
Add new APIs for Liveness and Readiness Probes
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Jogeleit committed Jun 10, 2021
1 parent 469f7d9 commit 726179c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,30 @@ func PolicyHandler(s *kyverno.PolicyStore) http.HandlerFunc {
}
}
}

// HealthzHandler for the Liveness REST API
func HealthzHandler(s *kyverno.PolicyStore) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")

policies := s.List()
if len(policies) == 0 {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprint(w, `{ "error": "No Policies found" }`)

return
}

w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
}
}

// ReadyHandler for the Readiness REST API
func ReadyHandler() http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "{}")
}
}
82 changes: 82 additions & 0 deletions pkg/api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,85 @@ func Test_PolicyReportAPI(t *testing.T) {
}
})
}

func Test_HealthzAPI(t *testing.T) {
t.Run("No Policies Respose", func(t *testing.T) {
req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.HealthzHandler(kyverno.NewPolicyStore()))

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusServiceUnavailable {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

expected := `{ "error": "No Policies found" }`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})
t.Run("Success Respose", func(t *testing.T) {
req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

result := kyverno.Rule{
ValidateMessage: "validation error: requests and limits required. Rule autogen-check-for-requests-and-limits failed at path /spec/template/spec/containers/0/resources/requests/",
Name: "autogen-check-for-requests-and-limits",
}

policy := kyverno.Policy{
Kind: "Policy",
Name: "require-ressources",
Namespace: "test",
Rules: []kyverno.Rule{result},
CreationTimestamp: time.Now(),
}

store := kyverno.NewPolicyStore()
store.Add(policy)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.HealthzHandler(store))

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

expected := `{}`
if !strings.Contains(rr.Body.String(), expected) {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})
}

func Test_ReadyAPI(t *testing.T) {
t.Run("Success Respose", func(t *testing.T) {
req, err := http.NewRequest("GET", "/healthz", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(api.ReadyHandler())

handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

expected := `{}`
if !strings.Contains(rr.Body.String(), expected) {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
}
})
}
2 changes: 2 additions & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type httpServer struct {

func (s *httpServer) registerHandler() {
s.mux.HandleFunc("/policies", Gzip(PolicyHandler(s.store)))
s.mux.HandleFunc("/healthz", HealthzHandler(s.store))
s.mux.HandleFunc("/ready", ReadyHandler())
}

func (s *httpServer) Start() error {
Expand Down

0 comments on commit 726179c

Please sign in to comment.