-
Notifications
You must be signed in to change notification settings - Fork 616
Environment variables in cog.yaml #2274
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
Conversation
e15456f
to
c3df37a
Compare
8fc45f7
to
e59d056
Compare
While impossible to be exhaustive, should w 8000 e at least add some heuristics to disallow any env we might over ride in prod, to prevent surprises?
Sources:
|
@nevillelyh awesome thanks. I'm all for a denylist. I'll update |
@meatballhat would love to hear your thoughts on variable name restrictions! |
Current thinking based on @nevillelyh's excellent list... I'm going to add a For
I'm still waffling on |
Signed-off-by: Michael Dwan <m@dwan.io>
If the user wants to pass We actually manipulate
These are needed for Python packages that compile native code during install and/or require dependencies in the base layer, e.g. Torch, Cython, etc. And since we don't allow |
Signed-off-by: Michael Dwan <m@dwan.io>
Alright I updated the logic based on @nevillelyh's suggestions. There's now a denylist, anything on the denylist is rejected. Thanks for the feedback! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for declaring environment variables in the cog.yaml file so that they are correctly injected into the model image. Key changes include:
- New tests and fixtures for predicting with environment variables.
- Implementation of environment variable parsing and validation in the config and dockerfile packages.
- Integration of environment variables into the Dockerfile generation process.
Reviewed Changes
Copilot reviewed 11 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
test-integration/test_integration/test_predict.py | Added integration tests for environment variable support. |
test-integration/test_integration/fixtures/env-project/predict.py | Updated predictor to output environment variable values. |
test-integration/test_integration/fixtures/env-project/cog.yaml | Added environment variable declarations in cog.yaml. |
test-integration/test_integration/conftest.py | Added and updated fixtures to support the cog binary and docker image creation. |
pkg/dockerfile/standard_generator.go | Updated Dockerfile generation to include env vars. |
pkg/dockerfile/fast_generator.go | Appended environment variable lines in the fast generator. |
pkg/dockerfile/env.go | Introduced envLineFromConfig to generate the ENV instruction. |
pkg/config/env_variables_test.go | Added tests for valid and invalid environment variable configurations. |
pkg/config/env_variables.go | Added parsing and validation logic for environment variables and denylist. |
pkg/config/config.go | Integrated environment variable parsing in the overall configuration validation. |
Files not reviewed (3)
- go.mod: Language not supported
- pkg/config/data/config_schema_v1.0.json: Language not supported
- tox.ini: Language not supported
out := "ENV" | ||
for _, name := range slices.Sorted(maps.Keys(vars)) { | ||
out = out + " " + name + "=" + vars[name] | ||
} | ||
out += "\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider using a strings.Builder to construct the ENV line more efficiently for better readability and potential performance improvements.
out := "ENV" | |
for _, name := range slices.Sorted(maps.Keys(vars)) { | |
out = out + " " + name + "=" + vars[name] | |
} | |
out += "\n" | |
var out strings.Builder | |
out.WriteString("ENV") | |
for _, name := range slices.Sorted(maps.Keys(vars)) { | |
out.WriteString(" ") | |
out.WriteString(name) | |
out.WriteString("=") | |
out.WriteString(vars[name]) | |
} | |
out.WriteString("\n") |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me! Great to see this in here. A couple of questions:
- Why is the
environment
field typed as a list of strings rather than a map of key/value pairs? - Have we thought about being able to provide environment variables dynamically the calling environment much like docker compose e.g.
- "XYZ_API_TOKEN=${XYZ_API_TOKEN:-default-value}"
? This would allow a user to change configuration without editing the file. Doesn't need to be in this PR but curious on your thoughts.
"array", | ||
"null" | ||
], | ||
"description": "A list of environment variables to make available at build time, in the format `NAME=value`", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct? I think this is at runtime no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, fixed!
) | ||
assert build_process.returncode == 0 | ||
result = subprocess.run( | ||
[cog_binary, "predict", "--debug", docker_image, "-i", "name=TEST_VAR"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we update the other call sites to use cog_binary
for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! I'll submit another PR to keep this tidy.
Thanks @aron, and good questions!
I made this a string array instead of Along with |
This adds support for declaring environment variables in the
cog.yaml
file. Closes #291For example, this
cog.yaml
file:would result in the
COLOR
env variable being saved in the model image.Note: this is only the cog portion, we have a separate PR that enables env vars on replicate.