Skip to content

Commit

Permalink
Keep on updating internal structures to complete clustering ui
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Nov 20, 2024
1 parent 3001396 commit a956f7b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 13 deletions.
101 changes: 93 additions & 8 deletions adminui/clustering.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,81 @@ import (
"encoding/json"
"log"
"net/http"
"time"

"github.com/NHAS/wag/internal/data"
"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
)

type MembershipDTO struct {
*membership.Member
IsDrained bool
IsWitness bool
func (au *AdminUI) members(w http.ResponseWriter, r *http.Request) {
var members []MembershipDTO
for _, member := range data.GetMembers() {
drained, err := data.IsDrained(member.ID.String())
if err != nil {
log.Println("unable to get drained state: ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

witness, err := data.IsWitness(member.ID.String())
if err != nil {
log.Println("unable to witness state: ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}

version, err := data.GetVersion(member.ID.String())
if err != nil {
log.Println("unable to get version: ", err)
version = "unknown"
}

status := "healthy" // full liveness
if drained {
status = "drained"
} else if !member.IsStarted() {
status = "wait for first connection..."
} else if member.IsLearner {
status = "learner"
}

ping := ""
if status != "learner" {
lastPing, err := data.GetLastPing(member.ID.String())
if err != nil {
log.Println("unable to fetch last ping: ", err)
status = "no last ping"
} else {

if lastPing.Before(time.Now().Add(-6 * time.Second)) {
status += "(lagging ping)"
}

if lastPing.Before(time.Now().Add(-14 * time.Second)) {
status = "dead"
}

Version string
Ping string
Status string
ping = lastPing.Format(time.RFC822)
}
}

members = append(members, MembershipDTO{
ID: member.ID,
PeerUrls: member.PeerURLs,
Name: member.Name,
IsLearner: member.IsLearner,
IsDrained: drained,
IsWitness: witness,
IsCurrentNode: data.GetServerID() == member.ID,
IsLeader: data.GetLeader() == member.ID,
Status: status,
Ping: ping,
Version: version,
})

}

w.Header().Set("content-type", "application/json")
json.NewEncoder(w).Encode(members)
}

func (au *AdminUI) newNode(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -45,6 +107,29 @@ func (au *AdminUI) newNode(w http.ResponseWriter, r *http.Request) {
w.Write(b)
}

func (au *AdminUI) getClusterEvents(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")

events := data.EventsQueue.ReadAll()

var (
es EventsResponseDTO
err error
)
es.EventLog = events
es.Errors, err = data.GetAllErrors()
if err != nil {
var e GenericFailureResponseDTO
e.Message = err.Error()
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(e)

return
}

json.NewEncoder(w).Encode(es)
}

func (au *AdminUI) nodeControl(w http.ResponseWriter, r *http.Request) {
var ncR data.NodeControlRequest
err := json.NewDecoder(r.Body).Decode(&ncR)
Expand Down
27 changes: 27 additions & 0 deletions adminui/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package adminui
import (
"github.com/NHAS/wag/internal/acls"
"github.com/NHAS/wag/internal/data"
"go.etcd.io/etcd/client/pkg/v3/types"
)

type Page struct {
Expand Down Expand Up @@ -90,3 +91,29 @@ type LoginResponsetDTO struct {
CsrfToken string `json:"csrfToken"`
CsrfHeader string `json:"csrfHeader"`
}

type EventsResponseDTO struct {
EventLog []string `json:"events"`
Errors []data.EventError `json:"errors"`
}

type GenericFailureResponseDTO struct {
Message string `json:"failure_message"`
}

type MembershipDTO struct {
ID types.ID `json:"id"`
Name string `json:"name"`

IsDrained bool `json:"drained"`
IsWitness bool `json:"witness"`
IsLeader bool `json:"leader"`
IsLearner bool `json:"learner"`
IsCurrentNode bool `json:"current_node"`

Version string `json:"version"`
Ping string `json:"last_ping"`
Status string `json:"status"`

PeerUrls []string `json:"peer_urls"`
}
2 changes: 2 additions & 0 deletions adminui/ui_webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,11 @@ func New(firewall *router.Firewall, errs chan<- error) (ui *AdminUI, err error)
protectedRoutes.HandleFunc("GET /api/info", adminUI.serverInfo)
protectedRoutes.HandleFunc("GET /api/console_log", adminUI.consoleLog)

protectedRoutes.HandleFunc("GET /api/cluster/members", adminUI.members)
protectedRoutes.HandleFunc("POST /api/cluster/members/new", adminUI.newNode)
protectedRoutes.HandleFunc("POST /api/cluster/members/control", adminUI.nodeControl)

protectedRoutes.HandleFunc("GET /api/cluster/events", adminUI.getClusterEvents)
protectedRoutes.HandleFunc("PUT /api/cluster/events/acknowledge", adminUI.clusterEventsAcknowledge)

protectedRoutes.HandleFunc("GET /api/diag/wg", adminUI.wgDiagnositicsData)
Expand Down
10 changes: 5 additions & 5 deletions internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ func notifyHealthy() {
}

type EventError struct {
NodeID string
ErrorID string
FailedEventData string
Error string
Time time.Time
NodeID string `json:"node_id"`
ErrorID string `json:"error_id"`
FailedEventData string `json:"failed_event_data"`
Error string `json:"error"`
Time time.Time `json:"time"`
}

func RaiseError(raisedError error, value []byte) (err error) {
Expand Down

0 comments on commit a956f7b

Please sign in to comment.