generated from nginxinc/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from nginxinc/add-health-endpoints
Add basic probe checks
- Loading branch information
Showing
5 changed files
with
146 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,30 @@ | ||
// Copyright 2023 f5 Inc. All rights reserved. | ||
// Use of this source code is governed by the Apache | ||
// license that can be found in the LICENSE file. | ||
|
||
package probation | ||
|
||
type Check interface { | ||
Check() bool | ||
} | ||
|
||
type LiveCheck struct { | ||
} | ||
|
||
type ReadyCheck struct { | ||
} | ||
|
||
type StartupCheck struct { | ||
} | ||
|
||
func (l *LiveCheck) Check() bool { | ||
return true | ||
} | ||
|
||
func (r *ReadyCheck) Check() bool { | ||
return true | ||
} | ||
|
||
func (s *StartupCheck) Check() bool { | ||
return true | ||
} |
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,9 @@ | ||
// Copyright 2023 f5 Inc. All rights reserved. | ||
// Use of this source code is governed by the Apache | ||
// license that can be found in the LICENSE file. | ||
|
||
/* | ||
Package probation includes support for Kubernetes health and wellness checks. | ||
*/ | ||
|
||
package probation |
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,87 @@ | ||
// Copyright 2023 f5 Inc. All rights reserved. | ||
// Use of this source code is governed by the Apache | ||
// license that can be found in the LICENSE file. | ||
|
||
package probation | ||
|
||
import ( | ||
"fmt" | ||
"github.com/sirupsen/logrus" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
Ok = "OK" | ||
ServiceNotAvailable = "Service Not Available" | ||
ListenPort = 51031 | ||
) | ||
|
||
type HealthServer struct { | ||
httpServer *http.Server | ||
LiveCheck LiveCheck | ||
ReadyCheck ReadyCheck | ||
StartupCheck StartupCheck | ||
} | ||
|
||
func NewHealthServer() *HealthServer { | ||
return &HealthServer{ | ||
LiveCheck: LiveCheck{}, | ||
ReadyCheck: ReadyCheck{}, | ||
StartupCheck: StartupCheck{}, | ||
} | ||
} | ||
|
||
func (hs *HealthServer) Start() { | ||
logrus.Debugf("Starting probe listener on port %d", ListenPort) | ||
|
||
address := fmt.Sprintf(":%d", ListenPort) | ||
|
||
mux := http.NewServeMux() | ||
mux.HandleFunc("/livez", hs.HandleLive) | ||
mux.HandleFunc("/readyz", hs.HandleReady) | ||
mux.HandleFunc("/startupz", hs.HandleStartup) | ||
hs.httpServer = &http.Server{Addr: address, Handler: mux} | ||
|
||
go func() { | ||
if err := hs.httpServer.ListenAndServe(); err != nil { | ||
logrus.Errorf("unable to start probe listener on %s: %v", hs.httpServer.Addr, err) | ||
} | ||
}() | ||
|
||
logrus.Info("Started probe listener on", hs.httpServer.Addr) | ||
} | ||
|
||
func (hs *HealthServer) Stop() { | ||
if err := hs.httpServer.Close(); err != nil { | ||
logrus.Errorf("unable to stop probe listener on %s: %v", hs.httpServer.Addr, err) | ||
} | ||
} | ||
|
||
func (hs *HealthServer) HandleLive(writer http.ResponseWriter, request *http.Request) { | ||
handleProbe(writer, request, &hs.LiveCheck) | ||
} | ||
|
||
func (hs *HealthServer) HandleReady(writer http.ResponseWriter, request *http.Request) { | ||
handleProbe(writer, request, &hs.ReadyCheck) | ||
} | ||
|
||
func (hs *HealthServer) HandleStartup(writer http.ResponseWriter, request *http.Request) { | ||
handleProbe(writer, request, &hs.StartupCheck) | ||
} | ||
|
||
func handleProbe(writer http.ResponseWriter, _ *http.Request, check Check) { | ||
if check.Check() { | ||
writer.WriteHeader(http.StatusOK) | ||
|
||
if _, err := fmt.Fprint(writer, Ok); err != nil { | ||
logrus.Error(err) | ||
} | ||
|
||
} else { | ||
writer.WriteHeader(http.StatusServiceUnavailable) | ||
|
||
if _, err := fmt.Fprint(writer, ServiceNotAvailable); err != nil { | ||
logrus.Error(err) | ||
} | ||
} | ||
} |