6
6
import io .fabric8 .kubernetes .api .model .ConfigMapBuilder ;
7
7
import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
8
8
import io .fabric8 .kubernetes .client .KubernetesClient ;
9
+ import io .javaoperatorsdk .operator .api .config .informer .InformerConfiguration ;
9
10
import io .javaoperatorsdk .operator .api .reconciler .*;
10
11
import io .javaoperatorsdk .operator .junit .KubernetesClientAware ;
12
+ import io .javaoperatorsdk .operator .processing .event .source .EventSource ;
13
+ import io .javaoperatorsdk .operator .processing .event .source .informer .InformerEventSource ;
14
+ import io .javaoperatorsdk .operator .processing .event .source .informer .Mappers ;
11
15
12
16
@ ControllerConfiguration
13
17
public class ClusterScopedCustomResourceReconciler
14
- implements Reconciler <ClusterScopedCustomResource >, Cleaner < ClusterScopedCustomResource >,
15
- KubernetesClientAware {
18
+ implements Reconciler <ClusterScopedCustomResource >,
19
+ KubernetesClientAware , EventSourceInitializer < ClusterScopedCustomResource > {
16
20
17
21
public static final String DATA_KEY = "data-key" ;
18
22
23
+ public static final String TEST_LABEL_VALUE = "clusterscopecrtest" ;
24
+ public static final String TEST_LABEL_KEY = "test" ;
25
+
19
26
private KubernetesClient client ;
20
27
21
28
@ Override
22
29
public UpdateControl <ClusterScopedCustomResource > reconcile (
23
30
ClusterScopedCustomResource resource , Context <ClusterScopedCustomResource > context ) {
24
31
25
- client .configMaps ().resource (desired (resource )).createOrReplace ();
32
+ var optionalConfigMap = context .getSecondaryResource (ConfigMap .class );
33
+
34
+ optionalConfigMap .ifPresentOrElse (cm -> {
35
+ if (!resource .getSpec ().getData ().equals (cm .getData ().get (DATA_KEY ))) {
36
+ client .configMaps ().resource (desired (resource )).replace ();
37
+ }
38
+ }, () -> client .configMaps ().resource (desired (resource )).create ());
26
39
27
40
resource .setStatus (new ClusterScopedCustomResourceStatus ());
28
41
resource .getStatus ().setCreated (true );
29
42
return UpdateControl .patchStatus (resource );
30
43
}
31
44
32
45
private ConfigMap desired (ClusterScopedCustomResource resource ) {
33
- return new ConfigMapBuilder ()
46
+ var cm = new ConfigMapBuilder ()
34
47
.withMetadata (new ObjectMetaBuilder ()
35
48
.withName (resource .getMetadata ().getName ())
36
49
.withNamespace (resource .getSpec ().getTargetNamespace ())
50
+ .withLabels (Map .of (TEST_LABEL_KEY , TEST_LABEL_VALUE ))
37
51
.build ())
38
52
.withData (Map .of (DATA_KEY , resource .getSpec ().getData ()))
39
53
.build ();
54
+ cm .addOwnerReference (resource );
55
+ return cm ;
40
56
}
41
57
42
58
@ Override
@@ -50,9 +66,12 @@ public void setKubernetesClient(KubernetesClient kubernetesClient) {
50
66
}
51
67
52
68
@ Override
53
- public DeleteControl cleanup (ClusterScopedCustomResource resource ,
54
- Context <ClusterScopedCustomResource > context ) {
55
- client .configMaps ().resource (desired (resource )).delete ();
56
- return DeleteControl .defaultDelete ();
69
+ public Map <String , EventSource > prepareEventSources (
70
+ EventSourceContext <ClusterScopedCustomResource > context ) {
71
+ var ies = new InformerEventSource <>(InformerConfiguration .from (ConfigMap .class , context )
72
+ .withSecondaryToPrimaryMapper (Mappers .fromOwnerReference (true ))
73
+ .withLabelSelector (TEST_LABEL_KEY + "=" + TEST_LABEL_VALUE )
74
+ .build (), context );
75
+ return EventSourceInitializer .nameEventSources (ies );
57
76
}
58
77
}
0 commit comments