mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-18 09:17:49 +08:00
Add an option to buildx rm to keep the buildkitd daemon running
Add --keep-daemon to the `rm` command option to preserve the buildkitd daemon after the buildx context is deleted. Signed-off-by: Mayeul Blanzat <mayeul.blanzat@datadoghq.com>
This commit is contained in:
parent
bcfd434829
commit
72dab552b5
@ -12,8 +12,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type rmOptions struct {
|
type rmOptions struct {
|
||||||
builder string
|
builder string
|
||||||
keepState bool
|
keepState bool
|
||||||
|
keepDaemon bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRm(dockerCli command.Cli, in rmOptions) error {
|
func runRm(dockerCli command.Cli, in rmOptions) error {
|
||||||
@ -30,7 +31,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err1 := rm(ctx, dockerCli, ng, in.keepState)
|
err1 := rm(ctx, dockerCli, ng, in.keepState, in.keepDaemon)
|
||||||
if err := txn.Remove(ng.Name); err != nil {
|
if err := txn.Remove(ng.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -42,7 +43,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if ng != nil {
|
if ng != nil {
|
||||||
err1 := rm(ctx, dockerCli, ng, in.keepState)
|
err1 := rm(ctx, dockerCli, ng, in.keepState, in.keepDaemon)
|
||||||
if err := txn.Remove(ng.Name); err != nil {
|
if err := txn.Remove(ng.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -70,23 +71,28 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
|||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.BoolVar(&options.keepState, "keep-state", false, "Keep BuildKit state")
|
flags.BoolVar(&options.keepState, "keep-state", false, "Keep BuildKit state")
|
||||||
|
flags.BoolVar(&options.keepDaemon, "keep-daemon", false, "Keep the buildkitd daemon running")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func rm(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, keepState bool) error {
|
func rm(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, keepState, keepDaemon bool) error {
|
||||||
dis, err := driversForNodeGroup(ctx, dockerCli, ng, "")
|
dis, err := driversForNodeGroup(ctx, dockerCli, ng, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, di := range dis {
|
for _, di := range dis {
|
||||||
if di.Driver != nil {
|
if di.Driver == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Do not stop the buildkitd daemon when --keep-daemon is provided
|
||||||
|
if !keepDaemon {
|
||||||
if err := di.Driver.Stop(ctx, true); err != nil {
|
if err := di.Driver.Stop(ctx, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := di.Driver.Rm(ctx, true, !keepState); err != nil {
|
}
|
||||||
return err
|
if err := di.Driver.Rm(ctx, true, !keepState, !keepDaemon); err != nil {
|
||||||
}
|
return err
|
||||||
}
|
}
|
||||||
if di.Err != nil {
|
if di.Err != nil {
|
||||||
err = di.Err
|
err = di.Err
|
||||||
|
@ -12,6 +12,7 @@ Remove a builder instance
|
|||||||
| Name | Description |
|
| Name | Description |
|
||||||
| --- | --- |
|
| --- | --- |
|
||||||
| [`--builder string`](#builder) | Override the configured builder instance |
|
| [`--builder string`](#builder) | Override the configured builder instance |
|
||||||
|
| [`--keep-daemon`](#keep-daemon) | Keep the buildkitd daemon running |
|
||||||
| [`--keep-state`](#keep-state) | Keep BuildKit state |
|
| [`--keep-state`](#keep-state) | Keep BuildKit state |
|
||||||
|
|
||||||
|
|
||||||
@ -32,3 +33,8 @@ Same as [`buildx --builder`](buildx.md#builder).
|
|||||||
|
|
||||||
Keep BuildKit state, so it can be reused by a new builder with the same name.
|
Keep BuildKit state, so it can be reused by a new builder with the same name.
|
||||||
Currently, only supported by the [`docker-container` driver](buildx_create.md#driver).
|
Currently, only supported by the [`docker-container` driver](buildx_create.md#driver).
|
||||||
|
|
||||||
|
### <a name="keep-daemon"></a> Keep the buildkitd daemon running (--keep-daemon)
|
||||||
|
|
||||||
|
Keep the buildkitd daemon running after the buildx context is removed. This is useful when you manage buildkitd daemons and buildx contexts independently.
|
||||||
|
Currently, only supported by the [`docker-container` and `kubernetes` drivers](buildx_create.md#driver).
|
||||||
|
@ -295,7 +295,7 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
|
func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
|
||||||
info, err := d.Info(ctx)
|
info, err := d.Info(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -305,20 +305,22 @@ func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := d.DockerAPI.ContainerRemove(ctx, d.Name, dockertypes.ContainerRemoveOptions{
|
if rmDaemon {
|
||||||
RemoveVolumes: true,
|
if err := d.DockerAPI.ContainerRemove(ctx, d.Name, dockertypes.ContainerRemoveOptions{
|
||||||
Force: force,
|
RemoveVolumes: true,
|
||||||
}); err != nil {
|
Force: force,
|
||||||
return err
|
}); err != nil {
|
||||||
}
|
return err
|
||||||
for _, v := range container.Mounts {
|
}
|
||||||
if v.Name == d.Name+volumeStateSuffix {
|
for _, v := range container.Mounts {
|
||||||
|
if v.Name != d.Name+volumeStateSuffix {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if rmVolume {
|
if rmVolume {
|
||||||
return d.DockerAPI.VolumeRemove(ctx, d.Name+volumeStateSuffix, false)
|
return d.DockerAPI.VolumeRemove(ctx, d.Name+volumeStateSuffix, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
|
func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ type Driver interface {
|
|||||||
Bootstrap(context.Context, progress.Logger) error
|
Bootstrap(context.Context, progress.Logger) error
|
||||||
Info(context.Context) (*Info, error)
|
Info(context.Context) (*Info, error)
|
||||||
Stop(ctx context.Context, force bool) error
|
Stop(ctx context.Context, force bool) error
|
||||||
Rm(ctx context.Context, force bool, rmVolume bool) error
|
Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error
|
||||||
Client(ctx context.Context) (*client.Client, error)
|
Client(ctx context.Context) (*client.Client, error)
|
||||||
Features() map[Feature]bool
|
Features() map[Feature]bool
|
||||||
IsMobyDriver() bool
|
IsMobyDriver() bool
|
||||||
|
@ -165,7 +165,11 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
|
func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
|
||||||
|
if !rmDaemon {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if err := d.deploymentClient.Delete(ctx, d.deployment.Name, metav1.DeleteOptions{}); err != nil {
|
if err := d.deploymentClient.Delete(ctx, d.deployment.Name, metav1.DeleteOptions{}); err != nil {
|
||||||
if !apierrors.IsNotFound(err) {
|
if !apierrors.IsNotFound(err) {
|
||||||
return errors.Wrapf(err, "error while calling deploymentClient.Delete for %q", d.deployment.Name)
|
return errors.Wrapf(err, "error while calling deploymentClient.Delete for %q", d.deployment.Name)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user