From 5c74b09f455139fa6fbcbb71bccc80aeaf4f7fc2 Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Sun, 10 Nov 2024 19:38:19 +1000 Subject: [PATCH] allow running a service on a specified machine --- cmd/uncloud/main.go | 4 +++- cmd/uncloud/service/root.go | 16 ++++++++++++++++ cmd/uncloud/{ => service}/run.go | 8 ++++++-- internal/cli/service.go | 22 ++++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 cmd/uncloud/service/root.go rename cmd/uncloud/{ => service}/run.go (83%) diff --git a/cmd/uncloud/main.go b/cmd/uncloud/main.go index dce2ad7a..2e1c8547 100644 --- a/cmd/uncloud/main.go +++ b/cmd/uncloud/main.go @@ -7,6 +7,7 @@ import ( "os" "strings" "uncloud/cmd/uncloud/machine" + "uncloud/cmd/uncloud/service" "uncloud/internal/cli" ) @@ -41,7 +42,8 @@ func main() { cmd.AddCommand( machine.NewRootCommand(), - NewRunCommand(), + service.NewRootCommand(), + service.NewRunCommand(), ) cobra.CheckErr(cmd.Execute()) } diff --git a/cmd/uncloud/service/root.go b/cmd/uncloud/service/root.go new file mode 100644 index 00000000..3d355afa --- /dev/null +++ b/cmd/uncloud/service/root.go @@ -0,0 +1,16 @@ +package service + +import ( + "github.com/spf13/cobra" +) + +func NewRootCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "service", + Short: "Manage services in an Uncloud cluster.", + } + cmd.AddCommand( + NewRunCommand(), + ) + return cmd +} diff --git a/cmd/uncloud/run.go b/cmd/uncloud/service/run.go similarity index 83% rename from cmd/uncloud/run.go rename to cmd/uncloud/service/run.go index c1fcb7fc..17176494 100644 --- a/cmd/uncloud/run.go +++ b/cmd/uncloud/service/run.go @@ -1,4 +1,4 @@ -package main +package service import ( "github.com/spf13/cobra" @@ -13,7 +13,7 @@ func NewRunCommand() *cobra.Command { cmd := &cobra.Command{ Use: "run IMAGE", - Short: "Run a service in a cluster.", + Short: "Run a service.", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { uncli := cmd.Context().Value("cli").(*cli.CLI) @@ -25,6 +25,10 @@ func NewRunCommand() *cobra.Command { cmd.Flags().StringVarP(&opts.Name, "name", "n", "", "Assign a name to the service. A random name is generated if not specified.") + cmd.Flags().StringVarP( + &opts.Machine, "machine", "m", "", + "Name or ID of the machine to run the service on. (default is first available)", + ) cmd.Flags().StringSliceVarP(&opts.Publish, "publish", "p", nil, "Publish a service port to make it accessible outside the cluster. Can be specified multiple times. "+ "Format: [load_balancer_port:]container_port[/protocol]") diff --git a/internal/cli/service.go b/internal/cli/service.go index 8613500b..6d87d613 100644 --- a/internal/cli/service.go +++ b/internal/cli/service.go @@ -4,12 +4,15 @@ import ( "context" "fmt" "github.com/docker/docker/api/types/container" + "google.golang.org/grpc/metadata" + "google.golang.org/protobuf/types/known/emptypb" ) // ServiceOptions contains all the options for creating a service. type ServiceOptions struct { Image string Name string + Machine string Publish []string } @@ -22,6 +25,25 @@ func (cli *CLI) RunService(ctx context.Context, clusterName string, opts *Servic _ = c.Close() }() + machResp, err := c.ListMachines(ctx, &emptypb.Empty{}) + if err != nil { + return fmt.Errorf("list machines: %w", err) + } + // TODO: update ListMachine endpoint to return machine status based on the Corrosion member list. + + machineIP, _ := machResp.Machines[0].Network.ManagementIp.ToAddr() + if opts.Machine != "" { + for _, m := range machResp.Machines { + if m.Name == opts.Machine || m.Id == opts.Machine { + machineIP, _ = m.Network.ManagementIp.ToAddr() + break + } + } + } + + md := metadata.Pairs("machines", machineIP.String()) + ctx = metadata.NewOutgoingContext(ctx, md) + config := &container.Config{ Image: opts.Image, }