Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/v1alpha/KnownErrorMultiPattern.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: scope.github.com/v1alpha
kind: ScopeKnownError
metadata:
name: disk-full-or-oom
description: Check if the output contains a disk-full or out-of-memory error
spec:
pattern:
- "No space left on device"
- "Out of memory"
help: "The system ran out of disk space or memory. Free up resources and try again."
21 changes: 19 additions & 2 deletions schema/merged.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,23 @@
"ScopeKnownError"
]
},
"KnownErrorPattern": {
"description": "One or more regexes that determine whether a log line matches this known error.\nAccepts either a single pattern string or a list of pattern strings; a line matching\nany one of the patterns triggers the error.",
"anyOf": [
{
"description": "A single regex pattern.",
"type": "string"
},
{
"description": "A list of regex patterns (at least one); the known error fires when any one matches.",
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
}
]
},
"KnownErrorSpec": {
"description": "Definition of the known error",
"type": "object",
Expand All @@ -236,8 +253,8 @@
"type": "string"
},
"pattern": {
"description": "A Regex used to determine if the line is an error.",
"type": "string"
"description": "A regex (or list of regexes) used to determine if the line is an error.\nA single string or a list of strings are both accepted; the known error fires when any\npattern matches.",
"$ref": "#/$defs/KnownErrorPattern"
}
},
"additionalProperties": false,
Expand Down
21 changes: 19 additions & 2 deletions schema/v1alpha.com.github.scope.ScopeDoctorGroup.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@
"ScopeKnownError"
]
},
"KnownErrorPattern": {
"description": "One or more regexes that determine whether a log line matches this known error.\nAccepts either a single pattern string or a list of pattern strings; a line matching\nany one of the patterns triggers the error.",
"anyOf": [
{
"description": "A single regex pattern.",
"type": "string"
},
{
"description": "A list of regex patterns (at least one); the known error fires when any one matches.",
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
}
]
},
"KnownErrorSpec": {
"description": "Definition of the known error",
"type": "object",
Expand All @@ -252,8 +269,8 @@
"type": "string"
},
"pattern": {
"description": "A Regex used to determine if the line is an error.",
"type": "string"
"description": "A regex (or list of regexes) used to determine if the line is an error.\nA single string or a list of strings are both accepted; the known error fires when any\npattern matches.",
"$ref": "#/$defs/KnownErrorPattern"
}
},
"additionalProperties": false,
Expand Down
21 changes: 19 additions & 2 deletions schema/v1alpha.com.github.scope.ScopeKnownError.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@
"ScopeKnownError"
]
},
"KnownErrorPattern": {
"description": "One or more regexes that determine whether a log line matches this known error.\nAccepts either a single pattern string or a list of pattern strings; a line matching\nany one of the patterns triggers the error.",
"anyOf": [
{
"description": "A single regex pattern.",
"type": "string"
},
{
"description": "A list of regex patterns (at least one); the known error fires when any one matches.",
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
}
]
},
"KnownErrorSpec": {
"description": "Definition of the known error",
"type": "object",
Expand All @@ -252,8 +269,8 @@
"type": "string"
},
"pattern": {
"description": "A Regex used to determine if the line is an error.",
"type": "string"
"description": "A regex (or list of regexes) used to determine if the line is an error.\nA single string or a list of strings are both accepted; the known error fires when any\npattern matches.",
"$ref": "#/$defs/KnownErrorPattern"
}
},
"additionalProperties": false,
Expand Down
21 changes: 19 additions & 2 deletions schema/v1alpha.com.github.scope.ScopeReportLocation.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@
"ScopeKnownError"
]
},
"KnownErrorPattern": {
"description": "One or more regexes that determine whether a log line matches this known error.\nAccepts either a single pattern string or a list of pattern strings; a line matching\nany one of the patterns triggers the error.",
"anyOf": [
{
"description": "A single regex pattern.",
"type": "string"
},
{
"description": "A list of regex patterns (at least one); the known error fires when any one matches.",
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
}
]
},
"KnownErrorSpec": {
"description": "Definition of the known error",
"type": "object",
Expand All @@ -252,8 +269,8 @@
"type": "string"
},
"pattern": {
"description": "A Regex used to determine if the line is an error.",
"type": "string"
"description": "A regex (or list of regexes) used to determine if the line is an error.\nA single string or a list of strings are both accepted; the known error fires when any\npattern matches.",
"$ref": "#/$defs/KnownErrorPattern"
}
},
"additionalProperties": false,
Expand Down
5 changes: 2 additions & 3 deletions src/doctor/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ pub(crate) mod tests {
use super::*;
use crate::models::prelude::{ModelMetadata, ModelMetadataAnnotations};
use crate::shared::analyze::AnalyzeStatus;
use regex::Regex;
use regex::RegexSet;

fn make_known_error(pattern: &str, with_fix: bool) -> KnownError {
let fix = if with_fix {
Expand Down Expand Up @@ -1601,8 +1601,7 @@ pub(crate) mod tests {
},
labels: BTreeMap::new(),
},
pattern: pattern.to_string(),
regex: Regex::new(pattern).unwrap(),
regexes: RegexSet::new([pattern]).unwrap(),
help_text: "test help".to_string(),
fix,
}
Expand Down
71 changes: 68 additions & 3 deletions src/models/v1alpha/known_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@ use serde::{Deserialize, Serialize};

use super::prelude::DoctorFixSpec;

/// Schema helper: the list variant must have at least one element.
/// Using `schema_with` (rather than `#[schemars(length(min = 1))]`) avoids a stray
/// `minLength: 1` keyword that `length` emits unconditionally alongside `minItems`.
fn non_empty_string_list(generator: &mut schemars::generate::SchemaGenerator) -> schemars::Schema {
let item = generator.subschema_for::<String>();
schemars::json_schema!({
"type": "array",
"items": item,
"minItems": 1_u64
})
}

/// One or more regexes that determine whether a log line matches this known error.
/// Accepts either a single pattern string or a list of pattern strings; a line matching
/// any one of the patterns triggers the error.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
#[serde(untagged)]
pub enum KnownErrorPattern {
/// A single regex pattern.
Single(String),
/// A list of regex patterns (at least one); the known error fires when any one matches.
Many(#[schemars(schema_with = "non_empty_string_list")] Vec<String>),
}

impl KnownErrorPattern {
/// Returns the list of pattern strings (a single value becomes a one-element vec).
pub fn patterns(&self) -> Vec<String> {
match self {
KnownErrorPattern::Single(p) => vec![p.clone()],
KnownErrorPattern::Many(p) => p.clone(),
}
}
}

/// Definition of the known error
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, JsonSchema)]
#[serde(rename_all = "camelCase")]
Expand All @@ -15,8 +49,10 @@ pub struct KnownErrorSpec {
/// Text that the user can use to fix the issue
pub help: String,

/// A Regex used to determine if the line is an error.
pub pattern: String,
/// A regex (or list of regexes) used to determine if the line is an error.
/// A single string or a list of strings are both accepted; the known error fires when any
/// pattern matches.
pub pattern: KnownErrorPattern,

/// An optional fix the user will be prompted to run.
pub fix: Option<DoctorFixSpec>,
Expand Down Expand Up @@ -82,6 +118,35 @@ impl InternalScopeModel<KnownErrorSpec, V1AlphaKnownError> for V1AlphaKnownError

#[cfg(test)]
fn examples() -> Vec<String> {
vec!["v1alpha/KnownError.yaml".to_string()]
vec![
"v1alpha/KnownError.yaml".to_string(),
"v1alpha/KnownErrorMultiPattern.yaml".to_string(),
]
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::models::InternalScopeModel;

#[test]
fn schema_rejects_empty_pattern_list() {
let value = serde_json::json!({
"apiVersion": "scope.github.com/v1alpha",
"kind": "ScopeKnownError",
"metadata": {
"name": "test",
"description": "test"
},
"spec": {
"pattern": [],
"help": "some help"
}
});
assert!(
V1AlphaKnownError::validate_resource(&value).is_err(),
"schema should reject an empty pattern list"
);
}
}
2 changes: 1 addition & 1 deletion src/shared/analyze/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
let mut known_errors_to_remove = Vec::new();
for (name, ke) in &known_errors {
debug!("Checking known error {}", ke.name());
if ke.regex.is_match(&line) {
if ke.regexes.is_match(&line) {
warn!(target: "always", "Known error '{}' found on line {}", ke.name(), line_number);
info!(target: "always", "\t==> {}", ke.help_text);

Expand Down
8 changes: 4 additions & 4 deletions src/shared/config_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,23 +368,23 @@ mod tests {
let canonical_nested = fs::canonicalize(&nested_dir).unwrap();

// Should include .scope directories for all ancestors
let expected_paths = vec![
let expected_paths = [
canonical_nested.join(".scope"),
canonical_nested.parent().unwrap().join(".scope"), // child
canonical_nested.parent().unwrap().join(".scope"),
canonical_nested
.parent()
.unwrap()
.parent()
.unwrap()
.join(".scope"), // parent
.join(".scope"),
canonical_nested
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join(".scope"), // temp_dir
.join(".scope"),
];

// Check that all expected ancestor paths are present (in order)
Expand Down
Loading
Loading