Search in sources :

Example 1 with V1ObjectMetaBuilder

use of io.kubernetes.client.openapi.models.V1ObjectMetaBuilder in project java by kubernetes-client.

the class ObjectReferenceResolvingEventRecorder method generateEvent.

private void generateEvent(V1ObjectReference eventRef, Map<String, String> annotations, EventType t, String reason, String message) {
    // defaulting event namespace for cluster-scoped resources..
    String namespace = eventRef.getNamespace();
    if (Strings.isNullOrEmpty(eventRef.getNamespace())) {
        namespace = "default";
    }
    // build event
    OffsetDateTime now = OffsetDateTime.now();
    CoreV1Event event = new CoreV1EventBuilder().withMetadata(new V1ObjectMetaBuilder().withName(EventUtils.generateName(eventRef.getName(), now)).withNamespace(namespace).withAnnotations(annotations).build()).withInvolvedObject(eventRef).withType(t.toString()).withReason(reason).withMessage(message).withFirstTimestamp(now).withLastTimestamp(now).withSource(this.eventSource).withCount(1).build();
    // fire event
    try {
        this.pendingEventQueue.offer(event, 100, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        // the queue is full, drop the event..
        logger.warn("the event queue is full, dropping requesting event upon {}/{}", eventRef.getNamespace(), eventRef.getName());
    }
}
Also used : 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)

Example 2 with V1ObjectMetaBuilder

use of io.kubernetes.client.openapi.models.V1ObjectMetaBuilder in project java by kubernetes-client.

the class EventCorrelatorTest method data.

// creates the test data
@Parameters
public static Collection<Object[]> data() {
    V1ObjectReference podRef = new V1ObjectReferenceBuilder().withApiVersion("v1").withName("my-pod").withNamespace("default").build();
    V1EventSource source = new V1EventSourceBuilder().withComponent("foo").withHost("bar").build();
    CoreV1Event firstEvent = new CoreV1EventBuilder().withMetadata(new V1ObjectMetaBuilder().withName("").withNamespace("").build()).withSource(source).withInvolvedObject(podRef).withReason("first").withCount(1).withMessage("am a first").build();
    CoreV1Event duplicatedEvent = new CoreV1EventBuilder().withMetadata(new V1ObjectMetaBuilder().withName("").withNamespace("").build()).withSource(source).withInvolvedObject(podRef).withReason("duplicated").withCount(1).withMessage("am a dup").build();
    CoreV1Event similarEvent = new CoreV1EventBuilder().withMetadata(new V1ObjectMetaBuilder().withName("").withNamespace("").build()).withSource(source).withInvolvedObject(podRef).withReason("similar").withCount(1).withMessage("am a similar").build();
    CoreV1Event aggregateEvent = new CoreV1EventBuilder().withMetadata(new V1ObjectMetaBuilder().withName("").withNamespace("").build()).withSource(source).withInvolvedObject(podRef).withReason("similar").withCount(1).withMessage(EventUtils.getAggregatedMessageByReason(similarEvent)).build();
    Object[][] data = new Object[][] { { // create a single event should work
    new CoreV1Event[] {}, deepCopy(firstEvent), deepCopy(firstEvent).count(1), false }, { // the same event should just count
    new CoreV1Event[] { deepCopy(duplicatedEvent) }, deepCopy(duplicatedEvent), deepCopy(duplicatedEvent).count(2), false }, { // the same event should just count even if more than aggregate
    deepCopyFor(duplicatedEvent, EventAggregator.DEFAULT_MAX_EVENT_LOCAL_KEYS), deepCopy(duplicatedEvent), deepCopy(duplicatedEvent).count(EventAggregator.DEFAULT_MAX_EVENT_LOCAL_KEYS + 1), false }, { // the same event is spam if too frequent
    deepCopyFor(duplicatedEvent, EventSpamFilter.DEFAULT_TOKEN_BUCKET_CAPACITY + 1), deepCopy(duplicatedEvent), null, true }, { // similar events should be aggregated
    deepCopyWithSimilarMessageFor(similarEvent, similarEvent.getMessage(), EventAggregator.DEFAULT_MAX_EVENT_LOCAL_KEYS - 1), deepCopy(similarEvent), deepCopy(aggregateEvent), false }, { // similar events many times should count the aggregate
    deepCopyWithSimilarMessageFor(similarEvent, similarEvent.getMessage(), EventAggregator.DEFAULT_MAX_EVENT_LOCAL_KEYS), deepCopy(similarEvent), deepCopy(aggregateEvent).count(2), false } };
    return Arrays.asList(data);
}
Also used : V1EventSourceBuilder(io.kubernetes.client.openapi.models.V1EventSourceBuilder) V1ObjectReference(io.kubernetes.client.openapi.models.V1ObjectReference) V1ObjectReferenceBuilder(io.kubernetes.client.openapi.models.V1ObjectReferenceBuilder) V1EventSource(io.kubernetes.client.openapi.models.V1EventSource) CoreV1EventBuilder(io.kubernetes.client.openapi.models.CoreV1EventBuilder) V1ObjectMetaBuilder(io.kubernetes.client.openapi.models.V1ObjectMetaBuilder) CoreV1Event(io.kubernetes.client.openapi.models.CoreV1Event) Parameters(org.junit.runners.Parameterized.Parameters)

