strip one proto layer in ListMachines method

This commit is contained in:
Pavel Sviderski
2024-12-03 11:05:58 +10:00
parent daada3bbe9
commit 77890b8a8a
6 changed files with 28 additions and 17 deletions
+2 -3
View File
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"github.com/spf13/cobra"
"google.golang.org/protobuf/types/known/emptypb"
"net/netip"
"os"
"strings"
@@ -39,7 +38,7 @@ func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
}
defer c.Close()
listResp, err := c.ListMachines(ctx, &emptypb.Empty{})
machines, err := c.ListMachines(ctx)
if err != nil {
return fmt.Errorf("list machines: %w", err)
}
@@ -51,7 +50,7 @@ func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
return fmt.Errorf("write header: %w", err)
}
// Print rows.
for _, member := range listResp.Machines {
for _, member := range machines {
m := member.Machine
subnet, _ := m.Network.Subnet.ToPrefix()
subnet = netip.PrefixFrom(network.MachineIP(subnet), subnet.Bits())
+2 -3
View File
@@ -5,7 +5,6 @@ import (
"fmt"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/go-units"
"google.golang.org/protobuf/types/known/emptypb"
"os"
"text/tabwriter"
"time"
@@ -50,12 +49,12 @@ func inspect(ctx context.Context, uncli *client.CLI, opts *inspectOptions) error
return fmt.Errorf("inspect service: %w", err)
}
resp, err := cli.ListMachines(ctx, &emptypb.Empty{})
machines, err := cli.ListMachines(ctx)
if err != nil {
return fmt.Errorf("list machines: %w", err)
}
machinesNamesByID := make(map[string]string)
for _, m := range resp.Machines {
for _, m := range machines {
machinesNamesByID[m.Machine.Id] = m.Machine.Name
}
+3 -3
View File
@@ -225,12 +225,12 @@ func (cli *CLI) AddMachine(ctx context.Context, remoteMachine RemoteMachine, clu
}
// List other machines in the cluster to include them in the join request.
listResp, err := c.ListMachines(ctx, &emptypb.Empty{})
machines, err := c.ListMachines(ctx)
if err != nil {
return fmt.Errorf("list cluster machines: %w", err)
}
otherMachines := make([]*pb.MachineInfo, 0, len(listResp.Machines)-1)
for _, m := range listResp.Machines {
otherMachines := make([]*pb.MachineInfo, 0, len(machines)-1)
for _, m := range machines {
if m.Machine.Id != addResp.Machine.Id {
otherMachines = append(otherMachines, m.Machine)
}
+15
View File
@@ -0,0 +1,15 @@
package client
import (
"context"
"google.golang.org/protobuf/types/known/emptypb"
"uncloud/internal/machine/api/pb"
)
func (cli *Client) ListMachines(ctx context.Context) ([]*pb.MachineMember, error) {
resp, err := cli.ClusterClient.ListMachines(ctx, &emptypb.Empty{})
if err != nil {
return nil, err
}
return resp.Machines, nil
}
+3 -4
View File
@@ -10,7 +10,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"slices"
"strings"
"uncloud/internal/machine/api/pb"
@@ -52,7 +51,7 @@ func (cli *Client) RunService(ctx context.Context, opts *ServiceOptions) (RunSer
}
// Find a machine to run the service on.
listResp, err := cli.ListMachines(ctx, &emptypb.Empty{})
machines, err := cli.ListMachines(ctx)
if err != nil {
return resp, fmt.Errorf("list machines: %w", err)
}
@@ -60,7 +59,7 @@ func (cli *Client) RunService(ctx context.Context, opts *ServiceOptions) (RunSer
var machine *pb.MachineMember
if opts.Machine != "" {
// Check if the machine ID or name exists if it's explicitly specified.
for _, m := range listResp.Machines {
for _, m := range machines {
if m.Machine.Name == opts.Machine || m.Machine.Id == opts.Machine {
machine = m
break
@@ -70,7 +69,7 @@ func (cli *Client) RunService(ctx context.Context, opts *ServiceOptions) (RunSer
return resp, fmt.Errorf("machine %q not found", opts.Machine)
}
} else {
machine, err = firstAvailableMachine(listResp.Machines)
machine, err = firstAvailableMachine(machines)
if err != nil {
return resp, err
}
+3 -4
View File
@@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"testing"
"time"
"uncloud/internal/cli/client"
@@ -58,7 +57,7 @@ func TestClusterLifecycle(t *testing.T) {
for i, cli := range clients {
// Wait for the machine to reconcile the cluster store.
require.Eventually(t, func() bool {
machines, err := cli.ListMachines(ctx, &emptypb.Empty{})
machines, err := cli.ListMachines(ctx)
if err != nil {
// FailedPrecondition "cluster is not initialised" is expected until the store is reconciled.
if s, ok := status.FromError(err); ok {
@@ -69,11 +68,11 @@ func TestClusterLifecycle(t *testing.T) {
require.NoError(t, err)
}
if len(machines.Machines) != 3 {
if len(machines) != 3 {
return false
}
for _, m := range machines.Machines {
for _, m := range machines {
if pb.MachineMember_UP != m.State {
return false
}