diff --git a/backend/src/routes/api/cluster-settings/clusterSettingsUtils.ts b/backend/src/routes/api/cluster-settings/clusterSettingsUtils.ts index fd04cd2ae6..d5165d0425 100644 --- a/backend/src/routes/api/cluster-settings/clusterSettingsUtils.ts +++ b/backend/src/routes/api/cluster-settings/clusterSettingsUtils.ts @@ -1,6 +1,9 @@ import { FastifyRequest } from 'fastify'; +import { scaleDeploymentConfig } from '../../../utils/deployment'; import { KubeFastifyInstance, ClusterSettings } from '../../../types'; +const name = 'jupyterhub-cfg'; + export const updateClusterSettings = async ( fastify: KubeFastifyInstance, request: FastifyRequest, @@ -9,9 +12,10 @@ export const updateClusterSettings = async ( const namespace = fastify.kube.namespace; const query = request.query as { [key: string]: string }; try { + const jupyterhubCM = await coreV1Api.readNamespacedConfigMap(name, namespace); if (query.pvcSize) { await coreV1Api.patchNamespacedConfigMap( - 'jupyterhub-cfg', + name, namespace, { data: { singleuser_pvc_size: `${query.pvcSize}Gi` }, @@ -26,6 +30,9 @@ export const updateClusterSettings = async ( }, }, ); + if (jupyterhubCM.body.data.singleuser_pvc_size.replace('Gi', '') !== query.pvcSize) { + await scaleDeploymentConfig(fastify, 'jupyterhub', 0); + } } return { success: true, error: null }; } catch (e) { @@ -42,7 +49,7 @@ export const getClusterSettings = async ( const coreV1Api = fastify.kube.coreV1Api; const namespace = fastify.kube.namespace; try { - const clusterSettingsRes = await coreV1Api.readNamespacedConfigMap('jupyterhub-cfg', namespace); + const clusterSettingsRes = await coreV1Api.readNamespacedConfigMap(name, namespace); return { pvcSize: Number(clusterSettingsRes.body.data.singleuser_pvc_size.replace('Gi', '')), }; diff --git a/backend/src/routes/api/status/index.ts b/backend/src/routes/api/status/index.ts index c8ae9d31e6..c2e6e41b86 100644 --- a/backend/src/routes/api/status/index.ts +++ b/backend/src/routes/api/status/index.ts @@ -6,6 +6,10 @@ type groupObjResponse = { users: string[]; }; +type groupObjResponse = { + users: string[]; +}; + const status = async ( fastify: KubeFastifyInstance, request: FastifyRequest, diff --git a/backend/src/utils/deployment.ts b/backend/src/utils/deployment.ts new file mode 100644 index 0000000000..e94a8e8a16 --- /dev/null +++ b/backend/src/utils/deployment.ts @@ -0,0 +1,36 @@ +import { KubeFastifyInstance } from '../types'; + +export const scaleDeploymentConfig = async ( + fastify: KubeFastifyInstance, + name: string, + replicas: number, +): Promise<void> => { + const customObjectsApi = fastify.kube.customObjectsApi; + const group = 'apps.openshift.io'; + const version = 'v1'; + const plural = 'deploymentconfigs'; + const namespace = fastify.kube.namespace; + try { + const res = await customObjectsApi.getNamespacedCustomObject( + group, + version, + namespace, + plural, + name, + ); + const deployment: any = res.body; + + deployment.spec.replicas = replicas; + + await customObjectsApi.replaceNamespacedCustomObject( + group, + version, + namespace, + plural, + name, + deployment, + ); + } catch (e) { + throw new Error('Error scale the deployment pods: ' + e.message); + } +};