From dae3c0edd2512e32bbd9a363fd84a6ae98281af1 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 24 Dec 2024 15:38:55 +0100 Subject: [PATCH] Add kubernetes-file test --- .../docker/search/FindDockerImageUses.java | 2 +- .../docker/trait/YamlImageReference.java | 7 ++- .../docker/FindDockerImagesUsedTest.java | 53 +++++++++++++++++++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/docker/search/FindDockerImageUses.java b/src/main/java/org/openrewrite/docker/search/FindDockerImageUses.java index dea8ee6..5d49a31 100644 --- a/src/main/java/org/openrewrite/docker/search/FindDockerImageUses.java +++ b/src/main/java/org/openrewrite/docker/search/FindDockerImageUses.java @@ -76,7 +76,7 @@ private static class ReferenceFindSearchResultVisitor extends TreeVisitor> matches; @Override - public Tree postVisit(Tree tree, ExecutionContext ctx) { + public @Nullable Tree postVisit(Tree tree, ExecutionContext ctx) { List references = matches.get(tree); if (references != null) { if (tree instanceof PlainText) { diff --git a/src/main/java/org/openrewrite/docker/trait/YamlImageReference.java b/src/main/java/org/openrewrite/docker/trait/YamlImageReference.java index 6602851..cdf4864 100644 --- a/src/main/java/org/openrewrite/docker/trait/YamlImageReference.java +++ b/src/main/java/org/openrewrite/docker/trait/YamlImageReference.java @@ -27,18 +27,17 @@ public static class Provider extends YamlProvider { @Override public SimpleTraitMatcher getMatcher() { return new SimpleTraitMatcher() { - private final Predicate image = Pattern.compile("image").asPredicate(); private final AtomicBoolean found = new AtomicBoolean(false); @Override protected @Nullable YamlReference test(Cursor cursor) { Object value = cursor.getValue(); if (value instanceof Yaml.Scalar) { - if (image.test(((Yaml.Scalar) value).getValue())) { - found.set(true); - } else if (found.get()) { + if (found.get()) { found.set(false); return new YamlImageReference(cursor); + } else if ("image".equals(((Yaml.Scalar) value).getValue())) { + found.set(true); } } return null; diff --git a/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java b/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java index 23ae9f9..2683caf 100644 --- a/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java +++ b/src/test/java/org/openrewrite/docker/FindDockerImagesUsedTest.java @@ -340,6 +340,59 @@ void gitlabCIFile() { ); } + @Test + void kubernetesFile() { + rewriteRun( + assertImages("image", "app:v1.2.3", "account/image:latest", "repo.id/account/bucket/image:v1.2.3@digest"), + //language=yaml + yaml( + """ + apiVersion: v1 + kind: Pod + spec: + containers: + - image: image + --- + apiVersion: v1 + kind: Pod + spec: + containers: + - image: app:v1.2.3 + initContainers: + - image: account/image:latest + --- + apiVersion: v1 + kind: Pod + spec: + containers: + - image: repo.id/account/bucket/image:v1.2.3@digest + """, + """ + apiVersion: v1 + kind: Pod + spec: + containers: + - image: ~~(image)~~>image + --- + apiVersion: v1 + kind: Pod + spec: + containers: + - image: ~~(app:v1.2.3)~~>app:v1.2.3 + initContainers: + - image: ~~(account/image:latest)~~>account/image:latest + --- + apiVersion: v1 + kind: Pod + spec: + containers: + - image: ~~(repo.id/account/bucket/image:v1.2.3@digest)~~>repo.id/account/bucket/image:v1.2.3@digest + """, + spec -> spec.path(".gitlab-ci") + ) + ); + } + private static Consumer assertImages(String... expected) { return spec -> spec.recipe(new FindDockerImageUses()) .dataTable(DockerBaseImages.Row.class,rows ->