Skip to content

Commit b1648fb

Browse files
authored
feat: label selectors for parent resource for GlueOperator (#88)
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 90a4226 commit b1648fb

File tree

7 files changed

+70
-11
lines changed

7 files changed

+70
-11
lines changed

docs/reference.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ The `DependentResource` implementation of JOSDK makes all kinds of optimizations
8282
## [GlueOperator resource](https://github.com/csviri/kubernetes-glue-operator/releases/latest/download/glueoperators.glue-v1.yml)
8383

8484
The specs of `GlueOperator` are almost identical to `Glue`, it just adds one additional attribute **`parent`**,
85-
which has two sub-attributes: **`apiVersion`** and **`kind`**. This structure specifies the resource
86-
types - usually but not necessarily custom resources - watched.
85+
which has the following sub-attributes:
86+
- **`apiVersion`** and **`kind`** - specifies the resources to reconciler according to the spec.
87+
Targets are usually custom resources but not necessarily, it also works with built-in Kubernetes
88+
resources.
89+
- **`labelSelector`** - an optional label selector for the target resources
8790

8891
See minimal `GlueOperator` [here](https://github.com/csviri/kubernetes-glue-operator/blob/main/src/test/resources/glueoperator/Templating.yaml).
8992

src/main/java/io/csviri/operator/glue/customresource/operator/Parent.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ public class Parent {
44

55
private String apiVersion;
66
private String kind;
7+
private String labelSelector;
8+
9+
public Parent() {}
710

811
public Parent(String apiVersion, String kind) {
912
this.apiVersion = apiVersion;
1013
this.kind = kind;
1114
}
1215

13-
public Parent() {}
14-
1516
public String getApiVersion() {
1617
return apiVersion;
1718
}
@@ -29,4 +30,12 @@ public Parent setKind(String kind) {
2930
this.kind = kind;
3031
return this;
3132
}
33+
34+
public String getLabelSelector() {
35+
return labelSelector;
36+
}
37+
38+
public void setLabelSelector(String labelSelector) {
39+
this.labelSelector = labelSelector;
40+
}
3241
}

src/main/java/io/csviri/operator/glue/reconciler/operator/GlueOperatorReconciler.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,17 @@ private InformerEventSource<GenericKubernetesResource, GlueOperator> getOrRegist
139139
.getResourceEventSourceFor(GenericKubernetesResource.class, gvk.toString());
140140
es.start();
141141
} catch (IllegalArgumentException e) {
142-
es = new InformerEventSource<>(InformerConfiguration.from(gvk,
142+
var configBuilder = InformerConfiguration.from(gvk,
143143
context.eventSourceRetriever().eventSourceContextForDynamicRegistration())
144144
.withSecondaryToPrimaryMapper(
145-
resource -> Set.of(ResourceID.fromResource(glueOperator)))
146-
.build(), context.eventSourceRetriever().eventSourceContextForDynamicRegistration());
145+
resource -> Set.of(ResourceID.fromResource(glueOperator)));
146+
147+
if (spec.getParent().getLabelSelector() != null) {
148+
configBuilder.withLabelSelector(spec.getParent().getLabelSelector());
149+
}
150+
151+
es = new InformerEventSource<>(configBuilder.build(),
152+
context.eventSourceRetriever().eventSourceContextForDynamicRegistration());
147153
context.eventSourceRetriever().dynamicallyRegisterEventSource(gvk.toString(), es);
148154
}
149155
return es;

src/test/java/io/csviri/operator/glue/GlueOperatorComplexLabelSelectorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ void testGlueOperatorLabelSelector() {
5454
testCR.getKind()));
5555
assertThat(glue).isNull();
5656
});
57-
delete(go);
5857
}
5958

6059
public static class GlueOperatorComplexLabelSelectorTestProfile implements QuarkusTestProfile {

src/test/java/io/csviri/operator/glue/GlueOperatorLabelSelectorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ void testGlueOperatorLabelSelector() {
6363
testCR.getKind()));
6464
assertThat(glue).isNull();
6565
});
66-
delete(go);
6766
}
6867

6968
public static class GlueOperatorLabelSelectorTestProfile implements QuarkusTestProfile {

src/test/java/io/csviri/operator/glue/GlueOperatorTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
@QuarkusTest
2828
class GlueOperatorTest extends TestBase {
2929

30-
31-
3230
@BeforeEach
3331
void applyCRD() {
3432
TestUtils.applyTestCrd(client, TestCustomResource.class, TestCustomResource2.class);
@@ -162,6 +160,33 @@ void nonUniqueNameTest() {
162160
});
163161
}
164162

163+
@Test
164+
void parentWithLabelSelector() {
165+
create(TestUtils
166+
.loadResourceFlowOperator("/glueoperator/ParentLabelSelector.yaml"));
167+
168+
var cr = create(testCustomResource());
169+
String name = cr.getMetadata().getName();
170+
171+
await().pollDelay(TestUtils.INITIAL_RECONCILE_WAIT_TIMEOUT).untilAsserted(() -> {
172+
var cm1 = get(ConfigMap.class, name);
173+
assertThat(cm1).isNull();
174+
});
175+
176+
cr.getMetadata().getLabels().put("mylabel", "value");
177+
update(cr);
178+
179+
await().untilAsserted(() -> {
180+
var cm1 = get(ConfigMap.class, name);
181+
assertThat(cm1).isNotNull();
182+
});
183+
184+
delete(cr);
185+
await().untilAsserted(() -> {
186+
var cm1 = get(ConfigMap.class, name);
187+
assertThat(cm1).isNull();
188+
});
189+
}
165190

166191

167192
GlueOperator testWorkflowOperator() {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: io.csviri.operator.glue/v1beta1
2+
kind: GlueOperator
3+
metadata:
4+
name: templating-sample
5+
spec:
6+
parent:
7+
apiVersion: io.csviri.operator.glue/v1
8+
kind: TestCustomResource
9+
labelSelector: "mylabel=value"
10+
childResources:
11+
- name: configMap1
12+
resource:
13+
apiVersion: v1
14+
kind: ConfigMap
15+
metadata:
16+
name: "{parent.metadata.name}"
17+
data:
18+
key: "{parent.spec.value}"

0 commit comments

Comments
 (0)