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

Cleanup Docker context and decrease build time #1498

Merged
merged 1 commit into from
Feb 17, 2021

Conversation

drGrove
Copy link
Contributor

@drGrove drGrove commented Apr 8, 2020

This is based off the work found in #1307 that was never merged. It
moves around the install and copy of certain conponents to take better
advantage of the Docker cache ad well as drops running tests during the
build of the image.

The reason for dropping tests is to improve build time and as running
tests within the build while they're already being run in CI seems like
an unnecessary added tax.

Signed-off-by: Danny Grove danny@drgrovellc.com

@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


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. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Apr 8, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @drGrove!

It looks like this is your first PR to kubernetes-sigs/external-dns 🎉. 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/external-dns 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 k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 8, 2020
@drGrove
Copy link
Contributor Author

drGrove commented Apr 8, 2020

/check-cla

@drGrove
Copy link
Contributor Author

drGrove commented Apr 9, 2020

/assign @Raffo

@njuettner
Copy link
Member

@drGrove please verify you have signed the CLA with the correct email which you have used for your commit (danny@drgrovellc.com).

@drGrove
Copy link
Contributor Author

drGrove commented Apr 9, 2020

/check-cla

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 9, 2020
@drGrove
Copy link
Contributor Author

drGrove commented Apr 10, 2020

@drGrove please verify you have signed the CLA with the correct email which you have used for your commit (danny@drgrovellc.com).

Done

@drGrove
Copy link
Contributor Author

drGrove commented Apr 26, 2020

/assign njuettner

@james-callahan
Copy link
Contributor

@njuettner ping?

Dockerfile Show resolved Hide resolved
Dockerfile.mini Show resolved Hide resolved
@Raffo Raffo removed their assignment Jul 1, 2020
@seanmalloy
Copy link
Member

/kind cleanup

@k8s-ci-robot k8s-ci-robot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Aug 19, 2020
@drGrove drGrove requested a review from njuettner August 19, 2020 06:31
@seanmalloy
Copy link
Member

@drGrove please rebase your PR to pull the the latest changes from the master branch. This will trigger the new GitHub actions CI test to run. I'll review this PR after this is done. Thanks!

/assign

@seanmalloy
Copy link
Member

@drGrove the below make targets are failing with the Dockerfile changes. I verified this all works on the master branch without an issue.

$ make build.mini
docker build --rm --tag "us.gcr.io/k8s-artifacts-prod/external-dns/external-dns:v0.7.1-312-g65a8f4ed-mini" --build-arg VERSION="v0.7.1-312-g65a8f4ed" -f Dockerfile.mini .

Sending build context to Docker daemon  70.59MB
Step 1/13 : FROM golang:1.14 as builder
 ---> baaca3151cdb
Step 2/13 : WORKDIR /sigs.k8s.io/external-dns
 ---> Using cache
 ---> dd0007934dde
Step 3/13 : RUN apt-get update     && apt-get install         ca-certificates     && update-ca-certificates
 ---> Running in b4d51c631adc
