From 0d5ffe52002a4555cb7110392e542f192b308833 Mon Sep 17 00:00:00 2001 From: Andrew Ballantyne Date: Mon, 10 Jun 2024 16:42:52 -0400 Subject: [PATCH] Added migration for disabling trusty bias for RHOAI --- backend/src/utils/resourceUtils.ts | 103 +++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 19 deletions(-) diff --git a/backend/src/utils/resourceUtils.ts b/backend/src/utils/resourceUtils.ts index 081ff8e6ba..3feeb60a5c 100644 --- a/backend/src/utils/resourceUtils.ts +++ b/backend/src/utils/resourceUtils.ts @@ -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'; @@ -56,24 +57,28 @@ let buildsWatcher: ResourceWatcher; let consoleLinksWatcher: ResourceWatcher; let quickStartWatcher: ResourceWatcher; +const DASHBOARD_CONFIG = { + group: 'opendatahub.io', + version: 'v1alpha', + plural: 'odhdashboardconfigs', + dashboardName: process.env['DASHBOARD_CONFIG'] || dashboardConfigMapName, +}; + const fetchDashboardCR = async (fastify: KubeFastifyInstance): Promise => { - 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 => { +const fetchOrCreateDashboardCR = async (fastify: KubeFastifyInstance): Promise => { 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; @@ -105,10 +110,10 @@ const fetchOrCreateDashboardCR = async ( const createDashboardCR = (fastify: KubeFastifyInstance): Promise => { 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]) @@ -830,7 +835,7 @@ export const cleanupDSPSuffix = async (fastify: KubeFastifyInstance): Promise + 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 => { + if (!isRHOAI(fastify)) { + // ODH deployment, leave trusty as-is + return dashboardConfig; + } + + // For RHOAI deployments... + const patchChange: RecursivePartial = { + 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;