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). 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..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 @@ -4,14 +4,15 @@ public class Parent { private String apiVersion; private String kind; + private String labelSelector; + + public Parent() {} public Parent(String apiVersion, String kind) { this.apiVersion = apiVersion; this.kind = kind; } - public Parent() {} - public String getApiVersion() { return apiVersion; } @@ -29,4 +30,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}"