From c0ea85cca19ae719a5f197c909b0cd4509631ccf Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Mon, 18 Sep 2023 14:05:07 +0400 Subject: [PATCH] use new orchestrator API --- internal/state/state.go | 9 +++++++++ internal/util/harness.go | 15 +++++++++++++-- pool.go | 14 ++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/internal/state/state.go b/internal/state/state.go index 9c9e5f0..96b959a 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -9,3 +9,12 @@ type State struct { type PoolController interface { DoRefresh() } + +type NodeInfo struct { + ID string `json:"id"` + IP string `json:"ip"` + Distance float32 `json:"distance"` + Weight int `json:"weight"` + ComplianceCid string `json:"complianceCid"` + Core bool `json:"core"` +} diff --git a/internal/util/harness.go b/internal/util/harness.go index 077e2ea..e89b6d2 100644 --- a/internal/util/harness.go +++ b/internal/util/harness.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "encoding/json" "errors" + "math/rand" "net/http" "net/http/httptest" "net/url" @@ -26,11 +27,21 @@ func BuildCabooseHarness(t *testing.T, n int, maxRetries int, opts ...HarnessOpt ch := &CabooseHarness{} ch.Endpoints = make([]*Endpoint, n) - purls := make([]string, n) + purls := make([]state.NodeInfo, n) for i := 0; i < len(ch.Endpoints); i++ { ch.Endpoints[i] = &Endpoint{} ch.Endpoints[i].Setup() - purls[i] = strings.TrimPrefix(ch.Endpoints[i].Server.URL, "https://") + ip := strings.TrimPrefix(ch.Endpoints[i].Server.URL, "https://") + + cid, _ := cid.V1Builder{Codec: uint64(multicodec.Raw), MhType: uint64(multicodec.Sha2_256)}.Sum([]byte(ip)) + + purls[i] = state.NodeInfo{ + IP: ip, + ID: "node-id", + Weight: rand.Intn(100), + Distance: rand.Float32(), + ComplianceCid: cid.String(), + } } ch.goodOrch = true orch := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/pool.go b/pool.go index 670adfe..8ff4aac 100644 --- a/pool.go +++ b/pool.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/filecoin-saturn/caboose/internal/state" "io" "math/rand" "net/url" @@ -38,13 +39,22 @@ func (p *pool) loadPool() ([]string, error) { } defer resp.Body.Close() - responses := make([]string, 0) + responses := make([]state.NodeInfo, 0) + if err := json.NewDecoder(resp.Body).Decode(&responses); err != nil { goLogger.Warnw("failed to decode backends from orchestrator", "err", err, "endpoint", p.config.OrchestratorEndpoint.String()) return nil, err } + goLogger.Infow("got backends from orchestrators", "cnt", len(responses), "endpoint", p.config.OrchestratorEndpoint.String()) - return responses, nil + + var ips []string + + for _, r := range responses { + ips = append(ips, r.IP) + } + + return ips, nil } type mirroredPoolRequest struct {