Search in sources :

Example 1 with KubernetesObject

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

the class KubectlApply method executeServerSideApply.

private ApiType executeServerSideApply() throws KubectlException {
    refreshDiscovery();
    GenericKubernetesApi<ApiType, KubernetesListObject> api = getGenericApi();
    PatchOptions patchOptions = new PatchOptions();
    patchOptions.setForce(this.forceConflict);
    patchOptions.setFieldManager(this.fieldManager);
    if (ModelMapper.isNamespaced(this.targetObj.getClass())) {
        String targetNamespace = namespace != null ? namespace : Strings.isNullOrEmpty(targetObj.getMetadata().getNamespace()) ? Namespaces.NAMESPACE_DEFAULT : targetObj.getMetadata().getNamespace();
        KubernetesApiResponse<KubernetesObject> response = null;
        try {
            return api.patch(targetNamespace, targetObj.getMetadata().getName(), V1Patch.PATCH_FORMAT_APPLY_YAML, new V1Patch(apiClient.getJSON().serialize(targetObj)), patchOptions).throwsApiException().getObject();
        } catch (ApiException e) {
            throw new KubectlException(e);
        }
    } else {
        try {
            return api.patch(targetObj.getMetadata().getName(), V1Patch.PATCH_FORMAT_APPLY_YAML, new V1Patch(apiClient.getJSON().serialize(targetObj)), patchOptions).throwsApiException().getObject();
        } catch (ApiException e) {
            throw new KubectlException(e);
        }
    }
}
Also used : KubernetesListObject(io.kubernetes.client.common.KubernetesListObject) PatchOptions(io.kubernetes.client.util.generic.options.PatchOptions) KubernetesObject(io.kubernetes.client.common.KubernetesObject) V1Patch(io.kubernetes.client.custom.V1Patch) KubectlException(io.kubernetes.client.extended.kubectl.exception.KubectlException) ApiException(io.kubernetes.client.openapi.ApiException)

Example 2 with KubernetesObject

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

the class KubernetesControllerFactory method buildController.

