Skip to content

Commit

Permalink
refactored k8 second pass
Browse files Browse the repository at this point in the history
  • Loading branch information
rsun19 committed Jun 25, 2024
1 parent 1c5b791 commit 9210700
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 122 deletions.
8 changes: 7 additions & 1 deletion frontend/src/__mocks__/mockServiceAccountK8sResource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { genUID } from '~/__mocks__/mockUtils';
import { ServiceAccountKind } from '~/k8sTypes';
import { K8sModelCommonMetadata, ServiceAccountKind } from '~/k8sTypes';

type MockResourceConfigType = {
name?: string;
Expand All @@ -19,3 +19,9 @@ export const mockServiceAccountK8sResource = ({
creationTimestamp: '2023-02-14T21:43:59Z',
},
});

export const ServiceAccountModelTest: K8sModelCommonMetadata = {
apiVersion: 'v1',
kind: 'ServiceAccount',
metadata: { name: 'test-name-sa', namespace: 'test-project', ownerReferences: [] },
};
23 changes: 23 additions & 0 deletions frontend/src/__mocks__/mockTrustyAIServiceK8sResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,26 @@ export const mockTrustyAIServiceK8sResource = ({
replicas: 0,
},
});

export const TrustyAITest: TrustyAIKind = {
apiVersion: 'trustyai.opendatahub.io/v1alpha1',
kind: 'TrustyAIService',
metadata: {
name: 'trustyai-service',
namespace: 'test-project',
},
spec: {
storage: {
format: 'PVC',
folder: '/inputs',
size: '1Gi',
},
data: {
filename: 'data.csv',
format: 'CSV',
},
metrics: {
schedule: '5s',
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import { InferenceServiceKind, ServingRuntimeKind } from '~/k8sTypes';
import { mockPrometheusServing } from '~/__mocks__/mockPrometheusServing';
import { mockPrometheusBias } from '~/__mocks__/mockPrometheusBias';
import { mockMetricsRequest } from '~/__mocks__/mockMetricsRequests';
import { mockTrustyAIServiceK8sResource } from '~/__mocks__/mockTrustyAIServiceK8sResource';
import {
TrustyAITest,
mockTrustyAIServiceK8sResource,
} from '~/__mocks__/mockTrustyAIServiceK8sResource';
import { mockRouteK8sResource } from '~/__mocks__/mockRouteK8sResource';
import { projectDetailsSettingsTab } from '~/__tests__/cypress/cypress/pages/projects';
import { mockServingRuntimeK8sResource } from '~/__mocks__/mockServingRuntimeK8sResource';
Expand Down Expand Up @@ -414,28 +417,7 @@ describe('Model Metrics', () => {
projectDetailsSettingsTab.findTrustyAIInstallCheckbox().check();

cy.wait('@installTrustyAI').then((interception) => {
expect(interception.request.body).to.be.eql({
apiVersion: 'trustyai.opendatahub.io/v1alpha1',
kind: 'TrustyAIService',
metadata: {
name: 'trustyai-service',
namespace: 'test-project',
},
spec: {
storage: {
format: 'PVC',
folder: '/inputs',
size: '1Gi',
},
data: {
filename: 'data.csv',
format: 'CSV',
},
metrics: {
schedule: '5s',
},
},
});
expect(interception.request.body).to.be.eql(TrustyAITest);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import {
mockRouteK8sResourceModelServing,
} from '~/__mocks__/mockRouteK8sResource';
import { mockSecretK8sResource } from '~/__mocks__/mockSecretK8sResource';
import { mockServiceAccountK8sResource } from '~/__mocks__/mockServiceAccountK8sResource';
import {
ServiceAccountModelTest,
mockServiceAccountK8sResource,
} from '~/__mocks__/mockServiceAccountK8sResource';
import {
mockServingRuntimeK8sResource,
mockServingRuntimeK8sResourceLegacy,
Expand Down Expand Up @@ -1021,11 +1024,7 @@ describe('Serving Runtime List', () => {
//dry run request
cy.wait('@createServiceAccount').then((interception) => {
expect(interception.request.url).to.include('?dryRun=All');
expect(interception.request.body).to.eql({
apiVersion: 'v1',
kind: 'ServiceAccount',
metadata: { name: 'test-name-sa', namespace: 'test-project', ownerReferences: [] },
});
expect(interception.request.body).to.eql(ServiceAccountModelTest);
});

//Actual request
Expand Down Expand Up @@ -1343,11 +1342,7 @@ describe('Serving Runtime List', () => {
//dry run request
cy.wait('@createServiceAccount').then((interception) => {
expect(interception.request.url).to.include('?dryRun=All');
expect(interception.request.body).to.eql({
apiVersion: 'v1',
kind: 'ServiceAccount',
metadata: { name: 'test-name-sa', namespace: 'test-project', ownerReferences: [] },
});
expect(interception.request.body).to.eql(ServiceAccountModelTest);
});

// Actual request
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/api/k8s/acceleratorProfiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { k8sGetResource, k8sListResource } from '@openshift/dynamic-plugin-sdk-utils';
import { AcceleratorProfileKind } from '~/k8sTypes';
import { AcceleratorProfileModel } from '~/api/models';
import { TolerationEffect, TolerationOperator } from '~/types';

export const listAcceleratorProfiles = async (
namespace: string,
Expand All @@ -20,3 +21,23 @@ export const getAcceleratorProfile = (
model: AcceleratorProfileModel,
queryOptions: { name, ns: namespace },
});

export const fakeAcceleratorProfile: AcceleratorProfileKind = {
apiVersion: 'dashboard.opendatahub.io/v1',
kind: 'AcceleratorProfile',
metadata: {
name: 'migrated-gpu',
},
spec: {
identifier: 'nvidia.com/gpu',
displayName: 'NVIDIA GPU',
enabled: true,
tolerations: [
{
key: 'nvidia.com/gpu',
operator: TolerationOperator.EXISTS,
effect: TolerationEffect.NO_SCHEDULE,
},
],
},
};
36 changes: 36 additions & 0 deletions frontend/src/api/k8sUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {
K8sModelCommon,
K8sResourceCommon,
} from '@openshift/dynamic-plugin-sdk-utils';
import { genUID } from '~/__mocks__/mockUtils';
import { KnownLabels } from '~/k8sTypes';
import { AwsKeys } from '~/pages/projects/dataConnections/const';
import { DataConnection } from '~/pages/projects/types';

export const addOwnerReference = <R extends K8sResourceCommon>(
resource: R,
Expand Down Expand Up @@ -40,3 +44,35 @@ export const groupVersionKind = (model: K8sModelCommon): K8sGroupVersionKind =>
version: model.apiVersion,
kind: model.kind,
});

export const dataConnection: DataConnection = {
type: 0,
data: {
kind: 'Secret',
apiVersion: 'v1',
metadata: {
name: 'test-secret',
namespace: 'test-project',
uid: genUID('secret'),
resourceVersion: '5985371',
creationTimestamp: '2023-03-22T16:18:56Z',
labels: {
[KnownLabels.DASHBOARD_RESOURCE]: 'true',
[KnownLabels.DATA_CONNECTION_AWS]: 'true',
},
annotations: {
'opendatahub.io/connection-type': 's3',
'openshift.io/display-name': 'Test Secret',
},
},
data: {
[AwsKeys.NAME]: 'test-secret',
AWS_ACCESS_KEY_ID: 'test',
AWS_DEFAULT_REGION: 'region',
AWS_S3_BUCKET: 'bucket',
AWS_S3_ENDPOINT: 'endpoint',
AWS_SECRET_ACCESS_KEY: 'test',
},
type: 'Opaque',
},
};
10 changes: 10 additions & 0 deletions frontend/src/k8sTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1329,3 +1329,13 @@ export type ModelRegistryKind = K8sResourceCommon & {
conditions?: K8sCondition[];
};
};

export type K8sModelCommonMetadata = {
apiVersion: string;
kind: string;
metadata: {
name: string;
namespace: string;
ownerReferences: string[];
};
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { KnownLabels } from '~/k8sTypes';
import { DataConnection, DataConnectionType } from '~/pages/projects/types';
import { DataConnectionType } from '~/pages/projects/types';
import {
convertAWSSecretData,
deleteDataConnection,
Expand All @@ -12,50 +11,16 @@ import {
isSecretAWSSecretKind,
} from '~/pages/projects/screens/detail/data-connections/utils';
import { AwsKeys } from '~/pages/projects/dataConnections/const';
import { deleteSecret } from '~/api';
import { mockSecretK8sResource } from '~/__mocks__/mockSecretK8sResource';

import { genUID } from '~/__mocks__/mockUtils';
import { mock200Status, mock404Error } from '~/__mocks__/mockK8sStatus';
import { dataConnection, deleteSecret } from '~/api';

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

const deletesecretMock = jest.mocked(deleteSecret);

const dataConnection: DataConnection = {
type: 0,
data: {
kind: 'Secret',
apiVersion: 'v1',
metadata: {
name: 'test-secret',
namespace: 'test-project',
uid: genUID('secret'),
resourceVersion: '5985371',
creationTimestamp: '2023-03-22T16:18:56Z',
labels: {
[KnownLabels.DASHBOARD_RESOURCE]: 'true',
[KnownLabels.DATA_CONNECTION_AWS]: 'true',
},
annotations: {
'opendatahub.io/connection-type': 's3',
'openshift.io/display-name': 'Test Secret',
},
},
data: {
[AwsKeys.NAME]: 'test-secret',
AWS_ACCESS_KEY_ID: 'test',
AWS_DEFAULT_REGION: 'region',
AWS_S3_BUCKET: 'bucket',
AWS_S3_ENDPOINT: 'endpoint',
AWS_SECRET_ACCESS_KEY: 'test',
},
type: 'Opaque',
},
};

const createDataConnection = (type: DataConnectionType) => ({
...dataConnection,
type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
import { K8sResourceCommon } from '@openshift/dynamic-plugin-sdk-utils';
import { servingRuntimeTemplate } from '~/__mocks__/mockServingRuntimeK8sResource';
import { addTypesToK8sListedResources } from '~/utilities/addTypesToK8sListedResources';

const servingRuntimeTemplate = {
apiVersion: 'template.openshift.io/v1',
kind: 'TemplateList',
items: [
{
metadata: {
name: 'test-model',
annotations: {
'openshift.io/display-name': 'New OVMS Server',
},
labels: {
'opendatahub.io/dashboard': 'true',
},
},
},
],
metadata: {
resourceVersion: '24348645',
continue: '',
},
};

describe('addTypesToK8sListedResources', () => {
it('should have apiVersion and kind as Template', () => {
const list = addTypesToK8sListedResources(servingRuntimeTemplate, 'Template');
Expand Down
30 changes: 2 additions & 28 deletions frontend/src/utilities/useAcceleratorProfileState.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import React from 'react';
import { fakeAcceleratorProfile } from '~/api';
import { AcceleratorProfileKind } from '~/k8sTypes';
import useAcceleratorProfiles from '~/pages/notebookController/screens/server/useAcceleratorProfiles';
import { useDashboardNamespace } from '~/redux/selectors';
import {
ContainerResourceAttributes,
ContainerResources,
Toleration,
TolerationEffect,
TolerationOperator,
} from '~/types';
import { ContainerResourceAttributes, ContainerResources, Toleration } from '~/types';
import useGenericObjectState, { GenericObjectState } from '~/utilities/useGenericObjectState';
import { getAcceleratorProfileCount, isEnumMember } from '~/utilities/utils';

Expand Down Expand Up @@ -96,27 +91,6 @@ const useAcceleratorProfileState = (
setData('additionalOptions', { useDisabled: acceleratorProfile });
}
} else {
// create a fake accelerator to use
const fakeAcceleratorProfile: AcceleratorProfileKind = {
apiVersion: 'dashboard.opendatahub.io/v1',
kind: 'AcceleratorProfile',
metadata: {
name: 'migrated-gpu',
},
spec: {
identifier: 'nvidia.com/gpu',
displayName: 'NVIDIA GPU',
enabled: true,
tolerations: [
{
key: 'nvidia.com/gpu',
operator: TolerationOperator.EXISTS,
effect: TolerationEffect.NO_SCHEDULE,
},
],
},
};

setData('acceleratorProfile', fakeAcceleratorProfile);
setData('acceleratorProfiles', [fakeAcceleratorProfile, ...acceleratorProfiles]);
setData('initialAcceleratorProfile', fakeAcceleratorProfile);
Expand Down

0 comments on commit 9210700

Please sign in to comment.