Skip to content

Commit

Permalink
feat: Add update information to the status endpoint
Browse files Browse the repository at this point in the history
Add additional API call to gather information about cluster updates and
create additional tests

Signed-off-by: SamoKopecky <[email protected]>
  • Loading branch information
SamoKopecky committed Jan 6, 2023
1 parent b71fe68 commit 34f88ea
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 10 deletions.
3 changes: 2 additions & 1 deletion plugins/ocm-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
"express": "^4.17.1",
"express-promise-router": "^4.1.0",
"node-fetch": "^3.0.0",
"semver": "^7.3.8",
"winston": "^3.2.1",
"yn": "^5.0.0"
},
"devDependencies": {
"@types/express": "4.17.14",
"@backstage/cli": "0.21.1",
"@types/express": "4.17.14",
"@types/supertest": "2.0.12",
"msw": "0.49.1",
"nock": "13.2.9",
Expand Down
32 changes: 32 additions & 0 deletions plugins/ocm-backend/src/helpers/kubernetes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
hubApiClient,
getManagedCluster,
getManagedClusters,
getManagedClustersInfo,
} from './kubernetes';
import { createLogger } from 'winston';
import transports from 'winston/lib/winston/transports';
Expand Down Expand Up @@ -176,3 +177,34 @@ describe('getManagedCluster', () => {
expect(result.name).toBe('NotFound');
});
});

describe('getManagedClustersInfo', () => {
it('should return some clusters', async () => {
nock(kubeConfig.clusters[0].server)
.get(
'/apis/internal.open-cluster-management.io/v1beta1/managedclusterinfos',
)
.reply(200, {
body: {
items: [
{
kind: 'ManagedClusterInfo',
metadata: {
name: 'cluster1',
},
},
{
kind: 'ManagedClusterInfo',
metadata: {
name: 'cluster2',
},
},
],
},
});

const result: any = await getManagedClustersInfo(getApi());
expect(result.body.items[0].metadata.name).toBe('cluster1');
expect(result.body.items[1].metadata.name).toBe('cluster2');
});
});
10 changes: 10 additions & 0 deletions plugins/ocm-backend/src/helpers/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ export const getManagedClusters = (api: CustomObjectsApi) => {
),
);
};

export const getManagedClustersInfo = (api: CustomObjectsApi) => {
return kubeApiResponseHandler(
api.listClusterCustomObject(
'internal.open-cluster-management.io',
'v1beta1',
'managedclusterinfos',
),
);
};
160 changes: 159 additions & 1 deletion plugins/ocm-backend/src/helpers/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { ClusterDetails } from '@janus-idp/backstage-plugin-ocm-common';
import { getClaim, parseManagedCluster, parseResources } from './parser';
import { createLogger, transports } from 'winston';
import {
getClaim,
getClusterInfoByName,
parseManagedCluster,
parseResources,
parseUpdateInfo,
} from './parser';

const logger = createLogger({
transports: [new transports.Console({ silent: true })],
});

