このコンテンツは選択した言語では利用できません。
Chapter 20. Configure Visual Studio Code - Open Source ("Code - OSS")
Configure Visual Studio Code - Open Source ("Code - OSS") for OpenShift Dev Spaces workspaces, including multi-root project layout, trusted and default extensions, and editor settings.
20.1. Configure single and multiroot workspaces リンクのコピーリンクがクリップボードにコピーされました!
Work with multiple project folders in the same workspace by using the multi-root workspace feature. This is useful when you are working on several related projects at once, such as product documentation and product code repositories.
By default, workspaces open in multi-root mode. After a workspace starts, the /projects/.code-workspace workspace file is generated. The workspace file contains all the projects described in the devfile.
{
"folders": [
{
"name": "project-1",
"path": "/projects/project-1"
},
{
"name": "project-2",
"path": "/projects/project-2"
}
]
}
If the workspace file already exists, it is updated and all missing projects are taken from the devfile. If you remove a project from the devfile, it remains in the workspace file.
You can change the default behavior and provide your own workspace file or switch to a single-root workspace.
Prerequisites
- You have a running instance of OpenShift Dev Spaces.
Procedure
Add a workspace file with the name
.code-workspaceto the root of your repository. After workspace creation, the Visual Studio Code - Open Source ("Code - OSS") uses the workspace file as it is.{ "folders": [ { "name": "project-name", "path": "." } ] }ImportantBe careful when creating a workspace file. In case of errors, an empty Visual Studio Code - Open Source ("Code - OSS") opens instead. If you have several projects, the workspace file is taken from the first project. If the workspace file does not exist in the first project, a new one is created and placed in the
/projectsdirectory.Define the
VSCODE_DEFAULT_WORKSPACEenvironment variable in your devfile with the path to an alternative workspace file.env: - name: VSCODE_DEFAULT_WORKSPACE value: "/projects/project-name/workspace-file"Define the
VSCODE_DEFAULT_WORKSPACEenvironment variable and set it to/to open a workspace in single-root mode.env: - name: VSCODE_DEFAULT_WORKSPACE value: "/"
Verification
- Start or restart the workspace and verify that Code - OSS opens with the expected workspace mode (single-root or multi-root).
20.2. Configure trusted extensions for Microsoft Visual Studio Code リンクのコピーリンクがクリップボードにコピーされました!
Grant specific extensions access to OAuth authentication tokens in Microsoft Visual Studio Code by configuring the trustedExtensionAuthAccess field. This allows extensions that require access to services such as GitHub, Microsoft, or any other OAuth-enabled service to authenticate without manual intervention.
"trustedExtensionAuthAccess": [
"<publisher1>.<extension1>",
"<publisher2>.<extension2>"
]
Define the variable in the devfile or in a ConfigMap.
Use the trustedExtensionAuthAccess field with caution as it could potentially lead to security risks if misused. Give access only to trusted extensions.
Since the Microsoft Visual Studio Code editor is bundled within che-code image, you can only change the product.json file when the workspace is started up.
Prerequisites
- You have a running instance of OpenShift Dev Spaces.
Procedure
Define the
VSCODE_TRUSTED_EXTENSIONSenvironment variable in devfile.yaml:env: - name: VSCODE_TRUSTED_EXTENSIONS value: "<publisher1>.<extension1>,<publisher2>.<extension2>"Alternatively, mount a ConfigMap with the
VSCODE_TRUSTED_EXTENSIONSenvironment variable. With a ConfigMap, the variable is propagated to all your workspaces and you do not need to add the variable to each devfile you are using.kind: ConfigMap apiVersion: v1 metadata: name: trusted-extensions labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-configmap: 'true' annotations: controller.devfile.io/mount-as: env data: VSCODE_TRUSTED_EXTENSIONS: '<publisher1>.<extension1>,<publisher2>.<extension2>'
Verification
-
Start or restart the workspace and verify that the
trustedExtensionAuthAccesssection is added to theproduct.jsonfile.
20.3. Configure default extensions リンクのコピーリンクがクリップボードにコピーされました!
Pre-install VS Code extensions in OpenShift Dev Spaces workspaces by configuring the DEFAULT_EXTENSIONS environment variable to provide a consistent set of editor extensions on workspace startup.
After startup, the editor checks for the DEFAULT_EXTENSIONS environment variable and installs the specified extensions in the background. To specify multiple extensions, separate the paths with a semicolon.
There are three ways to embed default .vsix extensions into your workspace:
- Add the extension binary to the source repository.
-
Use the devfile
postStartevent to fetch extension binaries from the network. -
Include the extensions'
.vsixbinaries in theche-codeimage.
Prerequisites
- You have a running OpenShift Dev Spaces instance.
Procedure
Add the extension binary to the source repository.
Adding the extension binary to the Git repository and defining the environment variable in the devfile is the easiest way to add default extensions to your workspace. If the
extension.vsixfile exists in the repository root, set theDEFAULT_EXTENSIONSenvironment variable for the tooling container in your.devfile.yaml:schemaVersion: 2.3.0 metadata: generateName: example-project components: - name: tools container: image: quay.io/devfile/universal-developer-image:ubi8-latest env: - name: 'DEFAULT_EXTENSIONS' value: '/projects/example-project/extension.vsix'Use the devfile
postStartevent to fetch extension binaries from the network.Use cURL or GNU Wget to download extensions to your workspace. Specify a devfile command to download extensions and add a
postStartevent to run the command on workspace startup. Define theDEFAULT_EXTENSIONSenvironment variable in the devfile:schemaVersion: 2.3.0 metadata: generateName: example-project components: - name: tools container: image: quay.io/devfile/universal-developer-image:ubi8-latest env: - name: DEFAULT_EXTENSIONS value: '/tmp/extension-1.vsix;/tmp/extension-2.vsix' commands: - id: add-default-extensions exec: # name of the tooling container component: tools # download several extensions using curl commandLine: | curl https://.../extension-1.vsix --location -o /tmp/extension-1.vsix curl https://.../extension-2.vsix --location -o /tmp/extension-2.vsix events: postStart: - add-default-extensionsWarningIn some cases curl may download a
.gzipcompressed file. This might make installing the extension impossible. To fix that, save the file as a .vsix.gz file and then decompress it with gunzip. This replaces the .vsix.gz file with an unpacked .vsix file:curl https://some-extension-url --location -o /tmp/extension.vsix.gz && gunzip /tmp/extension.vsix.gzInclude the extensions
.vsixbinaries in theche-codeimage.Bundling extensions in the editor image and defining the
DEFAULT_EXTENSIONSenvironment variable in a ConfigMap applies default extensions without changing the devfile.-
Create a directory and place your selected
.vsixextensions in this directory. Create a Dockerfile with the following content:
# inherit che-incubator/che-code:latest FROM quay.io/che-incubator/che-code:latest USER 0 # copy all .vsix files to /default-extensions directory RUN mkdir --mode=775 /default-extensions COPY --chmod=755 *.vsix /default-extensions/ # add instruction to the script to copy default extensions to the working container RUN echo "cp -r /default-extensions /checode/" >> /entrypoint-init-container.shBuild the image and then push it to a registry:
$ docker build -t yourname/che-code:next . $ docker push yourname/che-code:nextAdd the new ConfigMap to the user’s project, define the
DEFAULT_EXTENSIONSenvironment variable, and specify the absolute paths to the extensions. This ConfigMap sets the environment variable to all workspaces in the user’s project.kind: ConfigMap apiVersion: v1 metadata: name: vscode-default-extensions labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-configmap: 'true' annotations: controller.devfile.io/mount-as: env data: DEFAULT_EXTENSIONS: '/checode/default-extensions/extension1.vsix;/checode/default-extensions/extension2.vsix'- Open the OpenShift Dev Spaces Dashboard and navigate to the Create Workspace tab on the left side.
-
In the Editor Selector section, expand the Use an Editor Definition dropdown and set the editor URI to
yourname/che-code:next. - Create a workspace by selecting a sample or entering a Git repository URL.
-
Create a directory and place your selected
Verification
- Verify that the extensions are installed in the workspace by checking the Extensions panel in the editor.
20.4. Visual Studio Code - Open Source editor configuration sections リンクのコピーリンクがクリップボードにコピーされました!
The Visual Studio Code - Open Source ("Code - OSS") editor supports several configuration sections in a ConfigMap. Each section maps to a specific editor config file and controls a different aspect of editor behavior.
The following sections are currently supported:
settings.json- Contains various settings with which you can customize different parts of the Code - OSS editor.
extensions.json- Contains recommended extensions that are installed when a workspace is started.
product.json- Contains properties that you need to add to the editor’s product.json file. If the property already exists, its value is updated.
configurations.jsonContains properties for Code - OSS editor configuration. For example, you can use the
extensions.install-from-vsix-enabledproperty to disable theInstall from VSIXmenu item in the Extensions panel.NoteThe
extensions.install-from-vsix-enabledproperty disables only the UI action. Extensions can still be installed by using theworkbench.extensions.command.installFromVSIXAPI command or the CLI. To block these paths as well, manage extension installation policies.policy.json-
Controls Code - OSS extension installation by using the
AllowedExtensionspolicy and the ability to fully block extension installation.
20.5. Apply Code - OSS editor configurations with a ConfigMap リンクのコピーリンクがクリップボードにコピーされました!
Configure the Code - OSS editor for all workspaces by defining settings, recommended extensions, and product properties in a ConfigMap. When you start a workspace, the editor reads this ConfigMap and applies the configurations to the corresponding config files.
Prerequisites
- You have an active OpenShift Dev Spaces workspace or you are ready to start one.
-
You have an active
ocsession with permissions to create ConfigMaps in user projects.
Procedure
Add a new ConfigMap in valid JSON format to the user’s project, define the supported sections, and specify the properties you want to add.
apiVersion: v1 kind: ConfigMap metadata: name: vscode-editor-configurations labels: app.kubernetes.io/part-of: che.eclipse.org data: extensions.json: | { "recommendations": [ "dbaeumer.vscode-eslint", "github.vscode-pull-request-github" ] } settings.json: | { "window.header": "A HEADER MESSAGE", "window.commandCenter": false, "workbench.colorCustomizations": { "titleBar.activeBackground": "#CCA700", "titleBar.activeForeground": "#ffffff" } } product.json: | { "extensionEnabledApiProposals": { "ms-python.python": [ "contribEditorContentMenu", "quickPickSortByLabel" ] }, "trustedExtensionAuthAccess": [ "<publisher1>.<extension1>", "<publisher2>.<extension2>" ] } configurations.json: | { "extensions.install-from-vsix-enabled": false }where:
<publisher1>.<extension1>,<publisher2>.<extension2>-
The publisher and extension name pairs for extensions that are granted trusted authentication access. Use the format
publisher.extensionName.
-
Optional: To replicate the ConfigMap across all user projects while preventing user modifications, add the ConfigMap to the
openshift-devspacesnamespace instead of individual user projects. - Start or restart your workspace.
Verification
Verify that settings defined in the ConfigMap are applied using one of the following methods:
-
Use
F1to check if the defined settings are applied.Preferences: Open Remote Settings -
Ensure that the settings from the ConfigMap are present in the
/checode/remote/data/Machine/settings.jsonfile by using theF1command to inspect the file’s content.File: Open File…
-
Use
Verify that extensions defined in the ConfigMap are applied:
-
Go to the
Extensionsview (F1) and check that the extensions are installedView: Show Extensions -
Ensure that the extensions from the ConfigMap are present in the
.code-workspacefile by using theF1command. By default, the workspace file is placed atFile: Open File… /projects/.code-workspace.
-
Go to the
Verify that product properties defined in the ConfigMap are being added to the Visual Studio Code product.json:
-
Open a terminal, run the command
cat /checode/entrypoint-logs.txt | grep -a "Node.js dir"and copy the Visual Studio Code path. -
Press
Ctrl + O, paste the copied path and open product.json file. - Ensure that product.json file contains all the properties defined in the ConfigMap.
-
Open a terminal, run the command
Verify that
extensions.install-from-vsix-enabledproperty defined in the ConfigMap is applied to the Code - OSS editor:-
Open the Command Palette (use
F1) to check thatInstall from VSIXcommand is not present in the list of commands. -
Use
F1to open theOpen View Extensions Extensionspanel, then click…on the view (Views and More Actionstooltip) to check thatInstall from VSIXaction is absent in the list of actions. -
Go to the Explorer, find a file with the
vsixextension (redhat.vscode-yaml-1.17.0.vsix, for example), open menu for that file.Install from VSIXaction should be absent in the menu.
-
Open the Command Palette (use
20.6. Manage extension installation with a ConfigMap リンクのコピーリンクがクリップボードにコピーされました!
Control Code - OSS extension installation by using a ConfigMap. Enforce a fine-grained allow or deny list by using the AllowedExtensions policy.
You can also block installs through the CLI, default extensions, and the workbench.extensions.command.installFromVSIX API command. The following properties are supported:
-
BlockCliExtensionsInstallation— when enabled, blocks installation of extensions through the CLI. -
BlockDefaultExtensionsInstallation— when enabled, blocks installation of default extensions. See Section 20.3, “Configure default extensions”. -
BlockInstallFromVSIXCommandExtensionsInstallation— when enabled, blocks installation of extensions through theworkbench.extensions.command.installFromVSIXAPI command. -
AllowedExtensions— provides fine-grained control over Code - OSS extension installation. When this policy is applied, already installed extensions that are not allowed are disabled and display a warning. For conceptual background, see Configure allowed extensions.
Prerequisites
- You have administrator access to the OpenShift cluster.
Procedure
Add a new ConfigMap to the
openshift-devspacesnamespace and specify the properties you want to add:kind: ConfigMap apiVersion: v1 metadata: name: vscode-editor-configurations namespace: openshift-devspaces labels: app.kubernetes.io/component: workspaces-config app.kubernetes.io/part-of: che.eclipse.org annotations: controller.devfile.io/mount-as: subpath controller.devfile.io/mount-path: /checode-config controller.devfile.io/read-only: 'true' data: policy.json: | { "BlockCliExtensionsInstallation": true, "BlockDefaultExtensionsInstallation": true, "BlockInstallFromVSIXCommandExtensionsInstallation": true, "AllowedExtensions": { "*": true, "dbaeumer.vscode-eslint": false, "ms-python.python": false, "redhat": false } }NoteEnsure that the ConfigMap contains data in a valid JSON format.
Optional: To completely disable extension installation instead of using fine-grained control, set all extensions to disallowed:
kind: ConfigMap apiVersion: v1 metadata: name: vscode-editor-configurations namespace: openshift-devspaces labels: app.kubernetes.io/component: workspaces-config app.kubernetes.io/part-of: che.eclipse.org annotations: controller.devfile.io/mount-as: subpath controller.devfile.io/mount-path: /checode-config controller.devfile.io/read-only: 'true' data: policy.json: | { "AllowedExtensions": { "*": false } }- Start or restart your workspace.
Optional: Add the ConfigMap in the user’s project:
kind: ConfigMap apiVersion: v1 metadata: name: vscode-editor-configurations labels: controller.devfile.io/mount-to-devworkspace: 'true' controller.devfile.io/watch-configmap: 'true' annotations: controller.devfile.io/mount-as: subpath controller.devfile.io/mount-path: /checode-config controller.devfile.io/read-only: 'true' data: policy.json: | { "AllowedExtensions": { "*": false } }NoteWhen the ConfigMap is stored in the user’s project, the user can edit its values.
Verification
Verify that the
BlockCliExtensionsInstallationproperty is applied:-
Press F1, select Preferences: Open Settings (UI), and enter
BlockCliExtensionsInstallationin search. -
Provide a
.vsixfile and try CLI install. The installation fails with "Installation of extensions via CLI has been blocked by an administrator".
-
Press F1, select Preferences: Open Settings (UI), and enter
Verify that the
BlockDefaultExtensionsInstallationproperty is applied:- Check Settings for the property.
- Configure default extensions and verify they are not installed on workspace start or restart.
Verify that the
BlockInstallFromVSIXCommandExtensionsInstallationproperty is applied:- Check Settings for the property.
-
The
workbench.extensions.command.installFromVSIXAPI command is blocked.
Verify that rules defined in the
AllowedExtensionssection are applied:-
Check Settings
extensions.allowed. - Disallowed extensions display a "This extension cannot be installed because it is not in the allowed list" warning.
-
Check Settings