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

Make container environment variables accessible as application variables #149

Merged

Conversation

kate-goldenring
Copy link
Collaborator

@kate-goldenring kate-goldenring commented Jun 25, 2024

Prefixes all container env vars with SPIN_VARIABLE. This means that Spin components can get access to these variables if specifically configured in the Spin manifest to have access. For example, say a a component wants access to the KUBERNETES_SERVICE_ADDRESS container environment variable, it would configure this in the Spin.toml as follows:

[variables]
kubernetes_service_address = { required = true }

[component.example]
source = "target/wasm32-wasi/release/example.wasm"
allowed_outbound_hosts = []
[component.example.variables]
kubernetes_service_address = "{{ kubernetes_service_address }}"

Then, it can be accessed in the Spin app using the variables SDK:

let addr = variables::get("kubernetes_service_address")?;

Should affect/reflect the "Configuring Runtime Options" SKIP spinframework/skips#4

@kate-goldenring kate-goldenring changed the title Parse container environment variables for Spin execution configuration Make container environment variables accessible as application variables Jun 26, 2024
@kate-goldenring kate-goldenring force-pushed the runtime-and-app-env-config branch from 2a32c71 to ce35a35 Compare June 26, 2024 18:02
@jsturtevant jsturtevant self-assigned this Jun 27, 2024
Copy link
Collaborator

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

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

Added some comments, this overall LGTM!

containerd-shim-spin/src/engine.rs Outdated Show resolved Hide resolved
containerd-shim-spin/src/engine.rs Outdated Show resolved Hide resolved
containerd-shim-spin/src/engine.rs Outdated Show resolved Hide resolved
@kate-goldenring kate-goldenring force-pushed the runtime-and-app-env-config branch from 02ff7fb to 5d8868a Compare July 10, 2024 20:56
@kate-goldenring
Copy link
Collaborator Author

I updated the implementation to check the spin.toml for the variables first and only prefix the associated env vars with SPIN_VARIABLE. For casing, it is expected that the container env vars are the uppercased version of the application variable. Spin variables must always be in lowercase snake ID format but for the CLI must be provided by an uppercased env var. For example: SPIN_VARIABLE_FOO_BAR spin up works but SPIN_VARIABLE_foo_bar spin up` does not work.

@kate-goldenring kate-goldenring marked this pull request as ready for review July 10, 2024 20:58
Copy link
Contributor

@vdice vdice left a comment

Choose a reason for hiding this comment

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

Quick comment; otherwise looks good to me but defer to previous/requested reviewers.

@@ -43,6 +43,9 @@ const OCI_LAYER_MEDIA_TYPE_WASM: &str = "application/vnd.wasm.content.layer.v1+w
/// Expected location of the Spin manifest when loading from a file rather than
/// an OCI image
const SPIN_MANIFEST_FILE_PATH: &str = "/spin.toml";
/// Known prefix for the Spin application variables environment variable
/// provider: https://github.com/fermyon/spin/blob/436ad589237c02f7aa4693e984132808fd80b863/crates/variables/src/provider/env.rs#L9
const SPIN_APPLICATION_VARIABLE_PREFIX: &str = "SPIN_VARIABLE";
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not sure it is worth bringing in the variables crate for this constant (if we did make it pub). Maybe with the spin factors work, this will be more directly configured by the shim

Copy link
Collaborator

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

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

LGTM, added a few suggestions

containerd-shim-spin/src/engine.rs Outdated Show resolved Hide resolved
containerd-shim-spin/src/engine.rs Outdated Show resolved Hide resolved
containerd-shim-spin/src/engine.rs Show resolved Hide resolved
@kate-goldenring kate-goldenring force-pushed the runtime-and-app-env-config branch from 5d8868a to 4554a80 Compare July 15, 2024 20:23
- Sets any non-execution environment variables in the Spin app unless a
disable env var is set

Signed-off-by: Kate Goldenring <[email protected]>
…s application variables

Signed-off-by: Kate Goldenring <[email protected]>
@kate-goldenring kate-goldenring force-pushed the runtime-and-app-env-config branch from 4554a80 to 40fa6df Compare July 17, 2024 23:10
@kate-goldenring
Copy link
Collaborator Author

Just rebased. @Mossaka @jsturtevant @devigned @radu-matei this is ready to review.

Copy link
Collaborator

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

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

LGTM!

@Mossaka Mossaka merged commit 5ecf6d6 into spinframework:main Jul 18, 2024
9 checks passed
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.

4 participants