describe('getClaim', () => {
it('should extract a cluster claim value from a cluster object', () => {
Expand Down Expand Up @@ -274,3 +285,150 @@ describe('parseManagedCluster', () => {
expect(result).toStrictEqual(expected);
});
});

describe('parseUpdateInfo', () => {
it('should correctly parse update information from ClusterInfo', () => {
const clusterInfo = {
metadata: {},
status: {
distributionInfo: {
ocp: {
availableUpdates: ['1.0.1', '1.0.2', '1.0.3', '1.0.0'],
versionAvailableUpdates: [
{
url: 'http://exampleone.com',
version: '1.0.1',
},
{
url: 'http://exampletwo.com',
version: '1.0.2',
},
{
url: 'http://examplethree.com',
version: '1.0.3',
},
{
url: 'http://examplezero.com',
version: '1.0.0',
},
],
},
},
},
};

const result = parseUpdateInfo(clusterInfo);

expect(result).toEqual({
update: {
available: true,
version: '1.0.3',
url: 'http://examplethree.com',
},
});
});

it('should correctly parse while there are no updates available', () => {
const clusterInfo = {
metadata: {},
status: {
distributionInfo: {
ocp: {},
},
},
};

const result = parseUpdateInfo(clusterInfo);

expect(result).toEqual({
update: {
available: false,
},
});
});

it('should correctly parse while there is only one update available', () => {
const clusterInfo = {
metadata: {},
status: {
distributionInfo: {
ocp: {
availableUpdates: ['1.0.1'],
versionAvailableUpdates: [
{
url: 'http://exampleone.com',
version: '1.0.1',
},
],
},
},
},
};

const result = parseUpdateInfo(clusterInfo);

expect(result).toEqual({
update: {
available: true,
version: '1.0.1',
url: 'http://exampleone.com',
},
});
});
});

describe('getClusterInfoByName', () => {
it('should get the correct clusterInfo from many clusterInfos', () => {
const clusterInfo = [
{
metadata: {
name: 'cluster1',
},
},
{
metadata: {
name: 'cluster2',
},
},
{
metadata: {
name: 'cluster3',
},
},
];

const result = getClusterInfoByName(clusterInfo, 'cluster2', logger);

expect(result).toEqual({
metadata: {
name: 'cluster2',
},
});
});

it('should get the first clusterInfo from many clusterInfos if there are duplicate names', () => {
const clusterInfo = [
{
metadata: {
name: 'cluster1',
uid: '15c0f22e-8d0a-11ed-a1eb-0242ac120002',
},
},
{
metadata: {
name: 'cluster1',
uid: '15c0f7d8-8d0a-11ed-a1eb-0242ac120002',
},
},
];

const result = getClusterInfoByName(clusterInfo, 'cluster1', logger);

expect(result).toEqual({
metadata: {
name: 'cluster1',
uid: '15c0f22e-8d0a-11ed-a1eb-0242ac120002',
},
});
});
});
40 changes: 40 additions & 0 deletions plugins/ocm-backend/src/helpers/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CONSOLE_CLAIM } from '../constants';
import { ClusterDetails } from '@janus-idp/backstage-plugin-ocm-common';
import { Logger } from 'winston';
import { maxSatisfying } from 'semver';

const convertCpus = (cpus: string | undefined): number | undefined => {
if (!cpus) {
Expand Down Expand Up @@ -58,3 +60,41 @@ export const parseManagedCluster = (cluster: any): ClusterDetails => {
...parsedClusterInfo,
};
};

export const parseUpdateInfo = (clusterInfo: any) => {
const versionAvailableUpdates =
clusterInfo.status.distributionInfo.ocp.versionAvailableUpdates;
const versionsAvailable =
clusterInfo.status.distributionInfo.ocp.availableUpdates;
const version = versionsAvailable
? maxSatisfying(versionsAvailable, '*')
: undefined;
const url = versionAvailableUpdates
? versionAvailableUpdates[versionsAvailable.indexOf(version)].url
: undefined;

return {
update: {
available: versionsAvailable ? versionsAvailable.length !== 0 : false,
version: version,
url: url,
},
};
};

export const getClusterInfoByName = (
clusterInfos: Array<any>,
clusterName: string,
logger: Logger,
) => {
const clusterInfo = clusterInfos.filter(
(cluster: any) => cluster.metadata.name === clusterName,
);

if (clusterInfo.length > 1) {
logger.warn(
'Found more then one clusters with the same name while filtering clusters, picking the first one',
);
}
return clusterInfo[0];
};
37 changes: 29 additions & 8 deletions plugins/ocm-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import { Config } from '@backstage/config';
import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
import { ClusterDetails } from '@janus-idp/backstage-plugin-ocm-common';
import { HUB_CLUSTER_NAME_IN_OCM } from '../constants';
import {
getManagedCluster,
getManagedClusters,
getManagedClustersInfo,
hubApiClient,
} from '../helpers/kubernetes';
import { parseManagedCluster } from '../helpers/parser';
import {
getClusterInfoByName,
parseManagedCluster,
parseUpdateInfo,
} from '../helpers/parser';
import { getHubClusterName } from '../helpers/config';

export interface RouterOptions {
Expand Down Expand Up @@ -56,19 +60,36 @@ export async function createRouter(

return (
getManagedCluster(api, normalizedClusterName) as Promise<any>
).then(resp => {
response.send(parseManagedCluster(resp));
).then(async resp => {
response.send({
...parseManagedCluster(resp),
...parseUpdateInfo(
getClusterInfoByName(
(await (getManagedClustersInfo(api) as Promise<any>)).items,
normalizedClusterName,
logger,
),
),
});
});
},
);

router.get('/status', (_, response) => {
logger.info(`Incoming status request for all clusters`);

return (getManagedClusters(api) as Promise<any>).then(resp => {
const parsedClusters: Array<ClusterDetails> =
resp.items.map(parseManagedCluster);
response.send(parsedClusters);
return (getManagedClusters(api) as Promise<any>).then(async resp => {
const clusterInfo = (await (getManagedClustersInfo(api) as Promise<any>))
.items;

response.send(
resp.items.map((clusterStatus: any, index: number) => {
return {
...parseManagedCluster(clusterStatus),
...parseUpdateInfo(clusterInfo[index]),
};
}),
);
});
});

Expand Down
5 changes: 5 additions & 0 deletions plugins/ocm-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type ClusterDetails = {
memorySize?: string;
numberOfPods?: number;
};
update?: {
available?: boolean;
version?: string;
url?: string;
};
status: {
available: boolean;
reason: string;
Expand Down

0 comments on commit 34f88ea

Please sign in to comment.