Search in sources :

Example 6 with KubernetesObject

use of io.kubernetes.client.common.KubernetesObject in project java by kubernetes-client.

the class DeltaFIFO method syncKeyLocked.

/**
 * Add Sync delta. Caller must hold the lock.
 */
private void syncKeyLocked(String key) {
    KubernetesObject obj = this.knownObjects.getByKey(key);
    if (obj == null) {
        return;
    }
    String id = this.keyOf(obj);
    Deque<MutablePair<DeltaType, KubernetesObject>> deltas = this.items.get(id);
    if (deltas != null && !(CollectionUtils.isEmpty(deltas))) {
        return;
    }
    this.queueActionLocked(DeltaType.Sync, obj);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) KubernetesObject(io.kubernetes.client.common.KubernetesObject)

Example 7 with KubernetesObject

use of io.kubernetes.client.common.KubernetesObject in project java by kubernetes-client.

the class DeltaFIFOTest method testDeltaFIFOBasic.

@Test
public void testDeltaFIFOBasic() throws InterruptedException {
    Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> receivingDeltas = new LinkedList<>();
    V1Pod foo1 = new V1Pod().metadata(new V1ObjectMeta().name("foo1").namespace("default"));
    Cache cache = new Cache();
    DeltaFIFO deltaFIFO = new DeltaFIFO(Caches::deletionHandlingMetaNamespaceKeyFunc, cache);
    MutablePair<DeltaFIFO.DeltaType, KubernetesObject> receivingDelta;
    // basic add operation
    deltaFIFO.add(foo1);
    cache.add(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Added, receivingDelta.getLeft());
    // basic update operation
    deltaFIFO.update(foo1);
    cache.update(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Updated, receivingDelta.getLeft());
    // basic delete operation
    deltaFIFO.delete(foo1);
    cache.delete(foo1);
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Deleted, receivingDelta.getLeft());
    // basic sync operation
    deltaFIFO.replace(Arrays.asList(foo1), "0");
    cache.replace(Arrays.asList(foo1), "0");
    deltaFIFO.pop((deltas) -> {
        MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta = deltas.peekFirst();
        receivingDeltas.add(delta);
    });
    receivingDelta = receivingDeltas.peekFirst();
    receivingDeltas.removeFirst();
    assertEquals(foo1, receivingDelta.getRight());
    assertEquals(DeltaFIFO.DeltaType.Sync, receivingDelta.getLeft());
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) KubernetesObject(io.kubernetes.client.common.KubernetesObject) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 8 with KubernetesObject

use of io.kubernetes.client.common.KubernetesObject in project java by kubernetes-client.

the class DynamicKubernetesTypeAdaptorFactoryTest method testSingleDynamicObjectRoundTrip.

@Test
public void testSingleDynamicObjectRoundTrip() {
    KubernetesObject dynamicObj = gson.fromJson(jsonContent, KubernetesObject.class);
    assertTrue(dynamicObj instanceof DynamicKubernetesObject);
    assertEquals("v1", dynamicObj.getApiVersion());
    assertEquals("Namespace", dynamicObj.getKind());
    assertEquals(new V1ObjectMeta().name("foo"), dynamicObj.getMetadata());
    String dumped = gson.toJson(dynamicObj);
    assertEquals(jsonContent, dumped);
}
Also used : KubernetesObject(io.kubernetes.client.common.KubernetesObject) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) Test(org.junit.Test)

Example 9 with KubernetesObject

use of io.kubernetes.client.common.KubernetesObject in project java by kubernetes-client.

the class KubernetesKubectlCreateProcessor method postProcessAfterInitialization.

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (!(bean instanceof KubernetesObject)) {
        // no-op
        return bean;
    }
    KubectlCreate create = beanFactory.findAnnotationOnBean(beanName, KubectlCreate.class);
    if (create == null) {
        return bean;
    }
    Class<? extends KubernetesObject> apiTypeClass = (Class<? extends KubernetesObject>) bean.getClass();
    try {
        log.info("@KubectlCreate ensuring resource upon bean {}", beanName);
        return create(apiTypeClass, bean);
    } catch (KubectlException e) {
        if (e.getCause() instanceof ApiException) {
            ApiException apiException = (ApiException) e.getCause();
            if (HttpURLConnection.HTTP_CONFLICT == apiException.getCode()) {
                // already exists
                log.info("Skipped processing {} @KubectlCreate resource already exists", beanName);
                return bean;
            }
        }
        log.error("Failed ensuring resource from @KubectlCreate", e);
        throw new BeanCreationException("Failed ensuring resource from @KubectlCreate", e);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) KubernetesObject(io.kubernetes.client.common.KubernetesObject) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) KubectlCreate(io.kubernetes.client.spring.extended.manifests.annotation.KubectlCreate) ApiException(io.kubernetes.client.openapi.ApiException)

Example 10 with KubernetesObject

use of io.kubernetes.client.common.KubernetesObject in project java by kubernetes-client.

the class KubernetesKubectlApplyProcessor method postProcessAfterInitialization.

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (!(bean instanceof KubernetesObject)) {
        // no-op
        return bean;
    }
    KubectlApply apply = beanFactory.findAnnotationOnBean(beanName, KubectlApply.class);
    if (apply == null) {
        return bean;
    }
    Class<? extends KubernetesObject> apiTypeClass = (Class<? extends KubernetesObject>) bean.getClass();
    try {
        log.info("@KubectlApply ensuring resource upon bean {}", beanName);
        return apply(apiTypeClass, bean);
    } catch (KubectlException e) {
        log.error("Failed ensuring resource from @KubectlApply", e);
        throw new BeanCreationException("Failed ensuring resource from @KubectlApply", e);
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) KubernetesObject(io.kubernetes.client.common.KubernetesObject) KubectlApply(io.kubernetes.client.spring.extended.manifests.annotation.KubectlApply) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException)

Aggregations

KubernetesObject (io.kubernetes.client.common.KubernetesObject)11 MutablePair (org.apache.commons.lang3.tuple.MutablePair)4 KubectlException (io.kubernetes.client.extended.kubectl.exception.KubectlException)3 KubernetesListObject (io.kubernetes.client.common.KubernetesListObject)2 ApiException (io.kubernetes.client.openapi.ApiException)2 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)2 Deque (java.util.Deque)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 V1Patch (io.kubernetes.client.custom.V1Patch)1 Controller (io.kubernetes.client.extended.controller.Controller)1 ControllerBuilder (io.kubernetes.client.extended.controller.builder.ControllerBuilder)1 DefaultControllerBuilder (io.kubernetes.client.extended.controller.builder.DefaultControllerBuilder)1 Reconciler (io.kubernetes.client.extended.controller.reconciler.Reconciler)1 Request (io.kubernetes.client.extended.controller.reconciler.Request)1 DefaultRateLimitingQueue (io.kubernetes.client.extended.workqueue.DefaultRateLimitingQueue)1 RateLimitingQueue (io.kubernetes.client.extended.workqueue.RateLimitingQueue)1 WorkQueue (io.kubernetes.client.extended.workqueue.WorkQueue)1