3
3
import java .util .Collections ;
4
4
import java .util .Map ;
5
5
6
+ import io .fabric8 .kubernetes .api .model .HasMetadata ;
7
+ import io .javaoperatorsdk .operator .api .reconciler .Context ;
6
8
import io .javaoperatorsdk .operator .api .reconciler .RetryInfo ;
7
9
import io .javaoperatorsdk .operator .processing .event .Event ;
8
10
import io .javaoperatorsdk .operator .processing .event .ResourceID ;
9
11
12
+ /**
13
+ * An interface that metrics providers can implement and that the SDK will call at different times
14
+ * of its execution cycle.
15
+ */
10
16
public interface Metrics {
17
+
18
+ /**
19
+ * The default Metrics provider: a no-operation implementation.
20
+ */
11
21
Metrics NOOP = new Metrics () {};
12
22
13
- default void receivedEvent (Event event ) {}
23
+ /**
24
+ * Called when an event has been accepted by the SDK from an event source, which would result in
25
+ * potentially triggering the associated Reconciler.
26
+ *
27
+ * @param event the event
28
+ * @param metadata metadata associated with the resource being processed
29
+ * @see io.javaoperatorsdk.operator.api.reconciler.Context#metadataFor(HasMetadata)
30
+ */
31
+ default void receivedEvent (Event event , Map <String , Object > metadata ) {}
32
+
33
+ /**
34
+ *
35
+ * @deprecated Use (and implement) {@link #receivedEvent(Event, Map)} instead
36
+ */
37
+ @ Deprecated
38
+ default void receivedEvent (Event event ) {
39
+ receivedEvent (event , Collections .emptyMap ());
40
+ }
14
41
15
42
/**
16
43
*
@@ -22,6 +49,14 @@ default void reconcileCustomResource(ResourceID resourceID, RetryInfo retryInfo)
22
49
reconcileCustomResource (resourceID , retryInfo , Collections .emptyMap ());
23
50
}
24
51
52
+ /**
53
+ * Called right before a resource is dispatched to the ExecutorService for reconciliation.
54
+ *
55
+ * @param resourceID the {@link ResourceID} associated with the resource
56
+ * @param retryInfo the current retry state information for the reconciliation request
57
+ * @param metadata metadata associated with the resource being processed
58
+ * @see io.javaoperatorsdk.operator.api.reconciler.Context#metadataFor(HasMetadata)
59
+ */
25
60
default void reconcileCustomResource (ResourceID resourceID , RetryInfo retryInfo ,
26
61
Map <String , Object > metadata ) {}
27
62
@@ -35,6 +70,16 @@ default void failedReconciliation(ResourceID resourceID, Exception exception) {
35
70
failedReconciliation (resourceID , exception , Collections .emptyMap ());
36
71
}
37
72
73
+ /**
74
+ * Called when a precedent reconciliation for the resource associated with the specified
75
+ * {@link ResourceID} resulted in the provided exception, resulting in a retry of the
76
+ * reconciliation.
77
+ *
78
+ * @param resourceID the {@link ResourceID} associated with the resource being processed
79
+ * @param exception the exception that caused the failed reconciliation resulting in a retry
80
+ * @param metadata metadata associated with the resource being processed
81
+ * @see io.javaoperatorsdk.operator.api.reconciler.Context#metadataFor(HasMetadata)
82
+ */
38
83
default void failedReconciliation (ResourceID resourceID , Exception exception ,
39
84
Map <String , Object > metadata ) {}
40
85
@@ -47,6 +92,14 @@ default void cleanupDoneFor(ResourceID resourceID) {
47
92
cleanupDoneFor (resourceID , Collections .emptyMap ());
48
93
}
49
94
95
+ /**
96
+ * Called when the resource associated with the specified {@link ResourceID} has been successfully
97
+ * deleted and the clean-up performed by the associated reconciler is finished.
98
+ *
99
+ * @param resourceID the {@link ResourceID} associated with the resource being processed
100
+ * @param metadata metadata associated with the resource being processed
101
+ * @see io.javaoperatorsdk.operator.api.reconciler.Context#metadataFor(HasMetadata)
102
+ */
50
103
default void cleanupDoneFor (ResourceID resourceID , Map <String , Object > metadata ) {}
51
104
52
105
/**
@@ -58,27 +111,108 @@ default void finishedReconciliation(ResourceID resourceID) {
58
111
finishedReconciliation (resourceID , Collections .emptyMap ());
59
112
}
60
113
114
+ /**
115
+ * Called when the
116
+ * {@link io.javaoperatorsdk.operator.api.reconciler.Reconciler#reconcile(HasMetadata, Context)}
117
+ * method of the Reconciler associated with the resource associated with the specified
118
+ * {@link ResourceID} has sucessfully finished.
119
+ *
120
+ * @param resourceID the {@link ResourceID} associated with the resource being processed
121
+ * @param metadata metadata associated with the resource being processed
122
+ * @see io.javaoperatorsdk.operator.api.reconciler.Context#metadataFor(HasMetadata)
123
+ */
61
124
default void finishedReconciliation (ResourceID resourceID , Map <String , Object > metadata ) {}
62
125
63
-
126
+ /**
127
+ * Encapsulates the information about a controller execution i.e. a call to either
128
+ * {@link io.javaoperatorsdk.operator.api.reconciler.Reconciler#reconcile(HasMetadata, Context)}
129
+ * or {@link io.javaoperatorsdk.operator.api.reconciler.Cleaner#cleanup(HasMetadata, Context)}.
130
+ * Note that instances are automatically created for you by the SDK and passed to your Metrics
131
+ * implementation at the appropriate time to the
132
+ * {@link #timeControllerExecution(ControllerExecution)} method.
133
+ *
134
+ * @param <T> the outcome type associated with the controller execution. Currently, one of
135
+ * {@link io.javaoperatorsdk.operator.api.reconciler.UpdateControl} or
136
+ * {@link io.javaoperatorsdk.operator.api.reconciler.DeleteControl}
137
+ */
64
138
interface ControllerExecution <T > {
139
+
140
+ /**
141
+ * Retrieves the name of type of reconciliation being performed: either {@code reconcile} or
142
+ * {@code cleanup}.
143
+ *
144
+ * @return the name of type of reconciliation being performed
145
+ */
65
146
String name ();
66
147
148
+ /**
149
+ * Retrieves the name of the controller executing the reconciliation.
150
+ *
151
+ * @return the associated controller name
152
+ */
67
153
String controllerName ();
68
154
155
+ /**
156
+ * Retrieves the name of the successful result when the reconciliation ended positively.
157
+ * Possible values comes from the different outcomes provided by
158
+ * {@link io.javaoperatorsdk.operator.api.reconciler.UpdateControl} or
159
+ * {@link io.javaoperatorsdk.operator.api.reconciler.DeleteControl}.
160
+ *
161
+ * @param result the reconciliation result
162
+ * @return a name associated with the specified outcome
163
+ */
69
164
String successTypeName (T result );
70
165
166
+ /**
167
+ * Retrieves the {@link ResourceID} of the resource associated with the controller execution
168
+ * being considered
169
+ *
170
+ * @return the {@link ResourceID} of the resource being reconciled
171
+ */
71
172
ResourceID resourceID ();
72
173
174
+ /**
175
+ * Retrieves metadata associated with the current reconciliation, typically additional
176
+ * information (such as kind) about the resource being reconciled
177
+ *
178
+ * @return metadata associated with the current reconciliation
179
+ */
73
180
Map <String , Object > metadata ();
74
181
182
+ /**
183
+ * Performs the controller execution.
184
+ *
185
+ * @return the result of the controller execution
186
+ * @throws Exception if an error occurred during the controller's execution
187
+ */
75
188
T execute () throws Exception ;
76
189
}
77
190
191
+ /**
192
+ * Times the execution of the controller operation encapsulated by the provided
193
+ * {@link ControllerExecution}.
194
+ *
195
+ * @param execution the controller operation to be timed
196
+ * @return the result of the controller's execution if successful
197
+ * @param <T> the type of the outcome/result of the controller's execution
198
+ * @throws Exception if an error occurred during the controller's execution, usually this should
199
+ * just be a pass-through of whatever the controller returned
200
+ */
78
201
default <T > T timeControllerExecution (ControllerExecution <T > execution ) throws Exception {
79
202
return execution .execute ();
80
203
}
81
204
205
+ /**
206
+ * Monitors the size of the specified map. This currently isn't used directly by the SDK but could
207
+ * be used by operators to monitor some of their structures, such as cache size.
208
+ *
209
+ * @param map the Map which size is to be monitored
210
+ * @param name the name of the provided Map to be used in metrics data
211
+ * @return the Map that was passed in so the registration can be done as part of an assignment
212
+ * statement.
213
+ * @param <T> the type of the Map being monitored
214
+ */
215
+ @ SuppressWarnings ("unused" )
82
216
default <T extends Map <?, ?>> T monitorSizeOf (T map , String name ) {
83
217
return map ;
84
218
}
0 commit comments