mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
chore: show container image for the remove container plan operation
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
|
"github.com/docker/docker/pkg/stringid"
|
||||||
"github.com/docker/go-units"
|
"github.com/docker/go-units"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -144,6 +145,11 @@ type ServiceContainer struct {
|
|||||||
ServiceSpec ServiceSpec
|
ServiceSpec ServiceSpec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShortID returns the truncated ID of the container (12 characters).
|
||||||
|
func (c *ServiceContainer) ShortID() string {
|
||||||
|
return stringid.TruncateID(c.ID)
|
||||||
|
}
|
||||||
|
|
||||||
// ServiceID returns the ID of the service this container belongs to.
|
// ServiceID returns the ID of the service this container belongs to.
|
||||||
func (c *ServiceContainer) ServiceID() string {
|
func (c *ServiceContainer) ServiceID() string {
|
||||||
return c.Config.Labels[LabelServiceID]
|
return c.Config.Labels[LabelServiceID]
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ type NameResolver interface {
|
|||||||
ContainerName(containerID string) string
|
ContainerName(containerID string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: pass api.ServiceContainer to operations to simplify operation formatting in the plan.
|
||||||
|
|
||||||
// RunContainerOperation creates and starts a new container on a specific machine.
|
// RunContainerOperation creates and starts a new container on a specific machine.
|
||||||
type RunContainerOperation struct {
|
type RunContainerOperation struct {
|
||||||
ServiceID string
|
ServiceID string
|
||||||
@@ -56,8 +58,8 @@ func (o *RunContainerOperation) Format(resolver NameResolver) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *RunContainerOperation) String() string {
|
func (o *RunContainerOperation) String() string {
|
||||||
return fmt.Sprintf("RunContainerOperation[service_id=%s, image=%s, machine_id=%s]",
|
return fmt.Sprintf("RunContainerOperation[machine_id=%s service_id=%s image=%s]",
|
||||||
o.ServiceID, o.Spec.Container.Image, o.MachineID)
|
o.MachineID, o.ServiceID, o.Spec.Container.Image)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StopContainerOperation stops a container on a specific machine.
|
// StopContainerOperation stops a container on a specific machine.
|
||||||
@@ -76,26 +78,26 @@ func (o *StopContainerOperation) Execute(ctx context.Context, cli Client) error
|
|||||||
|
|
||||||
func (o *StopContainerOperation) Format(resolver NameResolver) string {
|
func (o *StopContainerOperation) Format(resolver NameResolver) string {
|
||||||
machineName := resolver.MachineName(o.MachineID)
|
machineName := resolver.MachineName(o.MachineID)
|
||||||
return fmt.Sprintf("%s: Stop container [name=%s]", machineName, resolver.ContainerName(o.ContainerID))
|
return fmt.Sprintf("%s: Stop container [id=%s name=%s]", machineName,
|
||||||
|
o.ContainerID, resolver.ContainerName(o.ContainerID))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *StopContainerOperation) String() string {
|
func (o *StopContainerOperation) String() string {
|
||||||
return fmt.Sprintf("StopContainerOperation[service_id=%s, container_id=%s, machine_id=%s]",
|
return fmt.Sprintf("StopContainerOperation[machine_id=%s service_id=%s container_id=%s]",
|
||||||
o.ServiceID, o.ContainerID, o.MachineID)
|
o.MachineID, o.ServiceID, o.ContainerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveContainerOperation stops and removes a container from a specific machine.
|
// RemoveContainerOperation stops and removes a container from a specific machine.
|
||||||
type RemoveContainerOperation struct {
|
type RemoveContainerOperation struct {
|
||||||
ServiceID string
|
MachineID string
|
||||||
ContainerID string
|
Container api.ServiceContainer
|
||||||
MachineID string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *RemoveContainerOperation) Execute(ctx context.Context, cli Client) error {
|
func (o *RemoveContainerOperation) Execute(ctx context.Context, cli Client) error {
|
||||||
if err := cli.StopContainer(ctx, o.ServiceID, o.ContainerID, container.StopOptions{}); err != nil {
|
if err := cli.StopContainer(ctx, o.Container.ServiceID(), o.Container.ID, container.StopOptions{}); err != nil {
|
||||||
return fmt.Errorf("stop container: %w", err)
|
return fmt.Errorf("stop container: %w", err)
|
||||||
}
|
}
|
||||||
if err := cli.RemoveContainer(ctx, o.ServiceID, o.ContainerID, container.RemoveOptions{
|
if err := cli.RemoveContainer(ctx, o.Container.ServiceID(), o.Container.ID, container.RemoveOptions{
|
||||||
// Remove anonymous volumes created by the container.
|
// Remove anonymous volumes created by the container.
|
||||||
RemoveVolumes: true,
|
RemoveVolumes: true,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
@@ -107,12 +109,13 @@ func (o *RemoveContainerOperation) Execute(ctx context.Context, cli Client) erro
|
|||||||
|
|
||||||
func (o *RemoveContainerOperation) Format(resolver NameResolver) string {
|
func (o *RemoveContainerOperation) Format(resolver NameResolver) string {
|
||||||
machineName := resolver.MachineName(o.MachineID)
|
machineName := resolver.MachineName(o.MachineID)
|
||||||
return fmt.Sprintf("%s: Remove container [name=%s]", machineName, resolver.ContainerName(o.ContainerID))
|
return fmt.Sprintf("%s: Remove container [ID=%s image=%s]",
|
||||||
|
machineName, o.Container.ShortID(), o.Container.Config.Image)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *RemoveContainerOperation) String() string {
|
func (o *RemoveContainerOperation) String() string {
|
||||||
return fmt.Sprintf("RemoveContainerOperation[service_id=%s, container_id=%s, machine_id=%s]",
|
return fmt.Sprintf("RemoveContainerOperation[machine_id=%s service_id=%s container_id=%s]",
|
||||||
o.ServiceID, o.ContainerID, o.MachineID)
|
o.MachineID, o.Container.ServiceID(), o.Container.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateVolumeOperation creates a volume on a specific machine.
|
// CreateVolumeOperation creates a volume on a specific machine.
|
||||||
@@ -151,8 +154,8 @@ func (o *CreateVolumeOperation) Format(_ NameResolver) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *CreateVolumeOperation) String() string {
|
func (o *CreateVolumeOperation) String() string {
|
||||||
return fmt.Sprintf("CreateVolumeOperation[volume=%s, machine_id=%s]",
|
return fmt.Sprintf("CreateVolumeOperation[machine_id=%s volume=%s]",
|
||||||
o.VolumeSpec.DockerVolumeName(), o.MachineID)
|
o.MachineID, o.VolumeSpec.DockerVolumeName())
|
||||||
}
|
}
|
||||||
|
|
||||||
// SequenceOperation is a composite operation that executes a sequence of operations in order.
|
// SequenceOperation is a composite operation that executes a sequence of operations in order.
|
||||||
|
|||||||
@@ -182,9 +182,8 @@ func (s *RollingStrategy) planReplicated(svc *api.Service, spec api.ServiceSpec)
|
|||||||
|
|
||||||
// Remove the old container.
|
// Remove the old container.
|
||||||
plan.Operations = append(plan.Operations, &RemoveContainerOperation{
|
plan.Operations = append(plan.Operations, &RemoveContainerOperation{
|
||||||
ServiceID: plan.ServiceID,
|
MachineID: m.Id,
|
||||||
ContainerID: ctr.ID,
|
Container: ctr,
|
||||||
MachineID: m.Id,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,9 +191,8 @@ func (s *RollingStrategy) planReplicated(svc *api.Service, spec api.ServiceSpec)
|
|||||||
for mid, containers := range containersOnMachine {
|
for mid, containers := range containersOnMachine {
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
plan.Operations = append(plan.Operations, &RemoveContainerOperation{
|
plan.Operations = append(plan.Operations, &RemoveContainerOperation{
|
||||||
ServiceID: plan.ServiceID,
|
MachineID: mid,
|
||||||
ContainerID: c.ID,
|
Container: c,
|
||||||
MachineID: mid,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -244,9 +242,8 @@ func (s *RollingStrategy) planGlobal(svc *api.Service, spec api.ServiceSpec) (Pl
|
|||||||
for _, containers := range containersOnMachine {
|
for _, containers := range containersOnMachine {
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
plan.Operations = append(plan.Operations, &RemoveContainerOperation{
|
plan.Operations = append(plan.Operations, &RemoveContainerOperation{
|
||||||
ServiceID: plan.ServiceID,
|
MachineID: c.MachineID,
|
||||||
ContainerID: c.Container.ID,
|
Container: c.Container,
|
||||||
MachineID: c.MachineID,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -295,9 +292,8 @@ func reconcileGlobalContainer(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ops = append(ops, &RemoveContainerOperation{
|
ops = append(ops, &RemoveContainerOperation{
|
||||||
ServiceID: serviceID,
|
MachineID: old.MachineID,
|
||||||
ContainerID: old.Container.ID,
|
Container: old.Container,
|
||||||
MachineID: old.MachineID,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@@ -338,9 +334,8 @@ func reconcileGlobalContainer(
|
|||||||
// Remove the old containers.
|
// Remove the old containers.
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
ops = append(ops, &RemoveContainerOperation{
|
ops = append(ops, &RemoveContainerOperation{
|
||||||
ServiceID: serviceID,
|
MachineID: c.MachineID,
|
||||||
ContainerID: c.Container.ID,
|
Container: c.Container,
|
||||||
MachineID: c.MachineID,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user