Example 3 with V1ObjectMetaBuilder

use of io.kubernetes.client.openapi.models.V1ObjectMetaBuilder in project java by kubernetes-client.

the class EventCorrelatorTest method testEventCorrelate.

@Test
public void testEventCorrelate() throws InterruptedException {
    EventCorrelator correlator = new EventCorrelator();
    for (CoreV1Event event : previousEvents) {
        OffsetDateTime now = OffsetDateTime.now();
        event.setFirstTimestamp(now);
        event.setLastTimestamp(now);
        Optional<MutablePair<CoreV1Event, V1Patch>> result = correlator.correlate(event);
        if (!result.isPresent()) {
            correlator.updateState(event);
        }
    }
    Thread.sleep(100);
    OffsetDateTime now = OffsetDateTime.now();
    newEvent.setFirstTimestamp(now);
    newEvent.setLastTimestamp(now);
    Optional<MutablePair<CoreV1Event, V1Patch>> result = correlator.correlate(newEvent);
    assertEquals(expectedSkip, !result.isPresent());
    if (!expectedSkip) {
        CoreV1Event correlatedEvent = result.get().getLeft();
        correlatedEvent.setMetadata(new V1ObjectMetaBuilder().withName("").withNamespace("").build());
        validateEvent(expectedEvent, correlatedEvent);
    }
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) EventCorrelator(io.kubernetes.client.extended.event.legacy.EventCorrelator) OffsetDateTime(java.time.OffsetDateTime) V1ObjectMetaBuilder(io.kubernetes.client.openapi.models.V1ObjectMetaBuilder) CoreV1Event(io.kubernetes.client.openapi.models.CoreV1Event) Test(org.junit.Test)

Example 4 with V1ObjectMetaBuilder

use of io.kubernetes.client.openapi.models.V1ObjectMetaBuilder 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)4 V1ObjectMetaBuilder (io.kubernetes.client.openapi.models.V1ObjectMetaBuilder)4 CoreV1EventBuilder (io.kubernetes.client.openapi.models.CoreV1EventBuilder)3 OffsetDateTime (java.time.OffsetDateTime)3 MutablePair (org.apache.commons.lang3.tuple.MutablePair)2 EventCorrelator (io.kubernetes.client.extended.event.legacy.EventCorrelator)1 V1EventSource (io.kubernetes.client.openapi.models.V1EventSource)1 V1EventSourceBuilder (io.kubernetes.client.openapi.models.V1EventSourceBuilder)1 V1ObjectReference (io.kubernetes.client.openapi.models.V1ObjectReference)1 V1ObjectReferenceBuilder (io.kubernetes.client.openapi.models.V1ObjectReferenceBuilder)1 Test (org.junit.Test)1 Parameters (org.junit.runners.Parameterized.Parameters)1