Skip to content

Commit

Permalink
add health probes
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx committed May 27, 2024
1 parent 97d3842 commit 831c5eb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"node-agent/pkg/healthmanager"
"strings"

"net/http"
Expand Down Expand Up @@ -240,6 +241,10 @@ func main() {
// Start the prometheusExporter
prometheusExporter.Start()

// Start the health manager
healthManager := healthmanager.NewHealthManager(mainHandler)
healthManager.Start(ctx)

// Start the container handler
err = mainHandler.Start(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/containerwatcher/container_watcher_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type ContainerWatcher interface {
Ready() bool
Start(ctx context.Context) error
Stop()
}
4 changes: 4 additions & 0 deletions pkg/containerwatcher/container_watcher_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (

type ContainerWatcherMock struct{}

func (c ContainerWatcherMock) Ready() bool {
return true
}

func (c ContainerWatcherMock) Start(_ context.Context) error {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/containerwatcher/v1/container_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,7 @@ func (ch *IGContainerWatcher) Stop() {
ch.running = false
}
}

func (ch *IGContainerWatcher) Ready() bool {
return ch.running
}
52 changes: 52 additions & 0 deletions pkg/healthmanager/health_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package healthmanager

import (
"context"
"fmt"
"net/http"
"node-agent/pkg/containerwatcher/v1"
"time"

"github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
)

type HealthManager struct {
containerWatcher *containerwatcher.IGContainerWatcher
port int
}

func NewHealthManager(containerWatcher *containerwatcher.IGContainerWatcher) *HealthManager {
return &HealthManager{
containerWatcher: containerWatcher,
port: 8080,
}
}

func (h *HealthManager) Start(ctx context.Context) {
go func() {
http.HandleFunc("/livez", h.livenessProbe)
http.HandleFunc("/readyz", h.readinessProbe)
srv := &http.Server{
Addr: fmt.Sprintf(":%d", h.port),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
logger.L().Info("starting health manager", helpers.Int("port", h.port))
if err := srv.ListenAndServe(); err != nil {
logger.L().Ctx(ctx).Fatal("failed to start health manager", helpers.Error(err), helpers.Int("port", h.port))
}
}()
}

func (h *HealthManager) livenessProbe(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}

func (h *HealthManager) readinessProbe(w http.ResponseWriter, _ *http.Request) {
if h.containerWatcher.Ready() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}

0 comments on commit 831c5eb

Please sign in to comment.