Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an flag to buildx rm to keep the buildkitd daemon running #852

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

type rmOptions struct {
builder string
keepState bool
builder string
keepState bool
keepDaemon bool
}

func runRm(dockerCli command.Cli, in rmOptions) error {
Expand All @@ -30,7 +31,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
if err != nil {
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 {
return err
}
Expand All @@ -42,7 +43,7 @@ func runRm(dockerCli command.Cli, in rmOptions) error {
return err
}
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 {
return err
}
Expand Down Expand Up @@ -70,23 +71,28 @@ func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {

flags := cmd.Flags()
flags.BoolVar(&options.keepState, "keep-state", false, "Keep BuildKit state")
flags.BoolVar(&options.keepDaemon, "keep-daemon", false, "Keep the buildkitd daemon running")

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, "")
if err != nil {
return err
}
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 {
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 {
err = di.Err
Expand Down
6 changes: 6 additions & 0 deletions docs/reference/buildx_rm.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Remove a builder instance
| Name | Description |
| --- | --- |
| [`--builder string`](#builder) | Override the configured builder instance |
| [`--keep-daemon`](#keep-daemon) | Keep the buildkitd daemon running |
| [`--keep-state`](#keep-state) | Keep BuildKit state |


Expand All @@ -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.
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).
22 changes: 12 additions & 10 deletions driver/docker-container/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
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)
if err != nil {
return err
Expand All @@ -305,20 +305,22 @@ func (d *Driver) Rm(ctx context.Context, force bool, rmVolume bool) error {
if err != nil {
return err
}
if err := d.DockerAPI.ContainerRemove(ctx, d.Name, dockertypes.ContainerRemoveOptions{
RemoveVolumes: true,
Force: force,
}); err != nil {
return err
}
for _, v := range container.Mounts {
if v.Name == d.Name+volumeStateSuffix {
if rmDaemon {
if err := d.DockerAPI.ContainerRemove(ctx, d.Name, dockertypes.ContainerRemoveOptions{
RemoveVolumes: true,
Force: force,
}); err != nil {
return err
}
for _, v := range container.Mounts {
if v.Name != d.Name+volumeStateSuffix {
continue
}
if rmVolume {
return d.DockerAPI.VolumeRemove(ctx, d.Name+volumeStateSuffix, false)
}
}
}

}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion driver/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
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
}

Expand Down
2 changes: 1 addition & 1 deletion driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Driver interface {
Bootstrap(context.Context, progress.Logger) error
Info(context.Context) (*Info, 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)
Features() map[Feature]bool
IsMobyDriver() bool
Expand Down
6 changes: 5 additions & 1 deletion driver/kubernetes/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ func (d *Driver) Stop(ctx context.Context, force bool) error {
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 !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "error while calling deploymentClient.Delete for %q", d.deployment.Name)
Expand Down