feat: allow to filter machines for global service deployments

This commit is contained in:
Pavel Sviderski
2025-02-17 20:26:40 +10:00
parent 8610322682
commit e13408d79f
6 changed files with 227 additions and 10 deletions
+2 -2
View File
@@ -22,7 +22,7 @@ var caddyImageTagRegex = regexp.MustCompile(`^2\.\d+\.\d+$`)
// NewCaddyDeployment creates a new deployment for a Caddy reverse proxy service.
// The service is deployed in global mode to all machines in the cluster. If the image is not provided, the latest
// version of the official Caddy Docker image is used.
func (cli *Client) NewCaddyDeployment(image string) (*Deployment, error) {
func (cli *Client) NewCaddyDeployment(image string, filter MachineFilter) (*Deployment, error) {
latest, err := latestCaddyImage()
if err != nil {
return nil, fmt.Errorf("look up latest Caddy image: %w", err)
@@ -56,7 +56,7 @@ func (cli *Client) NewCaddyDeployment(image string) (*Deployment, error) {
},
}
return cli.NewDeployment(spec, &RollingStrategy{})
return cli.NewDeployment(spec, &RollingStrategy{MachineFilter: filter})
}
// latestCaddyImage returns the latest image of the official Caddy Docker image on Docker Hub.
+2 -2
View File
@@ -15,7 +15,7 @@ func TestClient_NewCaddyDeployment(t *testing.T) {
t.Run("latest image from Docker Hub", func(t *testing.T) {
t.Parallel()
deploy, err := cli.NewCaddyDeployment("")
deploy, err := cli.NewCaddyDeployment("", nil)
require.NoError(t, err)
assert.Equal(t, "caddy", deploy.Spec.Name)
@@ -42,7 +42,7 @@ func TestClient_NewCaddyDeployment(t *testing.T) {
t.Parallel()
image := "my-caddy:1.2.3"
deploy, err := cli.NewCaddyDeployment(image)
deploy, err := cli.NewCaddyDeployment(image, nil)
require.NoError(t, err)
assert.Equal(t, "caddy", deploy.Spec.Name)
+5
View File
@@ -7,6 +7,7 @@ import (
"github.com/distribution/reference"
"strings"
"uncloud/internal/api"
"uncloud/internal/machine/api/pb"
"uncloud/internal/secret"
)
@@ -25,6 +26,10 @@ type Plan struct {
SequenceOperation
}
// MachineFilter determines which machines participate in a deployment operation by returning true for
// machines that should be included.
type MachineFilter func(m *pb.MachineInfo) bool
// NewDeployment creates a new deployment for the given service specification.
// If strategy is nil, a default RollingStrategy will be used.
func (cli *Client) NewDeployment(spec api.ServiceSpec, strategy Strategy) (*Deployment, error) {
+11 -1
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"uncloud/internal/api"
"uncloud/internal/machine/api/pb"
"uncloud/internal/secret"
@@ -19,7 +20,10 @@ type Strategy interface {
// RollingStrategy implements a rolling update deployment pattern where containers are updated one at a time
// to minimize service disruption.
type RollingStrategy struct{}
type RollingStrategy struct {
// MachineFilter optionally restricts which machines participate in this deployment.
MachineFilter MachineFilter
}
func (s *RollingStrategy) Plan(
ctx context.Context, cli *Client, svc *api.Service, spec api.ServiceSpec,
@@ -72,6 +76,12 @@ func (s *RollingStrategy) planGlobal(
if err != nil {
return plan, fmt.Errorf("list machines: %w", err)
}
// Filter machines if a machine filter is provided.
if s.MachineFilter != nil {
machines = slices.DeleteFunc(machines, func(m *pb.MachineMember) bool {
return !s.MachineFilter(m.Machine)
})
}
// TODO: figure out how to return a warning if there are machines down. Embed the machinesDown in the plan?
// WARNING: failed to run a service container on machine '%s' which is Down.