Search in sources :

Example 1 with CakeList

use of it.fvaleri.integ.model.v1alpha1.CakeList in project integ-examples by fvaleri.

the class CakeOperator method main.

public static void main(String[] args) {
    try (KubernetesClient client = new DefaultKubernetesClient()) {
        String namespace = client.getNamespace();
        if (namespace == null) {
            LOG.warn("No namespace found via config, assuming default");
            namespace = "default";
        }
        LOG.info("Using namespace {}", namespace);
        OperationContext namespaced = new OperationContext().withNamespace(namespace);
        // an infomer creates a watcher for a specific resource type and caches related events
        // you can subscribe to these events by registering a resource event handler
        SharedInformerFactory informerFactory = client.informers();
        SharedIndexInformer<Pod> podSharedIndexInformer = informerFactory.sharedIndexInformerFor(Pod.class, namespaced, RESYNC_PERIOD_MS);
        SharedIndexInformer<Cake> cakeSharedIndexInformer = informerFactory.sharedIndexInformerForCustomResource(Cake.class, namespaced, RESYNC_PERIOD_MS);
        LOG.debug("Informer factories initialized");
        MixedOperation<Cake, CakeList, Resource<Cake>> cakeClient = client.customResources(Cake.class, CakeList.class);
        CakeController cakeController = new CakeController(client, cakeClient, podSharedIndexInformer, cakeSharedIndexInformer, namespace);
        LOG.debug("Controller initialized");
        cakeController.create();
        informerFactory.startAllRegisteredInformers();
        informerFactory.addSharedInformerEventListener(e -> LOG.error("Exception occurred", e));
        cakeController.run();
    } catch (KubernetesClientException e) {
        LOG.error("Kubernetes client exception: {}", e.getMessage());
    }
}
Also used : OperationContext(io.fabric8.kubernetes.client.dsl.base.OperationContext) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) CakeList(it.fvaleri.integ.model.v1alpha1.CakeList) Pod(io.fabric8.kubernetes.api.model.Pod) Cake(it.fvaleri.integ.model.v1alpha1.Cake) Resource(io.fabric8.kubernetes.client.dsl.Resource) CakeController(it.fvaleri.integ.controller.CakeController) SharedInformerFactory(io.fabric8.kubernetes.client.informers.SharedInformerFactory) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with CakeList

use of it.fvaleri.integ.model.v1alpha1.CakeList in project integ-examples by fvaleri.

the class CakeControllerTest method testReconcile.

@Test
@DisplayName("Should create requested number of pods")
void testReconcile() throws InterruptedException {
    String namespace = "test";
    Cake testCake = buildTestCake("apple-pie", namespace, "0800cff3-9d80-11ea-8973-0e13a02d8ebd");
    server.expect().post().withPath(String.format("/api/v1/namespaces/%s/pods", namespace)).andReturn(HttpURLConnection.HTTP_CREATED, new PodBuilder().withNewMetadata().withName("foo").endMetadata().build()).times(testCake.getSpec().getReplicas());
    SharedInformerFactory informerFactory = client.informers();
    MixedOperation<Cake, CakeList, Resource<Cake>> cakeClient = client.customResources(Cake.class, CakeList.class);
    SharedIndexInformer<Pod> podSharedIndexInformer = informerFactory.sharedIndexInformerFor(Pod.class, RESYNC_PERIOD_MILLIS);
    SharedIndexInformer<Cake> cakeSharedIndexInformer = informerFactory.sharedIndexInformerForCustomResource(Cake.class, RESYNC_PERIOD_MILLIS);
    CakeController cakeController = new CakeController(client, cakeClient, podSharedIndexInformer, cakeSharedIndexInformer, namespace);
    cakeController.reconcile(testCake);
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("POST", recordedRequest.getMethod());
    assertTrue(recordedRequest.getBody().readUtf8().contains(testCake.getMetadata().getName()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) CakeList(it.fvaleri.integ.model.v1alpha1.CakeList) SharedInformerFactory(io.fabric8.kubernetes.client.informers.SharedInformerFactory) Pod(io.fabric8.kubernetes.api.model.Pod) Cake(it.fvaleri.integ.model.v1alpha1.Cake) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Resource(io.fabric8.kubernetes.client.dsl.Resource) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Pod (io.fabric8.kubernetes.api.model.Pod)2 Resource (io.fabric8.kubernetes.client.dsl.Resource)2 SharedInformerFactory (io.fabric8.kubernetes.client.informers.SharedInformerFactory)2 Cake (it.fvaleri.integ.model.v1alpha1.Cake)2 CakeList (it.fvaleri.integ.model.v1alpha1.CakeList)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)1 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 OperationContext (io.fabric8.kubernetes.client.dsl.base.OperationContext)1 CakeController (it.fvaleri.integ.controller.CakeController)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 DisplayName (org.junit.jupiter.api.DisplayName)1 Test (org.junit.jupiter.api.Test)1