Get:1 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]
Get:2 http://deb.debian.org/debian buster InRelease [122 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [221 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7906 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [7868 B]
Fetched 8375 kB in 4s (2068 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
ca-certificates is already the newest version (20200601~deb10u1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
Removing intermediate container b4d51c631adc
 ---> 916ce81aadab
Step 4/13 : COPY go.mod .
 ---> 65a5a584a693
Step 5/13 : COPY go.sum .
 ---> 7bc4ed158c7e
Step 6/13 : RUN go mod vendor &&     go mod download
 ---> Running in a59599f69ab6
go mod vendor: open /sigs.k8s.io/external-dns/vendor/modules.txt: no such file or directory
The command '/bin/sh -c go mod vendor &&     go mod download' returned a non-zero code: 1
make: *** [build.mini] Error 1

$ make build.docker
docker build --rm --tag "us.gcr.io/k8s-artifacts-prod/external-dns/external-dns:v0.7.1-312-g65a8f4ed" --build-arg VERSION="v0.7.1-312-g65a8f4ed" .

Sending build context to Docker daemon  70.59MB
Step 1/13 : FROM golang:1.14 as builder
1.14: Pulling from library/golang
d6ff36c9ec48: Already exists
c958d65b3090: Already exists
edaf0a6b092f: Already exists
80931cf68816: Already exists
813643441356: Already exists
a54c81388677: Pull complete
31748c757dd4: Pull complete
Digest: sha256:1364cfbbcd1a5f38bdf8c814f02ebbd2170c93933415480480104834341f283e
Status: Downloaded newer image for golang:1.14
 ---> baaca3151cdb
Step 2/13 : WORKDIR /sigs.k8s.io/external-dns
 ---> Running in 264c9aa294c0
Removing intermediate container 264c9aa294c0
 ---> dd0007934dde
Step 3/13 : COPY go.mod .
 ---> ecc08a5bb407
Step 4/13 : COPY go.sum .
 ---> 8ec4a34b83c8
Step 5/13 : RUN go mod vendor &&     go mod download
 ---> Running in 25afb0991e1d
go mod vendor: open /sigs.k8s.io/external-dns/vendor/modules.txt: no such file or directory
The command '/bin/sh -c go mod vendor &&     go mod download' returned a non-zero code: 1
make: *** [build.docker] Error 1

@vinny-sabatini
Copy link
Contributor

/unassign @seanmalloy
/assign

@vinny-sabatini
Copy link
Contributor

The code changes look good to me so far, however I also tested build.docker and build.mini make targets and am running into the same error mentioned by @seanmalloy above.

@drGrove could you please update the pull request to fix these two make targets and then I believe this PR is ready for final review by the maintainers.

@drGrove
Copy link
Contributor Author

drGrove commented Sep 17, 2020

Will do! I was running into some weird issues and have been slammed at work so I'll take a look this weekend.

@vinny-sabatini
Copy link
Contributor

Hey @drGrove did you have a chance to look into why the build.docker and build.mini make targets are failing?

Copy link

@joshdk joshdk left a comment

Choose a reason for hiding this comment

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

@drGrove @vinny-sabatini To fix the Docker builds, make this change to both Dockerfiles:

-RUN go mod vendor && \
-    go mod download
+RUN go mod download

There are two reasons that the current setup doesn't work;

  1. There appears to be an issue with running go mod vendor (without any Go code to reference the dependencies) in go1.4.9. This appears to be fixed in go1.15.2 (thanks to https://go-review.googlesource.com/c/go/+/217135, I think?).
  2. The bigger issue is that go mod vendor only copies pre-download dependencies into the vendor dir, and subsequent (e.g. go build) commands will use vendoring if that dir exists. So what we're doing here is:
    • Running go mod vendor to copy no dependencies into vendor.
    • Running go mod download to download dependencies into the /go/mod system cache.
    • Running go build which then tries to use the empty vendor directory.

Testing with this change locally I get:

  • A ~5 minute fresh Docker build (make build.mini).
  • An instant rebuild when nothing changes.
  • A ~3 minute rebuild when I change Go code (like adding a line to main.go)

Normal builds still take 3 minutes, but at least you don't have to re-download the dependencies. Cheers!

@drGrove
Copy link
Contributor Author

drGrove commented Oct 4, 2020

@vinny-sabatini ready for review

Copy link
Contributor

@Raffo Raffo left a comment

Choose a reason for hiding this comment

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

Added a few comments.

@@ -17,10 +17,12 @@ FROM golang:1.14 as builder

WORKDIR /sigs.k8s.io/external-dns

COPY go.mod .
COPY go.sum .
RUN go mod download
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we don't explicitly need to run go mod download as make build will run go build which will download the needed dependencies with the version of Go that we use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an optimization in the docker caching mechanism.
If my go mod and go sum don't change why re-pull the deps when testing locally


COPY go.mod .
COPY go.sum .
RUN go mod download
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as 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.

See above

Comment on lines +19 to +22
RUN apt-get update \
&& apt-get install \
ca-certificates \
&& update-ca-certificates
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the need of moving those lines?

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 docker, you want things that change less often (or can be slow) to happen first. If the packages don't change between builds, why have the cache dump by COPY . . affect my package install.

@drGrove
Copy link
Contributor Author

drGrove commented Oct 26, 2020

@Raffo ping

@drGrove drGrove requested a review from Raffo November 11, 2020 08:50
@Raffo
Copy link
Contributor

Raffo commented Feb 3, 2021

@drGrove Sorry for the delay on this one. Changes are good, but we need to keep the make test in all the dockerfiles because of CI requirements (actions being not connected to google's CI and we need to make sure tests are green for building images). If you make this change, we can merge this improvement.

This is based off the work found in kubernetes-sigs#1307 that was never merged. It
moves around the install and copy of certain conponents to take better
advantage of the Docker cache ad well as drops running tests during the
build of the image.

The reason for dropping tests is to improve build time and as running
tests within the build while they're already being run in CI seems like
an unnecessary added tax.

Signed-off-by: Danny Grove <danny@drgrovellc.com>
@drGrove
Copy link
Contributor Author

drGrove commented Feb 8, 2021

@Raffo done, sorry about the delay

@Raffo
Copy link
Contributor

Raffo commented Feb 17, 2021

/approve
/lgtm

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: drGrove, Raffo

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 added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 17, 2021
@k8s-ci-robot k8s-ci-robot merged commit 5efb9ba into kubernetes-sigs:master Feb 17, 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. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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.

None yet

8 participants