Skip to content

Commit

Permalink
feat: Add resource parser
Browse files Browse the repository at this point in the history
Signed-off-by: SamoKopecky <[email protected]>
  • Loading branch information
SamoKopecky committed Nov 14, 2022
1 parent e5aa510 commit 34aa643
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
25 changes: 12 additions & 13 deletions plugins/rhacm-backend/src/helpers/parser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { CONSOLE_CLAIM } from '../constants';
import { ClusterDetails } from '@internal/backstage-plugin-rhacm-common';

const convertCpus = (cpus: string): number => {
const convertCpus = (cpus: string | undefined): number | undefined => {
if (!cpus) {
return undefined;
}
if (cpus.slice(-1) === 'm') {
return parseInt(cpus.slice(0, cpus.length - 1), 10) / 1000;
}
return parseInt(cpus, 10);
};

export const parseResources = (resources: any | undefined): Object => ({
cpuCores: convertCpus(resources?.cpu),
memorySize: resources?.memory,
numberOfPods: parseInt(resources?.pods, 10) || undefined,
});

export const getClaim = (cluster: any, claimName: string): string =>
cluster.status.clusterClaims.find((value: any) => value.name === claimName)
?.value;
Expand All @@ -25,8 +34,6 @@ export const parseManagedCluster = (cluster: any): ClusterDetails => {
},
};

const allocatable = cluster.status.allocatable;
const capacity = cluster.status.capacity;
const parsedClusterInfo = {
consoleUrl: getClaim(cluster, CONSOLE_CLAIM),
kubernetesVersion: getClaim(
Expand All @@ -42,16 +49,8 @@ export const parseManagedCluster = (cluster: any): ClusterDetails => {
getClaim(cluster, 'version.openshift.io'),
platform: getClaim(cluster, 'platform.open-cluster-management.io'),
region: getClaim(cluster, 'region.open-cluster-management.io'),
allocatableResources: {
cpuCores: convertCpus(allocatable.cpu),
memorySize: allocatable.memory,
numberOfPods: parseInt(allocatable.pods, 10),
},
availableResources: {
cpuCores: convertCpus(capacity.cpu),
memorySize: capacity.memory,
numberOfPods: parseInt(capacity.pods, 10),
},
allocatableResources: parseResources(cluster.status?.allocatable),
availableResources: parseResources(cluster.status?.capacity),
};

return {
Expand Down
12 changes: 6 additions & 6 deletions plugins/rhacm-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export type ClusterDetails = {
platform?: string;
region?: string;
allocatableResources?: {
cpuCores: number;
memorySize: string;
numberOfPods: number;
cpuCores?: number;
memorySize?: string;
numberOfPods?: number;
};
availableResources?: {
cpuCores: number;
memorySize: string;
numberOfPods: number;
cpuCores?: number;
memorySize?: string;
numberOfPods?: number;
};
status: {
available: boolean;
Expand Down

0 comments on commit 34aa643

Please sign in to comment.