Skip to content

Commit

Permalink
Add Kubernetes Probes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Jan 5, 2024
1 parent f38ddfb commit 3ba1c6e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
51 changes: 51 additions & 0 deletions pkg/probez.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package courier

import (
"net/http"

"github.com/gin-gonic/gin"
)

// Determines if the server is healthy or not.
func (s *Server) IsHealthy() bool {
s.RLock()
defer s.RUnlock()
return s.healthy
}

// Determines if the server is ready or not.
func (s *Server) IsReady() bool {
s.RLock()
defer s.RUnlock()
return s.ready
}

// Set the server health state to the status bool.
func (s *Server) SetHealthy(status bool) {
s.Lock()
defer s.Unlock()
s.healthy = status
}

// Set the server ready state to the status bool.
func (s *Server) SetReady(status bool) {
s.Lock()
defer s.Unlock()
s.ready = status
}

func (s *Server) Healthz(c *gin.Context) {
status := http.StatusOK
if !s.IsHealthy() {
status = http.StatusServiceUnavailable
}
c.Data(status, "text/plain", []byte(http.StatusText(status)))
}

func (s *Server) Readyz(c *gin.Context) {
status := http.StatusOK
if !s.IsReady() {
status = http.StatusServiceUnavailable
}
c.Data(status, "text/plain", []byte(http.StatusText(status)))
}
30 changes: 15 additions & 15 deletions pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ func New(conf config.Config) (s *Server, err error) {
// Server defines the courier service and its webhook handlers.
type Server struct {
sync.RWMutex
conf config.Config
srv *http.Server
router *gin.Engine
store store.Store
started time.Time
healthy bool
url string
echan chan error
conf config.Config // Primary source of truth for server configuration
srv *http.Server // The HTTP server for handling requests
router *gin.Engine // The gin router for muxing requests to handlers
store store.Store // Manages certificate and password storage
healthy bool // Indicates that the service is online and healthy
ready bool // Indicates that the service is ready to accept requests
started time.Time // The timestamp the server was started (for uptime)
url string // The endpoint that the server is hosted on
echan chan error // Sending errors on this channel stops the server
}

// Serve API requests.
Expand Down Expand Up @@ -146,6 +147,12 @@ func (s *Server) Shutdown() (err error) {

// Setup the routes for the courier service.
func (s *Server) setupRoutes() (err error) {
// Kubernetes probe endpoints -- add routes before middleware to ensure these
// endpoints are not logged or subject to other handling that may harm correctness
s.router.GET("/healthz", s.Healthz)
s.router.GET("/livez", s.Healthz)
s.router.GET("/readyz", s.Readyz)

middlewares := []gin.HandlerFunc{
gin.Logger(),
gin.Recovery(),
Expand Down Expand Up @@ -176,13 +183,6 @@ func (s *Server) setupRoutes() (err error) {
return nil
}

// Set the healthy status of the server.
func (s *Server) SetHealthy(healthy bool) {
s.Lock()
s.healthy = healthy
s.Unlock()
}

// Set the URL of the server from the socket
func (s *Server) SetURL(sock net.Listener) {
s.Lock()
Expand Down
10 changes: 2 additions & 8 deletions pkg/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ const (
VersionReleaseNumber = 1
)

// Set the GitVersion via -ldflags="-X 'github.com/rotationalio/ensign/pkg.GitVersion=$(git rev-parse --short HEAD)'"
// Set the GitVersion via -ldflags="-X 'github.com/trisacrypto/courier/pkg.GitVersion=$(git rev-parse --short HEAD)'"
var GitVersion string

// Returns the semantic version for the current build.
func Version() string {
var versionCore string
if VersionPatch > 0 || VersionReleaseLevel != "" {
versionCore = fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
} else {
versionCore = fmt.Sprintf("%d.%d", VersionMajor, VersionMinor)
}

versionCore := fmt.Sprintf("%d.%d.%d", VersionMajor, VersionMinor, VersionPatch)
if VersionReleaseLevel != "" {
if VersionReleaseNumber > 0 {
versionCore = fmt.Sprintf("%s-%s.%d", versionCore, VersionReleaseLevel, VersionReleaseNumber)
Expand Down

0 comments on commit 3ba1c6e

Please sign in to comment.