-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: manage default uri plugins for DevWorkspaces #426
+566
−9
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/dashboard-frontend/src/services/dashboard-backend-client/serverConfigApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2018-2021 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import axios from 'axios'; | ||
import common from '@eclipse-che/common'; | ||
import { prefix } from './const'; | ||
import { api } from '@eclipse-che/common'; | ||
|
||
/** | ||
* Returns an array of default plug-ins per editor | ||
* | ||
* @returns Promise resolving with the array of default plug-ins for the specified editor | ||
*/ | ||
export async function getDefaultPlugins(): Promise<api.IWorkspacesDefaultPlugins[]> { | ||
const url = `${prefix}/server-config/default-plugins`; | ||
try { | ||
const response = await axios.get(url); | ||
return response.data ? response.data : []; | ||
} catch (e) { | ||
throw `Failed to fetch default plugins. ${common.helpers.errors.getMessage(e)}`; | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
...-frontend/src/services/workspace-client/devworkspace/DevWorkspaceDefaultPluginsHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* Copyright (c) 2018-2021 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
|
||
import * as DwApi from '../../dashboard-backend-client/devWorkspaceApi'; | ||
import devfileApi from '../../devfileApi'; | ||
import { api } from '@eclipse-che/common'; | ||
import { createHash } from 'crypto'; | ||
import { injectable } from 'inversify'; | ||
import { V1alpha2DevWorkspaceSpecTemplateComponents } from '@devfile/api'; | ||
import { WorkspacesDefaultPlugins } from 'dashboard-frontend/src/store/Plugins/devWorkspacePlugins'; | ||
|
||
const DEFAULT_PLUGIN_ATTRIBUTE = 'che.eclipse.org/default-plugin'; | ||
|
||
/** | ||
* This class manages the default plugins defined in | ||
* the DevWorkspace's spec.template.components array. | ||
*/ | ||
@injectable() | ||
export class DevWorkspaceDefaultPluginsHandler { | ||
public async handle( | ||
workspace: devfileApi.DevWorkspace, | ||
editorId: string, | ||
defaultPlugins: WorkspacesDefaultPlugins, | ||
): Promise<void> { | ||
if (!defaultPlugins[editorId]) { | ||
return; | ||
} | ||
|
||
const componentsUpdated = this.handleUriPlugins(workspace, defaultPlugins[editorId]); | ||
if (componentsUpdated) { | ||
this.patchWorkspaceComponents(workspace); | ||
} | ||
} | ||
|
||
/** | ||
* Manages the default uri plugins from the devworkspace's spec.template.components | ||
* @param workspace A devworkspace to manage default plugins for | ||
* @param defaultPlugins The set of current default plugins uris | ||
* @returns true if the devworkspace's spec.template.components has been updated | ||
*/ | ||
private handleUriPlugins(workspace: devfileApi.DevWorkspace, defaultPlugins: string[]): boolean { | ||
const defaultUriPlugins = new Set( | ||
defaultPlugins.filter(plugin => { | ||
if (this.isUri(plugin)) { | ||
return true; | ||
} | ||
console.log(`Default plugin ${plugin} is not a uri. Ignoring.`); | ||
return false; | ||
}), | ||
); | ||
|
||
let componentsUpdated = this.removeOldDefaultUriPlugins(workspace, defaultUriPlugins); | ||
defaultUriPlugins.forEach(plugin => { | ||
const hash = createHash('MD5').update(plugin).digest('hex').substring(0, 20).toLowerCase(); | ||
const added = this.addDefaultPluginByUri(workspace, 'default-' + hash, plugin); | ||
componentsUpdated = added || componentsUpdated; | ||
}); | ||
|
||
return componentsUpdated; | ||
} | ||
|
||
private isUri(str: string): boolean { | ||
try { | ||
new URL(str); | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Checks if there are default plugins in the workspace that are not | ||
* specified in defaultUriPlugins. If such plugins are found, this function | ||
* removes them. | ||
* @param workspace A devworkspace to remove old default plugins for | ||
* @param defaultUriPlugins The set of current default plugins | ||
* @returns true if a plugin has been removed, false otherwise | ||
*/ | ||
private removeOldDefaultUriPlugins( | ||
workspace: devfileApi.DevWorkspace, | ||
defaultUriPlugins: Set<string>, | ||
): boolean { | ||
if (!workspace.spec.template.components) { | ||
return false; | ||
} | ||
|
||
const components = workspace.spec.template.components.filter(component => { | ||
if (!this.isDefaultPluginComponent(component) || !component.plugin?.uri) { | ||
// component is not a default uri plugin, keep component. | ||
return true; | ||
} | ||
return defaultUriPlugins.has(component.plugin.uri); | ||
}); | ||
|
||
const removed = workspace.spec.template.components.length !== components.length; | ||
workspace.spec.template.components = components; | ||
return removed; | ||
} | ||
|
||
/** | ||
* Returns true if component is a default plugin managed by this class | ||
* @param component The component to check | ||
* @returns true if component is a default plugin managed by this class | ||
*/ | ||
private isDefaultPluginComponent(component: V1alpha2DevWorkspaceSpecTemplateComponents): boolean { | ||
return component.attributes && component.attributes[DEFAULT_PLUGIN_ATTRIBUTE] | ||
? component.attributes[DEFAULT_PLUGIN_ATTRIBUTE].toString() === 'true' | ||
: false; | ||
} | ||
|
||
/** | ||
* Adds a default plugin to the workspace by uri if the default plugin does not | ||
* already exist | ||
* @param workspace A devworkspace | ||
* @param pluginName The name of the plugin | ||
* @param pluginUri The uri of the plugin | ||
* @returns true if the default plugin has been added | ||
*/ | ||
private addDefaultPluginByUri( | ||
workspace: devfileApi.DevWorkspace, | ||
pluginName: string, | ||
pluginUri: string, | ||
): boolean { | ||
if (!workspace.spec.template.components) { | ||
workspace.spec.template.components = []; | ||
} | ||
|
||
if (workspace.spec.template.components.find(component => component.name === pluginName)) { | ||
// plugin already exists | ||
return false; | ||
} | ||
|
||
workspace.spec.template.components.push({ | ||
name: pluginName, | ||
attributes: { [DEFAULT_PLUGIN_ATTRIBUTE]: true }, | ||
plugin: { uri: pluginUri }, | ||
}); | ||
return true; | ||
} | ||
|
||
private async patchWorkspaceComponents(workspace: devfileApi.DevWorkspace) { | ||
const patch: api.IPatch[] = [ | ||
{ | ||
op: 'replace', | ||
path: '/spec/template/components', | ||
value: workspace.spec.template.components, | ||
}, | ||
]; | ||
return DwApi.patchWorkspace(workspace.metadata.namespace, workspace.metadata.name, patch); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to match env variable names from the che-operator: https://github.com/dkwon17/che-operator/blob/051d7bd9d30d1b48a9752b095426c39f2b19aee1/pkg/deploy/dashboard/deployment_dashboard.go#L59-L64