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

Use stable working directory paths #407

Merged
merged 6 commits into from
Feb 8, 2023
Merged

Conversation

squaremo
Copy link
Contributor

@squaremo squaremo commented Feb 1, 2023

Previously, the operator asked the OS to create a fresh tmp directory for each stack run. This is a reasonable enough design, since it means builds are always from a fresh clone, can't interfere with one another, and can be safely deleted afterwards. But it also meant that the go build cache would treat every source file as distinct source code, and grow its build cache accordingly -- to the tune of tens or hundreds of megabytes a build.

This commit changes the working directory to a stable path (still under /tmp), named for the stack object. Since each stack object is uniquely named and is processed serially, there should be no collisions. (There's also no security benefit to having random directory names, since a program or user will ill intent can always just poke around in the filesystem to find what they want).

This also centralises the creation of the directory, which removes a bit of repetition.

Fixes #381.


Further notes:

This will have a doubly beneficial effect on Go projects, since the go build cache will work as intended -- so, no unbounded disk use, but also much faster builds. I checked this by making a project which vendors its libraries and using it in a stack:

  • pulumi new kubernetes-go, then go mod vendor; git add .; git commit -m "Initial revision"
  • create a stack referring to that git repo
  • get a shell into the operator pod, and watch du -sh /tmp/.cache/go-build

When using stable paths, that cache grows to 320MB the first time it processes the stack, then stays there. Without this change, the cache directory grew by (just about) 320MB each time the stack was processed.

@squaremo squaremo requested a review from rquitales February 1, 2023 13:24
Copy link
Member

@rquitales rquitales left a comment

Choose a reason for hiding this comment

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

LGTM, just a minor comment.

pkg/controller/stack/stack_controller.go Show resolved Hide resolved
@squaremo
Copy link
Contributor Author

squaremo commented Feb 2, 2023

Thanks for having a look @rquitales 🙇

@squaremo squaremo force-pushed the stable-build-directory branch from 6845c61 to 581425f Compare February 2, 2023 16:21
@squaremo
Copy link
Contributor Author

squaremo commented Feb 3, 2023

I built a dev image for this: docker.io/squaremo/pulumi-kubernetes-operator:581425f
(digest sha256:f28997f94f5d3f5e9e4fc7ad0e5e4f1b41fcdeff1b3c5a17b49c28da13a6483b)

@squaremo squaremo force-pushed the stable-build-directory branch from 581425f to 4f498a4 Compare February 3, 2023 13:49
@squaremo squaremo requested a review from rquitales February 3, 2023 14:29
@squaremo squaremo force-pushed the stable-build-directory branch 2 times, most recently from f3ee53f to d167ee1 Compare February 3, 2023 15:39
pkg/controller/stack/stack_controller.go Show resolved Hide resolved
test/suite_test.go Outdated Show resolved Hide resolved
test/stable_build_test.go Outdated Show resolved Hide resolved
test/stable_build_test.go Show resolved Hide resolved
squaremo and others added 6 commits February 8, 2023 11:05
Previously, the operator asked the OS to create a fresh tmp directory
for each stack run. This is a reasonable enough design, since it means
builds are always from a fresh clone, can't interfere with one another,
and can be safely deleted afterwards. But it also meant that the go
build cache would treat every source file as distinct source code, and
grow its build cache accordingly -- to the tune of tens or hundreds of
megabytes a build.

This commit changes the working directory to a stable path (still under
/tmp), named for the stack object. Since each stack object is uniquely
named and is processed serially, there should be no collisions. (There's
also no security benefit to having random directory names, since a
program or user will ill intent can always just poke around in the
filesystem to find what they want).

This also centralises the creation of the directory, which removes a bit
of repetition.

Signed-off-by: Michael Bridgen <[email protected]>
A simple test to check that the controller doesn't cause the go build
cache to grow, unduly. Essentially:

 1. Run the stack
 2. Don't change anything
 3. Run the stack again
 4. Check that the Go build cache didn't change between 1. and 3.

If a stack is built in a fresh tmp directory each time (as it was
previously), this test will fail, because at least the stack's main.go
will look like a new file to the cache.

Signed-off-by: Michael Bridgen <[email protected]>
Update the setup-go actions to V3, and use its cache feature.

This will help builds in general; I'm also hoping it will help with the
test involving test/testdata/go-build, which can time out because it
spends a minute or so downloading the (minimal) dependencies for the Go
program therein.

Signed-off-by: Michael Bridgen <[email protected]>
Most of the time we want to fail quite fast if a stack fails to run, but
some stacks -- the one involving Go -- just take a while to run. So: let
a timeout be supplied.

Signed-off-by: Michael Bridgen <[email protected]>
Signed-off-by: Michael Bridgen <[email protected]>
When there's tests running concurrently, it's possible that something
will change the environment -- notably, $GOCACHE -- so, this commit adds
a check that the cache dir is the same before and after the test has
run. This is not bullet-proof, but is simple and has a decent chance of
catching errors of logic in the tests.

Signed-off-by: Michael Bridgen <[email protected]>
Co-authored-by: Ramon Quitales <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

go build use of disk space leads to pod eviction
2 participants