Search in sources :

Example 1 with EventBuilder

use of io.fabric8.kubernetes.api.model.events.v1beta1.EventBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class RestartingWatcher method onClose.

@Override
default void onClose(WatcherException cause) {
    final StringWriter stringWriter = new StringWriter();
    cause.printStackTrace(new PrintWriter(stringWriter));
    String message = stringWriter.toString();
    if (message.contains("too old")) {
        Logger.getLogger(getClass().getName()).log(Level.WARNING, () -> "EntandoResourceObserver closed due to out of date resourceVersion. Reconnecting ... ");
        getRestartingAction().run();
    } else {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, cause, () -> "EntandoResourceObserver closed. Can't reconnect. The container should restart now.");
        Event event = new EventBuilder().withNewMetadata().withName(EntandoOperatorSpiConfig.getControllerPodName() + "-restart-" + NameUtils.randomNumeric(4)).addToLabels("entando-operator-restarted", "true").endMetadata().withCount(1).withFirstTimestamp(FormatUtils.format(LocalDateTime.now())).withLastTimestamp(FormatUtils.format(LocalDateTime.now())).withMessage(message).build();
        issueOperatorDeathEvent(event);
        Liveness.dead();
    }
}
Also used : EventBuilder(io.fabric8.kubernetes.api.model.EventBuilder) StringWriter(java.io.StringWriter) Event(io.fabric8.kubernetes.api.model.Event) PrintWriter(java.io.PrintWriter)

Example 2 with EventBuilder

use of io.fabric8.kubernetes.api.model.events.v1beta1.EventBuilder in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleKubernetesClient method performStatusUpdate.

private SerializedEntandoResource performStatusUpdate(SerializedEntandoResource customResource, Consumer<SerializedEntandoResource> consumer, UnaryOperator<EventBuilder> eventPopulator) {
    final EventBuilder doneableEvent = new EventBuilder().withNewMetadata().withNamespace(customResource.getMetadata().getNamespace()).withName(customResource.getMetadata().getName() + "-" + NameUtils.randomNumeric(8)).withOwnerReferences(ResourceUtils.buildOwnerReference(customResource)).withLabels(ResourceUtils.labelsFromResource(customResource)).endMetadata().withCount(1).withFirstTimestamp(dateTimeFormatter.get().format(LocalDateTime.now())).withLastTimestamp(dateTimeFormatter.get().format(LocalDateTime.now())).withNewSource(NameUtils.controllerNameOf(customResource), null).withNewInvolvedObject().withKind(customResource.getKind()).withNamespace(customResource.getMetadata().getNamespace()).withName(customResource.getMetadata().getName()).withUid(customResource.getMetadata().getUid()).withResourceVersion(customResource.getMetadata().getResourceVersion()).withApiVersion(customResource.getApiVersion()).withFieldPath("status").endInvolvedObject();
    client.v1().events().inNamespace(customResource.getMetadata().getNamespace()).create(eventPopulator.apply(doneableEvent).build());
    return ioSafe(() -> {
        SerializedEntandoResource ser = customResource;
        CustomResourceDefinitionContext definition = Optional.ofNullable(ser.getDefinition()).orElse(resolveDefinitionContext(ser));
        ser.setDefinition(definition);
        RawCustomResourceOperationsImpl resource = client.customResource(definition).inNamespace(customResource.getMetadata().getNamespace()).withName(customResource.getMetadata().getName());
        final ObjectMapper objectMapper = new ObjectMapper();
        ser = objectMapper.readValue(objectMapper.writeValueAsString(resource.get()), SerializedEntandoResource.class);
        ser.setDefinition(definition);
        consumer.accept(ser);
        final Map<String, Object> map = resource.updateStatus(objectMapper.writeValueAsString(ser));
        return objectMapper.readValue(objectMapper.writeValueAsString(map), SerializedEntandoResource.class);
    });
}
Also used : CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) RawCustomResourceOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.RawCustomResourceOperationsImpl) EventBuilder(io.fabric8.kubernetes.api.model.EventBuilder) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with EventBuilder

use of io.fabric8.kubernetes.api.model.events.v1beta1.EventBuilder in project kubernetes-client by fabric8io.

the class EventTest method testInvolvedObjectFieldQuery.

