15
15
*/
16
16
package io .fabric8 .kubernetes .client .dsl .internal ;
17
17
18
- import io . fabric8 . kubernetes . api . model . HasMetadata ;
19
- import io . fabric8 . kubernetes . api . model . KubernetesResource ;
20
- import io . fabric8 . kubernetes . api . model . KubernetesResourceList ;
21
- import io . fabric8 . kubernetes . api . model . ListOptions ;
22
- import io . fabric8 . kubernetes . api . model . Status ;
23
- import io .fabric8 .kubernetes .api .model .WatchEvent ;
18
+ import com . fasterxml . jackson . annotation . JsonProperty ;
19
+ import com . fasterxml . jackson . core . JsonProcessingException ;
20
+ import com . fasterxml . jackson . databind . JsonNode ;
21
+ import com . fasterxml . jackson . databind . ObjectMapper ;
22
+ import com . fasterxml . jackson . databind . node . ObjectNode ;
23
+ import io .fabric8 .kubernetes .api .model .* ;
24
24
import io .fabric8 .kubernetes .client .KubernetesClientException ;
25
25
import io .fabric8 .kubernetes .client .Watch ;
26
26
import io .fabric8 .kubernetes .client .Watcher ;
@@ -232,9 +232,31 @@ public void close() {
232
232
closeRequest ();
233
233
cancelReconnect ();
234
234
}
235
+
236
+ private WatchEvent contextAwareWatchEventDeserializer (String messageSource ) {
237
+ try {
238
+ return Serialization .unmarshal (messageSource , WatchEvent .class );
239
+ } catch (Exception ex1 ) {
240
+ try {
241
+ JsonNode json = Serialization .jsonMapper ().readTree (messageSource );
242
+ JsonNode objectJson = null ;
243
+ if (json instanceof ObjectNode && json .has ("object" )) {
244
+ objectJson = ((ObjectNode ) json ).remove ("object" );
245
+ }
246
+
247
+ WatchEvent watchEvent = Serialization .jsonMapper ().treeToValue (json , WatchEvent .class );
248
+ KubernetesResource object = Serialization .jsonMapper ().treeToValue (objectJson , baseOperation .getType ());
249
+
250
+ watchEvent .setObject (object );
251
+ return watchEvent ;
252
+ } catch (JsonProcessingException ex2 ) {
253
+ throw new IllegalArgumentException ("Failed to deserialize WatchEvent" , ex2 );
254
+ }
255
+ }
256
+ }
235
257
236
258
protected WatchEvent readWatchEvent (String messageSource ) {
237
- WatchEvent event = Serialization . unmarshal (messageSource , WatchEvent . class );
259
+ WatchEvent event = contextAwareWatchEventDeserializer (messageSource );
238
260
KubernetesResource object = null ;
239
261
if (event != null ) {
240
262
object = event .getObject ();
@@ -261,12 +283,10 @@ protected void onMessage(String message) {
261
283
try {
262
284
WatchEvent event = readWatchEvent (message );
263
285
Object object = event .getObject ();
264
- if (object instanceof HasMetadata ) {
265
- @ SuppressWarnings ("unchecked" )
266
- T obj = (T ) object ;
267
- updateResourceVersion (obj .getMetadata ().getResourceVersion ());
268
- Action action = Action .valueOf (event .getType ());
269
- eventReceived (action , obj );
286
+ if (object instanceof Status ) {
287
+ Status status = (Status ) object ;
288
+
289
+ onStatus (status );
270
290
} else if (object instanceof KubernetesResourceList ) {
271
291
// Dirty cast - should always be valid though
272
292
KubernetesResourceList list = (KubernetesResourceList ) object ;
@@ -278,10 +298,12 @@ protected void onMessage(String message) {
278
298
eventReceived (action , item );
279
299
}
280
300
}
281
- } else if (object instanceof Status ) {
282
- Status status = (Status ) object ;
283
-
284
- onStatus (status );
301
+ } else if (object instanceof HasMetadata ) {
302
+ @ SuppressWarnings ("unchecked" )
303
+ T obj = (T ) object ;
304
+ updateResourceVersion (obj .getMetadata ().getResourceVersion ());
305
+ Action action = Action .valueOf (event .getType ());
306
+ eventReceived (action , obj );
285
307
} else {
286
308
logger .error ("Unknown message received: {}" , message );
287
309
}
0 commit comments