cleanup and refactor health service
This commit is contained in:
@@ -3,29 +3,54 @@ package internal
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func StartHealthEndpoint() {
|
||||
type HealthServer struct {
|
||||
mu sync.RWMutex
|
||||
state int
|
||||
}
|
||||
|
||||
func NewHealthServer() *HealthServer {
|
||||
return &HealthServer{
|
||||
state: http.StatusOK,
|
||||
}
|
||||
}
|
||||
|
||||
func (hs *HealthServer) SetHealthState(code int) {
|
||||
hs.mu.Lock()
|
||||
defer hs.mu.Unlock()
|
||||
hs.state = code
|
||||
}
|
||||
|
||||
func (hs *HealthServer) GetHealthState() int {
|
||||
hs.mu.RLock()
|
||||
defer hs.mu.RUnlock()
|
||||
return hs.state
|
||||
}
|
||||
|
||||
func (hs *HealthServer) Start() {
|
||||
r := mux.NewRouter()
|
||||
r.Use(mux.CORSMethodMiddleware(r))
|
||||
r.HandleFunc("/health", send200).Methods(http.MethodGet)
|
||||
r.HandleFunc("/health", hs.sendHealth).Methods(http.MethodGet)
|
||||
err := http.ListenAndServe("0.0.0.0:8080", r)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Caller": "StartHealthEndpoint",
|
||||
"Caller": "HealthServer.Start",
|
||||
}).Error(fmt.Sprintf("Error creating health endpoint: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
func send200(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
func (hs *HealthServer) sendHealth(w http.ResponseWriter, r *http.Request) {
|
||||
code := hs.GetHealthState()
|
||||
w.WriteHeader(code)
|
||||
_, err := w.Write([]byte{})
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Caller": "send200",
|
||||
"Caller": "HealthServer.sendHealth",
|
||||
}).Error(fmt.Sprintf("Error answering health endpoint: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
@@ -7,8 +7,9 @@ import (
|
||||
)
|
||||
|
||||
func TestHealth(t *testing.T) {
|
||||
hs := NewHealthServer()
|
||||
go func() {
|
||||
StartHealthEndpoint()
|
||||
hs.Start()
|
||||
}()
|
||||
request, _ := http.NewRequest(http.MethodGet, "http://localhost:8080/health", strings.NewReader(""))
|
||||
resp, err := http.DefaultClient.Do(request)
|
||||
|
@@ -25,7 +25,7 @@ func GetAllNodes(cfg *Config) ([]*hcloud.Server, error) {
|
||||
for _, instance := range servers {
|
||||
log.WithFields(log.Fields{
|
||||
"Caller": "GetAllNodes",
|
||||
}).Info(fmt.Sprintf("Found server: %s", instance.Name))
|
||||
}).Debugf("Found server: %s", instance.Name)
|
||||
}
|
||||
|
||||
return servers, nil
|
||||
@@ -39,7 +39,7 @@ func GetAllIps(servers []*hcloud.Server) ([]string, error) {
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"Caller": "GetAllIps",
|
||||
}).Info(fmt.Sprintf("Found IP: %s", instance.PublicNet.IPv4.IP.String()))
|
||||
}).Debugf("Found IP: %s", instance.PublicNet.IPv4.IP.String())
|
||||
ips[i] = instance.PublicNet.IPv4.IP.String()
|
||||
}
|
||||
return ips, nil
|
||||
|
@@ -166,12 +166,3 @@ func getResourceVersion(client *rest.RESTClient, name string) (string, error) {
|
||||
}
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
// func RegisterCiliumCrd() error {
|
||||
// SchemeBuilder := &apiSchema.Builder{GroupVersion: CILIUM_GROUP_VERSION}
|
||||
// err := SchemeBuilder.AddToScheme(scheme.Scheme)
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("could not register cilium crd: %v", err.Error())
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
|
Reference in New Issue
Block a user