mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
chore: replace machine filter with placement constraint in service spec
This commit is contained in:
+12
-30
@@ -19,9 +19,9 @@ import (
|
||||
)
|
||||
|
||||
type deployOptions struct {
|
||||
image string
|
||||
machine string
|
||||
context string
|
||||
image string
|
||||
machines []string
|
||||
context string
|
||||
}
|
||||
|
||||
func NewDeployCommand() *cobra.Command {
|
||||
@@ -40,8 +40,9 @@ func NewDeployCommand() *cobra.Command {
|
||||
|
||||
cmd.Flags().StringVar(&opts.image, "image", "",
|
||||
"Caddy Docker image to deploy. (default caddy:LATEST_VERSION)")
|
||||
cmd.Flags().StringVarP(&opts.machine, "machine", "m", "",
|
||||
"Machine names to deploy to (comma-separated). (default is all machines)")
|
||||
cmd.Flags().StringSliceVarP(&opts.machines, "machine", "m", nil,
|
||||
"Machine names to deploy to. Can be specified multiple times or as a comma-separated "+
|
||||
"list of machine names. (default is all machines)")
|
||||
cmd.Flags().StringVarP(
|
||||
&opts.context, "context", "c", "",
|
||||
"Name of the cluster context to deploy to. (default is the current context)",
|
||||
@@ -88,16 +89,10 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
||||
fmt.Println()
|
||||
fmt.Println("Preparing a deployment plan...")
|
||||
|
||||
var filter deploy.MachineFilter
|
||||
if opts.machine != "" {
|
||||
machines := strings.Split(opts.machine, ",")
|
||||
for i, m := range machines {
|
||||
machines[i] = strings.TrimSpace(m)
|
||||
}
|
||||
filter = machineFilter(machines)
|
||||
placement := api.Placement{
|
||||
Machines: cli.ExpandCommaSeparatedValues(opts.machines),
|
||||
}
|
||||
|
||||
d, err := clusterClient.NewCaddyDeployment(opts.image, filter)
|
||||
d, err := clusterClient.NewCaddyDeployment(opts.image, placement)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create caddy deployment: %w", err)
|
||||
}
|
||||
@@ -108,31 +103,18 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
||||
|
||||
plan, err := d.Plan(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, deploy.ErrNoMatchingMachines) {
|
||||
return fmt.Errorf("no machines found matching: %s", opts.machine)
|
||||
}
|
||||
return fmt.Errorf("plan caddy deployment: %w", err)
|
||||
}
|
||||
|
||||
if len(plan.Operations) == 0 {
|
||||
if opts.machine != "" {
|
||||
fmt.Printf("%s service is up to date on selected machines.\n", client.CaddyServiceName)
|
||||
} else {
|
||||
fmt.Printf("%s service is up to date.\n", client.CaddyServiceName)
|
||||
}
|
||||
fmt.Printf("%s service is up to date.\n", client.CaddyServiceName)
|
||||
} else {
|
||||
if svc.ID == "" {
|
||||
if opts.machine != "" {
|
||||
fmt.Println("This will run a Caddy container on selected machines.")
|
||||
if len(opts.machines) > 0 {
|
||||
fmt.Println("This will run a Caddy container on each selected machine.")
|
||||
} else {
|
||||
fmt.Println("This will run a Caddy container on each machine.")
|
||||
}
|
||||
} else {
|
||||
if opts.machine != "" {
|
||||
fmt.Println("This will perform a rolling update of Caddy containers on selected machines.")
|
||||
} else {
|
||||
fmt.Println("This will perform a rolling update of Caddy containers on each machine.")
|
||||
}
|
||||
}
|
||||
|
||||
// Initialise a machine and container name resolver to properly format the plan output.
|
||||
|
||||
@@ -44,6 +44,8 @@ func NewDeployCommand() *cobra.Command {
|
||||
"One or more Compose files to deploy services from. (default compose-ports-long.yaml)")
|
||||
cmd.Flags().StringVarP(&opts.context, "context", "c", "",
|
||||
"Name of the cluster context to deploy to (default is the current context)")
|
||||
// TODO: Consider adding a filter flag to specify which machines to deploy to but keep the rest running.
|
||||
// Could be useful to test a new version on a subset of machines before rolling out to all.
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -12,13 +12,11 @@ import (
|
||||
"github.com/psviderski/uncloud/cmd/uncloud/caddy"
|
||||
"github.com/psviderski/uncloud/internal/cli"
|
||||
"github.com/psviderski/uncloud/internal/cli/config"
|
||||
"github.com/psviderski/uncloud/internal/machine/api/pb"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"github.com/psviderski/uncloud/pkg/client"
|
||||
"github.com/spf13/cobra"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
type addOptions struct {
|
||||
@@ -105,14 +103,6 @@ func add(ctx context.Context, uncli *cli.CLI, remoteMachine cli.RemoteMachine, o
|
||||
return fmt.Errorf("wait for cluster to be initialised on machine: %w", err)
|
||||
}
|
||||
|
||||
// Inspect the added machine to get its ID to create a filter for the Caddy deployment.
|
||||
minfo, err := machineClient.Inspect(ctx, &emptypb.Empty{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("inspect machine: %w", err)
|
||||
}
|
||||
filter := func(m *pb.MachineInfo) bool {
|
||||
return m.Id == minfo.Id
|
||||
}
|
||||
// Deploy a Caddy service container to the added machine. If caddy service is already deployed on other machines,
|
||||
// use the deployed image version. Otherwise, use the latest version.
|
||||
caddyImage := ""
|
||||
@@ -137,7 +127,9 @@ func add(ctx context.Context, uncli *cli.CLI, remoteMachine cli.RemoteMachine, o
|
||||
}
|
||||
}
|
||||
|
||||
d, err := machineClient.NewCaddyDeployment(caddyImage, filter)
|
||||
// TODO: scale the existing Caddy service to the new machine instead of running a new deployment
|
||||
// that may cause a small downtime.
|
||||
d, err := machineClient.NewCaddyDeployment(caddyImage, api.Placement{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("create caddy deployment: %w", err)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/psviderski/uncloud/internal/cli/config"
|
||||
"github.com/psviderski/uncloud/internal/machine/api/pb"
|
||||
"github.com/psviderski/uncloud/internal/machine/cluster"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -134,7 +135,7 @@ func initCluster(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteM
|
||||
}
|
||||
|
||||
if !opts.noCaddy {
|
||||
d, err := client.NewCaddyDeployment("", nil)
|
||||
d, err := client.NewCaddyDeployment("", api.Placement{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("create caddy deployment: %w", err)
|
||||
}
|
||||
|
||||
+26
-33
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/docker/docker/api/types/volume"
|
||||
"github.com/docker/docker/daemon/names"
|
||||
"github.com/psviderski/uncloud/internal/cli"
|
||||
"github.com/psviderski/uncloud/internal/machine/api/pb"
|
||||
"github.com/psviderski/uncloud/internal/secret"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"github.com/psviderski/uncloud/pkg/client"
|
||||
@@ -66,7 +65,7 @@ func NewRunCommand() *cobra.Command {
|
||||
"the machines) or '%s' (one container on every machine).",
|
||||
api.ServiceModeReplicated, api.ServiceModeGlobal))
|
||||
cmd.Flags().StringSliceVarP(&opts.machines, "machine", "m", nil,
|
||||
"Placement constraint by machine name, limiting which machines the service can run on. Can be specified "+
|
||||
"Placement constraint by machine names, limiting which machines the service can run on. Can be specified "+
|
||||
"multiple times or as a comma-separated list of machine names. (default is any suitable machine)")
|
||||
cmd.Flags().StringVarP(&opts.name, "name", "n", "",
|
||||
"Assign a name to the service. A random name is generated if not specified.")
|
||||
@@ -114,26 +113,15 @@ func run(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
|
||||
}
|
||||
defer clusterClient.Close()
|
||||
|
||||
var deployFilter deploy.MachineFilter
|
||||
machines := cli.ExpandCommaSeparatedValues(opts.machines)
|
||||
if len(machines) > 0 {
|
||||
deployFilter = func(m *pb.MachineInfo) bool {
|
||||
return slices.Contains(machines, m.Name)
|
||||
}
|
||||
}
|
||||
|
||||
machineIDForVolumes, missingVolumes, err := selectMachineForVolumes(ctx, clusterClient, spec.Volumes, machines)
|
||||
machineIDForVolumes, missingVolumes, err := selectMachineForVolumes(
|
||||
ctx,
|
||||
clusterClient,
|
||||
spec.Volumes,
|
||||
spec.Placement.Machines,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// machineIDForVolumes is not empty if the spec includes named volumes.
|
||||
if machineIDForVolumes != "" {
|
||||
// The service must be deployed on the machine where the existing volumes are located and the missing ones
|
||||
// will be created.
|
||||
deployFilter = func(m *pb.MachineInfo) bool {
|
||||
return m.Id == machineIDForVolumes
|
||||
}
|
||||
}
|
||||
|
||||
var resp client.RunServiceResponse
|
||||
err = progress.RunWithTitle(ctx, func(ctx context.Context) error {
|
||||
@@ -145,7 +133,7 @@ func run(ctx context.Context, uncli *cli.CLI, opts runOptions) error {
|
||||
}
|
||||
}
|
||||
|
||||
resp, err = clusterClient.RunService(ctx, spec, deployFilter)
|
||||
resp, err = clusterClient.RunService(ctx, spec)
|
||||
if err != nil {
|
||||
return fmt.Errorf("run service: %w", err)
|
||||
}
|
||||
@@ -207,6 +195,10 @@ func prepareServiceSpec(opts runOptions) (api.ServiceSpec, error) {
|
||||
return spec, err
|
||||
}
|
||||
|
||||
placement := api.Placement{
|
||||
Machines: cli.ExpandCommaSeparatedValues(opts.machines),
|
||||
}
|
||||
|
||||
spec = api.ServiceSpec{
|
||||
Container: api.ContainerSpec{
|
||||
Command: opts.command,
|
||||
@@ -215,11 +207,12 @@ func prepareServiceSpec(opts runOptions) (api.ServiceSpec, error) {
|
||||
PullPolicy: opts.pull,
|
||||
VolumeMounts: mounts,
|
||||
},
|
||||
Mode: opts.mode,
|
||||
Name: opts.name,
|
||||
Ports: ports,
|
||||
Replicas: opts.replicas,
|
||||
Volumes: volumes,
|
||||
Mode: opts.mode,
|
||||
Name: opts.name,
|
||||
Placement: placement,
|
||||
Ports: ports,
|
||||
Replicas: opts.replicas,
|
||||
Volumes: volumes,
|
||||
}
|
||||
|
||||
// Overwrite the default ENTRYPOINT of the image or reset it if an empty string is passed.
|
||||
@@ -372,9 +365,9 @@ func selectMachineForVolumes(
|
||||
ctx context.Context, clusterClient *client.Client, volumes []api.VolumeSpec, machinesFilter []string,
|
||||
) (machineID string, missingVolumes []api.VolumeSpec, err error) {
|
||||
var volumeNames []string
|
||||
for _, volume := range volumes {
|
||||
if volume.Type == api.VolumeTypeVolume {
|
||||
volumeNames = append(volumeNames, volume.Name)
|
||||
for _, v := range volumes {
|
||||
if v.Type == api.VolumeTypeVolume {
|
||||
volumeNames = append(volumeNames, v.Name)
|
||||
}
|
||||
}
|
||||
if len(volumeNames) == 0 {
|
||||
@@ -424,15 +417,15 @@ func selectMachineForVolumes(
|
||||
}
|
||||
|
||||
// Find missing volumes that need to be created on the selected machine.
|
||||
for _, volume := range volumes {
|
||||
if volume.Type != api.VolumeTypeVolume {
|
||||
for _, v := range volumes {
|
||||
if v.Type != api.VolumeTypeVolume {
|
||||
continue
|
||||
}
|
||||
|
||||
if !slices.ContainsFunc(vols, func(v api.MachineVolume) bool {
|
||||
return v.Volume.Name == volume.Name && v.MachineID == machineID
|
||||
if !slices.ContainsFunc(vols, func(mv api.MachineVolume) bool {
|
||||
return mv.Volume.Name == v.Name && mv.MachineID == machineID
|
||||
}) {
|
||||
missingVolumes = append(missingVolumes, volume)
|
||||
missingVolumes = append(missingVolumes, v)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user