Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit 59efac9

Browse files
authored
Merge pull request #1142 from flavio/feat-scaffold-artifacthub-run-without-params
feat: scaffold artifacthub runs without params
2 parents 774560b + b70750a commit 59efac9

File tree

6 files changed

+252
-4
lines changed

6 files changed

+252
-4
lines changed

cli-docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ Scaffold an AdmissionRequest object
388388

389389
Output an artifacthub-pkg.yml file from a metadata.yml file
390390

391-
**Usage:** `kwctl scaffold artifacthub [OPTIONS] --metadata-path <PATH>`
391+
**Usage:** `kwctl scaffold artifacthub [OPTIONS]`
392392

393393
###### **Options:**
394394

src/cli.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ fn subcommand_scaffold() -> Command {
419419
Arg::new("metadata-path")
420420
.long("metadata-path")
421421
.short('m')
422-
.required(true)
423422
.value_name("PATH")
424423
.help("File containing the metadata of the policy"),
425424
Arg::new("version")

src/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::{
2020
str::FromStr,
2121
sync::Arc,
2222
};
23+
use utils::find_file_matching_file;
2324
use verify::VerificationAnnotations;
2425

2526
use crate::utils::LookupError;
@@ -322,13 +323,27 @@ async fn main() -> Result<()> {
322323
let metadata_file = artifacthub_matches
323324
.get_one::<String>("metadata-path")
324325
.map(|output| PathBuf::from_str(output).unwrap())
325-
.unwrap();
326+
.or_else(|| find_file_matching_file(&["metadata.yml", "metadata.yaml"]))
327+
.ok_or_else(|| {
328+
anyhow!(
329+
"path to metadata file not provided, plus 'metadata.yml' not found"
330+
)
331+
})?;
332+
326333
if artifacthub_matches.get_one::<String>("version").is_some() {
327334
tracing::warn!("The 'version' flag is deprecated and will be removed in a future release. The value of the `io.kubewarden.policy.version` field in the policy metadata file is used instead.");
328335
}
329336
let questions_file = artifacthub_matches
330337
.get_one::<String>("questions-path")
331-
.map(|output| PathBuf::from_str(output).unwrap());
338+
.map(|output| PathBuf::from_str(output).unwrap())
339+
.or_else(|| {
340+
find_file_matching_file(&[
341+
"questions-ui.yml",
342+
"questions-ui.yaml",
343+
"questions.yml",
344+
"questions.yaml",
345+
])
346+
});
332347
let content = scaffold::artifacthub(metadata_file, questions_file)?;
333348
if let Some(output) = artifacthub_matches.get_one::<String>("output") {
334349
let output_path = PathBuf::from_str(output)?;

src/utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ pub(crate) fn new_policy_execution_mode_from_str(name: &str) -> Result<PolicyExe
9292
Ok(execution_mode)
9393
}
9494

95+
pub(crate) fn find_file_matching_file(possible_names: &[&str]) -> Option<PathBuf> {
96+
possible_names
97+
.iter()
98+
.map(PathBuf::from)
99+
.find(|path| path.exists())
100+
}
101+
95102
#[cfg(test)]
96103
mod tests {
97104
use std::collections::HashMap;
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
rules:
2+
- apiGroups:
3+
- admissionregistration.k8s.io
4+
apiVersions:
5+
- v1beta1
6+
resources:
7+
- "*"
8+
operations:
9+
- CREATE
10+
- apiGroups:
11+
- apiextensions.k8s.io
12+
apiVersions:
13+
- v1beta1
14+
resources:
15+
- "*"
16+
operations:
17+
- CREATE
18+
- apiGroups:
19+
- apiregistration.k8s.io
20+
apiVersions:
21+
- v1beta1
22+
resources:
23+
- "*"
24+
operations:
25+
- CREATE
26+
- apiGroups:
27+
- apps
28+
apiVersions:
29+
- v1beta1
30+
- v1beta2
31+
resources:
32+
- "*"
33+
operations:
34+
- CREATE
35+
- apiGroups:
36+
- audit.k8s.io
37+
apiVersions:
38+
- v1alpha1
39+
- v1beta1
40+
resources:
41+
- "*"
42+
operations:
43+
- CREATE
44+
- apiGroups:
45+
- authentication.k8s.io
46+
apiVersions:
47+
- v1beta1
48+
resources:
49+
- "*"
50+
operations:
51+
- CREATE
52+
- apiGroups:
53+
- autoscaling
54+
apiVersions:
55+
- v2beta1
56+
- v2beta2
57+
resources:
58+
- "*"
59+
operations:
60+
- CREATE
61+
- apiGroups:
62+
- batch
63+
apiVersions:
64+
- v1beta1
65+
resources:
66+
- "*"
67+
operations:
68+
- CREATE
69+
- apiGroups:
70+
- certificates.k8s.io
71+
apiVersions:
72+
- v1beta1
73+
resources:
74+
- "*"
75+
operations:
76+
- CREATE
77+
- apiGroups:
78+
- coordination.k8s.io
79+
apiVersions:
80+
- v1beta1
81+
resources:
82+
- "*"
83+
operations:
84+
- CREATE
85+
- apiGroups:
86+
- discovery.k8s.io
87+
apiVersions:
88+
- v1beta1
89+
resources:
90+
- "*"
91+
operations:
92+
- CREATE
93+
- apiGroups:
94+
- events.k8s.io
95+
apiVersions:
96+
- v1beta1
97+
resources:
98+
- "*"
99+
operations:
100+
- CREATE
101+
- apiGroups:
102+
- extensions
103+
apiVersions:
104+
- v1beta1
105+
resources:
106+
- "*"
107+
operations:
108+
- CREATE
109+
- apiGroups:
110+
- flowcontrol.apiserver.k8s.io
111+
apiVersions:
112+
- v1beta1
113+
- v1beta2
114+
- v1beta3
115+
resources:
116+
- "*"
117+
operations:
118+
- CREATE
119+
- apiGroups:
120+
- networking.k8s.io
121+
apiVersions:
122+
- v1beta1
123+
resources:
124+
- "*"
125+
operations:
126+
- CREATE
127+
- apiGroups:
128+
- node.k8s.io
129+
apiVersions:
130+
- v1beta1
131+
resources:
132+
- "*"
133+
operations:
134+
- CREATE
135+
- apiGroups:
136+
- policy
137+
apiVersions:
138+
- v1beta1
139+
resources:
140+
- "*"
141+
operations:
142+
- CREATE
143+
- apiGroups:
144+
- rbac.authorization.k8s.io
145+
apiVersions:
146+
- v1alpha1
147+
- v1beta1
148+
resources:
149+
- "*"
150+
operations:
151+
- CREATE
152+
- apiGroups:
153+
- scheduling.k8s.io
154+
apiVersions:
155+
- v1alpha1
156+
- v1beta1
157+
resources:
158+
- "*"
159+
operations:
160+
- CREATE
161+
- apiGroups:
162+
- storage.k8s.io
163+
apiVersions:
164+
- v1beta1
165+
resources:
166+
- "*"
167+
operations:
168+
- CREATE
169+
mutating: false
170+
contextAware: false
171+
executionMode: kubewarden-wapc
172+
backgroundAudit: false
173+
annotations:
174+
# artifacthub specific
175+
io.artifacthub.displayName: Deprecated API Versions
176+
io.artifacthub.resources: "*"
177+
io.artifacthub.keywords: compliance, deprecated API
178+
io.kubewarden.policy.ociUrl: ghcr.io/kubewarden/policies/deprecated-api-versions
179+
# kubewarden specific
180+
io.kubewarden.policy.title: deprecated-api-versions
181+
io.kubewarden.policy.description: Find deprecated and removed Kubernetes resources
182+
io.kubewarden.policy.version: 0.2.0
183+
io.kubewarden.policy.author: Kubewarden developers <cncf-kubewarden-maintainers@lists.cncf.io>
184+
io.kubewarden.policy.url: https://github.com/kubewarden/deprecated-api-versions-policy
185+
io.kubewarden.policy.source: https://github.com/kubewarden/deprecated-api-versions-policy
186+
io.kubewarden.policy.license: Apache-2.0
187+
io.kubewarden.policy.category: Kubernetes API Versions
188+
io.kubewarden.policy.severity: low

tests/e2e.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,45 @@ fn test_inspect_policy_yml_output(#[case] show_signatures: bool) {
575575
assert_eq!(show_signatures, report.contains_key("signatures"))
576576
}
577577

578+
#[test]
579+
fn test_artifacthub_scaffold_find_metadata_automatically() {
580+
let tempdir = tempdir().unwrap();
581+
582+
std::fs::copy(
583+
test_data("artifacthub/metadata.yml"),
584+
tempdir.path().join("metadata.yml"),
585+
)
586+
.expect("cannot copy metadata.yml");
587+
588+
let mut cmd = setup_command(tempdir.path());
589+
cmd.arg("scaffold").arg("artifacthub");
590+
591+
cmd.assert().success();
592+
}
593+
594+
#[test]
595+
fn test_artifacthub_scaffold_with_custom_metadata() {
596+
let tempdir = tempdir().unwrap();
597+
598+
let mut cmd = setup_command(tempdir.path());
599+
cmd.arg("scaffold")
600+
.arg("artifacthub")
601+
.arg("--metadata-path")
602+
.arg(test_data("artifacthub/metadata.yml"));
603+
604+
cmd.assert().success();
605+
}
606+
607+
#[test]
608+
fn test_artifacthub_scaffold_fail_when_metadata_not_provided_nor_found() {
609+
let tempdir = tempdir().unwrap();
610+
611+
let mut cmd = setup_command(tempdir.path());
612+
cmd.arg("scaffold").arg("artifacthub");
613+
614+
cmd.assert().failure();
615+
}
616+
578617
fn get_wasm_annotations(dir: &Path, oci_ref: &str) -> Result<BTreeMap<String, String>> {
579618
let mut cmd = setup_command(dir);
580619
cmd.arg("inspect").arg(oci_ref).arg("-o").arg("yaml");

0 commit comments

Comments
 (0)