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

Issue #5871 -- Doc change and unit test to verify docker buildArgs support templates #5884

Conversation

torenware
Copy link
Contributor

WIP #5871
Related: Relevant tracking issues, for context
Merge before/after: Dependent or prerequisite PRs

Description

First pass on the issue. I'd expect that acceptance tests will fail when I create the PR.

I've switched EvalBuildArgs to EvalBuildArgsWithEnv, merging in enough from the Kaniko implementation so that docker.go actually compiles.

Some issues:

  • Working with the built skaffold, I have verified that this does NOT resolve the issue. Behavior in the app is the same as before the change as near as I can tell.
  • Odd behavior in make test: a number of docker related tests failed, with errors related to being unable to find Dockerfile. As an experiment, I added a simple Dockerfile to the same directory, and to my surprise, after adding the missed ARG statements, all of the tests but one now pass. FWIW :-) I'd guess there's a mocking mechanism for Dockerfiles which is not being invoked for some reason. Couldn't figure that out.
  • If someone can tell me how to run only the tests for /pkg/skaffold/build/gcb, much obliged. Tried a couple of the obvious things with prefixing SKAFFOLD_TEST_PACKAGES to the invocation of make, still got a complete set of tests.

User facing changes (remove if N/A)

Follow-up Work (remove if N/A)

@torenware torenware requested a review from a team as a code owner May 21, 2021 05:46
@torenware torenware requested a review from gsquared94 May 21, 2021 05:46
@google-cla google-cla bot added the cla: yes label May 21, 2021
Copy link
Member

@briandealwis briandealwis left a comment

Choose a reason for hiding this comment

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

I don't think Docker artifacts should support an env field: Kaniko does support setting environment variables for the build's pod, but Docker does not propagate environment variables into the container: that's the purpose of the build-args.

Plus v1.EnvVar is a Kubernetes object and shouldn't be included in the docker/GCB build — I opened #5877 to refactor this.

That will simplify this implementation.

pkg/skaffold/schema/latest/v1/config.go Outdated Show resolved Hide resolved
Comment on lines 1211 to 1214
// Env are environment variables passed to the docker build.
// It also accepts environment variables via the go template syntax.
// For example: `[{"name": "key1", "value": "value1"}, {"name": "key2", "value": "value2"}, {"name": "key3", "value": "'{{.ENV_VARIABLE}}'"}]`.
Env []v1.EnvVar `yaml:"env,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Docker doesn't propagate environment fields, so this doesn't really make sense to be included 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.

The irony is that there's no need to modify the config for Docker artifacts. At least running Skaffold locally, the file pkg/skaffold/build/gcb/docker.go is not called at all. Instead, most of the work is done in pkg/skaffold/docker/image.go. func (l *localDaemon) Build() calls EvalBuildArgs(), which turns out to be just fine; the missing env parameter isn't really well named, since it's used for non-environmental variables that need to be injected into templates.

I learned this by setting up delve with VS Code, and once I put in a .env file into the top skaffold/ directory of the repo, to my shock the templates took up the environment automatically. master / HEAD is not broken in this respect. But outside of the debugger, broken.

I believe the problem is with how skaffold populates the environment. I can't get .env files to work with skaffold, pretty much no matter where I put them. Do you know how .env files are implemented? I can't find any loader code for the life of me; neither of the two common golang modules used for loading env files is called anywhere, and I could not find any code that was parsing the files and inserting them into the process environment. I'm guessing that VS Code exports the file in code, and then calls fork(), thus passing on the environment pristine. Whatever skaffold is currently doing does not work as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, my theory seems sound. I wrote a script to parse my .env file and exported the variable. Do that, and calling skaffold dev resolves my build arg template.

So how can we fix the way that skaffold initializes its environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a non-trivial test of the templating feature. The style of the test is a little different from most of the table driven tests used for docker, but was needed to actually exercise EvalBuildArgs(). Short version: the feature works fine in 1.24, and likely earlier than that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR meandered too much. I've taken the final version and put it in as PR-5901, which should be much easier to understand. Leaving this one open for now so people know the background of it, since this is my first issue in the Skaffold repo.

@codecov
Copy link

codecov bot commented May 22, 2021

Codecov Report

Merging #5884 (b0e71b3) into master (ff5038a) will decrease coverage by 0.00%.
The diff coverage is 61.01%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5884      +/-   ##
==========================================
- Coverage   70.91%   70.90%   -0.01%     
==========================================
  Files         447      447              
  Lines       16908    16909       +1     
==========================================
  Hits        11990    11990              
- Misses       4028     4029       +1     
  Partials      890      890              
Impacted Files Coverage Δ
cmd/skaffold/app/cmd/diagnose.go 64.51% <0.00%> (ø)
cmd/skaffold/app/cmd/find_configs.go 48.48% <0.00%> (ø)
cmd/skaffold/app/cmd/generate_pipeline.go 61.53% <0.00%> (ø)
cmd/skaffold/app/cmd/runner.go 58.49% <0.00%> (ø)
cmd/skaffold/skaffold.go 0.00% <0.00%> (ø)
pkg/skaffold/build/bazel/build.go 67.24% <0.00%> (ø)
pkg/skaffold/build/gcb/cloud_build.go 0.00% <0.00%> (ø)
pkg/skaffold/build/gcb/docker.go 87.87% <ø> (ø)
pkg/skaffold/build/local/local.go 61.36% <0.00%> (ø)
pkg/skaffold/build/result.go 83.33% <0.00%> (ø)
... and 40 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ff5038a...b0e71b3. Read the comment docs.

@torenware torenware changed the title Issue #5871 WIP: obvious mods, but this does not fix the issue Issue #5871 -- Doc change and unit test to verify docker buildArgs support templates May 24, 2021
@briandealwis
Copy link
Member

Closing — thanks for digging into this @torenware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants