Search in sources :

Example 1 with V1EventSource

use of io.kubernetes.client.openapi.models.V1EventSource 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 2 with V1EventSource

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

the class EventAggregatorTest method testSpammingEventsShouldBeAggregated.

@Test
public void testSpammingEventsShouldBeAggregated() {
    String aggregatedMessage = "noxu";
    EventAggregator aggregator = new EventAggregator(100, EventUtils::getAggregatedAndLocalKeyByReason, event -> aggregatedMessage);
    for (int i = 0; i < aggregator.getMaxEvents() - 1; i++) {
        String message = aggregator.aggregate(new CoreV1EventBuilder().withSource(new V1EventSource()).withInvolvedObject(new V1ObjectReference()).withMessage("foo: " + i).build()).getLeft().getMessage();
        assertNotEquals(aggregatedMessage, message);
    }
    CoreV1Event aggregatedEvent = aggregator.aggregate(new CoreV1EventBuilder().withSource(new V1EventSource()).withInvolvedObject(new V1ObjectReference()).withMessage("not_noxu").build()).getLeft();
    assertEquals(aggregatedMessage, aggregatedEvent.getMessage());
}
Also used : V1ObjectReference(io.kubernetes.client.openapi.models.V1ObjectReference) EventAggregator(io.kubernetes.client.extended.event.legacy.EventAggregator) EventUtils(io.kubernetes.client.extended.event.legacy.EventUtils) V1EventSource(io.kubernetes.client.openapi.models.V1EventSource) CoreV1EventBuilder(io.kubernetes.client.openapi.models.CoreV1EventBuilder) CoreV1Event(io.kubernetes.client.openapi.models.CoreV1Event) Test(org.junit.Test)

Example 3 with V1EventSource

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

the class ControllerExample method main.

public static void main(String[] args) throws IOException {
    CoreV1Api coreV1Api = new CoreV1Api();
    ApiClient apiClient = coreV1Api.getApiClient();
    OkHttpClient httpClient = apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
    apiClient.setHttpClient(httpClient);
    // instantiating an informer-factory, and there should be only one informer-factory
    // globally.
    SharedInformerFactory informerFactory = new SharedInformerFactory();
    // registering node-informer into the informer-factory.
    SharedIndexInformer<V1Node> nodeInformer = informerFactory.sharedIndexInformerFor((CallGeneratorParams params) -> {
        return coreV1Api.listNodeCall(null, null, null, null, null, null, params.resourceVersion, null, params.timeoutSeconds, params.watch, null);
    }, V1Node.class, V1NodeList.class);
    informerFactory.startAllRegisteredInformers();
    EventBroadcaster eventBroadcaster = new LegacyEventBroadcaster(coreV1Api);
    // nodeReconciler prints node information on events
    NodePrintingReconciler nodeReconciler = new NodePrintingReconciler(nodeInformer, eventBroadcaster.newRecorder(new V1EventSource().host("localhost").component("node-printer")));
    // Use builder library to construct a default controller.
    Controller controller = ControllerBuilder.defaultBuilder(informerFactory).watch((workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Node.class, workQueue).withWorkQueueKeyFunc((V1Node node) -> new Request(node.getMetadata().getName())).withOnAddFilter((V1Node createdNode) -> createdNode.getMetadata().getName().startsWith(// optional, set onAdd filter
    "docker-")).withOnUpdateFilter((V1Node oldNode, V1Node newNode) -> newNode.getMetadata().getName().startsWith(// optional, set onUpdate filter
    "docker-")).withOnDeleteFilter((V1Node deletedNode, Boolean stateUnknown) -> deletedNode.getMetadata().getName().startsWith(// optional, set onDelete filter
    "docker-")).build()).withReconciler(// required, set the actual reconciler
    nodeReconciler).withName(// optional, set name for controller
    "node-printing-controller").withWorkerCount(// optional, set worker thread count
    4).withReadyFunc(// optional, only starts controller when the
    nodeInformer::hasSynced).build();
    // Use builder library to manage one or multiple controllers.
    ControllerManager controllerManager = ControllerBuilder.controllerManagerBuilder(informerFactory).addController(controller).build();
    LeaderElectingController leaderElectingController = new LeaderElectingController(new LeaderElector(new LeaderElectionConfig(new EndpointsLock("kube-system", "leader-election", "foo"), Duration.ofMillis(10000), Duration.ofMillis(8000), Duration.ofMillis(5000))), controllerManager);
    leaderElectingController.run();
}
Also used : EventBroadcaster(io.kubernetes.client.extended.event.legacy.EventBroadcaster) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) V1NodeList(io.kubernetes.client.openapi.models.V1NodeList) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) Result(io.kubernetes.client.extended.controller.reconciler.Result) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) V1Node(io.kubernetes.client.openapi.models.V1Node) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) ApiClient(io.kubernetes.client.openapi.ApiClient) Request(io.kubernetes.client.extended.controller.reconciler.Request) Lister(io.kubernetes.client.informer.cache.Lister) ControllerManager(io.kubernetes.client.extended.controller.ControllerManager) SharedIndexInformer(io.kubernetes.client.informer.SharedIndexInformer) Duration(java.time.Duration) EventRecorder(io.kubernetes.client.extended.event.legacy.EventRecorder) EventBroadcaster(io.kubernetes.client.extended.event.legacy.EventBroadcaster) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) EventType(io.kubernetes.client.extended.event.EventType) EndpointsLock(io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock) Controller(io.kubernetes.client.extended.controller.Controller) ControllerBuilder(io.kubernetes.client.extended.controller.builder.ControllerBuilder) V1EventSource(io.kubernetes.client.openapi.models.V1EventSource) IOException(java.io.IOException) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) OkHttpClient(okhttp3.OkHttpClient) V1Node(io.kubernetes.client.openapi.models.V1Node) Request(io.kubernetes.client.extended.controller.reconciler.Request) ControllerManager(io.kubernetes.client.extended.controller.ControllerManager) ApiClient(io.kubernetes.client.openapi.ApiClient) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) Controller(io.kubernetes.client.extended.controller.Controller) CallGeneratorParams(io.kubernetes.client.util.CallGeneratorParams) LeaderElectingController(io.kubernetes.client.extended.controller.LeaderElectingController) LeaderElectionConfig(io.kubernetes.client.extended.leaderelection.LeaderElectionConfig) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) EndpointsLock(io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock) V1EventSource(io.kubernetes.client.openapi.models.V1EventSource) LeaderElector(io.kubernetes.client.extended.leaderelection.LeaderElector) LegacyEventBroadcaster(io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster) CoreV1Api(io.kubernetes.client.openapi.apis.CoreV1Api)