private Controller buildController(SharedInformerFactory sharedInformerFactory, Reconciler r) throws BeansException {
    KubernetesReconciler kubernetesReconciler = r.getClass().getAnnotation(KubernetesReconciler.class);
    String reconcilerName = kubernetesReconciler.value();
    KubernetesReconcilerWatches watches = kubernetesReconciler.watches();
    DefaultControllerBuilder builder = ControllerBuilder.defaultBuilder(sharedInformerFactory);
    RateLimitingQueue<Request> workQueue = new DefaultRateLimitingQueue<>();
    builder = builder.withWorkQueue(workQueue);
    Map<Class, AddFilterAdaptor> addFilters = getAddFilters(watches, r);
    Map<Class, UpdateFilterAdaptor> updateFilters = getUpdateFilters(watches, r);
    Map<Class, DeleteFilterAdaptor> deleteFilters = getDeleteFilters(watches, r);
    List<ReadyFuncAdaptor> readyFuncs = getReadyFuncs(r);
    for (KubernetesReconcilerWatch watch : watches.value()) {
        try {
            Function<? extends KubernetesObject, Request> workQueueKeyFunc;
            try {
                workQueueKeyFunc = watch.workQueueKeyFunc().getConstructor(WorkQueue.class).newInstance(workQueue);
            } catch (NoSuchMethodException e) {
                workQueueKeyFunc = watch.workQueueKeyFunc().newInstance();
            } catch (InvocationTargetException e) {
                throw new BeanCreationException("Failed instantiating controller watch: " + e.getMessage());
            }
            final Function<? extends KubernetesObject, Request> finalWorkQueueKeyFunc = workQueueKeyFunc;
            builder = builder.watch((q) -> {
                return ControllerBuilder.controllerWatchBuilder(watch.apiTypeClass(), q).withOnAddFilter(addFilters.get(watch.apiTypeClass())).withOnUpdateFilter(updateFilters.get(watch.apiTypeClass())).withOnDeleteFilter(deleteFilters.get(watch.apiTypeClass())).withWorkQueueKeyFunc(finalWorkQueueKeyFunc).withResyncPeriod(Duration.ofMillis(watch.resyncPeriodMillis())).build();
            });
            for (Supplier<Boolean> readyFunc : readyFuncs) {
                builder = builder.withReadyFunc(readyFunc);
            }
        } catch (IllegalAccessException | InstantiationException e) {
            throw new BeanCreationException("Failed instantiating controller: " + e.getMessage());
        }
    }
    builder = builder.withWorkerCount(kubernetesReconciler.workerCount());
    return builder.withReconciler(r).withName(reconcilerName).build();
}
Also used : WorkQueue(io.kubernetes.client.extended.workqueue.WorkQueue) AddWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.AddWatchEventFilter) LoggerFactory(org.slf4j.LoggerFactory) KubernetesReconcilerWatch(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch) HashMap(java.util.HashMap) Reconciler(io.kubernetes.client.extended.controller.reconciler.Reconciler) Function(java.util.function.Function) Supplier(java.util.function.Supplier) StringUtils(org.apache.commons.lang3.StringUtils) DefaultControllerBuilder(io.kubernetes.client.extended.controller.builder.DefaultControllerBuilder) SharedInformerFactory(io.kubernetes.client.informer.SharedInformerFactory) ArrayList(java.util.ArrayList) Request(io.kubernetes.client.extended.controller.reconciler.Request) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) BeanCreationException(org.springframework.beans.factory.BeanCreationException) FactoryBean(org.springframework.beans.factory.FactoryBean) KubernetesObject(io.kubernetes.client.common.KubernetesObject) Duration(java.time.Duration) Map(java.util.Map) DeleteWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.DeleteWatchEventFilter) DefaultRateLimitingQueue(io.kubernetes.client.extended.workqueue.DefaultRateLimitingQueue) Method(java.lang.reflect.Method) RateLimitingQueue(io.kubernetes.client.extended.workqueue.RateLimitingQueue) KubernetesReconcilerReadyFunc(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerReadyFunc) KubernetesReconcilerWatches(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatches) Logger(org.slf4j.Logger) Controller(io.kubernetes.client.extended.controller.Controller) ControllerBuilder(io.kubernetes.client.extended.controller.builder.ControllerBuilder) Predicate(java.util.function.Predicate) Set(java.util.Set) BeansException(org.springframework.beans.BeansException) KubernetesReconciler(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconciler) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) UpdateWatchEventFilter(io.kubernetes.client.spring.extended.controller.annotation.UpdateWatchEventFilter) DefaultControllerBuilder(io.kubernetes.client.extended.controller.builder.DefaultControllerBuilder) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Request(io.kubernetes.client.extended.controller.reconciler.Request) DefaultRateLimitingQueue(io.kubernetes.client.extended.workqueue.DefaultRateLimitingQueue) KubernetesReconcilerWatch(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch) InvocationTargetException(java.lang.reflect.InvocationTargetException) KubernetesReconciler(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconciler) KubernetesReconcilerWatches(io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatches)

Example 3 with KubernetesObject

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

the class DefaultSharedIndexInformer method handleDeltas.

/**
 * handleDeltas handles deltas and call processor distribute.
 *
 * @param deltas deltas
 */
public void handleDeltas(Deque<MutablePair<DeltaFIFO.DeltaType, KubernetesObject>> deltas) {
    if (CollectionUtils.isEmpty(deltas)) {
        return;
    }
    // from oldest to newest
    for (MutablePair<DeltaFIFO.DeltaType, KubernetesObject> delta : deltas) {
        DeltaFIFO.DeltaType deltaType = delta.getLeft();
        KubernetesObject obj = delta.getRight();
        if (transform != null) {
            obj = transform.transform(obj);
        }
        switch(deltaType) {
            case Sync:
            case Added:
            case Updated:
                boolean isSync = deltaType == DeltaFIFO.DeltaType.Sync;
                Object oldObj = this.indexer.get((ApiType) obj);
                if (oldObj != null) {
                    this.indexer.update((ApiType) obj);
                    this.processor.distribute(new ProcessorListener.UpdateNotification(oldObj, obj), isSync);
                } else {
                    this.indexer.add((ApiType) obj);
                    this.processor.distribute(new ProcessorListener.AddNotification(obj), isSync);
                }
                break;
            case Deleted:
                this.indexer.delete((ApiType) obj);
                this.processor.distribute(new ProcessorListener.DeleteNotification(obj), false);
                break;
        }
    }
}
Also used : ProcessorListener(io.kubernetes.client.informer.cache.ProcessorListener) KubernetesObject(io.kubernetes.client.common.KubernetesObject) KubernetesListObject(io.kubernetes.client.common.KubernetesListObject) KubernetesObject(io.kubernetes.client.common.KubernetesObject) DeltaFIFO(io.kubernetes.client.informer.cache.DeltaFIFO)