@Test
void testInvolvedObjectFieldQuery() {
    // Given
    server.expect().get().withPath("/api/v1/namespaces/ns1/events?fieldSelector=" + Utils.toUrlEncoded("involvedObject.name=foo," + "involvedObject.namespace=ns1," + "involvedObject.kind=Deployment," + "involvedObject.uid=6d71451a-f8df-11ea-a8ac-0e13a02d8ebd")).andReturn(HttpURLConnection.HTTP_OK, new EventListBuilder().withItems(new EventBuilder().withNewMetadata().withName("foo-event").endMetadata().build()).build()).once();
    // When
    EventList eventList = client.v1().events().inNamespace("ns1").withInvolvedObject(new io.fabric8.kubernetes.api.model.ObjectReferenceBuilder().withName("foo").withNamespace("ns1").withKind("Deployment").withUid("6d71451a-f8df-11ea-a8ac-0e13a02d8ebd").build()).list();
    // Then
    assertNotNull(eventList);
    assertEquals(1, eventList.getItems().size());
}
Also used : EventBuilder(io.fabric8.kubernetes.api.model.EventBuilder) EventListBuilder(io.fabric8.kubernetes.api.model.EventListBuilder) EventList(io.fabric8.kubernetes.api.model.EventList) Test(org.junit.jupiter.api.Test)

Example 4 with EventBuilder

use of io.fabric8.kubernetes.api.model.events.v1beta1.EventBuilder in project stackgres by ongres.

the class ClusterEventResourceTest method ifEventsAreCreated_itShouldReturnThenInAnArray.

