diff --git a/frontend/src/api/__tests__/servingRuntimes.spec.ts b/frontend/src/api/__tests__/servingRuntimes.spec.ts new file mode 100644 index 0000000000..0b7c5117f1 --- /dev/null +++ b/frontend/src/api/__tests__/servingRuntimes.spec.ts @@ -0,0 +1,100 @@ +import { mockServingRuntimeK8sResource } from '~/__mocks__/mockServingRuntimeK8sResource'; +import { mockServingRuntimeTemplateK8sResource } from '~/__mocks__/mockServingRuntimeTemplateK8sResource'; +import { assembleServingRuntime } from '~/api/k8s/servingRuntimes'; +import { ServingRuntimeKind } from '~/k8sTypes'; + +global.structuredClone = (val: unknown) => JSON.parse(JSON.stringify(val)); + +describe('assembleServingRuntime', () => { + it('should omit enable-xxxx annotations when creating', async () => { + const servingRuntime = assembleServingRuntime( + { + name: 'my-serving-runtime', + servingRuntimeTemplateName: 'ovms', + numReplicas: 2, + modelSize: { name: 'Small', resources: {} }, + tokens: [], + // test false values + externalRoute: false, + tokenAuth: false, + }, + 'test', + mockServingRuntimeTemplateK8sResource({}).objects[0] as ServingRuntimeKind, + false, + false, // isEditing + ); + + expect(servingRuntime.metadata.annotations).toBeDefined(); + expect(servingRuntime.metadata.annotations?.['enable-auth']).toBe(undefined); + expect(servingRuntime.metadata.annotations?.['enable-route']).toBe(undefined); + }); + + it('should remove enable-xxxx annotations when editing', async () => { + const servingRuntime = assembleServingRuntime( + { + name: 'my-serving-runtime', + servingRuntimeTemplateName: 'ovms', + numReplicas: 2, + modelSize: { name: 'Small', resources: {} }, + tokens: [], + // test false values + externalRoute: false, + tokenAuth: false, + }, + 'test', + mockServingRuntimeK8sResource({ auth: true, route: true }), + false, + true, // isEditing + ); + + expect(servingRuntime.metadata.annotations).toBeDefined(); + expect(servingRuntime.metadata.annotations?.['enable-auth']).toBe(undefined); + expect(servingRuntime.metadata.annotations?.['enable-route']).toBe(undefined); + }); + + it('should add enable-xxxx annotations when creating', async () => { + const servingRuntime = assembleServingRuntime( + { + name: 'my-serving-runtime', + servingRuntimeTemplateName: 'ovms', + numReplicas: 2, + modelSize: { name: 'Small', resources: {} }, + tokens: [], + // test true values + externalRoute: true, + tokenAuth: true, + }, + 'test', + mockServingRuntimeTemplateK8sResource({}).objects[0] as ServingRuntimeKind, + false, + false, // isEditing + ); + + expect(servingRuntime.metadata.annotations).toBeDefined(); + expect(servingRuntime.metadata.annotations?.['enable-auth']).toBe('true'); + expect(servingRuntime.metadata.annotations?.['enable-route']).toBe('true'); + }); + + it('should add enable-xxxx annotations when editing', async () => { + const servingRuntime = assembleServingRuntime( + { + name: 'my-serving-runtime', + servingRuntimeTemplateName: 'ovms', + numReplicas: 2, + modelSize: { name: 'Small', resources: {} }, + tokens: [], + // test true values + externalRoute: true, + tokenAuth: true, + }, + 'test', + mockServingRuntimeK8sResource({ auth: false, route: false }), + false, + true, // isEditing + ); + + expect(servingRuntime.metadata.annotations).toBeDefined(); + expect(servingRuntime.metadata.annotations?.['enable-auth']).toBe('true'); + expect(servingRuntime.metadata.annotations?.['enable-route']).toBe('true'); + }); +}); diff --git a/frontend/src/api/k8s/servingRuntimes.ts b/frontend/src/api/k8s/servingRuntimes.ts index f1e757d932..899109086b 100644 --- a/frontend/src/api/k8s/servingRuntimes.ts +++ b/frontend/src/api/k8s/servingRuntimes.ts @@ -7,7 +7,12 @@ import { k8sUpdateResource, } from '@openshift/dynamic-plugin-sdk-utils'; import { ServingRuntimeModel } from '~/api/models'; -import { K8sAPIOptions, ServingContainer, ServingRuntimeKind } from '~/k8sTypes'; +import { + K8sAPIOptions, + ServingContainer, + ServingRuntimeAnnotations, + ServingRuntimeKind, +} from '~/k8sTypes'; import { CreatingServingRuntimeObject } from '~/pages/modelServing/screens/types'; import { ContainerResources } from '~/types'; import { getModelServingRuntimeName } from '~/pages/modelServing/utils'; @@ -17,7 +22,7 @@ import { AcceleratorState } from '~/utilities/useAcceleratorState'; import { getModelServingProjects } from './projects'; import { assemblePodSpecOptions, getshmVolume, getshmVolumeMount } from './utils'; -const assembleServingRuntime = ( +export const assembleServingRuntime = ( data: CreatingServingRuntimeObject, namespace: string, servingRuntime: ServingRuntimeKind, @@ -31,6 +36,21 @@ const assembleServingRuntime = ( : getModelServingRuntimeName(namespace); const updatedServingRuntime = { ...servingRuntime }; + const annotations: ServingRuntimeAnnotations = { + ...updatedServingRuntime.metadata.annotations, + }; + + if (externalRoute) { + annotations['enable-route'] = 'true'; + } else { + delete annotations['enable-route']; + } + if (tokenAuth) { + annotations['enable-auth'] = 'true'; + } else { + delete annotations['enable-auth']; + } + // TODO: Enable GRPC if (!isEditing) { updatedServingRuntime.metadata = { @@ -43,9 +63,7 @@ const assembleServingRuntime = ( 'opendatahub.io/dashboard': 'true', }, annotations: { - ...updatedServingRuntime.metadata.annotations, - 'enable-route': externalRoute ? 'true' : 'false', - 'enable-auth': tokenAuth ? 'true' : 'false', + ...annotations, ...(isCustomServingRuntimesEnabled && { 'openshift.io/display-name': displayName.trim() }), ...(isCustomServingRuntimesEnabled && { 'opendatahub.io/template-name': servingRuntime.metadata.name, @@ -60,9 +78,7 @@ const assembleServingRuntime = ( updatedServingRuntime.metadata = { ...updatedServingRuntime.metadata, annotations: { - ...updatedServingRuntime.metadata.annotations, - 'enable-route': externalRoute ? 'true' : 'false', - 'enable-auth': tokenAuth ? 'true' : 'false', + ...annotations, 'opendatahub.io/accelerator-name': acceleratorState?.accelerator?.metadata.name || '', ...(isCustomServingRuntimesEnabled && { 'openshift.io/display-name': displayName.trim() }), },