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

fix(runtime): remove container after run #5796

Merged
merged 1 commit into from
Jun 22, 2024

Conversation

johnathan-sq
Copy link
Contributor

@johnathan-sq johnathan-sq commented Jun 21, 2024

Description of your changes

This pull request includes a change to the Start method in the RuntimeDocker struct within the cmd/crank/beta/render/runtime_docker.go file. The change enhances the stop functionality of a Docker container. Now, in addition to stopping the container, the container is also removed along with its volumes. This is a crucial change as it ensures proper cleanup of resources when a Docker container is stopped.

Reason for this change when running crossplane beta render commands the docker containers do not remove

I have:

  • Read and followed Crossplane's [contribution process].
  • Run earthly +reviewable to ensure this PR is ready for review.
  • Added or updated unit tests.
  • Added or updated e2e tests.
  • Linked a PR or a [docs tracking issue] to [document this change].
  • Added backport release-x.y labels to auto-backport this PR.

Unsure on how to unit test or e2e test this, suggestions would be appreciated.

From my manual testing, I compiled the binary and ran it locally to confirm the containers do get removed.

@johnathan-sq johnathan-sq requested review from phisco and a team as code owners June 21, 2024 01:16
@johnathan-sq johnathan-sq requested a review from negz June 21, 2024 01:16
Comment on lines 232 to 233
err = c.ContainerRemove(ctx, rsp.ID, container.RemoveOptions{Force: true, RemoveVolumes: true})
return errors.Wrap(err, "cannot remove Docker container")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be optional? I could imagine wanting to inspect the container after the fact for debugging.

Perhaps remove it by default, with an opt-out flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I can update this. Good point raised.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in a Remove option, which is default. People can override this by

apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
  name: function-go-templating
  annotations.render.crossplane.io/runtime-docker-cleanup: "Stop"
spec:
  package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.4.1

cmd/crank/beta/render/runtime_docker.go Outdated Show resolved Hide resolved
@@ -93,6 +96,9 @@ type RuntimeDocker struct {
// Stop container once rendering is done
Stop bool

// Stop container once rendering is done
Remove bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have two bools, I think it makes sense to replace both with an enum. For example:

type RuntimeDocker struct {
	Image string
	Cleanup DockerCleanup
	PullPolicy DockerPullPolicy
	// etc...
}

I prefer the enum, since remove always implies stop. It wouldn't make sense to set Stop = false but Remove = true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, will fix this up

Comment on lines 232 to 241
if r.Stop || r.Remove {
stop = func(ctx context.Context) error {
err := c.ContainerStop(ctx, rsp.ID, container.StopOptions{})
if err != nil {
return errors.Wrap(err, "cannot stop Docker container")
}

err = c.ContainerRemove(ctx, rsp.ID, container.RemoveOptions{Force: true, RemoveVolumes: true})
return errors.Wrap(err, "cannot remove Docker container")
if r.Remove {
err = c.ContainerRemove(ctx, rsp.ID, container.RemoveOptions{Force: true, RemoveVolumes: true})
if err != nil {
return errors.Wrap(err, "cannot remove Docker container")
}
}
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we now have multiple cases inside the stop function, I'd suggest moving everything in there. Assuming you add the Cleanup enum per my other comment, I think something like this would work well:

	stop := func(ctx context.Context) error {
		switch r.Cleanup {
		case AnnotationValueRuntimeDockerCleanupOrphan:
			r.log.Debug("Container left running", "container", rsp.ID, "image", r.Image)
			return nil
		case AnnotationValueRuntimeDockerCleanupStop:
			if err := c.ContainerStop(ctx, rsp.ID, container.StopOptions{}); err != nil {
				return errors.Wrap(err, "cannot stop Docker container")
			}
		case AnnotationValueRuntimeDockerCleanupRemove:
			if err := c.ContainerStop(ctx, rsp.ID, container.StopOptions{}); err != nil {
				return errors.Wrap(err, "cannot stop Docker container")
			}
			if err := c.ContainerRemove(ctx, rsp.ID, container.RemoveOptions{}); err != nil {
				return errors.Wrap(err, "cannot remove Docker container")
			}
		}
		return nil
	}

(I know the stop is repeated, but I prefer repeating it to nesting it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip, I was thinking something along the lines and I agree with you on the stop repeated but it's better than nesting it

Copy link
Member

@negz negz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, looks good thank you!

Would you mind squashing your commits please, then I'll merge.

@jbw976 jbw976 added this to the v1.17 milestone Jun 21, 2024
@johnathan-sq
Copy link
Contributor Author

Awesome, looks good thank you!

Would you mind squashing your commits please, then I'll merge.

Squashed the commits, appreciate the response on getting the PR over the line