Skip to content
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

omit enable-auth and enable-route annotations for ServingRuntime when disabled #2037

Merged
Show file tree
Hide file tree
Changes from all commits
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
100 changes: 100 additions & 0 deletions frontend/src/api/__tests__/servingRuntimes.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
32 changes: 24 additions & 8 deletions frontend/src/api/k8s/servingRuntimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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 = {
Expand All @@ -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,
Expand All @@ -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() }),
},
Expand Down
Loading