Skip to content

Commit

Permalink
Merge pull request #1181 from threefoldtech/development_fix_darwin_build
Browse files Browse the repository at this point in the history
remove zos imports from farmerbot and proxy
  • Loading branch information
rawdaGastan authored Sep 3, 2024
2 parents d4c534e + 5a7af5b commit 9be543d
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 235 deletions.
17 changes: 1 addition & 16 deletions farmerbot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,22 @@ require (
github.com/threefoldtech/tfchain/clients/tfchain-client-go v0.0.0-20240827163226-d4e15e206974
github.com/threefoldtech/tfgrid-sdk-go/grid-proxy v0.15.10
github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go v0.15.14
github.com/threefoldtech/zos v0.5.6-0.20240902110349-172a0a29a6ee
github.com/vedhavyas/go-subkey v1.0.3
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/ChainSafe/go-schnorrkel v1.1.0 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/dave/jennifer v1.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/base58 v1.0.5 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/ethereum/go-ethereum v1.11.6 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/schema v1.4.1 // indirect
Expand All @@ -55,21 +50,11 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/threefoldtech/zbus v1.0.1 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/vishvananda/netlink v1.2.1-beta.2 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
github.com/threefoldtech/zos v0.5.6-0.20240902110349-172a0a29a6ee // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.23.0 // indirect
gonum.org/v1/gonum v0.15.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
)
Expand Down
53 changes: 0 additions & 53 deletions farmerbot/go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions farmerbot/internal/farmerbot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"github.com/stretchr/testify/assert"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/mocks"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
proxyTypes "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/types"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
"github.com/threefoldtech/zos/pkg/gridtypes"
)

const (
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestFarmerbot(t *testing.T) {
farmerbot.gridProxyClient = proxy

// mock state
resources := gridtypes.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1}
resources := pkg.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1}
mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false)

state, err := newState(ctx, sub, rmb, inputs, farmTwinID)
Expand Down
6 changes: 3 additions & 3 deletions farmerbot/internal/find_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/rs/zerolog/log"
zos "github.com/threefoldtech/zos/client"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
)

// FindNode finds an available node in the farm
Expand Down Expand Up @@ -133,7 +133,7 @@ func (f *FarmerBot) findNode(sub Substrate, nodeOptions NodeFilterOption) (uint3
return uint32(nodeFound.ID), nil
}

