Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

chore: Store user preferences in a config-map #1260

Merged
merged 5 commits into from
Jan 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,36 @@
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import * as k8s from '@kubernetes/client-node';
import * as nsfw from 'nsfw';

import { CheK8SService, WorkspaceService } from '@eclipse-che/theia-remote-api/lib/common';
import { Emitter, Event } from '@theia/core';
import { Preferences, UserService } from '@eclipse-che/theia-remote-api/lib/common/user-service';
import { dirname, resolve } from 'path';
import { ensureDir, readFile, writeFile } from 'fs-extra';
import { inject, injectable } from 'inversify';

import { CheK8SServiceImpl } from '@eclipse-che/theia-remote-impl-che-server/lib/node/che-server-k8s-service-impl';
import { homedir } from 'os';
import { readFileSync } from 'fs';

export const THEIA_PREFERENCES_KEY = 'theia-user-preferences';
export const WORKSPACE_PREFERENCES_CONFIGMAP_NAME = 'workspace-preferences-configmap';
export const THEIA_USER_PREFERENCES_PATH = resolve(homedir(), '.theia', 'settings.json');
const INFRASTRUCTURE_NAMESPACE = 'infrastructureNamespace';

@injectable()
export class CheTheiaUserPreferencesSynchronizer {
@inject(UserService)
protected userService: UserService;

@inject(CheK8SService)
private readonly cheK8SService: CheK8SServiceImpl;

@inject(WorkspaceService)
private readonly workspaceService: WorkspaceService;

protected settingsJsonWatcher: nsfw.NSFW | undefined;

protected readonly onUserPreferencesModifyEmitter = new Emitter<object>();
Expand All @@ -39,11 +51,32 @@ export class CheTheiaUserPreferencesSynchronizer {
* Provides stored Theia user preferences into workspace.
*/
public async readTheiaUserPreferencesFromCheSettings(): Promise<void> {
const chePreferences = await this.userService.getUserPreferences(THEIA_PREFERENCES_KEY);
const theiaPreferences = chePreferences[THEIA_PREFERENCES_KEY] ? chePreferences[THEIA_PREFERENCES_KEY] : '{}';
const theiaPreferencesBeautified = JSON.stringify(JSON.parse(theiaPreferences), undefined, 3);
await ensureDir(dirname(THEIA_USER_PREFERENCES_PATH));
await writeFile(THEIA_USER_PREFERENCES_PATH, theiaPreferencesBeautified, 'utf8');
const client = this.cheK8SService.makeApiClient(k8s.CoreV1Api);
client.defaultHeaders = {
Accept: 'application/json',
'Content-Type': k8s.PatchUtils.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
};
const request = await client.readNamespacedConfigMap(
WORKSPACE_PREFERENCES_CONFIGMAP_NAME,
await this.getWorkspaceNamespace()
);
let content = '';
if (request.body && request.body.data && request.body.data[THEIA_PREFERENCES_KEY]) {
content = JSON.stringify(JSON.parse(request.body.data[THEIA_PREFERENCES_KEY]), undefined, 3);
}
await writeFile(THEIA_USER_PREFERENCES_PATH, content, 'utf8');
}

private async getWorkspaceNamespace(): Promise<string> {
// devworkspace use-case
const workspaceNamespace = process.env.DEVWORKSPACE_NAMESPACE;
if (workspaceNamespace) {
return Promise.resolve(workspaceNamespace);
}
// grab current workspace
const workspace = await this.workspaceService.currentWorkspace();
return workspace.attributes?.[INFRASTRUCTURE_NAMESPACE] || workspace.namespace || '';
}

public async getPreferences(): Promise<object> {
Expand All @@ -64,10 +97,23 @@ export class CheTheiaUserPreferencesSynchronizer {
return;
}

this.settingsJsonWatcher = await nsfw(THEIA_USER_PREFERENCES_PATH, (events: nsfw.FileChangeEvent[]) => {
this.settingsJsonWatcher = await nsfw(THEIA_USER_PREFERENCES_PATH, async (events: nsfw.FileChangeEvent[]) => {
for (const event of events) {
if (event.action === nsfw.actions.MODIFIED) {
this.updateTheiaUserPreferencesInCheSettings();
const client = this.cheK8SService.makeApiClient(k8s.CoreV1Api);
client.defaultHeaders = {
Accept: 'application/json',
'Content-Type': k8s.PatchUtils.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
};
const preferences = readFileSync(THEIA_USER_PREFERENCES_PATH).toString();
const theiaPreferencesBeautified = JSON.stringify(JSON.parse(preferences), undefined, 3);
await client.patchNamespacedConfigMap(
WORKSPACE_PREFERENCES_CONFIGMAP_NAME,
await this.getWorkspaceNamespace(),
{
data: { [THEIA_PREFERENCES_KEY]: theiaPreferencesBeautified },
}
);
return;
}
}
Expand Down