Skip to content

Commit

Permalink
refactor: fix mock gen (#88)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Apr 17, 2023
1 parent 2859d54 commit f93eee9
Show file tree
Hide file tree
Showing 457 changed files with 61,507 additions and 3,869 deletions.
5 changes: 5 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
quiet: False
inpackage: False
with-expecter: True
keeptree: True
case: underscore
32 changes: 29 additions & 3 deletions cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
"k8s.io/client-go/tools/remotecommand"

manifest "github.com/akash-network/akash-api/go/manifest/v2beta2"
"github.com/akash-network/node/sdl"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
mtypes "github.com/akash-network/akash-api/go/node/market/v1beta3"
"github.com/akash-network/akash-api/go/node/types/unit"
types "github.com/akash-network/akash-api/go/node/types/v1beta3"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
"github.com/akash-network/node/sdl"
mquery "github.com/akash-network/node/x/market/query"
mtypes "github.com/akash-network/akash-api/go/node/market/v1beta3"

ctypes "github.com/akash-network/provider/cluster/types/v1beta3"
akashtypes "github.com/akash-network/provider/pkg/apis/akash.network/v2beta2"
Expand All @@ -44,6 +44,7 @@ var (

var _ Client = (*nullClient)(nil)

//go:generate mockery --name ReadClient
type ReadClient interface {
LeaseStatus(context.Context, mtypes.LeaseID) (map[string]*ctypes.ServiceStatus, error)
ForwardedPortStatus(context.Context, mtypes.LeaseID) (map[string][]ctypes.ForwardedPortStatus, error)
Expand All @@ -62,6 +63,8 @@ type ReadClient interface {
}

// Client interface lease and deployment methods
//
//go:generate mockery --name Client
type Client interface {
ReadClient
Deploy(ctx context.Context, lID mtypes.LeaseID, mgroup *manifest.Group) error
Expand Down Expand Up @@ -143,6 +146,7 @@ func (rp resourcePair) available() sdk.Int {
type node struct {
id string
cpu resourcePair
gpu resourcePair
memory resourcePair
ephemeralStorage resourcePair
}
Expand Down Expand Up @@ -189,6 +193,11 @@ nodes:
continue nodes
}

gpu := nd.gpu.dup()
if adjusted = gpu.subNLZ(res.Resources.GPU.Units); !adjusted {
continue nodes
}

memory := nd.memory.dup()
if adjusted = memory.subNLZ(res.Resources.Memory.Quantity); !adjusted {
continue nodes
Expand Down Expand Up @@ -242,6 +251,7 @@ nodes:
currInventory.nodes[nodeName] = &node{
id: nd.id,
cpu: cpu,
gpu: gpu,
memory: memory,
ephemeralStorage: ephemeralStorage,
}
Expand All @@ -266,11 +276,13 @@ nodes:

func (inv *inventory) Metrics() ctypes.InventoryMetrics {
cpuTotal := uint64(0)
gpuTotal := uint64(0)
memoryTotal := uint64(0)
storageEphemeralTotal := uint64(0)
storageTotal := make(map[string]int64)

cpuAvailable := uint64(0)
gpuAvailable := uint64(0)
memoryAvailable := uint64(0)
storageEphemeralAvailable := uint64(0)
storageAvailable := make(map[string]int64)
Expand All @@ -290,13 +302,19 @@ func (inv *inventory) Metrics() ctypes.InventoryMetrics {
}

cpuTotal += nd.cpu.allocatable.Uint64()
gpuTotal += nd.gpu.allocatable.Uint64()

memoryTotal += nd.memory.allocatable.Uint64()
storageEphemeralTotal += nd.ephemeralStorage.allocatable.Uint64()

tmp := nd.cpu.allocatable.Sub(nd.cpu.allocated)
invNode.Available.CPU = tmp.Uint64()
cpuAvailable += invNode.Available.CPU

tmp = nd.gpu.allocatable.Sub(nd.gpu.allocated)
invNode.Available.GPU = tmp.Uint64()
gpuAvailable += invNode.Available.GPU

tmp = nd.memory.allocatable.Sub(nd.memory.allocated)
invNode.Available.Memory = tmp.Uint64()
memoryAvailable += invNode.Available.Memory
Expand All @@ -310,13 +328,15 @@ func (inv *inventory) Metrics() ctypes.InventoryMetrics {

ret.TotalAllocatable = ctypes.InventoryMetricTotal{
CPU: cpuTotal,
GPU: gpuTotal,
Memory: memoryTotal,
StorageEphemeral: storageEphemeralTotal,
Storage: storageTotal,
}

ret.TotalAvailable = ctypes.InventoryMetricTotal{
CPU: cpuAvailable,
GPU: gpuAvailable,
Memory: memoryAvailable,
StorageEphemeral: storageEphemeralAvailable,
Storage: storageAvailable,
Expand All @@ -334,6 +354,7 @@ func (inv *inventory) dup() *inventory {
res.nodes = append(res.nodes, &node{
id: nd.id,
cpu: nd.cpu.dup(),
gpu: nd.gpu.dup(),
memory: nd.memory.dup(),
ephemeralStorage: nd.ephemeralStorage.dup(),
})
Expand All @@ -345,6 +366,7 @@ func (inv *inventory) dup() *inventory {
const (
// 5 CPUs, 5Gi memory for null client.
nullClientCPU = 5000
nullClientGPU = 2
nullClientMemory = 32 * unit.Gi
nullClientStorage = 512 * unit.Gi
)
Expand Down Expand Up @@ -543,6 +565,10 @@ func (c *nullClient) Inventory(context.Context) (ctypes.Inventory, error) {
allocatable: sdk.NewInt(nullClientCPU),
allocated: sdk.NewInt(nullClientCPU - 100),
},
gpu: resourcePair{
allocatable: sdk.NewInt(nullClientGPU),
allocated: sdk.NewInt(1),
},
memory: resourcePair{
allocatable: sdk.NewInt(nullClientMemory),
allocated: sdk.NewInt(nullClientMemory - unit.Gi),
Expand Down
12 changes: 12 additions & 0 deletions cluster/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ func (is *inventoryService) lookup(order mtypes.OrderID, resources atypes.Resour
}

func (is *inventoryService) reserve(order mtypes.OrderID, resources atypes.ResourceGroup) (ctypes.Reservation, error) {
for idx, res := range resources.GetResources() {
if res.Resources.CPU == nil {
return nil, fmt.Errorf("%w: CPU resource at idx %d is nil", ErrInvalidResource, idx)
}
if res.Resources.GPU == nil {
return nil, fmt.Errorf("%w: GPU resource at idx %d is nil", ErrInvalidResource, idx)
}
if res.Resources.Memory == nil {
return nil, fmt.Errorf("%w: Memory resource at idx %d is nil", ErrInvalidResource, idx)
}
}

ch := make(chan inventoryResponse, 1)
req := inventoryRequest{
order: order,
Expand Down
47 changes: 33 additions & 14 deletions cluster/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/stretchr/testify/require"

manifest "github.com/akash-network/akash-api/go/manifest/v2beta2"
"github.com/akash-network/node/pubsub"
"github.com/akash-network/node/testutil"
"github.com/akash-network/akash-api/go/node/types/unit"
types "github.com/akash-network/akash-api/go/node/types/v1beta3"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
mtypes "github.com/akash-network/akash-api/go/node/market/v1beta3"
"github.com/akash-network/akash-api/go/node/types/unit"
types "github.com/akash-network/akash-api/go/node/types/v1beta3"
"github.com/akash-network/node/pubsub"
"github.com/akash-network/node/testutil"

"github.com/akash-network/provider/cluster/mocks"
"github.com/akash-network/provider/cluster/operatorclients"
Expand Down Expand Up @@ -46,6 +46,10 @@ func newInventory(nodes ...string) ctypes.Inventory {
allocatable: sdk.NewInt(nullClientCPU),
allocated: sdk.NewInt(100),
},
gpu: resourcePair{
allocatable: sdk.NewInt(nullClientGPU),
allocated: sdk.NewInt(1),
},
memory: resourcePair{
allocatable: sdk.NewInt(nullClientMemory),
allocated: sdk.NewInt(1 * unit.Gi),
Expand All @@ -62,14 +66,17 @@ func newInventory(nodes ...string) ctypes.Inventory {
return inv
}

func TestInventory_reservationAllocateable(t *testing.T) {
mkrg := func(cpu uint64, memory uint64, storage uint64, endpointsCount uint, count uint32) dtypes.Resource {
func TestInventory_reservationAllocatable(t *testing.T) {
mkrg := func(cpu uint64, gpu uint64, memory uint64, storage uint64, endpointsCount uint, count uint32) dtypes.Resource {
endpoints := make([]types.Endpoint, endpointsCount)
return dtypes.Resource{
Resources: types.ResourceUnits{
CPU: &types.CPU{
Units: types.NewResourceValue(cpu),
},
GPU: &types.GPU{
Units: types.NewResourceValue(gpu),
},
Memory: &types.Memory{
Quantity: types.NewResourceValue(memory),
},
Expand All @@ -94,14 +101,14 @@ func TestInventory_reservationAllocateable(t *testing.T) {
inv := newInventory("a", "b")

reservations := []*reservation{
mkres(true, mkrg(750, 3*unit.Gi, 1*unit.Gi, 0, 1)),
mkres(true, mkrg(100, 4*unit.Gi, 1*unit.Gi, 0, 2)),
mkres(true, mkrg(2000, 3*unit.Gi, 1*unit.Gi, 0, 2)),
mkres(true, mkrg(250, 12*unit.Gi, 1*unit.Gi, 0, 2)),
mkres(true, mkrg(100, 1*unit.G, 1*unit.Gi, 1, 2)),
mkres(true, mkrg(100, 4*unit.G, 1*unit.Gi, 0, 1)),
mkres(true, mkrg(100, 4*unit.G, 98*unit.Gi, 0, 1)),
mkres(true, mkrg(250, 1*unit.G, 1*unit.Gi, 0, 1)),
mkres(true, mkrg(750, 0, 3*unit.Gi, 1*unit.Gi, 0, 1)),
mkres(true, mkrg(100, 0, 4*unit.Gi, 1*unit.Gi, 0, 2)),
mkres(true, mkrg(2000, 0, 3*unit.Gi, 1*unit.Gi, 0, 2)),
mkres(true, mkrg(250, 0, 12*unit.Gi, 1*unit.Gi, 0, 2)),
mkres(true, mkrg(100, 0, 1*unit.G, 1*unit.Gi, 1, 2)),
mkres(true, mkrg(100, 0, 4*unit.G, 1*unit.Gi, 0, 1)),
mkres(true, mkrg(100, 0, 4*unit.G, 98*unit.Gi, 0, 1)),
mkres(true, mkrg(250, 0, 1*unit.G, 1*unit.Gi, 0, 1)),
}

for idx, r := range reservations {
Expand Down Expand Up @@ -186,6 +193,9 @@ func TestInventory_ClusterDeploymentDeployed(t *testing.T) {
CPU: &types.CPU{
Units: types.NewResourceValue(1),
},
GPU: &types.GPU{
Units: types.NewResourceValue(0),
},
Memory: &types.Memory{
Quantity: types.NewResourceValue(1 * unit.Gi),
},
Expand Down Expand Up @@ -321,6 +331,9 @@ func makeInventoryScaffold(t *testing.T, leaseQty uint, inventoryCall bool, node
CPU: &types.CPU{
Units: types.NewResourceValue(4000),
},
GPU: &types.GPU{
Units: types.NewResourceValue(0),
},
Memory: &types.Memory{
Quantity: types.NewResourceValue(30 * unit.Gi),
},
Expand Down Expand Up @@ -388,6 +401,9 @@ func makeGroupForInventoryTest(sharedHTTP, nodePort, leasedIP bool) manifest.Gro
CPU: &types.CPU{
Units: types.NewResourceValue(4000),
},
GPU: &types.GPU{
Units: types.NewResourceValue(0),
},
Memory: &types.Memory{
Quantity: types.NewResourceValue(30 * unit.Gi),
},
Expand Down Expand Up @@ -607,6 +623,9 @@ func TestInventory_OverReservations(t *testing.T) {
CPU: &types.CPU{
Units: types.NewResourceValue(4000),
},
GPU: &types.GPU{
Units: types.NewResourceValue(0),
},
Memory: &types.Memory{
Quantity: types.NewResourceValue(30 * unit.Gi),
},
Expand Down
4 changes: 2 additions & 2 deletions cluster/kube/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"k8s.io/client-go/rest"

manifest "github.com/akash-network/akash-api/go/manifest/v2beta2"
"github.com/akash-network/node/testutil"
types "github.com/akash-network/akash-api/go/node/types/v1beta3"
mtypes "github.com/akash-network/akash-api/go/node/market/v1beta3"
types "github.com/akash-network/akash-api/go/node/types/v1beta3"
"github.com/akash-network/node/testutil"

"github.com/akash-network/provider/cluster/kube/builder"
kubeclienterrors "github.com/akash-network/provider/cluster/kube/errors"
Expand Down
9 changes: 6 additions & 3 deletions cluster/kube/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/akash-network/node/testutil"
"github.com/akash-network/akash-api/go/node/types/unit"
atypes "github.com/akash-network/akash-api/go/node/types/v1beta3"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
mtypes "github.com/akash-network/akash-api/go/node/market/v1beta3"
"github.com/akash-network/akash-api/go/node/types/unit"
atypes "github.com/akash-network/akash-api/go/node/types/v1beta3"
"github.com/akash-network/node/testutil"

ctypes "github.com/akash-network/provider/cluster/types/v1beta3"
akashclientfake "github.com/akash-network/provider/pkg/client/clientset/versioned/fake"
Expand Down Expand Up @@ -608,6 +608,9 @@ func multipleReplicasGenReservations(cpuUnits uint64, count uint32) *testReserva
CPU: &atypes.CPU{
Units: atypes.NewResourceValue(cpuUnits),
},
GPU: &atypes.GPU{
Units: atypes.NewResourceValue(0),
},
Memory: &atypes.Memory{
Quantity: atypes.NewResourceValue(16 * unit.Gi),
},
Expand Down
1 change: 1 addition & 0 deletions cluster/kube/metallb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var (
errInvalidLeaseService = fmt.Errorf("%w lease service error", errMetalLB)
)

//go:generate mockery --name Client --structname MetalLBClient --filename metallb_client.go --output ../../mocks
type Client interface {
GetIPAddressUsage(ctx context.Context) (uint, uint, error)
GetIPAddressStatusForLease(ctx context.Context, leaseID mtypes.LeaseID) ([]v1beta2.IPLeaseState, error)
Expand Down
Loading

0 comments on commit f93eee9

Please sign in to comment.