Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion ui/rspack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ const config: Configuration = {
'./platformLibrary': './src/services/platformlibrary/k8s.ts',
'./AlertsNavbarUpdater':
'./src/components/AlertNavbarUpdaterComponent.tsx',
'./Metalk8sLocalVolumeProvider':
'./src/services/k8s/Metalk8sLocalVolumeProvider.ts',
},
remotes: !isProduction
? {
Expand Down Expand Up @@ -148,7 +150,19 @@ const config: Configuration = {
},
}),
new rspack.CopyRspackPlugin({
patterns: [{ from: 'public' }],
patterns: [
{ from: 'public' },
{
from: path.resolve(__dirname, 'build/static/js/@mf-types.zip'),
to: '@mf-types.zip',
noErrorOnMissing: true,
},
{
from: path.resolve(__dirname, 'build/static/js/@mf-types.d.ts'),
to: '@mf-types.d.ts',
noErrorOnMissing: true,
},
],
}),
new rspack.DefinePlugin({
NODE_ENV: process.env.NODE_ENV,
Expand Down
156 changes: 156 additions & 0 deletions ui/src/services/k8s/Metalk8sLocalVolumeProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { CoreV1Api, CustomObjectsApi } from '@kubernetes/client-node';
import Metalk8sLocalVolumeProvider from './Metalk8sLocalVolumeProvider';
import { Metalk8sV1alpha1VolumeClient } from './Metalk8sVolumeClient.generated';
import { updateApiServerConfig } from './api';

jest.mock('../k8s/api', () => ({
updateApiServerConfig: jest.fn(),
}));

describe('Metalk8sLocalVolumeProvider', () => {
let provider: Metalk8sLocalVolumeProvider;
const mockUrl = 'mock-url';
const mockToken = 'mock-token';

const mockCustomObjectsApi = {
listClusterCustomObject: jest.fn(),
} as unknown as CustomObjectsApi;

const mockVolumeClient = new Metalk8sV1alpha1VolumeClient(
mockCustomObjectsApi,
);

const mockCoreV1Api = {
listNode: jest.fn(),
listPersistentVolume: jest.fn(),
} as unknown as CoreV1Api;

beforeEach(() => {
(updateApiServerConfig as jest.Mock).mockReturnValue({
coreV1: mockCoreV1Api,
customObjects: mockCustomObjectsApi,
});

provider = new Metalk8sLocalVolumeProvider(mockUrl, mockToken);
provider.k8sClient = mockCoreV1Api;
provider.volumeClient = mockVolumeClient;
});

describe('listLocalPersistentVolumes', () => {
it('should return local persistent volumes for a given node.', async () => {
(mockCoreV1Api.listNode as jest.Mock).mockResolvedValue({
body: {
items: [
{
metadata: { name: 'test-node' },
status: {
addresses: [
{ type: 'Hostname', address: 'test-node' },
{ type: 'InternalIP', address: '192.168.1.100' },
],
},
},
],
},
});

(mockCoreV1Api.listPersistentVolume as jest.Mock).mockResolvedValue({
body: {
items: [
{
metadata: { name: 'test-volume' },
spec: {},
},
],
},
});

(
mockCustomObjectsApi.listClusterCustomObject as jest.Mock
).mockResolvedValue({
body: {
items: [
{
metadata: { name: 'test-volume' },
spec: {
nodeName: 'test-node',
rawBlockDevice: { devicePath: '/dev/sda' },
},
},
{
metadata: { name: 'test-lvm' },
spec: {
nodeName: 'test-node',
lvmLogicalVolume: { vgName: 'test-lvm', size: '10Gi' },
},
},
{
metadata: { name: 'test-sparseLoop' },
spec: {
nodeName: 'test-node',
sparseLoopDevice: {
size: '1Gi',
},
},
},
],
},
});

const volumes = await provider.listLocalPersistentVolumes('test-node');

expect(volumes).toHaveLength(3);
expect(volumes[0]).toMatchObject({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also test virtual| hardware here IMO

IP: '192.168.1.100',
devicePath: '/dev/sda',
nodeName: 'test-node',
});
expect(volumes[1]).toMatchObject({
IP: '192.168.1.100',
devicePath: 'test-lvm',
nodeName: 'test-node',
});
expect(volumes[2]).toMatchObject({
IP: '192.168.1.100',
devicePath: 'test-sparseLoop',
nodeName: 'test-node',
});
});

it('should raise an error if the node cannot be found', async () => {
(mockCoreV1Api.listNode as jest.Mock).mockResolvedValue({
body: { items: [] },
});

await expect(
provider.listLocalPersistentVolumes('non-existent-node'),
).rejects.toThrow('Failed to find IP for node non-existent-node');
});

it('should raise an error if volume retrieval fails', async () => {
(mockCoreV1Api.listNode as jest.Mock).mockResolvedValue({
body: {
items: [
{
metadata: { name: 'test-node' },
status: {
addresses: [
{ type: 'Hostname', address: 'test-node' },
{ type: 'InternalIP', address: '192.168.1.100' },
],
},
},
],
},
});

(
mockCustomObjectsApi.listClusterCustomObject as jest.Mock
).mockRejectedValue(new Error('Failed to fetch volumes'));

await expect(
provider.listLocalPersistentVolumes('test-node'),
).rejects.toThrow('Failed to fetch metalk8s volumes');
});
});
});
83 changes: 83 additions & 0 deletions ui/src/services/k8s/Metalk8sLocalVolumeProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { CoreV1Api, V1PersistentVolume } from '@kubernetes/client-node';
import * as ApiK8s from './api';
import {
Metalk8sV1alpha1VolumeClient,
Result,
} from './Metalk8sVolumeClient.generated';

function isError<T>(result: Result<T>): result is { error: any } {
return (result as { error: any }).error !== undefined;
}

export enum VolumeType {
Hardware = 'Hardware',
Virtual = 'Virtual',
}

export type LocalPersistentVolume = V1PersistentVolume & {
IP: string;
devicePath: string;
nodeName: string;
volumeType: VolumeType;
};

export default class Metalk8sLocalVolumeProvider {
volumeClient: Metalk8sV1alpha1VolumeClient;
k8sClient: CoreV1Api;
constructor(url: string, token: string) {
const { coreV1, customObjects } = ApiK8s.updateApiServerConfig(url, token);
this.volumeClient = new Metalk8sV1alpha1VolumeClient(customObjects);
this.k8sClient = coreV1;
}
public listLocalPersistentVolumes = async (
serverName: string,
): Promise<LocalPersistentVolume[]> => {
try {
const nodes = await this.k8sClient.listNode();
const nodeIP = nodes.body.items
.find((node) => node.metadata.name === serverName)
?.status.addresses.find((address) => address.type === 'InternalIP');

if (!nodeIP) {
throw new Error(`Failed to find IP for node ${serverName}`);
}

const volumes = await this.volumeClient.getMetalk8sV1alpha1VolumeList();

if (!isError(volumes)) {
const nodeVolumes = volumes.body.items.filter(
(volume) => volume.spec.nodeName === serverName,
);
const pv = await this.k8sClient.listPersistentVolume();

const localPv = nodeVolumes.reduce((acc, item) => {
const isLocalPv = pv.body.items.find(
(p) => p.metadata.name === item.metadata['name'],
);

return [
...acc,
{
...isLocalPv,
IP: nodeIP.address,
devicePath:
item.spec?.rawBlockDevice?.devicePath || item.metadata['name'],
Comment on lines +63 to +64
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you might be missing an info on which adapter to use to retrieve the disk capacity.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to return the volumeType : 'rawBlockDevice' | 'sparseLoopDevice' | 'lvmLogicalVolume'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Virtual | Hardware

nodeName: item.spec.nodeName,
volumeType: item.spec.rawBlockDevice
? VolumeType.Hardware
: VolumeType.Virtual,
},
];
}, [] as LocalPersistentVolume[]);

return localPv;
} else {
throw new Error(`Failed to fetch metalk8s volumes: ${volumes.error}`);
}
} catch (error) {
throw new Error(
`Failed to fetch local persistent volumes: ${error.message}`,
);
}
};
}
Loading