1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-02-04 14:39:24 +02:00
parent 5287e7e528
commit 4fcdf01ba2
3 changed files with 56 additions and 28 deletions

View File

@ -37,8 +37,9 @@ export interface IKubeWatchReconnectOptions {
} }
export interface IKubeWatchLog { export interface IKubeWatchLog {
message: string | Error; message: string | string[] | Error;
meta?: object; meta?: object;
cssStyle?: string;
} }
@autobind() @autobind()
@ -250,7 +251,7 @@ export class KubeWatchApi {
if (done) break; // exit if (done) break; // exit
const events = (jsonBuffer + value).split("\n"); const events = (jsonBuffer + value).trim().split("\n");
jsonBuffer = this.processBuffer(events); jsonBuffer = this.processBuffer(events);
} }
@ -273,12 +274,29 @@ export class KubeWatchApi {
try { try {
const kubeEvent: IKubeWatchEvent = JSON.parse(json); const kubeEvent: IKubeWatchEvent = JSON.parse(json);
const message = this.getMessage(kubeEvent); const message = this.getMessage(kubeEvent);
const { data, namespace } = message;
if (!this.namespaces.includes(message.namespace)) { if (!this.isAllowedApi(message.api)) {
continue; // skip updates from non-watching resources context return;
} }
this.onMessage.emit(message); // log all data events
if (data) {
this.log({
message: `[${data.type}] ${data.object.kind} in ${namespace || "(cluster)"}`,
meta: data,
cssStyle: `color: ${[
data.type === "ADDED" && "green",
data.type === "MODIFIED" && "darkgray",
data.type === "DELETED" && "red",
].filter(Boolean)};`,
});
}
// skip updates from non-watching resources context
if (!namespace || this.namespaces.includes(namespace)) {
this.onMessage.emit(message);
}
} catch (error) { } catch (error) {
return json; return json;
} }
@ -353,20 +371,21 @@ export class KubeWatchApi {
} }
} }
protected log({ message, meta = {} }: IKubeWatchLog) { protected log({ message, cssStyle = "", meta = {} }: IKubeWatchLog) {
if (isProduction && !isDebugging) { if (isProduction && !isDebugging) {
return; return;
} }
const logMessage = `%c[KUBE-WATCH-API]: ${String(message).toUpperCase()}`; const logInfo = [`%c[KUBE-WATCH-API]:`, `font-weight: bold; ${cssStyle}`, message].flat().map(String);
const isError = message instanceof Error; const logMeta = {
const textStyle = `font-weight: bold;`; time: new Date().toLocaleString(),
const time = new Date().toLocaleString(); ...meta,
};
if (isError) { if (message instanceof Error) {
console.error(logMessage, textStyle, { time, ...meta }); console.error(...logInfo, logMeta);
} else { } else {
console.info(logMessage, textStyle, { time, ...meta }); console.info(...logInfo, logMeta);
} }
} }
} }

View File

@ -27,6 +27,10 @@ export abstract class ItemStore<T extends ItemObject = ItemObject> {
return this.items.find(item => item.getName() === name); return this.items.find(item => item.getName() === name);
} }
getIndex(item: T): number {
return this.items.findIndex(i => i === item);
}
@action @action
protected sortItems(items: T[] = this.items, sorting?: ((item: T) => any)[], order?: "asc" | "desc"): T[] { protected sortItems(items: T[] = this.items, sorting?: ((item: T) => any)[], order?: "asc" | "desc"): T[] {
return orderBy(items, sorting || this.defaultSorting, order); return orderBy(items, sorting || this.defaultSorting, order);

View File

@ -204,12 +204,12 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
protected eventsBuffer = observable.array<IKubeWatchEvent<KubeJsonApiData>>([], { deep: false }); protected eventsBuffer = observable.array<IKubeWatchEvent<KubeJsonApiData>>([], { deep: false });
protected bindWatchEventsUpdater(delay = 1000) { protected bindWatchEventsUpdater(delay = 1000) {
kubeWatchApi.onMessage.addListener(({ store, data }: IKubeWatchMessage<T>) => { kubeWatchApi.onMessage.addListener((evt: IKubeWatchMessage<T>) => {
if (!this.isLoaded || store !== this) return; if (!this.isLoaded || evt.store !== this) return;
this.eventsBuffer.push(data); this.eventsBuffer.push(evt.data);
}); });
reaction(() => this.eventsBuffer.length > 0, this.updateFromEventsBuffer, { reaction(() => this.eventsBuffer.length, this.updateFromEventsBuffer, {
delay delay
}); });
} }
@ -224,11 +224,14 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
@action @action
protected updateFromEventsBuffer() { protected updateFromEventsBuffer() {
const items = this.items.toJS(); const eventsBuffer = this.eventsBuffer.clear();
for (const { type, object } of this.eventsBuffer.clear()) { if (!eventsBuffer.length) {
const index = items.findIndex(item => item.getId() === object.metadata?.uid); return;
const item = items[index]; }
for (const { type, object } of eventsBuffer) {
const item = this.getById(object.metadata?.uid);
const api = apiManager.getApiByKind(object.kind, object.apiVersion); const api = apiManager.getApiByKind(object.kind, object.apiVersion);
switch (type) { switch (type) {
@ -237,20 +240,22 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
const newItem = new api.objectConstructor(object); const newItem = new api.objectConstructor(object);
if (!item) { if (!item) {
items.push(newItem); this.items.push(newItem);
} else { } else {
items.splice(index, 1, newItem); const index = this.getIndex(item);
this.items.splice(index, 1, newItem);
} }
break; break;
case "DELETED": case "DELETED":
if (item) { this.items.remove(item);
items.splice(index, 1);
}
break; break;
} }
} }
// update items // sort and limit items to store's maximum
this.items.replace(this.sortItems(items.slice(-this.bufferSize))); const newItems = this.sortItems(this.items.slice(-this.bufferSize));
this.items.replace(newItems);
} }
} }