Converting Nodes, Node, Pods, and Pod to TS

This commit is contained in:
Eric Herbrandson
2020-08-09 18:26:49 -05:00
parent a71ce9a620
commit cca2f33c86
24 changed files with 245 additions and 124 deletions

View File

@@ -1,4 +1,4 @@
import {TODO} from './types';
import {ApiItem} from './types';
export default function test(filter = '', ...values: string[]) {
const value = filter.toLowerCase();
@@ -7,8 +7,8 @@ export default function test(filter = '', ...values: string[]) {
.some(x => x.toLowerCase().includes(value));
}
export function filterByOwner(items: TODO[], owner: TODO) {
if (!items || !owner) return null;
export function filterByOwner<T extends ApiItem<any, any>>(items?: T[], owner?: ApiItem<any, any>): T[] | undefined {
if (!items || !owner) return undefined;
const {uid} = owner.metadata;
@@ -20,15 +20,15 @@ export function filterByOwner(items: TODO[], owner: TODO) {
});
}
export function filterByOwners(items: TODO[], owners: TODO) {
if (!items || !owners) return null;
export function filterByOwners<T extends ApiItem<any, any>>(items?: T[], owners?: ApiItem<any, any>[]): T[] | undefined {
if (!items || !owners) return undefined;
const uids = owners.map((x: any) => x.metadata.uid);
const uidList = owners.map(x => x.metadata.uid);
return items.filter((x) => {
if (x.involvedObject && uids.includes(x.involvedObject.uid)) return true;
if (x.involvedObject && uidList.includes(x.involvedObject.uid)) return true;
const {ownerReferences} = x.metadata;
return ownerReferences && ownerReferences.some((y: {uid: string}) => uids.includes(y.uid));
return ownerReferences && ownerReferences.some((y: {uid: string}) => uidList.includes(y.uid));
});
}

View File

@@ -1,8 +1,10 @@
import _ from 'lodash';
import {parseRam, parseCpu} from './unitHelpers';
import {TODO, Pod, Node, Metrics} from './types';
import {TODO, ApiItem, Pod, Node, Metrics, MetricsUsage} from './types';
export default function getMetrics(items?: Pod[], metrics?: Metrics[]) {
type ResourceType = 'cpu' | 'memory'
export default function getMetrics(items?: ApiItem<any, any>[], metrics?: Metrics[]) {
if (!items || !metrics) return undefined;
const names = _.map(items, x => x.metadata.name);
@@ -13,7 +15,7 @@ export default function getMetrics(items?: Pod[], metrics?: Metrics[]) {
// Node helpers
export function getNodeResourceValue(node: Node | undefined, pods: Pod[] | undefined, resource: string, type: string, phases: string[]) {
export function getNodeResourceValue(node: Node | undefined, pods: Pod[] | undefined, resource: ResourceType, type: string, phases: string[]) {
if (!node || !pods) return null;
return _(pods)
@@ -22,23 +24,23 @@ export function getNodeResourceValue(node: Node | undefined, pods: Pod[] | undef
.sumBy(x => getPodResourceValue(x, resource, type));
}
export function getNodeResourcePercent(node: TODO, pods: TODO, resource: string, type: string) {
export function getNodeResourcePercent(node: Node, pods: Pod[], resource: ResourceType, type: string) {
const used = getNodeResourceValue(node, pods, resource, type, ['Running']);
const available = getNodeResourcesAvailable(node, resource);
return used && available ? used / available : null;
}
export function getNodeUsagePercent(node: TODO, metrics: TODO, resource: string) {
export function getNodeUsagePercent(node: Node, metrics: _.Dictionary<Metrics>, resource: ResourceType) {
const used = getNodeUsage(node, metrics, resource);
const available = getNodeResourcesAvailable(node, resource);
return used && available ? used / available : null;
}
export function getNodeResourcesAvailable(node: TODO, resource: string) {
export function getNodeResourcesAvailable(node: Node, resource: ResourceType) {
return node ? parse(resource, node.status.capacity) : null;
}
export function getNodeUsage(node: TODO, metrics: TODO, resource: string) {
export function getNodeUsage(node: Node, metrics: _.Dictionary<Metrics>, resource: ResourceType) {
if (!node || !metrics) return null;
const result = metrics[node.metadata.name];
@@ -47,26 +49,27 @@ export function getNodeUsage(node: TODO, metrics: TODO, resource: string) {
// Pod helpers
export function getPodResourcePercent(item: TODO, metrics: TODO, resource: string, type: string) {
const actual = getPodUsage(item, metrics, resource);
const request = getPodResourceValue(item, resource, type);
export function getPodResourcePercent(pod: Pod, metrics: _.Dictionary<Metrics>, resource: ResourceType, type: string) {
const actual = getPodUsage(pod, metrics, resource);
const request = getPodResourceValue(pod, resource, type);
return actual ? actual / request : null;
}
export function getPodUsage(pod: TODO, metrics: TODO, resource: string) {
export function getPodUsage(pod: Pod, metrics: _.Dictionary<Metrics>, resource: ResourceType) {
if (!pod || !metrics) return null;
const metric = metrics[pod.metadata.name] || {};
return _.sumBy(metric.containers, (x: TODO) => parse(resource, x.usage));
return _.sumBy(metric.containers, x => parse(resource, x.usage));
}
export function getPodResourceValue(pod: TODO, resource: string, type: string) {
export function getPodResourceValue(pod: TODO, resource: ResourceType, type: string) {
return _(pod.spec.containers)
.filter(x => x.resources && x.resources[type])
.sumBy(x => parse(resource, x.resources[type]));
}
function parse(resource: string, target: {[key: string]: string}) {
function parse(resource: ResourceType, target: MetricsUsage) {
const parser = resource === 'cpu' ? parseCpu : parseRam;
// @ts-ignore
return parser(target[resource]);
}

View File

@@ -1,12 +1,12 @@
import {TODO} from './types';
import {Node} from './types';
/**
* @param {*status* object with a status field (most likely the row from the TableBody)}
* @returns the status text, as defined in https://kubernetes.io/docs/concepts/architecture/nodes/#condition
*/
function getReadyStatus({status}: TODO) {
if (!status.conditions) return null;
const ready = status.conditions.find((y: {type: string}) => y.type === 'Ready');
function getReadyStatus({status}: Node) {
if (!status.conditions) return undefined;
const ready = status.conditions.find(y => y.type === 'Ready');
return ready && ready.status;
}

View File

@@ -2,15 +2,31 @@ export type TODO = any;
export type ApiItem<TSpec, TStatus> = {
kind: string;
apiVersion: string;
metadata: Metadata;
spec: TSpec;
status: TStatus
status: TStatus;
involvedObject?: InvolvedObject;
}
export interface InvolvedObject {
uid: string;
kind: string;
namespace: string;
name: string;
}
interface Metadata {
uid: string;
resourceVersion: string;
creationTimestamp: number;
name: string;
labels: {[name: string]: string};
ownerReferences?: {
uid: string;
kind: string;
name: string;
}[];
}
interface Container {
@@ -26,8 +42,28 @@ interface Container {
}
}
interface Condition {
type: string;
status: string;
lastTransitionTime: number;
reason: string;
message: string;
}
export interface MetricsUsage {
cpu?: string;
memory?: string;
}
export interface Metrics extends ApiItem<undefined, undefined> {
containers: Container[];
resources?: {
requests?: {
cpu?: string;
memory?: string;
}
}
usage: MetricsUsage
}
interface NamespaceStatus {
@@ -37,20 +73,50 @@ interface NamespaceStatus {
export interface Namespace extends ApiItem<undefined, NamespaceStatus> {
}
export interface Node extends ApiItem<undefined, undefined> {
interface NodeSpec {
taints: {[name: string]: string}
}
export interface NodeStatus {
capacity: {
cpu?: string;
memory?: string;
}
conditions: Condition[];
nodeInfo: {
kernelVersion: string;
osImage: string;
operatingSystem: string;
architecture: string;
containerRuntimeVersion: string;
kubeletVersion: string;
kubeProxyVersion: string;
}
}
export interface Node extends ApiItem<NodeSpec, NodeStatus> {
}
export interface K8sEvent extends ApiItem<undefined, undefined>{
export interface K8sEvent extends ApiItem<undefined, undefined> {
type: string;
reason: string;
message: string;
}
interface PodSpec {
nodeName: string;
containers: Container[];
nodeSelector?: {[key: string]: string};
}
interface PodStatus {
phase: string;
hostIP: string;
podIP: string;
qosClass: string;
message: string;
conditions?: Condition[];
}
export interface Pod extends ApiItem<PodSpec, PodStatus>{