feat: add --recreate flag for deploy command to force container recreation

This commit is contained in:
Pasha Sviderski
2025-08-07 18:09:35 +10:00
parent ae9f943404
commit 2e585d0183
5 changed files with 211 additions and 27 deletions
+11 -2
View File
@@ -25,11 +25,16 @@ type Deployment struct {
Client Client
Project *types.Project
SpecResolver *deploy.ServiceSpecResolver
Strategy deploy.Strategy
state *scheduler.ClusterState
plan *deploy.SequenceOperation
}
func NewDeployment(ctx context.Context, cli Client, project *types.Project) (*Deployment, error) {
return NewDeploymentWithStrategy(ctx, cli, project, nil)
}
func NewDeploymentWithStrategy(ctx context.Context, cli Client, project *types.Project, strategy deploy.Strategy) (*Deployment, error) {
state, err := scheduler.InspectClusterState(ctx, cli)
if err != nil {
return nil, fmt.Errorf("inspect cluster state: %w", err)
@@ -39,16 +44,20 @@ func NewDeployment(ctx context.Context, cli Client, project *types.Project) (*De
if err != nil && !errors.Is(err, api.ErrNotFound) {
return nil, fmt.Errorf("get cluster domain: %w", err)
}
resolver := &deploy.ServiceSpecResolver{
// If the domain is not found (not reserved), an empty domain is used for the resolver.
ClusterDomain: domain,
}
if strategy == nil {
strategy = &deploy.RollingStrategy{State: state}
}
return &Deployment{
Client: cli,
Project: project,
SpecResolver: resolver,
Strategy: strategy,
state: state,
}, nil
}
@@ -90,7 +99,7 @@ func (d *Deployment) Plan(ctx context.Context) (deploy.SequenceOperation, error)
for _, spec := range serviceSpecs {
// TODO: properly handle depends_on conditions in the service deployment plan as the first operation.
// Pass the update cluster state with scheduled volumes to the deployment.
deployment := deploy.NewDeployment(d.Client, spec, &deploy.RollingStrategy{State: d.state})
deployment := deploy.NewDeployment(d.Client, spec, d.Strategy)
servicePlan, err := deployment.Plan(ctx)
if err != nil {
return plan, fmt.Errorf("create deployment plan for service '%s': %w", spec.Name, err)
+17 -5
View File
@@ -25,7 +25,8 @@ 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 {
State *scheduler.ClusterState
State *scheduler.ClusterState
ForceRecreate bool
}
func (s *RollingStrategy) Type() string {
@@ -92,7 +93,12 @@ func (s *RollingStrategy) planReplicated(svc *api.Service, spec api.ServiceSpec)
continue
}
status := EvalContainerSpecChange(c.Container.ServiceSpec, spec)
var status ContainerSpecStatus
if s.ForceRecreate {
status = ContainerNeedsRecreate
} else {
status = EvalContainerSpecChange(c.Container.ServiceSpec, spec)
}
containerSpecStatuses[c.Container.ID] = status
if status == ContainerUpToDate {
@@ -225,7 +231,7 @@ func (s *RollingStrategy) planGlobal(svc *api.Service, spec api.ServiceSpec) (Pl
for _, m := range availableMachines {
containers := containersOnMachine[m.Info.Id]
ops, err := reconcileGlobalContainer(containers, spec, plan.ServiceID, m.Info.Id)
ops, err := reconcileGlobalContainer(containers, spec, plan.ServiceID, m.Info.Id, s.ForceRecreate)
if err != nil {
return plan, err
}
@@ -252,7 +258,7 @@ func (s *RollingStrategy) planGlobal(svc *api.Service, spec api.ServiceSpec) (Pl
// It ensures exactly one container with the desired spec is running on the machine by creating a new container and
// removing old ones. If there is a host port conflict, it stops the old container before starting a new one.
func reconcileGlobalContainer(
containers []api.MachineServiceContainer, spec api.ServiceSpec, serviceID, machineID string,
containers []api.MachineServiceContainer, spec api.ServiceSpec, serviceID, machineID string, forceRecreate bool,
) ([]Operation, error) {
var ops []Operation
@@ -274,7 +280,13 @@ func reconcileGlobalContainer(
continue
}
status := EvalContainerSpecChange(c.Container.ServiceSpec, spec)
var status ContainerSpecStatus
if forceRecreate {
status = ContainerNeedsRecreate
} else {
status = EvalContainerSpecChange(c.Container.ServiceSpec, spec)
}
if status == ContainerUpToDate {
// The container is already running with the same spec.
upToDate = true