use of io.kubernetes.client.openapi.models.CoreV1EventBuilder in project java by kubernetes-client.
the class EventSpamFilterTest method testSpammingEventFiltering.
@Test
public void testSpammingEventFiltering() {
int burst = 10;
EventSpamFilter filter = new EventSpamFilter(100, EventUtils::getSpamKey, burst, Duration.ofDays(1), // never refill
1);
CoreV1Event spammingEvent1 = new CoreV1EventBuilder().withMessage("1").withSource(new V1EventSourceBuilder().withComponent("c1").build()).withInvolvedObject(new V1ObjectReference()).build();
CoreV1Event spammingEvent2 = new CoreV1EventBuilder().withMessage("2").withSource(new V1EventSourceBuilder().withComponent("c1").build()).withInvolvedObject(new V1ObjectReference()).build();
CoreV1Event spammingEvent3 = new CoreV1EventBuilder().withMessage("3").withSource(new V1EventSourceBuilder().withComponent("c2").build()).withInvolvedObject(new V1ObjectReference()).build();
for (int i = 0; i < burst; i++) {
assertEquals(true, filter.filter(spammingEvent1));
}
assertEquals(false, filter.filter(spammingEvent1));
assertEquals(false, filter.filter(spammingEvent2));
assertEquals(true, filter.filter(spammingEvent3));
}
use of io.kubernetes.client.openapi.models.CoreV1EventBuilder in project java by kubernetes-client.
the class EventAggregator method aggregate.
public synchronized MutablePair<CoreV1Event, String> aggregate(CoreV1Event event) {
OffsetDateTime now = OffsetDateTime.now();
MutablePair<String, String> aggregatedKeys = keyFunc.apply(event);
String aggregatedKey = aggregatedKeys.getLeft();
String localKey = aggregatedKeys.getRight();
AggregatedRecord record = this.spammingCache.get(aggregatedKey, k -> new AggregatedRecord());
record.lastTimestamp = now;
record.localKeys.add(localKey);
if (record.localKeys.size() < this.maxEvents) {
this.spammingCache.put(aggregatedKey, record);
return new MutablePair<>(event, EventUtils.getEventKey(event));
}
// remove any keys
record.localKeys.remove(record.localKeys.stream().findAny().get());
CoreV1Event aggregatedEvent = new CoreV1EventBuilder(event).withMetadata(new V1ObjectMetaBuilder().withName(EventUtils.generateName(event.getInvolvedObject().getName(), now)).withNamespace(event.getInvolvedObject().getNamespace()).build()).withCount(1).withFirstTimestamp(now).withLastTimestamp(now).withMessage(this.messageFunc.apply(event)).build();
this.spammingCache.put(aggregatedKey, record);
return new MutablePair<>(aggregatedEvent, aggregatedKey);
}
Aggregations