Aggregations

V1EventSource (io.kubernetes.client.openapi.models.V1EventSource)3 CoreV1Event (io.kubernetes.client.openapi.models.CoreV1Event)2 CoreV1EventBuilder (io.kubernetes.client.openapi.models.CoreV1EventBuilder)2 V1ObjectReference (io.kubernetes.client.openapi.models.V1ObjectReference)2 Controller (io.kubernetes.client.extended.controller.Controller)1 ControllerManager (io.kubernetes.client.extended.controller.ControllerManager)1 LeaderElectingController (io.kubernetes.client.extended.controller.LeaderElectingController)1 ControllerBuilder (io.kubernetes.client.extended.controller.builder.ControllerBuilder)1 Reconciler (io.kubernetes.client.extended.controller.reconciler.Reconciler)1 Request (io.kubernetes.client.extended.controller.reconciler.Request)1 Result (io.kubernetes.client.extended.controller.reconciler.Result)1 EventType (io.kubernetes.client.extended.event.EventType)1 EventAggregator (io.kubernetes.client.extended.event.legacy.EventAggregator)1 EventBroadcaster (io.kubernetes.client.extended.event.legacy.EventBroadcaster)1 EventRecorder (io.kubernetes.client.extended.event.legacy.EventRecorder)1 EventUtils (io.kubernetes.client.extended.event.legacy.EventUtils)1 LegacyEventBroadcaster (io.kubernetes.client.extended.event.legacy.LegacyEventBroadcaster)1 LeaderElectionConfig (io.kubernetes.client.extended.leaderelection.LeaderElectionConfig)1 LeaderElector (io.kubernetes.client.extended.leaderelection.LeaderElector)1 EndpointsLock (io.kubernetes.client.extended.leaderelection.resourcelock.EndpointsLock)1