Skip to content

Commit

Permalink
Merge pull request #2037 from christianvogt/kserve-annotations
Browse files Browse the repository at this point in the history
omit enable-auth and enable-route annotations for ServingRuntime when disabled
  • Loading branch information
openshift-ci[bot] authored Nov 2, 2023
2 parents 0ac1401 + 33650e1 commit 693ffc6
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 8 deletions.
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

0 comments on commit 693ffc6

Please sign in to comment.