import { k8sPatch, k8sGet, K8sResourceCommon, K8sModel, } from '@openshift-console/dynamic-plugin-sdk'; const projectPauseModel: K8sModel = { apiVersion: 'projectpause.openshift.io/v1alpha1', kind: 'ProjectPauseConfig', plural: 'projectpauseconfigs', abbr: 'PPC', label: 'Project Pause Config', labelPlural: 'Project Pause Configs', }; export const DEFAULT_OPERATOR_CONFIG = { name: 'project-pause-operator-config', namespace: 'openshift-operators', }; type OperatorStatus = K8sResourceCommon & { status?: { ready?: boolean; }; }; export const getOperatorStatus = async (): Promise => { try { const response = await k8sGet({ model: projectPauseModel, name: DEFAULT_OPERATOR_CONFIG.name, ns: DEFAULT_OPERATOR_CONFIG.namespace, }); return response?.status?.ready === true; } catch (error) { console.error('Failed to get operator status:', error); return false; } }; export const pauseProject = async (namespace: string, isPaused: boolean): Promise => { await k8sPatch({ model: projectPauseModel, name: DEFAULT_OPERATOR_CONFIG.name, ns: DEFAULT_OPERATOR_CONFIG.namespace, data: [ { op: 'add', path: `/spec/projects/-`, value: { name: namespace, paused: isPaused, }, }, ], }); }; export const getProjectPauseState = async ( namespace: string, ): Promise<{ isPaused: boolean; isOperatorReady: boolean }> => { try { const [namespaceResource, operatorReady] = await Promise.all([ k8sGet({ model: projectPauseModel, name: namespace, }), getOperatorStatus(namespace), ]); const isPaused = namespaceResource?.metadata?.annotations?.['project-pause-operator.openshift.io/paused'] === 'true'; return { isPaused, isOperatorReady: operatorReady, }; } catch (error) { console.error('Failed to get project pause state:', error); return { isPaused: false, isOperatorReady: false, }; } };