Skip to content

Commit

Permalink
Added migration for disabling trusty bias for RHOAI
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewballantyne committed Jun 12, 2024
1 parent 5f915b0 commit 0d5ffe5
Showing 1 changed file with 84 additions and 19 deletions.
103 changes: 84 additions & 19 deletions backend/src/utils/resourceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { blankDashboardCR } from './constants';
import { getIsAppEnabled, getRouteForApplication, getRouteForClusterId } from './componentUtils';
import { createCustomError } from './requestUtils';
import { getDetectedAccelerators } from '../routes/api/accelerators/acceleratorUtils';
import { RecursivePartial } from '../typeHelpers';

const dashboardConfigMapName = 'odh-dashboard-config';
const consoleLinksGroup = 'console.openshift.io';
Expand All @@ -56,24 +57,28 @@ let buildsWatcher: ResourceWatcher<BuildStatus>;
let consoleLinksWatcher: ResourceWatcher<ConsoleLinkKind>;
let quickStartWatcher: ResourceWatcher<QuickStart>;

const DASHBOARD_CONFIG = {
group: 'opendatahub.io',
version: 'v1alpha',
plural: 'odhdashboardconfigs',
dashboardName: process.env['DASHBOARD_CONFIG'] || dashboardConfigMapName,
};

const fetchDashboardCR = async (fastify: KubeFastifyInstance): Promise<DashboardConfig[]> => {
const dashboardName = process.env['DASHBOARD_CONFIG'] || dashboardConfigMapName;
return fetchOrCreateDashboardCR(fastify, dashboardName).then((dashboardCR) => {
return migrateTemplateDisablement(fastify, dashboardCR).then((dashboardCR) => [dashboardCR]);
});
return fetchOrCreateDashboardCR(fastify)
.then((dashboardCR) => migrateTemplateDisablement(fastify, dashboardCR))
.then((dashboardCR) => migrateBiasTrustyForRHOAI(fastify, dashboardCR))
.then((dashboardCR) => [dashboardCR]);
};

const fetchOrCreateDashboardCR = async (
fastify: KubeFastifyInstance,
dashboardName: string,
): Promise<DashboardConfig> => {
const fetchOrCreateDashboardCR = async (fastify: KubeFastifyInstance): Promise<DashboardConfig> => {
return fastify.kube.customObjectsApi
.getNamespacedCustomObject(
'opendatahub.io',
'v1alpha',
DASHBOARD_CONFIG.group,
DASHBOARD_CONFIG.version,
fastify.kube.namespace,
'odhdashboardconfigs',
dashboardName,
DASHBOARD_CONFIG.plural,
DASHBOARD_CONFIG.dashboardName,
)
.then((res) => {
const dashboardCR = res?.body as DashboardConfig;
Expand Down Expand Up @@ -105,10 +110,10 @@ const fetchOrCreateDashboardCR = async (
const createDashboardCR = (fastify: KubeFastifyInstance): Promise<DashboardConfig> => {
return fastify.kube.customObjectsApi
.createNamespacedCustomObject(
'opendatahub.io',
'v1alpha',
DASHBOARD_CONFIG.group,
DASHBOARD_CONFIG.version,
fastify.kube.namespace,
'odhdashboardconfigs',
DASHBOARD_CONFIG.plural,
blankDashboardCR,
)
.then((result) => [result.body])
Expand Down Expand Up @@ -830,7 +835,7 @@ export const cleanupDSPSuffix = async (fastify: KubeFastifyInstance): Promise<vo
/**
* @deprecated - Look to remove asap (see comments below)
* Migrate the template enablement from a current annotation in the Template Object to a list in ODHDashboardConfig
* We are migrating this from v2.11.0, so we can remove this code when we no longer support that version.
* We are migrating this from Dashboard v2.11.0, so we can remove this code when we no longer support that version.
*/
export const migrateTemplateDisablement = async (
fastify: KubeFastifyInstance,
Expand Down Expand Up @@ -867,10 +872,10 @@ export const migrateTemplateDisablement = async (

return fastify.kube.customObjectsApi
.patchNamespacedCustomObject(
'opendatahub.io',
'v1alpha',
DASHBOARD_CONFIG.group,
DASHBOARD_CONFIG.version,
dashboardConfig.metadata.namespace,
'odhdashboardconfigs',
DASHBOARD_CONFIG.plural,
dashboardConfig.metadata.name,
[
{
Expand Down Expand Up @@ -905,6 +910,66 @@ export const migrateTemplateDisablement = async (
});
};

/**
* TODO: There should be a better way to go about this... but the namespace is unlikely to ever change
*/
export const isRHOAI = (fastify: KubeFastifyInstance): boolean =>
fastify.kube.namespace === 'redhat-ods-applications';

/**
* As we release Dashboard v2.24.0 we will be re-enabling Trusty for RHOAI, but not for Bias Metrics.
* This means we will need to make sure Bias is in a state of disabled as the API won't be available.
*/
export const migrateBiasTrustyForRHOAI = async (
fastify: KubeFastifyInstance,
dashboardConfig: DashboardConfig,
): Promise<DashboardConfig> => {
if (!isRHOAI(fastify)) {
// ODH deployment, leave trusty as-is
return dashboardConfig;
}

// For RHOAI deployments...
const patchChange: RecursivePartial<DashboardConfig> = {
spec: { dashboardConfig: { disableBiasMetrics: true } },
};
try {
switch (dashboardConfig.spec.dashboardConfig.disableBiasMetrics) {
case true:
// Explicitly disabled - ideal state, ignore
fastify.log.info('TrustyAI BiasMetrics disabled. No change applied.');
return dashboardConfig;
case false:
// Explicitly enabled - bad state, disable both locally and with a patch
await fastify.kube.customObjectsApi.patchNamespacedCustomObject(
DASHBOARD_CONFIG.group,
DASHBOARD_CONFIG.version,
dashboardConfig.metadata.namespace,
DASHBOARD_CONFIG.plural,
dashboardConfig.metadata.name,
patchChange,
undefined,
undefined,
undefined,
{
headers: { 'Content-type': PatchUtils.PATCH_FORMAT_JSON_MERGE_PATCH },
},
);
fastify.log.info(
'TrustyAI BiasMetrics was enabled. Patched OdhDashboardConfig to disable.',
);
return _.merge({}, dashboardConfig, patchChange);
case undefined:
default:
// Invalid / Missing state - do nothing
}
} catch (e) {
fastify.log.error(e, 'TrustyAI BiasMetrics error.');
}

return dashboardConfig;
};

export const getServingRuntimeNameFromTemplate = (template: Template): string =>
template.objects[0].metadata.name;

Expand Down

0 comments on commit 0d5ffe5

Please sign in to comment.