refactor: postpone spec resolver initialisation when creating Deployment

This commit is contained in:
Pavel Sviderski
2025-03-24 21:56:30 +10:00
parent 2d923380a3
commit 58b51b974c
10 changed files with 48 additions and 90 deletions
+2 -5
View File
@@ -1,7 +1,6 @@
package client
import (
"context"
"fmt"
"github.com/Masterminds/semver"
"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.
// 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(
ctx context.Context, image string, filter deploy.MachineFilter,
) (*deploy.Deployment, error) {
func (cli *Client) NewCaddyDeployment(image string, filter deploy.MachineFilter) (*deploy.Deployment, error) {
latest, err := LatestCaddyImage()
if err != nil {
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.
+1 -1
View File
@@ -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.
deployment, err := deploy.NewDeployment(ctx, d.Client, spec, nil)
deployment := deploy.NewDeployment(d.Client, spec, nil)
if err != nil {
return fmt.Errorf("create deployment for service '%s': %w", name, err)
}
+2 -5
View File
@@ -1,15 +1,12 @@
package client
import (
"context"
"github.com/psviderski/uncloud/pkg/api"
"github.com/psviderski/uncloud/pkg/client/deploy"
)
// NewDeployment creates a new deployment for the given service specification.
// If strategy is nil, a default deploy.RollingStrategy will be used.
func (cli *Client) NewDeployment(
ctx context.Context, spec api.ServiceSpec, strategy deploy.Strategy,
) (*deploy.Deployment, error) {
return deploy.NewDeployment(ctx, cli, spec, strategy)
func (cli *Client) NewDeployment(spec api.ServiceSpec, strategy deploy.Strategy) *deploy.Deployment {
return deploy.NewDeployment(cli, spec, strategy)
}
+21 -23
View File
@@ -18,12 +18,11 @@ type Client interface {
// 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.
type Deployment struct {
Service *api.Service
Spec api.ServiceSpec
Strategy Strategy
cli Client
specResolver *ServiceSpecResolver
plan *Plan
Service *api.Service
Spec api.ServiceSpec
Strategy Strategy
cli Client
plan *Plan
}
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.
// 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 {
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{
Spec: spec,
Strategy: strategy,
cli: cli,
specResolver: specResolver,
}, nil
Spec: spec,
Strategy: strategy,
cli: cli,
}
}
// 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)
}
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 {
return Plan{}, fmt.Errorf("resolve service spec: %w", err)
}
+1 -5
View File
@@ -42,11 +42,7 @@ func (cli *Client) RunService(
}
}
deployment, err := cli.NewDeployment(ctx, spec, &deploy.RollingStrategy{MachineFilter: filter})
if err != nil {
return resp, fmt.Errorf("create deployment: %w", err)
}
deployment := cli.NewDeployment(spec, &deploy.RollingStrategy{MachineFilter: filter})
plan, err := deployment.Plan(ctx)
if err != nil {
return resp, fmt.Errorf("plan deployment: %w", err)