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);
}
}
}
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();
}
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;
}
}
}
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();
}
}
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);
}
Aggregations