-
Notifications
You must be signed in to change notification settings - Fork 47
ARTESCA-14623 // implement Metalk8sVolumeProvider #4530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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({ | ||
| 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'); | ||
| }); | ||
| }); | ||
| }); | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to return the volumeType :
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`, | ||
| ); | ||
| } | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
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