mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
allow running a service on a specified machine
This commit is contained in:
+3
-1
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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]")
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user