@Test
void ifEventsAreCreated_itShouldReturnThenInAnArray() {
    try (KubernetesClient client = factory.create()) {
        client.v1().events().inNamespace("test-namespace").create(new EventBuilder().withNewMetadata().withNamespace("test-namespace").withName("test.1").endMetadata().withType("Normal").withMessage("Test").withLastTimestamp(DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(1))).withInvolvedObject(new ObjectReferenceBuilder().withKind(StackGresCluster.KIND).withNamespace("test-namespace").withName("test").withUid("1").build()).build());
        client.v1().events().inNamespace("test-namespace").create(new EventBuilder().withNewMetadata().withNamespace("test-namespace").withName("test.2").endMetadata().withType("Normal").withMessage("All good!").withLastTimestamp(DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(2))).withInvolvedObject(new ObjectReferenceBuilder().withKind("StatefulSet").withNamespace("test-namespace").withName("test").withUid("1").build()).build());
        client.v1().events().inNamespace("test-namespace").create(new EventBuilder().withNewMetadata().withNamespace("test-namespace").withName("test.3").endMetadata().withType("Warning").withMessage("Something wrong :(").withLastTimestamp(DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(3))).withInvolvedObject(new ObjectReferenceBuilder().withKind("Pod").withNamespace("test-namespace").withName("test-0").withUid("1").build()).build());
        client.v1().events().inNamespace("test-namespace").create(new EventBuilder().withNewMetadata().withNamespace("test-namespace").withName("test.4").endMetadata().withType("Normal").withMessage("I am here too").withLastTimestamp(DateTimeFormatter.ISO_INSTANT.format(Instant.ofEpochMilli(0))).withInvolvedObject(new ObjectReferenceBuilder().withKind(StackGresDbOps.KIND).withNamespace("test-namespace").withName("test-operation").withUid("1").build()).build());
    }
    given().when().header(AUTHENTICATION_HEADER).get("/stackgres/namespaces/test-namespace/sgclusters/test/events").then().statusCode(200).body("", Matchers.hasSize(4)).body("[0].metadata.name", Matchers.equalTo("test.4")).body("[0].type", Matchers.equalTo("Normal")).body("[0].message", Matchers.equalTo("I am here too")).body("[1].metadata.name", Matchers.equalTo("test.1")).body("[1].type", Matchers.equalTo("Normal")).body("[1].message", Matchers.equalTo("Test")).body("[2].metadata.name", Matchers.equalTo("test.2")).body("[2].type", Matchers.equalTo("Normal")).body("[2].message", Matchers.equalTo("All good!")).body("[3].metadata.name", Matchers.equalTo("test.3")).body("[3].type", Matchers.equalTo("Warning")).body("[3].message", Matchers.equalTo("Something wrong :("));
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) EventBuilder(io.fabric8.kubernetes.api.model.EventBuilder) ObjectReferenceBuilder(io.fabric8.kubernetes.api.model.ObjectReferenceBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 5 with EventBuilder

use of io.fabric8.kubernetes.api.model.events.v1beta1.EventBuilder in project flink-kubernetes-operator by apache.

the class FlinkDeploymentControllerTest method testUpgradeNotReadyCluster.

public void testUpgradeNotReadyCluster(FlinkDeployment appCluster, boolean allowUpgrade) {
    mockServer.expect().delete().withPath("/apis/apps/v1/namespaces/flink-operator-test/deployments/test-cluster").andReturn(HttpURLConnection.HTTP_CREATED, new EventBuilder().withNewMetadata().endMetadata().build()).always();
    mockServer.expect().get().withPath("/api/v1/namespaces/flink-operator-test/pods?labelSelector=app%3Dtest-cluster%2Ccomponent%3Djobmanager%2Ctype%3Dflink-native-kubernetes").andReturn(HttpURLConnection.HTTP_CREATED, new EventBuilder().withNewMetadata().endMetadata().build()).always();
    testController.reconcile(appCluster, TestUtils.createEmptyContext());
    assertEquals(appCluster.getSpec(), appCluster.getStatus().getReconciliationStatus().getLastReconciledSpec());
    flinkService.setPortReady(false);
    testController.reconcile(appCluster, context);
    assertEquals(JobManagerDeploymentStatus.DEPLOYING, appCluster.getStatus().getJobManagerDeploymentStatus());
    // trigger change
    appCluster.getSpec().setServiceAccount(appCluster.getSpec().getServiceAccount() + "-2");
    // Verify that even in DEPLOYING state we still redeploy
    testController.reconcile(appCluster, context);
    testController.reconcile(appCluster, context);
    if (allowUpgrade) {
        assertEquals(JobManagerDeploymentStatus.DEPLOYING, appCluster.getStatus().getJobManagerDeploymentStatus());
        assertEquals(appCluster.getSpec(), appCluster.getStatus().getReconciliationStatus().getLastReconciledSpec());
        flinkService.setPortReady(true);
        testController.reconcile(appCluster, context);
        testController.reconcile(appCluster, context);
        if (appCluster.getSpec().getJob() != null) {
            assertEquals("RUNNING", appCluster.getStatus().getJobStatus().getState());
        } else {
            assertNull(appCluster.getStatus().getJobStatus().getState());
        }
        assertEquals(JobManagerDeploymentStatus.READY, appCluster.getStatus().getJobManagerDeploymentStatus());
    } else {
        assertEquals(JobManagerDeploymentStatus.DEPLOYING, appCluster.getStatus().getJobManagerDeploymentStatus());
        assertNotEquals(appCluster.getSpec(), appCluster.getStatus().getReconciliationStatus().getLastReconciledSpec());
        flinkService.setPortReady(true);
        testController.reconcile(appCluster, context);
        testController.reconcile(appCluster, context);
        assertEquals(JobManagerDeploymentStatus.DEPLOYING, appCluster.getStatus().getJobManagerDeploymentStatus());
        assertEquals(appCluster.getSpec(), appCluster.getStatus().getReconciliationStatus().getLastReconciledSpec());
        testController.reconcile(appCluster, context);
        assertEquals(JobManagerDeploymentStatus.DEPLOYED_NOT_READY, appCluster.getStatus().getJobManagerDeploymentStatus());
        testController.reconcile(appCluster, context);
        assertEquals("RUNNING", appCluster.getStatus().getJobStatus().getState());
        assertEquals(JobManagerDeploymentStatus.READY, appCluster.getStatus().getJobManagerDeploymentStatus());
    }
}
Also used : EventBuilder(io.fabric8.kubernetes.api.model.EventBuilder)

Aggregations

EventBuilder (io.fabric8.kubernetes.api.model.EventBuilder)15 Test (org.junit.jupiter.api.Test)12 Event (io.fabric8.kubernetes.api.model.Event)4 ObjectReferenceBuilder (io.fabric8.kubernetes.api.model.ObjectReferenceBuilder)3 EventSourceBuilder (io.fabric8.kubernetes.api.model.EventSourceBuilder)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 EventBuilder (io.fabric8.kubernetes.api.model.events.v1.EventBuilder)2 EventBuilder (io.fabric8.kubernetes.api.model.events.v1beta1.EventBuilder)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 QuarkusTest (io.quarkus.test.junit.QuarkusTest)2 FlinkDeployment (org.apache.flink.kubernetes.operator.crd.FlinkDeployment)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 EventList (io.fabric8.kubernetes.api.model.EventList)1 EventListBuilder (io.fabric8.kubernetes.api.model.EventListBuilder)1 ObjectReference (io.fabric8.kubernetes.api.model.ObjectReference)1 OwnerReferenceBuilder (io.fabric8.kubernetes.api.model.OwnerReferenceBuilder)1 StatusBuilder (io.fabric8.kubernetes.api.model.StatusBuilder)1 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)1