From 03a9c71b0622dd061e0e1374417665459a317224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Fri, 26 Apr 2024 16:54:55 +0200 Subject: [PATCH 1/3] feat: label selectors for parent resource for GlueOperator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- .../glue/customresource/operator/Parent.java | 11 +++++-- .../operator/GlueOperatorReconciler.java | 12 ++++++-- .../GlueOperatorComplexLabelSelectorTest.java | 1 - .../glue/GlueOperatorLabelSelectorTest.java | 1 - .../operator/glue/GlueOperatorTest.java | 29 +++++++++++++++++-- .../glueoperator/ParentLabelSelector.yaml | 18 ++++++++++++ 6 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/test/resources/glueoperator/ParentLabelSelector.yaml diff --git a/src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java b/src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java index 49d34d6..61d9e14 100644 --- a/src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java +++ b/src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java @@ -4,14 +4,13 @@ public class Parent { private String apiVersion; private String kind; + private String labelSelector; public Parent(String apiVersion, String kind) { this.apiVersion = apiVersion; this.kind = kind; } - public Parent() {} - public String getApiVersion() { return apiVersion; } @@ -29,4 +28,12 @@ public Parent setKind(String kind) { this.kind = kind; return this; } + + public String getLabelSelector() { + return labelSelector; + } + + public void setLabelSelector(String labelSelector) { + this.labelSelector = labelSelector; + } } diff --git a/src/main/java/io/csviri/operator/glue/reconciler/operator/GlueOperatorReconciler.java b/src/main/java/io/csviri/operator/glue/reconciler/operator/GlueOperatorReconciler.java index 76bedde..7559a53 100644 --- a/src/main/java/io/csviri/operator/glue/reconciler/operator/GlueOperatorReconciler.java +++ b/src/main/java/io/csviri/operator/glue/reconciler/operator/GlueOperatorReconciler.java @@ -139,11 +139,17 @@ private InformerEventSource getOrRegist .getResourceEventSourceFor(GenericKubernetesResource.class, gvk.toString()); es.start(); } catch (IllegalArgumentException e) { - es = new InformerEventSource<>(InformerConfiguration.from(gvk, + var configBuilder = InformerConfiguration.from(gvk, context.eventSourceRetriever().eventSourceContextForDynamicRegistration()) .withSecondaryToPrimaryMapper( - resource -> Set.of(ResourceID.fromResource(glueOperator))) - .build(), context.eventSourceRetriever().eventSourceContextForDynamicRegistration()); + resource -> Set.of(ResourceID.fromResource(glueOperator))); + + if (spec.getParent().getLabelSelector() != null) { + configBuilder.withLabelSelector(spec.getParent().getLabelSelector()); + } + + es = new InformerEventSource<>(configBuilder.build(), + context.eventSourceRetriever().eventSourceContextForDynamicRegistration()); context.eventSourceRetriever().dynamicallyRegisterEventSource(gvk.toString(), es); } return es; diff --git a/src/test/java/io/csviri/operator/glue/GlueOperatorComplexLabelSelectorTest.java b/src/test/java/io/csviri/operator/glue/GlueOperatorComplexLabelSelectorTest.java index 30a76e6..c9a719c 100644 --- a/src/test/java/io/csviri/operator/glue/GlueOperatorComplexLabelSelectorTest.java +++ b/src/test/java/io/csviri/operator/glue/GlueOperatorComplexLabelSelectorTest.java @@ -54,7 +54,6 @@ void testGlueOperatorLabelSelector() { testCR.getKind())); assertThat(glue).isNull(); }); - delete(go); } public static class GlueOperatorComplexLabelSelectorTestProfile implements QuarkusTestProfile { diff --git a/src/test/java/io/csviri/operator/glue/GlueOperatorLabelSelectorTest.java b/src/test/java/io/csviri/operator/glue/GlueOperatorLabelSelectorTest.java index 6ad918a..64e6216 100644 --- a/src/test/java/io/csviri/operator/glue/GlueOperatorLabelSelectorTest.java +++ b/src/test/java/io/csviri/operator/glue/GlueOperatorLabelSelectorTest.java @@ -63,7 +63,6 @@ void testGlueOperatorLabelSelector() { testCR.getKind())); assertThat(glue).isNull(); }); - delete(go); } public static class GlueOperatorLabelSelectorTestProfile implements QuarkusTestProfile { diff --git a/src/test/java/io/csviri/operator/glue/GlueOperatorTest.java b/src/test/java/io/csviri/operator/glue/GlueOperatorTest.java index fbd08ed..40b8216 100644 --- a/src/test/java/io/csviri/operator/glue/GlueOperatorTest.java +++ b/src/test/java/io/csviri/operator/glue/GlueOperatorTest.java @@ -27,8 +27,6 @@ @QuarkusTest class GlueOperatorTest extends TestBase { - - @BeforeEach void applyCRD() { TestUtils.applyTestCrd(client, TestCustomResource.class, TestCustomResource2.class); @@ -162,6 +160,33 @@ void nonUniqueNameTest() { }); } + @Test + void parentWithLabelSelector() { + create(TestUtils + .loadResourceFlowOperator("/glueoperator/ParentLabelSelector.yaml")); + + var cr = create(testCustomResource()); + String name = cr.getMetadata().getName(); + + await().pollDelay(TestUtils.INITIAL_RECONCILE_WAIT_TIMEOUT).untilAsserted(() -> { + var cm1 = get(ConfigMap.class, name); + assertThat(cm1).isNull(); + }); + + cr.getMetadata().getLabels().put("mylabel", "value"); + update(cr); + + await().untilAsserted(() -> { + var cm1 = get(ConfigMap.class, name); + assertThat(cm1).isNotNull(); + }); + + delete(cr); + await().untilAsserted(() -> { + var cm1 = get(ConfigMap.class, name); + assertThat(cm1).isNull(); + }); + } GlueOperator testWorkflowOperator() { diff --git a/src/test/resources/glueoperator/ParentLabelSelector.yaml b/src/test/resources/glueoperator/ParentLabelSelector.yaml new file mode 100644 index 0000000..325ade6 --- /dev/null +++ b/src/test/resources/glueoperator/ParentLabelSelector.yaml @@ -0,0 +1,18 @@ +apiVersion: io.csviri.operator.glue/v1beta1 +kind: GlueOperator +metadata: + name: templating-sample +spec: + parent: + apiVersion: io.csviri.operator.glue/v1 + kind: TestCustomResource + labelSelector: "mylabel=value" + childResources: + - name: configMap1 + resource: + apiVersion: v1 + kind: ConfigMap + metadata: + name: "{parent.metadata.name}" + data: + key: "{parent.spec.value}" From 0fd7a4662f273550fbc5ed23ec1d8d86fbe76807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Fri, 26 Apr 2024 17:01:38 +0200 Subject: [PATCH 2/3] fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- .../io/csviri/operator/glue/customresource/operator/Parent.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java b/src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java index 61d9e14..45a6f5e 100644 --- a/src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java +++ b/src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java @@ -6,6 +6,8 @@ public class Parent { private String kind; private String labelSelector; + public Parent() {} + public Parent(String apiVersion, String kind) { this.apiVersion = apiVersion; this.kind = kind; From 01050ef2f4d373943e3f9e52d3d3f17b8d53e115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Fri, 26 Apr 2024 17:28:21 +0200 Subject: [PATCH 3/3] docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- docs/reference.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index 3579563..0c26263 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -82,8 +82,11 @@ The `DependentResource` implementation of JOSDK makes all kinds of optimizations ## [GlueOperator resource](https://github.com/csviri/kubernetes-glue-operator/releases/latest/download/glueoperators.glue-v1.yml) The specs of `GlueOperator` are almost identical to `Glue`, it just adds one additional attribute **`parent`**, -which has two sub-attributes: **`apiVersion`** and **`kind`**. This structure specifies the resource -types - usually but not necessarily custom resources - watched. +which has the following sub-attributes: + - **`apiVersion`** and **`kind`** - specifies the resources to reconciler according to the spec. + Targets are usually custom resources but not necessarily, it also works with built-in Kubernetes + resources. + - **`labelSelector`** - an optional label selector for the target resources See minimal `GlueOperator` [here](https://github.com/csviri/kubernetes-glue-operator/blob/main/src/test/resources/glueoperator/Templating.yaml).