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: ignore non-go files during Docker build #2081

Merged
merged 1 commit into from
Jun 16, 2021
Merged

🐛 fix: ignore non-go files during Docker build #2081

merged 1 commit into from
Jun 16, 2021

Conversation

busser
Copy link
Contributor

@busser busser commented Mar 10, 2021

This change ensures that the scaffolded .dockerignore file makes Docker ignore non-Go files.

This avoids large files like test binaries being unnecessarily included in Docker's build context.

Fixes #2073.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 10, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @busser!

It looks like this is your first PR to kubernetes-sigs/kubebuilder 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kubebuilder has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @busser. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Mar 10, 2021
!go.mod
!go.sum
!main.go
!controllers/**/*.go
Copy link
Member

@camilamacedo86 camilamacedo86 Mar 10, 2021

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call.

I just tested it and it does not work. Further investigation lead me to this issue: moby/moby#30018. Basically, we can't write a whitelist-style .dockerignore file.

Should we opt for a different strategy, like ignoring directories that are known to contain large files that are unnecessary when building a Docker image for the operator? The .dockerignore file would look like this:

# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore build and test binaries.
bin/
testbin/

Copy link
Member

@camilamacedo86 camilamacedo86 Mar 17, 2021

Choose a reason for hiding this comment

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

Hi @busser,

See that the idea to ignore any file which is not a GO type. Then, let's check the docker docs:

Beyond Go’s filepath.Match rules, Docker also supports a special wildcard string ** that matches any number of directories (including zero). For example, **/*.go will exclude all files that end with .go that are found in all directories, including the root of the build context.

https://docs.docker.com/engine/reference/builder/#dockerignore-file

So, according to the docker documentation !**/*.go is correct which means that we are generating the file right. In this way, could you please review your test and see if you are not missing anything?

Other options are:

If the previous suggestion works when is not multigroup then, see that we can:

  • (option A) duplicate that to add the respective paths for multigroup by default as well
  • (option B) we can change the file as we change the ignoredockerfile when we enable the multi-group support
  • (option C) we can check a syntax that will ignore both paths. (I think it is the best solution)
  • (option D) Currently, ignore any file which is not a GO type. (that would be the best, but if we have a problem with ! then, we also need to find or track an issue to docker to pin that here.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @camilamacedo86,

The pattern we currently use, !**/*.go should work according to the Docker docs, but in fact doesn't. My understanding is that we are facing a bug in how Docker handles .dockerignore logic. This issue (moby/moby#30018) reports a three year-old bug in Docker: when a pattern starts with !, wildcard strings like * and ** don't work for directories.

I don't believe this will ever be fixed. The Docker team's stated long-term goal is for .dockerignore to have the same behaviour as .gitignore. I found this section in the .gitignore specification:

It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.

So I guess that we won't ever be able to exclude the entire working directory except for GO files... 😞

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In its current state, the .dockerignore file scaffolded by Kubebuilder ignores nothing and includes the entire directory in the Docker build context. Most of the files are just code and are relatively small. However, the Kubebuilder workflow adds large binaries to the bin/ and testbin/ directories. I would recommend ignoring these directories by default and letting the user add more paths to their .dockerignore based on their specific needs.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Would not the following configuration solve the need for multi-group and not multi-group layout projects?

!go.mod
!go.sum
!main.go
!controllers/**/*.go
!api/**/*.go
!controllers/*.go
!api/*.go

Copy link
Member

@camilamacedo86 camilamacedo86 Mar 25, 2021

Choose a reason for hiding this comment

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

HI @busser,

After checking it better I think the best solution is the one suggested by you:

# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
# Ignore build and test binaries.
bin/
testbin/

Copy link
Contributor

Choose a reason for hiding this comment

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

What about the config directory? We should probably add a marker so that plugins can add their own exceptions, and that way the config plugin would be the one adding that line.

Copy link
Contributor Author

@busser busser May 5, 2021

Choose a reason for hiding this comment

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

@camilamacedo86 I've updated the pull request to ignore the build and test binaries, as decided above 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Adirio How would you go about doing this? I don't mind doing the implementation myself, but would need some guidance 🙂

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 5, 2021
@busser busser requested review from camilamacedo86 and Adirio May 27, 2021 15:44
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

/ok-to-test

It seems ok for me in order to solve the issue.

@Adirio makes the following suggestion:

What about the config directory? We should probably add a marker so that plugins can add their own exceptions, and that way the config plugin would be the one adding that line.

That is fine but seems an RFE. So, @Adirio wdyt about we move forward here and then raise that as an issue for we are able to track and work on that too?

HI @estroz @Adirio,

It is open for a while. WDYT about we move forward here and do any improvement and suggestion as a follow-up?

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. approved Indicates a PR has been approved by an approver from all required OWNERS files. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 28, 2021
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

/lgtm
I think it is fine.
@Adirio please feel free to raise any issue if you see that it requires a follow up for improvements.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 16, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: busser, camilamacedo86

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 2f8940f into kubernetes-sigs:master Jun 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scaffolded .dockerignore does not do what it claims
4 participants