Skip to content

Commit

Permalink
feat(validation): warn for link props source_configs and target_configs
Browse files Browse the repository at this point in the history
Signed-off-by: Márk Kővári <[email protected]>
  • Loading branch information
markkovari authored and thomastaylor312 committed Dec 20, 2024
1 parent b2a1082 commit 265f732
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
63 changes: 61 additions & 2 deletions crates/wadm-types/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,12 @@ pub async fn validate_manifest_file(
pub async fn validate_manifest_bytes(
content: impl AsRef<[u8]>,
) -> Result<(Manifest, Vec<ValidationFailure>)> {
let raw_yaml_content = content.as_ref();
let manifest =
serde_yaml::from_slice(content.as_ref()).context("failed to parse manifest content")?;
let failures = validate_manifest(&manifest).await?;
let mut failures = validate_manifest(&manifest).await?;
let mut yaml_issues = validate_raw_yaml(raw_yaml_content)?;
failures.append(&mut yaml_issues);
Ok((manifest, failures))
}

Expand Down Expand Up @@ -349,6 +352,14 @@ pub async fn validate_manifest(manifest: &Manifest) -> Result<Vec<ValidationFail
Ok(failures)
}

pub fn validate_raw_yaml(content: &[u8]) -> Result<Vec<ValidationFailure>> {
let mut failures = Vec::new();
let raw_content: serde_yaml::Value =
serde_yaml::from_slice(content).context("failed read raw yaml content")?;
failures.extend(validate_components_configs(&raw_content));
Ok(failures)
}

fn core_validation(manifest: &Manifest) -> Vec<ValidationFailure> {
let mut failures = Vec::new();
let mut name_registry: HashSet<String> = HashSet::new();
Expand Down Expand Up @@ -728,7 +739,55 @@ pub fn validate_link_configs(manifest: &Manifest) -> Vec<ValidationFailure> {
}
}
}
};
}
failures
}

/// Funtion to validate the component configs
/// from 0.13.0 source_config is deprecated and replaced with source:config:
/// this function validates the raw yaml to check for deprecated source_config and target_config
pub fn validate_components_configs(application: &serde_yaml::Value) -> Vec<ValidationFailure> {
let mut failures = Vec::new();

if let Some(specs) = application.get("spec") {
if let Some(components) = specs.get("components") {
if let Some(components_sequence) = components.as_sequence() {
for component in components_sequence.iter() {
failures.extend(get_deprecated_configs(component));
}
}
}
}
failures
}

fn get_deprecated_configs(component: &serde_yaml::Value) -> Vec<ValidationFailure> {
let mut failures = vec![];
if let Some(traits) = component.get("traits") {
if let Some(traits_sequence) = traits.as_sequence() {
for trait_ in traits_sequence.iter() {
if let Some(trait_type) = trait_.get("type") {
if trait_type.ne("link") {
continue;
}
}
if let Some(trait_properties) = trait_.get("properties") {
if let Some(_) = trait_properties.get("source_config") {
failures.push(ValidationFailure {
level: ValidationFailureLevel::Warning,
msg: "one of the components' link trait contains a source_config key, please use source:config: rather".to_string(),
});
}
if let Some(_) = trait_properties.get("target_config") {
failures.push(ValidationFailure {
level: ValidationFailureLevel::Warning,
msg: "one of the components' link trait contains a target_config key, please use target:config: rather".to_string(),
});
}
}
}
}
}
failures
}

Expand Down
53 changes: 53 additions & 0 deletions tests/fixtures/manifests/deprecated-source-and-target-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: rust-http-blobstore
annotations:
version: v0.0.1
description: 'HTTP Blobstore demo in Rust, using the WebAssembly Component Model and WebAssembly Interfaces Types (WIT)'
wasmcloud.dev/authors: wasmCloud team
wasmcloud.dev/source-url: https://github.com/wasmCloud/wasmCloud/blob/main/examples/rust/components/http-blobstore/wadm.yaml
wasmcloud.dev/readme-md-url: https://github.com/wasmCloud/wasmCloud/blob/main/examples/rust/components/http-blobstore/README.md
wasmcloud.dev/homepage: https://github.com/wasmCloud/wasmCloud/tree/main/examples/rust/components/http-blobstore
wasmcloud.dev/categories: |
http,http-server,rust,blobstore,object-storage,example
spec:
components:
- name: http-blobstore
type: component
properties:
image: ghcr.io/wasmcloud/components/http-blobstore-rust:0.2.0
traits:
- type: spreadscaler
properties:
instances: 1
- type: link
properties:
target: blobstore-fs
namespace: wasi
package: blobstore
interfaces: [blobstore]
target_config:
- name: root-directory
properties:
root: '/tmp'
- name: httpserver
type: capability
properties:
image: ghcr.io/wasmcloud/http-server:0.23.2
traits:
- type: link
properties:
target: http-blobstore
namespace: wasi
package: http
interfaces: [incoming-handler]
source_config:
- name: default-http
properties:
address: 0.0.0.0:8000
- name: blobstore-fs
type: capability
properties:
image: ghcr.io/wasmcloud/blobstore-fs:0.10.1
16 changes: 16 additions & 0 deletions tests/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,19 @@ async fn validate_link_config_names() -> Result<()> {
);
Ok(())
}

#[tokio::test]
async fn validate_deprecated_configs_raw_yaml() -> Result<()> {
let (_manifest, failures) = validate_manifest_file(
"./tests/fixtures/manifests/deprecated-source-and-target-config.yaml",
)
.await
.context("failed to validate manifest")?;
assert!(failures.valid(), "expected valid manifest");
assert_eq!(
failures.warnings().len(),
2,
"expected 2 warnings during validating manifest"
);
Ok(())
}

0 comments on commit 265f732

Please sign in to comment.