Search in sources :

Example 6 with CoreV1EventBuilder

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));
}
Also used : V1EventSourceBuilder(io.kubernetes.client.openapi.models.V1EventSourceBuilder) V1ObjectReference(io.kubernetes.client.openapi.models.V1ObjectReference) EventSpamFilter(io.kubernetes.client.extended.event.legacy.EventSpamFilter) EventUtils(io.kubernetes.client.extended.event.legacy.EventUtils) CoreV1EventBuilder(io.kubernetes.client.openapi.models.CoreV1EventBuilder) CoreV1Event(io.kubernetes.client.openapi.models.CoreV1Event) Test(org.junit.Test)

Example 7 with CoreV1EventBuilder

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);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) OffsetDateTime(java.time.OffsetDateTime) CoreV1EventBuilder(io.kubernetes.client.openapi.models.CoreV1EventBuilder) V1ObjectMetaBuilder(io.kubernetes.client.openapi.models.V1ObjectMetaBuilder) CoreV1Event(io.kubernetes.client.openapi.models.CoreV1Event)

Aggregations

CoreV1Event (io.kubernetes.client.openapi.models.CoreV1Event)7 CoreV1EventBuilder (io.kubernetes.client.openapi.models.CoreV1EventBuilder)7 EventUtils (io.kubernetes.client.extended.event.legacy.EventUtils)3 V1EventSourceBuilder (io.kubernetes.client.openapi.models.V1EventSourceBuilder)3 V1ObjectMetaBuilder (io.kubernetes.client.openapi.models.V1ObjectMetaBuilder)3 V1ObjectReference (io.kubernetes.client.openapi.models.V1ObjectReference)3 Test (org.junit.Test)3 V1EventSource (io.kubernetes.client.openapi.models.V1EventSource)2 V1ObjectReferenceBuilder (io.kubernetes.client.openapi.models.V1ObjectReferenceBuilder)2 OffsetDateTime (java.time.OffsetDateTime)2 V1Patch (io.kubernetes.client.custom.V1Patch)1 EventAggregator (io.kubernetes.client.extended.event.legacy.EventAggregator)1 EventLogger (io.kubernetes.client.extended.event.legacy.EventLogger)1 EventSpamFilter (io.kubernetes.client.extended.event.legacy.EventSpamFilter)1 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)1 MutablePair (org.apache.commons.lang3.tuple.MutablePair)1 Parameters (org.junit.runners.Parameterized.Parameters)1