mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
refactor: postpone spec resolver initialisation when creating Deployment
This commit is contained in:
@@ -96,7 +96,7 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
|
|||||||
filter = machineFilter(machines)
|
filter = machineFilter(machines)
|
||||||
}
|
}
|
||||||
|
|
||||||
d, err := clusterClient.NewCaddyDeployment(ctx, opts.image, filter)
|
d, err := clusterClient.NewCaddyDeployment(opts.image, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create caddy deployment: %w", err)
|
return fmt.Errorf("create caddy deployment: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ func add(ctx context.Context, uncli *cli.CLI, remoteMachine cli.RemoteMachine, o
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
d, err := machineClient.NewCaddyDeployment(ctx, caddyImage, filter)
|
d, err := machineClient.NewCaddyDeployment(caddyImage, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create caddy deployment: %w", err)
|
return fmt.Errorf("create caddy deployment: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ func initCluster(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteM
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !opts.noCaddy {
|
if !opts.noCaddy {
|
||||||
d, err := client.NewCaddyDeployment(ctx, "", nil)
|
d, err := client.NewCaddyDeployment("", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create caddy deployment: %w", err)
|
return fmt.Errorf("create caddy deployment: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,12 +88,7 @@ func scale(ctx context.Context, uncli *cli.CLI, opts scaleOptions) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
spec.Replicas = opts.replicas
|
spec.Replicas = opts.replicas
|
||||||
|
deployment := clusterClient.NewDeployment(spec, nil)
|
||||||
deployment, err := clusterClient.NewDeployment(ctx, spec, nil)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("create deployment: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
plan, err := deployment.Plan(ctx)
|
plan, err := deployment.Plan(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("plan deployment: %w", err)
|
return fmt.Errorf("plan deployment: %w", err)
|
||||||
|
|||||||
+2
-5
@@ -1,7 +1,6 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Masterminds/semver"
|
"github.com/Masterminds/semver"
|
||||||
"github.com/distribution/reference"
|
"github.com/distribution/reference"
|
||||||
@@ -23,9 +22,7 @@ var caddyImageTagRegex = regexp.MustCompile(`^2\.\d+\.\d+$`)
|
|||||||
// NewCaddyDeployment creates a new deployment for a Caddy reverse proxy service.
|
// 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
|
// 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.
|
// version of the official Caddy Docker image is used.
|
||||||
func (cli *Client) NewCaddyDeployment(
|
func (cli *Client) NewCaddyDeployment(image string, filter deploy.MachineFilter) (*deploy.Deployment, error) {
|
||||||
ctx context.Context, image string, filter deploy.MachineFilter,
|
|
||||||
) (*deploy.Deployment, error) {
|
|
||||||
latest, err := LatestCaddyImage()
|
latest, err := LatestCaddyImage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("look up latest Caddy image: %w", err)
|
return nil, fmt.Errorf("look up latest Caddy image: %w", err)
|
||||||
@@ -59,7 +56,7 @@ func (cli *Client) NewCaddyDeployment(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cli.NewDeployment(ctx, spec, &deploy.RollingStrategy{MachineFilter: filter})
|
return cli.NewDeployment(spec, &deploy.RollingStrategy{MachineFilter: filter}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LatestCaddyImage returns the latest image of the official Caddy Docker image on Docker Hub.
|
// LatestCaddyImage returns the latest image of the official Caddy Docker image on Docker Hub.
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ func (d *Deployment) Plan(ctx context.Context) (deploy.SequenceOperation, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: properly handle depends_on conditions in the service deployment plan as the first operation.
|
// TODO: properly handle depends_on conditions in the service deployment plan as the first operation.
|
||||||
deployment, err := deploy.NewDeployment(ctx, d.Client, spec, nil)
|
deployment := deploy.NewDeployment(d.Client, spec, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create deployment for service '%s': %w", name, err)
|
return fmt.Errorf("create deployment for service '%s': %w", name, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"github.com/psviderski/uncloud/pkg/api"
|
"github.com/psviderski/uncloud/pkg/api"
|
||||||
"github.com/psviderski/uncloud/pkg/client/deploy"
|
"github.com/psviderski/uncloud/pkg/client/deploy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDeployment creates a new deployment for the given service specification.
|
// NewDeployment creates a new deployment for the given service specification.
|
||||||
// If strategy is nil, a default deploy.RollingStrategy will be used.
|
// If strategy is nil, a default deploy.RollingStrategy will be used.
|
||||||
func (cli *Client) NewDeployment(
|
func (cli *Client) NewDeployment(spec api.ServiceSpec, strategy deploy.Strategy) *deploy.Deployment {
|
||||||
ctx context.Context, spec api.ServiceSpec, strategy deploy.Strategy,
|
return deploy.NewDeployment(cli, spec, strategy)
|
||||||
) (*deploy.Deployment, error) {
|
|
||||||
return deploy.NewDeployment(ctx, cli, spec, strategy)
|
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-23
@@ -18,12 +18,11 @@ type Client interface {
|
|||||||
// Deployment manages the process of creating or updating a service to match a desired state.
|
// Deployment manages the process of creating or updating a service to match a desired state.
|
||||||
// It coordinates the validation, planning, and execution of deployment operations.
|
// It coordinates the validation, planning, and execution of deployment operations.
|
||||||
type Deployment struct {
|
type Deployment struct {
|
||||||
Service *api.Service
|
Service *api.Service
|
||||||
Spec api.ServiceSpec
|
Spec api.ServiceSpec
|
||||||
Strategy Strategy
|
Strategy Strategy
|
||||||
cli Client
|
cli Client
|
||||||
specResolver *ServiceSpecResolver
|
plan *Plan
|
||||||
plan *Plan
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Plan struct {
|
type Plan struct {
|
||||||
@@ -40,27 +39,16 @@ var ErrNoMatchingMachines = errors.New("no machines match the filter")
|
|||||||
|
|
||||||
// NewDeployment creates a new deployment for the given service specification.
|
// NewDeployment creates a new deployment for the given service specification.
|
||||||
// If strategy is nil, a default RollingStrategy will be used.
|
// If strategy is nil, a default RollingStrategy will be used.
|
||||||
func NewDeployment(ctx context.Context, cli Client, spec api.ServiceSpec, strategy Strategy) (*Deployment, error) {
|
func NewDeployment(cli Client, spec api.ServiceSpec, strategy Strategy) *Deployment {
|
||||||
if strategy == nil {
|
if strategy == nil {
|
||||||
strategy = &RollingStrategy{}
|
strategy = &RollingStrategy{}
|
||||||
}
|
}
|
||||||
|
|
||||||
clusterDomain, err := cli.GetDomain(ctx)
|
|
||||||
if err != nil && !errors.Is(err, api.ErrNotFound) {
|
|
||||||
return nil, fmt.Errorf("get cluster domain: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
specResolver := &ServiceSpecResolver{
|
|
||||||
// If the domain is not found (not reserved), an empty domain is used for the resolver.
|
|
||||||
ClusterDomain: clusterDomain,
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Deployment{
|
return &Deployment{
|
||||||
Spec: spec,
|
Spec: spec,
|
||||||
Strategy: strategy,
|
Strategy: strategy,
|
||||||
cli: cli,
|
cli: cli,
|
||||||
specResolver: specResolver,
|
}
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plan returns a plan of operations to reconcile the service to the desired state.
|
// Plan returns a plan of operations to reconcile the service to the desired state.
|
||||||
@@ -75,7 +63,17 @@ func (d *Deployment) Plan(ctx context.Context) (Plan, error) {
|
|||||||
return Plan{}, fmt.Errorf("invalid deployment: %w", err)
|
return Plan{}, fmt.Errorf("invalid deployment: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resolvedSpec, err := d.specResolver.Resolve(d.Spec)
|
clusterDomain, err := d.cli.GetDomain(ctx)
|
||||||
|
if err != nil && !errors.Is(err, api.ErrNotFound) {
|
||||||
|
return Plan{}, fmt.Errorf("get cluster domain: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
specResolver := &ServiceSpecResolver{
|
||||||
|
// If the domain is not found (not reserved), an empty domain is used for the resolver.
|
||||||
|
ClusterDomain: clusterDomain,
|
||||||
|
}
|
||||||
|
|
||||||
|
resolvedSpec, err := specResolver.Resolve(d.Spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Plan{}, fmt.Errorf("resolve service spec: %w", err)
|
return Plan{}, fmt.Errorf("resolve service spec: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,11 +42,7 @@ func (cli *Client) RunService(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deployment, err := cli.NewDeployment(ctx, spec, &deploy.RollingStrategy{MachineFilter: filter})
|
deployment := cli.NewDeployment(spec, &deploy.RollingStrategy{MachineFilter: filter})
|
||||||
if err != nil {
|
|
||||||
return resp, fmt.Errorf("create deployment: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
plan, err := deployment.Plan(ctx)
|
plan, err := deployment.Plan(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return resp, fmt.Errorf("plan deployment: %w", err)
|
return resp, fmt.Errorf("plan deployment: %w", err)
|
||||||
|
|||||||
+17
-42
@@ -57,8 +57,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
Image: "portainer/pause:latest",
|
Image: "portainer/pause:latest",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
deployment, err := cli.NewDeployment(ctx, spec, nil)
|
deployment := cli.NewDeployment(spec, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = deployment.Validate(ctx)
|
err = deployment.Validate(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -102,8 +101,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
deployment, err = cli.NewDeployment(ctx, specWithPort, nil)
|
deployment = cli.NewDeployment(specWithPort, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
plan, err = deployment.Plan(ctx)
|
plan, err = deployment.Plan(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -143,8 +141,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
deployment, err = cli.NewDeployment(ctx, specWithPortAndInit, nil)
|
deployment = cli.NewDeployment(specWithPortAndInit, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
plan, err = deployment.Plan(ctx)
|
plan, err = deployment.Plan(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -167,8 +164,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
// Deploying the same spec should be a no-op.
|
// Deploying the same spec should be a no-op.
|
||||||
initialContainers = containers
|
initialContainers = containers
|
||||||
|
|
||||||
deployment, err = cli.NewDeployment(ctx, specWithPortAndInit, nil)
|
deployment = cli.NewDeployment(specWithPortAndInit, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
plan, err = deployment.Plan(ctx)
|
plan, err = deployment.Plan(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -203,9 +199,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
Image: "portainer/pause:latest",
|
Image: "portainer/pause:latest",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
deployment := cli.NewDeployment(spec, nil)
|
||||||
deployment, err := cli.NewDeployment(ctx, spec, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, err = deployment.Run(ctx)
|
_, err = deployment.Run(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -229,9 +223,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
return m.Name == c.Machines[0].Name || m.Name == c.Machines[2].Name
|
return m.Name == c.Machines[0].Name || m.Name == c.Machines[2].Name
|
||||||
}
|
}
|
||||||
strategy := &deploy.RollingStrategy{MachineFilter: filter}
|
strategy := &deploy.RollingStrategy{MachineFilter: filter}
|
||||||
|
deployment = cli.NewDeployment(specWithInit, strategy)
|
||||||
deployment, err = cli.NewDeployment(ctx, specWithInit, strategy)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, err = deployment.Run(ctx)
|
_, err = deployment.Run(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -277,10 +269,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
Mode: api.PortModeHost,
|
Mode: api.PortModeHost,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
deployment = cli.NewDeployment(specWithPort, nil)
|
||||||
deployment, err = cli.NewDeployment(ctx, specWithPort, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, err = deployment.Run(ctx)
|
_, err = deployment.Run(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -310,7 +299,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
deployment, err := cli.NewCaddyDeployment(ctx, "", nil)
|
deployment, err := cli.NewCaddyDeployment("", nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, err = deployment.Run(ctx)
|
_, err = deployment.Run(ctx)
|
||||||
@@ -362,7 +351,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
return m.Name == c.Machines[0].Name
|
return m.Name == c.Machines[0].Name
|
||||||
}
|
}
|
||||||
|
|
||||||
deployment, err := cli.NewCaddyDeployment(ctx, "", filter)
|
deployment, err := cli.NewCaddyDeployment("", filter)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
image := deployment.Spec.Container.Image
|
image := deployment.Spec.Container.Image
|
||||||
|
|
||||||
@@ -382,8 +371,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
filter = func(m *pb.MachineInfo) bool {
|
filter = func(m *pb.MachineInfo) bool {
|
||||||
return m.Name == c.Machines[0].Name || m.Name == c.Machines[2].Name
|
return m.Name == c.Machines[0].Name || m.Name == c.Machines[2].Name
|
||||||
}
|
}
|
||||||
|
deployment, err = cli.NewCaddyDeployment(image, filter)
|
||||||
deployment, err = cli.NewCaddyDeployment(ctx, image, filter)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, err = deployment.Run(ctx)
|
_, err = deployment.Run(ctx)
|
||||||
@@ -427,9 +415,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
},
|
},
|
||||||
Replicas: 2,
|
Replicas: 2,
|
||||||
}
|
}
|
||||||
|
deployment := cli.NewDeployment(spec, nil)
|
||||||
deployment, err := cli.NewDeployment(ctx, spec, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
err = deployment.Validate(ctx)
|
err = deployment.Validate(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -458,9 +444,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
init := true
|
init := true
|
||||||
updatedSpec := spec
|
updatedSpec := spec
|
||||||
updatedSpec.Container.Init = &init
|
updatedSpec.Container.Init = &init
|
||||||
|
deployment = cli.NewDeployment(updatedSpec, nil)
|
||||||
deployment, err = cli.NewDeployment(ctx, updatedSpec, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
plan, err = deployment.Plan(ctx)
|
plan, err = deployment.Plan(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -487,9 +471,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
|
|
||||||
threeReplicaSpec := updatedSpec
|
threeReplicaSpec := updatedSpec
|
||||||
threeReplicaSpec.Replicas = 3
|
threeReplicaSpec.Replicas = 3
|
||||||
|
deployment = cli.NewDeployment(threeReplicaSpec, nil)
|
||||||
deployment, err = cli.NewDeployment(ctx, threeReplicaSpec, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
plan, err = deployment.Plan(ctx)
|
plan, err = deployment.Plan(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -514,9 +496,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
fourReplicaSpec := updatedSpec
|
fourReplicaSpec := updatedSpec
|
||||||
fourReplicaSpec.Container.Command = []string{"updated"}
|
fourReplicaSpec.Container.Command = []string{"updated"}
|
||||||
fourReplicaSpec.Replicas = 5
|
fourReplicaSpec.Replicas = 5
|
||||||
|
deployment = cli.NewDeployment(fourReplicaSpec, nil)
|
||||||
deployment, err = cli.NewDeployment(ctx, fourReplicaSpec, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
plan, err = deployment.Plan(ctx)
|
plan, err = deployment.Plan(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -544,8 +524,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
// 5. Redeploy the exact same spec and verify it's a noop.
|
// 5. Redeploy the exact same spec and verify it's a noop.
|
||||||
initialContainers = containers // Reset container tracking.
|
initialContainers = containers // Reset container tracking.
|
||||||
|
|
||||||
deployment, err = cli.NewDeployment(ctx, fourReplicaSpec, nil)
|
deployment = cli.NewDeployment(fourReplicaSpec, nil)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
plan, err = deployment.Plan(ctx)
|
plan, err = deployment.Plan(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -586,9 +565,7 @@ func TestDeployment(t *testing.T) {
|
|||||||
return m.Name == c.Machines[0].Name || m.Name == c.Machines[1].Name
|
return m.Name == c.Machines[0].Name || m.Name == c.Machines[1].Name
|
||||||
}
|
}
|
||||||
strategy := &deploy.RollingStrategy{MachineFilter: machine01Filter}
|
strategy := &deploy.RollingStrategy{MachineFilter: machine01Filter}
|
||||||
|
deployment := cli.NewDeployment(spec, strategy)
|
||||||
deployment, err := cli.NewDeployment(ctx, spec, strategy)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, err = deployment.Run(ctx)
|
_, err = deployment.Run(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -615,10 +592,8 @@ func TestDeployment(t *testing.T) {
|
|||||||
machine2Filter := func(m *pb.MachineInfo) bool {
|
machine2Filter := func(m *pb.MachineInfo) bool {
|
||||||
return m.Name == c.Machines[2].Name
|
return m.Name == c.Machines[2].Name
|
||||||
}
|
}
|
||||||
|
|
||||||
strategy = &deploy.RollingStrategy{MachineFilter: machine2Filter}
|
strategy = &deploy.RollingStrategy{MachineFilter: machine2Filter}
|
||||||
deployment, err = cli.NewDeployment(ctx, spec, strategy)
|
deployment = cli.NewDeployment(spec, strategy)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
_, err = deployment.Run(ctx)
|
_, err = deployment.Run(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user