func filterGPUsByVendors(gpus []zos.GPU, vendorsOrDevices []string) (filtered []zos.GPU) {
func filterGPUsByVendors(gpus []pkg.GPU, vendorsOrDevices []string) (filtered []pkg.GPU) {
for _, gpu := range gpus {
for _, filter := range vendorsOrDevices {
if gpu.Vendor == filter {
Expand All @@ -144,7 +144,7 @@ func filterGPUsByVendors(gpus []zos.GPU, vendorsOrDevices []string) (filtered []
return
}

func filterGPUsByDevices(gpus []zos.GPU, vendorsOrDevices []string) (filtered []zos.GPU) {
func filterGPUsByDevices(gpus []pkg.GPU, vendorsOrDevices []string) (filtered []pkg.GPU) {
for _, gpu := range gpus {
for _, filter := range vendorsOrDevices {
if gpu.Device == filter {
Expand Down
7 changes: 3 additions & 4 deletions farmerbot/internal/find_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import (
"github.com/stretchr/testify/assert"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/mocks"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
zos "github.com/threefoldtech/zos/client"
"github.com/threefoldtech/zos/pkg/gridtypes"
)

func reset(t *testing.T, farmerbot FarmerBot, oldNode1, oldNode2 node, oldFarm substrate.Farm) {
Expand Down Expand Up @@ -46,7 +45,7 @@ func TestFindNode(t *testing.T) {
assert.Error(t, err)

// mock state
resources := gridtypes.Capacity{HRU: gridtypes.Unit(convertGBToBytes(1)), SRU: gridtypes.Unit(convertGBToBytes(1)), CRU: 1, MRU: gridtypes.Unit(convertGBToBytes(1))}
resources := pkg.Capacity{HRU: convertGBToBytes(1), SRU: convertGBToBytes(1), CRU: 1, MRU: convertGBToBytes(1)}
mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false)

state, err := newState(ctx, sub, rmb, inputs, farmTwinID)
Expand Down Expand Up @@ -143,7 +142,7 @@ func TestFindNode(t *testing.T) {

t.Run("test valid find node: no gpus with specified device/vendor in first node (second is found)", func(t *testing.T) {
node2 := farmerbot.nodes[1]
node2.gpus = []zos.GPU{
node2.gpus = []pkg.GPU{
{
Device: "device",
Vendor: "vendor",
Expand Down
6 changes: 3 additions & 3 deletions farmerbot/internal/power_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/stretchr/testify/assert"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/mocks"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go/peer"
"github.com/threefoldtech/zos/pkg/gridtypes"
)

func TestPowerLargeScale(t *testing.T) {
Expand All @@ -30,7 +30,7 @@ func TestPowerLargeScale(t *testing.T) {
}

// mock state
resources := gridtypes.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1}
resources := pkg.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1}
mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false)

state, err := newState(ctx, sub, rmb, inputs, farmTwinID)
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestPower(t *testing.T) {
assert.Error(t, err)

// mock state
resources := gridtypes.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1}
resources := pkg.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1}
mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false)

state, err := newState(ctx, sub, rmb, inputs, farmTwinID)
Expand Down
11 changes: 5 additions & 6 deletions farmerbot/internal/rmb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"context"
"time"

"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go"
zos "github.com/threefoldtech/zos/client"
"github.com/threefoldtech/zos/pkg"
)

// RMB is an rmb abstract client interface.
type RMB interface {
Statistics(ctx context.Context, nodeTwin uint32) (stats zos.Counters, err error)
Statistics(ctx context.Context, nodeTwin uint32) (stats pkg.Counters, err error)
GetStoragePools(ctx context.Context, nodeTwin uint32) (pools []pkg.PoolMetrics, err error)
ListGPUs(ctx context.Context, nodeTwin uint32) (gpus []zos.GPU, err error)
ListGPUs(ctx context.Context, nodeTwin uint32) (gpus []pkg.GPU, err error)
}

type RMBNodeClient struct {
Expand All @@ -29,7 +28,7 @@ func NewRmbNodeClient(rmb rmb.Client) *RMBNodeClient {
}

// Statistics returns some node statistics. Including total and available cpu, memory, storage, etc...
func (r *RMBNodeClient) Statistics(ctx context.Context, nodeTwin uint32) (stats zos.Counters, err error) {
func (r *RMBNodeClient) Statistics(ctx context.Context, nodeTwin uint32) (stats pkg.Counters, err error) {
ctx, cancel := context.WithTimeout(ctx, r.rmbTimeout)
defer cancel()

Expand All @@ -47,7 +46,7 @@ func (r *RMBNodeClient) GetStoragePools(ctx context.Context, nodeTwin uint32) (p
}

// ListGPUs return a list of all gpus on the node.
func (r *RMBNodeClient) ListGPUs(ctx context.Context, nodeTwin uint32) (gpus []zos.GPU, err error) {
func (r *RMBNodeClient) ListGPUs(ctx context.Context, nodeTwin uint32) (gpus []pkg.GPU, err error) {
ctx, cancel := context.WithTimeout(ctx, r.rmbTimeout)
defer cancel()

Expand Down
10 changes: 5 additions & 5 deletions farmerbot/internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
)

// state is the state data for farmerbot
Expand Down Expand Up @@ -178,11 +178,11 @@ func getNode(
if (configNode.powerState == off || configNode.powerState == wakingUp) &&
continueOnPoweringOnErr {
// update the total node resources from substrate
configNode.resources.total.update(gridtypes.Capacity{
configNode.resources.total.update(pkg.Capacity{
CRU: uint64(configNode.Resources.CRU),
SRU: gridtypes.Unit(configNode.Resources.SRU),
HRU: gridtypes.Unit(configNode.Resources.HRU),
MRU: gridtypes.Unit(configNode.Resources.MRU),
SRU: uint64(configNode.Resources.SRU),
HRU: uint64(configNode.Resources.HRU),
MRU: uint64(configNode.Resources.MRU),
})
log.Warn().Uint32("nodeID", uint32(nodeObj.ID)).Msg("Node state is off, will skip rmb calls")
return configNode, nil
Expand Down
18 changes: 8 additions & 10 deletions farmerbot/internal/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import (
"github.com/stretchr/testify/assert"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/mocks"
zos "github.com/threefoldtech/zos/client"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
)

func mockRMBAndSubstrateCalls(
ctx context.Context, sub *mocks.MockSubstrate, rmb *mocks.MockRMB,
inputs Config, on bool, noFarm bool,
resources gridtypes.Capacity, errs []string, emptyNode, emptyTwin bool,
resources pkg.Capacity, errs []string, emptyNode, emptyTwin bool,
) {
farmErr, nodesErr, nodeErr, dedicatedErr, rentErr, contractsErr, powerErr, statsErr, poolsErr, gpusErr := mocksErr(errs)

Expand Down Expand Up @@ -94,7 +92,7 @@ func mockRMBAndSubstrateCalls(
// continue
// }

rmb.EXPECT().Statistics(ctx, uint32(twinIDVal)).Return(zos.Counters{Total: resources}, statsErr)
rmb.EXPECT().Statistics(ctx, uint32(twinIDVal)).Return(pkg.Counters{Total: resources}, statsErr)
if statsErr != nil {
return
}
Expand All @@ -104,7 +102,7 @@ func mockRMBAndSubstrateCalls(
return
}

rmb.EXPECT().ListGPUs(ctx, uint32(twinIDVal)).Return([]zos.GPU{}, gpusErr)
rmb.EXPECT().ListGPUs(ctx, uint32(twinIDVal)).Return([]pkg.GPU{}, gpusErr)
if gpusErr != nil {
return
}
Expand All @@ -130,7 +128,7 @@ func TestSetConfig(t *testing.T) {
}},
}

resources := gridtypes.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1}
resources := pkg.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 1}

t.Run("test valid state: no periodic wake up start, wakeup threshold (< min => min)", func(t *testing.T) {
mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false)
Expand Down Expand Up @@ -293,23 +291,23 @@ func TestSetConfig(t *testing.T) {
})

t.Run("test invalid state no node sru", func(t *testing.T) {
resources := gridtypes.Capacity{HRU: 1, SRU: 0, CRU: 1, MRU: 1}
resources := pkg.Capacity{HRU: 1, SRU: 0, CRU: 1, MRU: 1}
mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false)

_, err := newState(ctx, sub, rmb, inputs, farmTwinID)
assert.Error(t, err)
})

t.Run("test invalid state no cru", func(t *testing.T) {
resources := gridtypes.Capacity{HRU: 1, SRU: 1, CRU: 0, MRU: 1}
resources := pkg.Capacity{HRU: 1, SRU: 1, CRU: 0, MRU: 1}
mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false)

_, err := newState(ctx, sub, rmb, inputs, farmTwinID)
assert.Error(t, err)
})

t.Run("test invalid state no mru", func(t *testing.T) {
resources := gridtypes.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 0}
resources := pkg.Capacity{HRU: 1, SRU: 1, CRU: 1, MRU: 0}
mockRMBAndSubstrateCalls(ctx, sub, rmb, inputs, true, false, resources, []string{}, false, false)

_, err := newState(ctx, sub, rmb, inputs, farmTwinID)
Expand Down
16 changes: 7 additions & 9 deletions farmerbot/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
zos "github.com/threefoldtech/zos/client"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
)

// Config is the inputs for configuration for farmerbot
Expand Down Expand Up @@ -41,7 +39,7 @@ type node struct {
resources consumableResources
publicIPsUsed uint64
pools []pkg.PoolMetrics
gpus []zos.GPU
gpus []pkg.GPU
hasActiveRentContract bool
hasActiveContracts bool
dedicated bool
Expand Down Expand Up @@ -188,7 +186,7 @@ func (n *node) update(
}

// UpdateResources updates the node resources from zos resources stats
func (n *node) updateResources(stats zos.Counters) {
func (n *node) updateResources(stats pkg.Counters) {
n.resources.total.update(stats.Total)
n.resources.used.update(stats.Used)
n.resources.system.update(stats.System)
Expand Down Expand Up @@ -256,11 +254,11 @@ func (cap *capacity) isEmpty() bool {
return cap.cru == 0 && cap.mru == 0 && cap.sru == 0 && cap.hru == 0
}

func (cap *capacity) update(c gridtypes.Capacity) {
func (cap *capacity) update(c pkg.Capacity) {
cap.cru = c.CRU
cap.mru = uint64(c.MRU)
cap.sru = uint64(c.SRU)
cap.hru = uint64(c.HRU)
cap.mru = c.MRU
cap.sru = c.SRU
cap.hru = c.HRU
}

// Add adds a new for capacity
Expand Down
17 changes: 8 additions & 9 deletions farmerbot/internal/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

"github.com/stretchr/testify/assert"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
zos "github.com/threefoldtech/zos/client"
"github.com/threefoldtech/zos/pkg/gridtypes"
"github.com/threefoldtech/tfgrid-sdk-go/farmerbot/pkg"
)

var cap = capacity{
Expand Down Expand Up @@ -37,16 +36,16 @@ func TestNodeModel(t *testing.T) {
}

t.Run("test update node resources", func(t *testing.T) {
zosResources := zos.Counters{
Total: gridtypes.Capacity{
zosResources := pkg.Counters{
Total: pkg.Capacity{
CRU: cap.cru,
SRU: gridtypes.Unit(cap.sru),
HRU: gridtypes.Unit(cap.hru),
MRU: gridtypes.Unit(cap.mru),
SRU: cap.sru,
HRU: cap.hru,
MRU: cap.mru,
IPV4U: 1,
},
Used: gridtypes.Capacity{},
System: gridtypes.Capacity{},
Used: pkg.Capacity{},
System: pkg.Capacity{},
}

node.updateResources(zosResources)
Expand Down
Loading

0 comments on commit 9be543d

Please sign in to comment.