Search in sources :

Example 1 with Cake

use of it.fvaleri.integ.model.v1alpha1.Cake in project Lectures by FER-OOP.

the class UniMasterChef method main.

public static void main(String[] args) {
    Dessert genericDessert = new Dessert("Chocolate Mousse", 120, 300);
    Cake cake = new Cake("Raspberry chocolate cake #3", 350.5, 400, false, "birthday");
    Teacher t1 = new Teacher("Dario", "Tušek", 42, "dario.tusek@fer.hr", "OOP", 10000);
    Teacher t2 = new Teacher("Doris", "Bezmalinović", 43, "doris.bezmalinovic@fer.hr", "OOP", 10000);
    Student s1 = new Student("Janko", "Horvat", 18, "0036312123", (short) 1);
    Student s2 = new Student("Ana", "Kovač", 19, "0036387656", (short) 2);
    Student s3 = new Student("Ivana", "Stanić", 19, "0036392357", (short) 1);
    UniMasterChef competition = new UniMasterChef(2);
    CompetitionEntry e1 = new CompetitionEntry(t1, genericDessert);
    competition.addEntry(e1);
    System.out.println("Entry 1 rating: " + e1.getRating());
    e1.addRating(s1, 4);
    e1.addRating(s2, 5);
    System.out.println("Entry 1 rating: " + e1.getRating());
    CompetitionEntry e2 = new CompetitionEntry(t2, cake);
    e2.addRating(s1, 4);
    e2.addRating(s3, 5);
    e2.addRating(s2, 5);
    competition.addEntry(e2);
    System.out.println("Entry 2 rating: " + e2.getRating());
    System.out.println("Best dessert is: " + competition.getBestDessert().getName());
    Person[] e2persons = UniMasterChef.getInvolvedPeople(e2);
    for (Person p : e2persons) System.out.println(p);
}
Also used : Cake(hr.fer.oop.homework_04.e02.Cake) Teacher(hr.fer.oop.homework_04.e04.Teacher) Student(hr.fer.oop.homework_04.e04.Student) CompetitionEntry(hr.fer.oop.homework_04.e06.CompetitionEntry) Dessert(hr.fer.oop.homework_04.e01.Dessert) Person(hr.fer.oop.homework_04.e03.Person)

Example 2 with Cake

use of it.fvaleri.integ.model.v1alpha1.Cake 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 3 with Cake

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

the class CakeController method handlePodObject.

private void handlePodObject(Pod pod) {
    LOG.debug("Handling pod {}", pod.getMetadata().getName());
    OwnerReference ownerReference = getControllerOf(pod);
    if (ownerReference == null || !ownerReference.getKind().equalsIgnoreCase(API_KIND)) {
        return;
    }
    Cake cake = cakeLister.get(ownerReference.getName());
    if (cake != null) {
        enqueueCake(cake);
    }
}
Also used : OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) Cake(it.fvaleri.integ.model.v1alpha1.Cake)

Example 4 with Cake

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

the class CakeController method run.

/**
 * Control/reconciliation loop.
 */
public void run() {
    LOG.info("Starting controller");
    while (!podInformer.hasSynced() || !cakeInformer.hasSynced()) {
    // wait till Informer syncs
    }
    while (true) {
        try {
            LOG.info("Trying to fetch resource from work queue");
            if (workQueue.isEmpty()) {
                LOG.info("Work queue is empty");
            }
            String key = workQueue.take();
            Objects.requireNonNull(key, "Key can't be null");
            LOG.info("Reconciling resource with key {}", key);
            if (key.isEmpty() || (!key.contains("/"))) {
                LOG.warn("Invalid resource with key {}", key);
            }
            // get the Cake resource's name from key which is in format namespace/name
            String name = key.split("/")[1];
            Cake cake = cakeLister.get(name);
            if (cake == null) {
                LOG.error("Resource {} no longer exists", name);
                return;
            }
            reconcile(cake);
        } catch (InterruptedException interruptedException) {
            Thread.currentThread().interrupt();
            LOG.error("Controller interrupted");
        }
    }
}
Also used : Cake(it.fvaleri.integ.model.v1alpha1.Cake)

Example 5 with Cake

use of it.fvaleri.integ.model.v1alpha1.Cake 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

Cake (it.fvaleri.integ.model.v1alpha1.Cake)5 Pod (io.fabric8.kubernetes.api.model.Pod)2 Resource (io.fabric8.kubernetes.client.dsl.Resource)2 SharedInformerFactory (io.fabric8.kubernetes.client.informers.SharedInformerFactory)2 CakeList (it.fvaleri.integ.model.v1alpha1.CakeList)2 Dessert (hr.fer.oop.homework_04.e01.Dessert)1 Cake (hr.fer.oop.homework_04.e02.Cake)1 Person (hr.fer.oop.homework_04.e03.Person)1 Student (hr.fer.oop.homework_04.e04.Student)1 Teacher (hr.fer.oop.homework_04.e04.Teacher)1 CompetitionEntry (hr.fer.oop.homework_04.e06.CompetitionEntry)1 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)1 OwnerReference (io.fabric8.kubernetes.api.model.OwnerReference)1 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 CakeSpec (it.fvaleri.integ.model.v1alpha1.CakeSpec)1