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