-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matthias Bertschy <[email protected]>
- Loading branch information
Showing
5 changed files
with
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
) | ||
|
||
type ContainerWatcher interface { | ||
Ready() bool | ||
Start(ctx context.Context) error | ||
Stop() | ||
} |
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,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) | ||
} | ||
} |