Skip to content
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

[Story] Replace LabelSelectorMatcher with MatchExpression #430

Open
andrewazores opened this issue May 2, 2024 · 0 comments
Open

[Story] Replace LabelSelectorMatcher with MatchExpression #430

andrewazores opened this issue May 2, 2024 · 0 comments
Labels
feat New feature or request

Comments

@andrewazores
Copy link
Member

diff --git a/src/main/java/io/cryostat/graphql/RootNode.java b/src/main/java/io/cryostat/graphql/RootNode.java
index 4af6eef..c2facb6 100644
--- a/src/main/java/io/cryostat/graphql/RootNode.java
+++ b/src/main/java/io/cryostat/graphql/RootNode.java
@@ -21,6 +21,8 @@ import java.util.Set;
 import java.util.function.Predicate;
 
 import io.cryostat.discovery.DiscoveryNode;
+import io.cryostat.expressions.MatchExpression;
+import io.cryostat.expressions.MatchExpressionEvaluator;
 import io.cryostat.graphql.matchers.LabelSelectorMatcher;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -30,6 +32,7 @@ import org.eclipse.microprofile.graphql.Description;
 import org.eclipse.microprofile.graphql.GraphQLApi;
 import org.eclipse.microprofile.graphql.Query;
 import org.eclipse.microprofile.graphql.Source;
+import org.projectnessie.cel.tools.ScriptException;
 
 @GraphQLApi
 public class RootNode {
@@ -77,6 +80,7 @@ public class RootNode {
         public @Nullable List<String> nodeTypes;
         public @Nullable List<String> labels;
         public @Nullable List<String> annotations;
+        public @Nullable List<String> matchExpressions;
 
         @Override
         public boolean test(DiscoveryNode t) {
@@ -114,6 +118,24 @@ public class RootNode {
                                                                                             .annotations
                                                                                             .merged())));
 
+            MatchExpressionEvaluator evaluator = new MatchExpressionEvaluator(); // WRONG
+            Predicate<DiscoveryNode> matchesExpression =
+                    n ->
+                            matchExpressions == null
+                                    || (n.target != null
+                                            && matchExpressions.stream()
+                                                    .map(MatchExpression::new)
+                                                    .allMatch(
+                                                            expr -> {
+                                                                try {
+                                                                    return evaluator.applies(
+                                                                            expr, n.target);
+                                                                } catch (ScriptException e) {
+                                                                    // FIXME log this
+                                                                    return false;
+                                                                }
+                                                            }));
+
             return List.of(
                             matchesId,
                             matchesIds,
@@ -122,7 +144,8 @@ public class RootNode {
                             matchesNames,
                             matchesNodeTypes,
                             matchesLabels,
-                            matchesAnnotations)
+                            matchesAnnotations,
+                            matchesExpression)
                     .stream()
                     .reduce(x -> true, Predicate::and)
                     .test(t);
diff --git a/src/test/java/itest/GraphQLTest.java b/src/test/java/itest/GraphQLTest.java
index 3d28654..15d1b69 100644
--- a/src/test/java/itest/GraphQLTest.java
+++ b/src/test/java/itest/GraphQLTest.java
@@ -307,8 +307,8 @@ class GraphQLTest extends StandardSelfTest {
         JsonObject query2 = new JsonObject();
         query2.put(
                 "query",
-                "mutation { createRecording( nodes:{annotations: ["
-                        + "\"REALM = Custom Targets\""
+                "mutation { createRecording( nodes:{matchExpressions: ["
+                        + "\"target.annotations[\"REALM\"] == \"Custom Targets\"\""
                         + "]}, recording: { name: \"test\", template:"
                         + " \"Profiling\", templateType: \"TARGET\", duration: 30, continuous:"
                         + " false, archiveOnStop: true, toDisk: true }) { name state duration"

Rough prototype of the idea. The LabelSelectorMatcher has a very limited syntax and is only used for matching on maps of labels and annotations. This could easily be a MatchExpression instead and use CEL syntax that is also already used for Automated Rules and Stored Credentials, instead of its own k8s-inspired syntax that only appears in GraphQL query filters.

@andrewazores andrewazores added the feat New feature or request label May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant