1
1
package io .csviri .operator .glue .reconciler .operator ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .List ;
5
- import java .util .Map ;
6
- import java .util .Set ;
3
+ import java .util .*;
7
4
5
+ import org .eclipse .microprofile .config .inject .ConfigProperty ;
8
6
import org .slf4j .Logger ;
9
7
import org .slf4j .LoggerFactory ;
10
8
9
+ import io .csviri .operator .glue .ControllerConfig ;
10
+ import io .csviri .operator .glue .GlueException ;
11
11
import io .csviri .operator .glue .customresource .glue .Glue ;
12
12
import io .csviri .operator .glue .customresource .glue .GlueSpec ;
13
13
import io .csviri .operator .glue .customresource .glue .RelatedResourceSpec ;
25
25
import io .javaoperatorsdk .operator .processing .event .source .EventSource ;
26
26
import io .javaoperatorsdk .operator .processing .event .source .informer .InformerEventSource ;
27
27
28
+ import jakarta .annotation .PostConstruct ;
28
29
import jakarta .inject .Inject ;
29
30
30
- @ ControllerConfiguration
31
+ import static io .csviri .operator .glue .reconciler .glue .GlueReconciler .GLUE_RECONCILER_NAME ;
32
+
33
+ @ ControllerConfiguration (name = GlueOperatorReconciler .GLUE_OPERATOR_RECONCILER_NAME )
31
34
public class GlueOperatorReconciler
32
35
implements Reconciler <GlueOperator >, EventSourceInitializer <GlueOperator >,
33
36
Cleaner <GlueOperator >, ErrorStatusHandler <GlueOperator > {
@@ -37,11 +40,25 @@ public class GlueOperatorReconciler
37
40
public static final String GLUE_LABEL_KEY = "foroperator" ;
38
41
public static final String GLUE_LABEL_VALUE = "true" ;
39
42
public static final String PARENT_RELATED_RESOURCE_NAME = "parent" ;
43
+ public static final String GLUE_OPERATOR_RECONCILER_NAME = "glue-operator" ;
40
44
41
45
@ Inject
42
46
ValidationAndErrorHandler validationAndErrorHandler ;
43
47
44
- private InformerEventSource <Glue , GlueOperator > resourceFlowEventSource ;
48
+ @ ConfigProperty (name = "quarkus.operator-sdk.controllers." + GLUE_RECONCILER_NAME + ".selector" )
49
+ Optional <String > glueLabelSelector ;
50
+
51
+ @ Inject
52
+ ControllerConfig controllerConfig ;
53
+
54
+ private Map <String , String > defaultGlueLabels ;
55
+
56
+ private InformerEventSource <Glue , GlueOperator > glueEventSource ;
57
+
58
+ @ PostConstruct
59
+ void init () {
60
+ defaultGlueLabels = initDefaultLabelsToAddToGlue ();
61
+ }
45
62
46
63
@ Override
47
64
public UpdateControl <GlueOperator > reconcile (GlueOperator glueOperator ,
@@ -54,9 +71,10 @@ public UpdateControl<GlueOperator> reconcile(GlueOperator glueOperator,
54
71
55
72
var targetCREventSource = getOrRegisterCustomResourceEventSource (glueOperator , context );
56
73
targetCREventSource .list ().forEach (cr -> {
57
- var actualResourceFlow = resourceFlowEventSource
58
- .get (new ResourceID (glueName (cr ), cr .getMetadata ().getNamespace ()));
59
- var desiredResourceFlow = createResourceFlow (cr , glueOperator );
74
+ var actualResourceFlow = glueEventSource
75
+ .get (new ResourceID (glueName (cr .getMetadata ().getName (), cr .getKind ()),
76
+ cr .getMetadata ().getNamespace ()));
77
+ var desiredResourceFlow = createGlue (cr , glueOperator );
60
78
if (actualResourceFlow .isEmpty ()) {
61
79
context .getClient ().resource (desiredResourceFlow ).serverSideApply ();
62
80
} else if (!actualResourceFlow .orElseThrow ().getSpec ()
@@ -72,17 +90,22 @@ public UpdateControl<GlueOperator> reconcile(GlueOperator glueOperator,
72
90
return UpdateControl .noUpdate ();
73
91
}
74
92
75
- private Glue createResourceFlow (GenericKubernetesResource targetParentResource ,
93
+ private Glue createGlue (GenericKubernetesResource targetParentResource ,
76
94
GlueOperator glueOperator ) {
77
95
var glue = new Glue ();
78
96
79
97
glue .setMetadata (new ObjectMetaBuilder ()
80
- .withName (glueName (targetParentResource ))
98
+ .withName (
99
+ glueName (targetParentResource .getMetadata ().getName (), targetParentResource .getKind ()))
81
100
.withNamespace (targetParentResource .getMetadata ().getNamespace ())
82
101
.withLabels (Map .of (GLUE_LABEL_KEY , GLUE_LABEL_VALUE ))
83
102
.build ());
84
103
glue .setSpec (toWorkflowSpec (glueOperator .getSpec ()));
85
104
105
+ if (!defaultGlueLabels .isEmpty ()) {
106
+ glue .getMetadata ().getLabels ().putAll (defaultGlueLabels );
107
+ }
108
+
86
109
var parent = glueOperator .getSpec ().getParent ();
87
110
RelatedResourceSpec parentRelatedSpec = new RelatedResourceSpec ();
88
111
parentRelatedSpec .setName (PARENT_RELATED_RESOURCE_NAME );
@@ -129,12 +152,12 @@ private InformerEventSource<GenericKubernetesResource, GlueOperator> getOrRegist
129
152
@ Override
130
153
public Map <String , EventSource > prepareEventSources (
131
154
EventSourceContext <GlueOperator > eventSourceContext ) {
132
- resourceFlowEventSource = new InformerEventSource <>(
155
+ glueEventSource = new InformerEventSource <>(
133
156
InformerConfiguration .from (Glue .class , eventSourceContext )
134
157
.withLabelSelector (GLUE_LABEL_KEY + "=" + GLUE_LABEL_VALUE )
135
158
.build (),
136
159
eventSourceContext );
137
- return EventSourceInitializer .nameEventSources (resourceFlowEventSource );
160
+ return EventSourceInitializer .nameEventSources (glueEventSource );
138
161
}
139
162
140
163
@ Override
@@ -155,9 +178,34 @@ public DeleteControl cleanup(GlueOperator glueOperator,
155
178
return DeleteControl .defaultDelete ();
156
179
}
157
180
158
- private static String glueName (GenericKubernetesResource cr ) {
159
- return KubernetesResourceUtil .sanitizeName (cr . getMetadata (). getName () + "-" + cr . getKind () );
181
+ public static String glueName (String name , String kind ) {
182
+ return KubernetesResourceUtil .sanitizeName (name + "-" + kind );
160
183
}
161
184
185
+ private Map <String , String > initDefaultLabelsToAddToGlue () {
186
+ Map <String , String > res = new HashMap <>();
187
+ if (!controllerConfig .glueOperatorManagedGlueLabels ().isEmpty ()) {
188
+ res .putAll (controllerConfig .glueOperatorManagedGlueLabels ());
189
+ } else {
190
+ glueLabelSelector .ifPresent (ls -> {
191
+ if (ls .contains ("," ) || ls .contains ("(" )) {
192
+ throw new GlueException (
193
+ "Glue reconciler label selector contains non-simple label selector: " + ls +
194
+ ". Specify Glue label selector in simple form ('key=value' or 'key') " +
195
+ "or configure 'glue.operator.glue-operator-managed-glue-labels'" );
196
+ }
197
+ String [] labelSelectorParts = ls .split ("=" );
198
+ if (labelSelectorParts .length > 2 ) {
199
+ throw new GlueException ("Invalid label selector: " + ls );
200
+ }
201
+ if (labelSelectorParts .length == 1 ) {
202
+ res .put (labelSelectorParts [0 ], "" );
203
+ } else {
204
+ res .put (labelSelectorParts [0 ], labelSelectorParts [1 ]);
205
+ }
206
+ });
207
+ }
208
+ return res ;
209
+ }
162
210
163
211
}
0 commit comments