eventObserve records an event, or updates an existing one if key is a cache hit
(newEvent *v1.Event, key string)
| 315 | |
| 316 | // eventObserve records an event, or updates an existing one if key is a cache hit |
| 317 | func (e *eventLogger) eventObserve(newEvent *v1.Event, key string) (*v1.Event, []byte, error) { |
| 318 | var ( |
| 319 | patch []byte |
| 320 | err error |
| 321 | ) |
| 322 | eventCopy := *newEvent |
| 323 | event := &eventCopy |
| 324 | |
| 325 | e.Lock() |
| 326 | defer e.Unlock() |
| 327 | |
| 328 | // Check if there is an existing event we should update |
| 329 | lastObservation := e.lastEventObservationFromCache(key) |
| 330 | |
| 331 | // If we found a result, prepare a patch |
| 332 | if lastObservation.count > 0 { |
| 333 | // update the event based on the last observation so patch will work as desired |
| 334 | event.Name = lastObservation.name |
| 335 | event.ResourceVersion = lastObservation.resourceVersion |
| 336 | event.FirstTimestamp = lastObservation.firstTimestamp |
| 337 | event.Count = int32(lastObservation.count) + 1 |
| 338 | |
| 339 | eventCopy2 := *event |
| 340 | eventCopy2.Count = 0 |
| 341 | eventCopy2.LastTimestamp = metav1.NewTime(time.Unix(0, 0)) |
| 342 | eventCopy2.Message = "" |
| 343 | |
| 344 | newData, _ := json.Marshal(event) |
| 345 | oldData, _ := json.Marshal(eventCopy2) |
| 346 | patch, err = strategicpatch.CreateTwoWayMergePatch(oldData, newData, event) |
| 347 | } |
| 348 | |
| 349 | // record our new observation |
| 350 | e.cache.Add( |
| 351 | key, |
| 352 | eventLog{ |
| 353 | count: uint(event.Count), |
| 354 | firstTimestamp: event.FirstTimestamp, |
| 355 | name: event.Name, |
| 356 | resourceVersion: event.ResourceVersion, |
| 357 | }, |
| 358 | ) |
| 359 | return event, patch, err |
| 360 | } |
| 361 | |
| 362 | // updateState updates its internal tracking information based on latest server state |
| 363 | func (e *eventLogger) updateState(event *v1.Event) { |
no test coverage detected