Example 4 with KubernetesObject

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

the class DeltaFIFO method replace.

/**
 * Replace the item forcibly.
 *
 * @param list the list
 * @param resourceVersion the resource version
 */
public void replace(List<KubernetesObject> list, String resourceVersion) {
    lock.writeLock().lock();
    try {
        Set<String> keys = new HashSet<>();
        for (KubernetesObject obj : list) {
            String key = this.keyOf(obj);
            keys.add(key);
            this.queueActionLocked(DeltaType.Sync, obj);
        }
        if (this.knownObjects == null) {
            for (Map.Entry<String, Deque<MutablePair<DeltaType, KubernetesObject>>> entry : this.items.entrySet()) {
                if (keys.contains(entry.getKey())) {
                    continue;
                }
                KubernetesObject deletedObj = null;
                MutablePair<DeltaType, KubernetesObject> delta = // get newest
                entry.getValue().peekLast();
                if (delta != null) {
                    deletedObj = delta.getRight();
                }
                this.queueActionLocked(DeltaType.Deleted, new DeletedFinalStateUnknown(entry.getKey(), deletedObj));
            }
            if (!this.populated) {
                this.populated = true;
                this.initialPopulationCount = list.size();
            }
            return;
        }
        // Detect deletions not already in the queue.
        List<String> knownKeys = this.knownObjects.listKeys();
        int queueDeletion = 0;
        for (String knownKey : knownKeys) {
            if (keys.contains(knownKey)) {
                continue;
            }
            KubernetesObject deletedObj = this.knownObjects.getByKey(knownKey);
            if (deletedObj == null) {
                log.warn("Key {} does not exist in known objects store, placing DeleteFinalStateUnknown marker without object", knownKey);
            }
            queueDeletion++;
            this.queueActionLocked(DeltaType.Deleted, new DeletedFinalStateUnknown(knownKey, deletedObj));
        }
        if (!this.populated) {
            this.populated = true;
            this.initialPopulationCount = list.size() + queueDeletion;
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : KubernetesObject(io.kubernetes.client.common.KubernetesObject) HashMap(java.util.HashMap) Map(java.util.Map) Deque(java.util.Deque) HashSet(java.util.HashSet)

Example 5 with KubernetesObject

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

the class DeltaFIFO method keyOf.

// KeyOf exposes f's keyFunc, but also detects the key of a Deltas object or
// DeletedFinalStateUnknown objects.
private String keyOf(KubernetesObject obj) {
    KubernetesObject innerObj = obj;
    if (obj instanceof Deque) {
        Deque<MutablePair<DeltaType, KubernetesObject>> deltas = (Deque<MutablePair<DeltaType, KubernetesObject>>) obj;
        if (deltas.size() == 0) {
            throw new NoSuchElementException("0 length Deltas object; can't get key");
        }
        innerObj = deltas.peekLast().getRight();
    }
    if (innerObj instanceof DeletedFinalStateUnknown) {
        return ((DeletedFinalStateUnknown) innerObj).key;
    }
    return keyFunc.apply(innerObj);
}
Also used : MutablePair(org.apache.commons.lang3.tuple.MutablePair) KubernetesObject(io.kubernetes.client.common.KubernetesObject) Deque(java.util.Deque) NoSuchElementException(java.util.NoSuchElementException)

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