forked from aws/aws-node-termination-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Liveness probe (without tests and docs update)
- Loading branch information
Yauheni Sliaptsou
committed
Mar 10, 2021
1 parent
23b97f4
commit 805f6c6
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package observability | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// Probes represents probes | ||
type Probes struct { | ||
server *http.Server | ||
} | ||
|
||
// InitProbes will initialize, register and expose, via http server, the probes. | ||
func InitProbes(enabled bool, port int, endpoint string) (Probes, error) { | ||
if !enabled { | ||
return Probes{}, nil | ||
} | ||
|
||
clear := fmt.Sprintf("%s", strings.TrimSpace(endpoint)) | ||
log.Info().Msgf("Starting to serve handler /%s, port %d", clear, port) | ||
http.HandleFunc(fmt.Sprintf("/%s", clear), LivenessHandler) | ||
|
||
probes := Probes{ | ||
server: &http.Server{ | ||
Addr: net.JoinHostPort("", strconv.Itoa(port)), | ||
ReadTimeout: 1 * time.Second, | ||
WriteTimeout: 1 * time.Second, | ||
}, | ||
} | ||
|
||
go func() { | ||
if err := probes.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
log.Err(err).Msg("Failed to listen and serve http server") | ||
} | ||
}() | ||
|
||
return probes, nil | ||
} | ||
|
||
func LivenessHandler(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(http.StatusText(http.StatusOK))) | ||
} | ||
|
||
func (p Probes) Shutdown(ctx context.Context) error { | ||
if p.server != nil { | ||
return nil | ||
} | ||
|
||
if err := p.server.Shutdown(ctx); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package observability | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestLivenessHandler(t *testing.T) { | ||
req := httptest.NewRequest("GET", "/healthz", nil) | ||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(LivenessHandler) | ||
|
||
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) | ||
} | ||
|
||
if body := rr.Body.String(); body != http.StatusText(http.StatusOK) { | ||
t.Errorf("handler returned wrong body: got %v want %v", | ||
body, http.StatusText(http.StatusOK)) | ||
} | ||
} |