mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-28 03:53:33 +00:00
add ListServices method and ls CLI command
This commit is contained in:
@@ -32,13 +32,13 @@ func NewListCommand() *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
|
func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
|
||||||
c, err := uncli.ConnectCluster(ctx, clusterName)
|
client, err := uncli.ConnectCluster(ctx, clusterName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("connect to cluster: %w", err)
|
return fmt.Errorf("connect to cluster: %w", err)
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer client.Close()
|
||||||
|
|
||||||
machines, err := c.ListMachines(ctx)
|
machines, err := client.ListMachines(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("list machines: %w", err)
|
return fmt.Errorf("list machines: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ func main() {
|
|||||||
machine.NewRootCommand(),
|
machine.NewRootCommand(),
|
||||||
service.NewRootCommand(),
|
service.NewRootCommand(),
|
||||||
service.NewInspectCommand(),
|
service.NewInspectCommand(),
|
||||||
|
service.NewListCommand(),
|
||||||
service.NewRunCommand(),
|
service.NewRunCommand(),
|
||||||
)
|
)
|
||||||
cobra.CheckErr(cmd.Execute())
|
cobra.CheckErr(cmd.Execute())
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
client "uncloud/internal/cli"
|
"uncloud/internal/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
type inspectOptions struct {
|
type inspectOptions struct {
|
||||||
@@ -25,7 +25,7 @@ func NewInspectCommand() *cobra.Command {
|
|||||||
Short: "Display detailed information on a service.",
|
Short: "Display detailed information on a service.",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
uncli := cmd.Context().Value("cli").(*client.CLI)
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||||
opts.service = args[0]
|
opts.service = args[0]
|
||||||
return inspect(cmd.Context(), uncli, &opts)
|
return inspect(cmd.Context(), uncli, &opts)
|
||||||
},
|
},
|
||||||
@@ -37,19 +37,19 @@ func NewInspectCommand() *cobra.Command {
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func inspect(ctx context.Context, uncli *client.CLI, opts *inspectOptions) error {
|
func inspect(ctx context.Context, uncli *cli.CLI, opts *inspectOptions) error {
|
||||||
cli, err := uncli.ConnectCluster(ctx, opts.cluster)
|
client, err := uncli.ConnectCluster(ctx, opts.cluster)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("connect to cluster: %w", err)
|
return fmt.Errorf("connect to cluster: %w", err)
|
||||||
}
|
}
|
||||||
defer cli.Close()
|
defer client.Close()
|
||||||
|
|
||||||
svc, err := cli.InspectService(ctx, opts.service)
|
svc, err := client.InspectService(ctx, opts.service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("inspect service: %w", err)
|
return fmt.Errorf("inspect service: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
machines, err := cli.ListMachines(ctx)
|
machines, err := client.ListMachines(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("list machines: %w", err)
|
return fmt.Errorf("list machines: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"os"
|
||||||
|
"text/tabwriter"
|
||||||
|
"uncloud/internal/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewListCommand() *cobra.Command {
|
||||||
|
var cluster string
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "ls",
|
||||||
|
Aliases: []string{"list"},
|
||||||
|
Short: "List services.",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||||
|
return runList(cmd.Context(), uncli, cluster)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmd.Flags().StringVarP(
|
||||||
|
&cluster, "cluster", "c", "",
|
||||||
|
"Name of the cluster. (default is the current cluster)",
|
||||||
|
)
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runList(ctx context.Context, uncli *cli.CLI, clusterName string) error {
|
||||||
|
client, err := uncli.ConnectCluster(ctx, clusterName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("connect to cluster: %w", err)
|
||||||
|
}
|
||||||
|
defer client.Close()
|
||||||
|
|
||||||
|
services, err := client.ListServices(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("list services: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print the list of services in a table format.
|
||||||
|
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
||||||
|
if _, err = fmt.Fprintln(tw, "SERVICE ID\tNAME\tMODE\tREPLICAS"); err != nil {
|
||||||
|
return fmt.Errorf("write header: %w", err)
|
||||||
|
}
|
||||||
|
for _, s := range services {
|
||||||
|
if _, err = fmt.Fprintf(tw, "%s\t%s\t%s\t%d\n", s.ID, s.Name, s.Mode, len(s.Containers)); err != nil {
|
||||||
|
return fmt.Errorf("write row: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tw.Flush()
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ func NewRootCommand() *cobra.Command {
|
|||||||
Short: "Manage services in an Uncloud cluster.",
|
Short: "Manage services in an Uncloud cluster.",
|
||||||
}
|
}
|
||||||
cmd.AddCommand(
|
cmd.AddCommand(
|
||||||
|
NewListCommand(),
|
||||||
NewRunCommand(),
|
NewRunCommand(),
|
||||||
)
|
)
|
||||||
return cmd
|
return cmd
|
||||||
|
|||||||
@@ -68,11 +68,11 @@ func runRun(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
|
|||||||
return fmt.Errorf("invalid replication mode: %q", opts.mode)
|
return fmt.Errorf("invalid replication mode: %q", opts.mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := uncli.ConnectCluster(ctx, opts.cluster)
|
client, err := uncli.ConnectCluster(ctx, opts.cluster)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("connect to cluster: %w", err)
|
return fmt.Errorf("connect to cluster: %w", err)
|
||||||
}
|
}
|
||||||
defer c.Close()
|
defer client.Close()
|
||||||
|
|
||||||
spec := api.ServiceSpec{
|
spec := api.ServiceSpec{
|
||||||
Container: api.ContainerSpec{
|
Container: api.ContainerSpec{
|
||||||
@@ -82,7 +82,7 @@ func runRun(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
|
|||||||
Mode: opts.mode,
|
Mode: opts.mode,
|
||||||
Name: opts.name,
|
Name: opts.name,
|
||||||
}
|
}
|
||||||
resp, err := c.RunService(ctx, spec)
|
resp, err := client.RunService(ctx, spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("run service: %w", err)
|
return fmt.Errorf("run service: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -437,3 +437,70 @@ func (cli *Client) RemoveService(ctx context.Context, id string) error {
|
|||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListServices returns a list of all services and their containers.
|
||||||
|
func (cli *Client) ListServices(ctx context.Context) ([]api.Service, error) {
|
||||||
|
machines, err := cli.ListMachines(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list machines: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Broadcast the container list request to all available machines.
|
||||||
|
md := metadata.New(nil)
|
||||||
|
for _, m := range machines {
|
||||||
|
if m.State == pb.MachineMember_UP || m.State == pb.MachineMember_SUSPECT {
|
||||||
|
machineIP, _ := m.Machine.Network.ManagementIp.ToAddr()
|
||||||
|
md.Append("machines", machineIP.String())
|
||||||
|
}
|
||||||
|
// TODO: warning about machines that are DOWN.
|
||||||
|
}
|
||||||
|
listCtx := metadata.NewOutgoingContext(ctx, md)
|
||||||
|
|
||||||
|
// List only uncloud-managed containers that belong to some service.
|
||||||
|
opts := container.ListOptions{
|
||||||
|
All: true,
|
||||||
|
Filters: filters.NewArgs(
|
||||||
|
filters.Arg("label", api.LabelServiceID),
|
||||||
|
filters.Arg("label", api.LabelManaged),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
machineContainers, err := cli.ListContainers(listCtx, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list containers: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: optimise by extracting services from the list of all containers instead of inspecting each service.
|
||||||
|
// Most of the code can be reused in both InspectService and ListServices.
|
||||||
|
servicesByID := make(map[string]api.Service)
|
||||||
|
for _, mc := range machineContainers {
|
||||||
|
if mc.Metadata != nil && mc.Metadata.Error != "" {
|
||||||
|
// TODO: return failed machines in the response.
|
||||||
|
fmt.Printf("WARNING: failed to list containers on machine '%s': %s\n",
|
||||||
|
mc.Metadata.Machine, mc.Metadata.Error)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range mc.Containers {
|
||||||
|
ctr := api.Container{Container: c}
|
||||||
|
if _, ok := servicesByID[ctr.ServiceID()]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
svc, err := cli.InspectService(ctx, ctr.ServiceID())
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, ErrNotFound) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("inspect service: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
servicesByID[ctr.ServiceID()] = svc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
services := make([]api.Service, 0, len(servicesByID))
|
||||||
|
for _, svc := range servicesByID {
|
||||||
|
services = append(services, svc)
|
||||||
|
}
|
||||||
|
return services, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -56,6 +56,21 @@ func TestRunService(t *testing.T) {
|
|||||||
assert.Equal(t, name, svc.Name)
|
assert.Equal(t, name, svc.Name)
|
||||||
assert.Equal(t, api.ServiceModeReplicated, svc.Mode)
|
assert.Equal(t, api.ServiceModeReplicated, svc.Mode)
|
||||||
assert.Len(t, svc.Containers, 1)
|
assert.Len(t, svc.Containers, 1)
|
||||||
|
|
||||||
|
services, err := cli.ListServices(ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.GreaterOrEqual(t, len(services), 1)
|
||||||
|
found := false
|
||||||
|
for _, s := range services {
|
||||||
|
if s.ID == svc.ID {
|
||||||
|
assert.Equal(t, name, s.Name)
|
||||||
|
assert.Equal(t, api.ServiceModeReplicated, s.Mode)
|
||||||
|
assert.Len(t, s.Containers, 1)
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, found)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("global mode", func(t *testing.T) {
|
t.Run("global mode", func(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user