-
Notifications
You must be signed in to change notification settings - Fork 0
Add store support for Spin adapter (KV, config, secret) #253
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
32998df
feat: allow spin as a supported config store adapter
1022cd7
docs: update adapters field comment to include spin
5bb8051
feat: add SpinConfigStore backed by spin_sdk::variables
d64e95d
fix: rename SpinConfigBackend and remove feature gate on config_store…
53dfa1c
fix: fmt ordering in lib.rs and add SpinConfigStore to ConfigStore tr…
917ff57
feat: add SpinSecretStore backed by spin_sdk::variables
8a4e227
feat: add SpinKvStore backed by spin_sdk::key_value
ac8facb
chore: remove stale 'not yet implemented' note from run_app doc comment
a0ec8e6
ci: add spin-adapter-tests job
cf57f58
fix: handle InvalidName in SpinSecretStore and restore allow(unused_v…
ec7dc14
feat: add configurable max_list_keys cap to SpinKvStore
a905d5a
feat: wire store handles into Spin dispatch and add contract tests
11aa462
feat: add Spin adapter support to smoke test scripts
58c7b6e
chore: ignore .spin/ runtime directory
b5129da
- Return KvError::Validation when key count exceeds max_list_keys
prk-Jr c001103
fix CI cache conflict
prk-Jr 3b995e5
Fix test.yml to use matrix run
prk-Jr 4644225
Fix pin viceroy pin and wasmtime install flag
prk-Jr ff0633b
Fix type mismatch for wasm32-wasip1
prk-Jr c9d1e25
Address pr review findings
prk-Jr 8956aba
chore: merge main into feat/spin-store-support
prk-Jr d0d6d60
fix: address spin store review feedback
prk-Jr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ bin/ | |
| pkg/ | ||
| target/ | ||
| .wrangler/ | ||
| .spin/ | ||
| .edgezero/ | ||
|
|
||
| # env | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| //! Spin adapter config store: wraps `spin_sdk::variables`. | ||
|
|
||
| use edgezero_core::config_store::{ConfigStore, ConfigStoreError}; | ||
|
|
||
| /// Config store backed by Spin component variables. | ||
| pub struct SpinConfigStore { | ||
| inner: SpinConfigBackend, | ||
| } | ||
|
|
||
| enum SpinConfigBackend { | ||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| Spin, | ||
| #[cfg(test)] | ||
| InMemory(std::collections::HashMap<String, String>), | ||
| /// Never constructed; keeps the enum inhabited outside production Spin and tests. | ||
| #[cfg(not(any(all(feature = "spin", target_arch = "wasm32"), test)))] | ||
| _Uninhabited(std::convert::Infallible), | ||
| } | ||
|
|
||
| impl SpinConfigStore { | ||
| /// Create a new `SpinConfigStore` using the Spin variables API. | ||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| pub fn new() -> Self { | ||
| Self { | ||
| inner: SpinConfigBackend::Spin, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| fn from_entries(entries: impl IntoIterator<Item = (String, String)>) -> Self { | ||
| Self { | ||
| inner: SpinConfigBackend::InMemory(entries.into_iter().collect()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| impl Default for SpinConfigStore { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl ConfigStore for SpinConfigStore { | ||
| fn get(&self, _key: &str) -> Result<Option<String>, ConfigStoreError> { | ||
| match &self.inner { | ||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| SpinConfigBackend::Spin => { | ||
| use spin_sdk::variables; | ||
|
prk-Jr marked this conversation as resolved.
|
||
| match variables::get(_key) { | ||
| Ok(value) => Ok(Some(value)), | ||
| Err(variables::Error::Undefined(_)) => Ok(None), | ||
| Err(variables::Error::InvalidName(msg)) => { | ||
| Err(ConfigStoreError::invalid_key(msg)) | ||
| } | ||
| Err(e) => Err(ConfigStoreError::unavailable(e.to_string())), | ||
| } | ||
| } | ||
| #[cfg(test)] | ||
| SpinConfigBackend::InMemory(data) => Ok(data.get(_key).cloned()), | ||
| #[cfg(not(any(all(feature = "spin", target_arch = "wasm32"), test)))] | ||
| SpinConfigBackend::_Uninhabited(never) => match *never {}, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| // These contract tests exercise the InMemory backend (not the real Spin | ||
| // variables API). Dotted keys such as "contract.key.a" are valid here but | ||
| // would trigger `InvalidName` on the real Spin backend, which requires | ||
| // lowercase variable names without dots. Real-backend behaviour is | ||
| // verified by the smoke tests in scripts/smoke_test_config.sh. | ||
| edgezero_core::config_store_contract_tests!(spin_config_store_contract, { | ||
| SpinConfigStore::from_entries([ | ||
| ("contract.key.a".to_string(), "value_a".to_string()), | ||
| ("contract.key.b".to_string(), "value_b".to_string()), | ||
| ]) | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| //! Spin KV store adapter. | ||
| //! | ||
| //! Wraps `spin_sdk::key_value::Store` to implement the | ||
| //! `edgezero_core::key_value_store::KvStore` trait. | ||
| //! | ||
| //! # Limitations | ||
| //! | ||
| //! - **TTL**: The Spin KV API has no TTL support. Calls to | ||
| //! `put_bytes_with_ttl` return `KvError::Validation` without writing. | ||
| //! - **Listing**: `spin_sdk::key_value::Store::get_keys()` returns all keys | ||
| //! with no prefix or cursor support. `list_keys_page` therefore returns | ||
| //! `KvError::Validation` instead of materializing the whole store. | ||
| //! | ||
| //! # Note | ||
| //! | ||
| //! This module is only compiled when the `spin` feature is enabled and the | ||
| //! target is `wasm32`. | ||
|
|
||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| use async_trait::async_trait; | ||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| use bytes::Bytes; | ||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| use edgezero_core::key_value_store::{KvError, KvPage, KvStore}; | ||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| use std::time::Duration; | ||
|
|
||
| /// KV store backed by the Spin KV API. | ||
| /// | ||
| /// Wraps a `spin_sdk::key_value::Store` handle obtained via | ||
| /// `Store::open(label)`. | ||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| pub struct SpinKvStore { | ||
| store: spin_sdk::key_value::Store, | ||
| } | ||
|
|
||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| impl SpinKvStore { | ||
| /// Open a Spin KV store by label. | ||
| /// | ||
| /// The `label` must match a `key_value_stores` entry in `spin.toml`. | ||
| /// Returns `KvError::Internal` if the store cannot be opened. | ||
| pub fn open(label: &str) -> Result<Self, KvError> { | ||
| let store = spin_sdk::key_value::Store::open(label) | ||
| .map_err(|e| KvError::Internal(anyhow::anyhow!("failed to open kv store: {e}")))?; | ||
| Ok(Self { store }) | ||
| } | ||
|
|
||
| /// Open the default EdgeZero KV store label (`"EDGEZERO_KV"`). | ||
| pub fn open_default() -> Result<Self, KvError> { | ||
| Self::open(edgezero_core::manifest::DEFAULT_KV_STORE_NAME) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(feature = "spin", target_arch = "wasm32"))] | ||
| #[async_trait(?Send)] | ||
| impl KvStore for SpinKvStore { | ||
| async fn get_bytes(&self, key: &str) -> Result<Option<Bytes>, KvError> { | ||
| self.store | ||
| .get(key) | ||
| .map(|opt| opt.map(Bytes::from)) | ||
| .map_err(|e| KvError::Internal(anyhow::anyhow!("get failed: {e}"))) | ||
| } | ||
|
|
||
| async fn put_bytes(&self, key: &str, value: Bytes) -> Result<(), KvError> { | ||
| self.store | ||
| .set(key, value.as_ref()) | ||
| .map_err(|e| KvError::Internal(anyhow::anyhow!("set failed: {e}"))) | ||
| } | ||
|
|
||
| async fn put_bytes_with_ttl( | ||
|
prk-Jr marked this conversation as resolved.
|
||
| &self, | ||
| _key: &str, | ||
| _value: Bytes, | ||
| _ttl: Duration, | ||
| ) -> Result<(), KvError> { | ||
| Err(KvError::Validation( | ||
| "Spin KV does not support TTL; use put_bytes for non-expiring values".to_string(), | ||
| )) | ||
| } | ||
|
|
||
| async fn delete(&self, key: &str) -> Result<(), KvError> { | ||
| self.store | ||
| .delete(key) | ||
| .map_err(|e| KvError::Internal(anyhow::anyhow!("delete failed: {e}"))) | ||
| } | ||
|
|
||
| async fn exists(&self, key: &str) -> Result<bool, KvError> { | ||
| self.store | ||
| .exists(key) | ||
| .map_err(|e| KvError::Internal(anyhow::anyhow!("exists failed: {e}"))) | ||
| } | ||
|
|
||
| async fn list_keys_page( | ||
|
prk-Jr marked this conversation as resolved.
|
||
| &self, | ||
| _prefix: &str, | ||
| _cursor: Option<&str>, | ||
| _limit: usize, | ||
| ) -> Result<KvPage, KvError> { | ||
| Err(KvError::Validation( | ||
| "Spin KV key listing is unsupported because Store::get_keys() is unbounded".to_string(), | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| // TODO: integration tests require the Spin runtime. | ||
| // Test `SpinKvStore` as part of a Spin E2E test suite. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.