Chapter 11. Integrate with OpenShift
Manage OpenShift Dev Spaces workspaces by using OpenShift APIs, automatic token injection, and the OpenShift web console.
OpenShift Dev Spaces workspaces run as OpenShift resources. You can create, list, stop, and remove workspaces by using standard OpenShift tools such as oc or the web console. You can also navigate between OpenShift Dev Spaces and the OpenShift Developer perspective.
11.1. OpenShift integration overview Copy linkLink copied to clipboard!
OpenShift Dev Spaces integrates with OpenShift to provide automatic API token injection, direct console access, and seamless navigation between workspaces and cluster resources.
Key integration features include:
- Managing workspaces using Kubernetes APIs
- Automatic token injection for cluster access
- Navigating to OpenShift Dev Spaces from the OpenShift Developer Perspective
- Navigating to the OpenShift Web Console from OpenShift Dev Spaces
On your organization’s OpenShift cluster, each OpenShift Dev Spaces workspace is represented as a DevWorkspace custom resource of the same name. For example, a workspace named my-workspace in the OpenShift Dev Spaces dashboard has a corresponding DevWorkspace custom resource in the user’s project. You can manage OpenShift Dev Spaces workspaces by using OpenShift APIs with clients such as the command-line oc.
Each DevWorkspace custom resource contains details derived from the devfile of the Git repository cloned for the workspace, such as devfile commands and workspace container configurations.
11.2. List all workspaces Copy linkLink copied to clipboard!
List your workspaces from the command line to check their status, identify stopped or failed workspaces, and monitor resource usage across your OpenShift Dev Spaces environment.
Prerequisites
-
You have an active
ocsession with permissions togettheDevWorkspaceresources in your project on the cluster. See Getting started with the CLI. -
You know the relevant OpenShift Dev Spaces user namespace on the cluster. Visit
https://<openshift_dev_spaces_fqdn>/api/kubernetes/namespaceto get your OpenShift Dev Spaces user namespace asname. -
You are in the OpenShift Dev Spaces user namespace on the cluster. On OpenShift, use
octo display your current namespace or switch to a namespace.
Procedure
List your workspaces:
$ oc get devworkspacesExample output:
NAMESPACE NAME DEVWORKSPACE ID PHASE INFO user1-dev spring-petclinic workspace6d99e9ffb9784491 Running https://url-to-workspace.com user1-dev golang-example workspacedf64e4a492cd4701 Stopped Stopped user1-dev python-hello-world workspace69c26884bbc141f2 Failed Container tooling has state CrashLoopBackOffOptional: Add the
--watchflag to show PHASE changes live:$ oc get devworkspaces --watchOptional: Add the
--all-namespacesflag to list workspaces from all OpenShift Dev Spaces users. This requires administrative permissions on the cluster.$ oc get devworkspaces --all-namespaces
11.3. Create workspaces Copy linkLink copied to clipboard!
If your use case does not permit use of the OpenShift Dev Spaces dashboard, create workspaces with OpenShift APIs by applying custom resources to the cluster.
Creating workspaces through the OpenShift Dev Spaces dashboard provides better user experience and configuration benefits compared to using the command line:
- As a user, you are automatically logged in to the cluster.
- OpenShift clients work automatically.
-
OpenShift Dev Spaces and its components automatically convert the target Git repository’s devfile into the
DevWorkspaceandDevWorkspaceTemplatecustom resources on the cluster. -
Access to the workspace is secured by default with the
routingClass: chein theDevWorkspaceof the workspace. -
Recognition of the
DevWorkspaceOperatorConfigconfiguration is managed by OpenShift Dev Spaces. Recognition of configurations in
spec.devEnvironmentsspecified in theCheClustercustom resource including:-
Persistent storage strategy is specified with
devEnvironments.storage. -
Default IDE is specified with
devEnvironments.defaultEditor. -
Default plugins are specified with
devEnvironments.defaultPlugins. -
Container build configuration is specified with
devEnvironments.containerBuildConfiguration.
-
Persistent storage strategy is specified with
Prerequisites
-
You have an active
ocsession with permissions to createDevWorkspaceresources in your project on the cluster. See Getting started with the CLI. -
You know your OpenShift Dev Spaces user namespace on the cluster. To retrieve it, visit
https://<openshift_dev_spaces_fqdn>/api/kubernetes/namespaceand find thenamevalue. You are in your OpenShift Dev Spaces user namespace on the cluster. On OpenShift, use
octo display your current namespace or switch to a namespace.To create workspaces for other users, create the
DevWorkspacecustom resource in the target user namespace. This namespace must be provisioned by OpenShift Dev Spaces or by the administrator. See Configuring a user namespace.
Procedure
Copy the contents of the target Git repository’s devfile to prepare the
DevWorkspacecustom resource.For example:
components: - name: tooling-container container: image: quay.io/devfile/universal-developer-image:ubi9-latestFor more details, see the devfile v2 documentation.
Create a
DevWorkspacecustom resource, pasting the devfile contents from the previous step under thespec.templatefield.For example:
kind: DevWorkspace apiVersion: workspace.devfile.io/v1alpha2 metadata: name: my-devworkspace namespace: user1-dev spec: routingClass: che started: true contributions: - name: ide uri: http://devspaces-dashboard.openshift-devspaces.svc.cluster.local:8080/dashboard/api/editors/devfile?che-editor=che-incubator/che-code/latest template: projects: - name: my-project-name git: remotes: origin: https://github.com/eclipse-che/che-docs components: - name: tooling-container container: image: quay.io/devfile/universal-developer-image:ubi9-latest env: - name: CHE_DASHBOARD_URL value: https://<openshift_dev_spaces_fqdn>/dashboard/where:
- name
-
Name of the
DevWorkspacecustom resource. This is the name of the new workspace. - namespace
- User namespace, which is the target project for the new workspace.
- started
-
Determines whether the workspace must be started when the
DevWorkspacecustom resource is created. - contributions
- URL reference to the Microsoft Visual Studio Code - Open Source IDE devfile.
- projects
- Details about the Git repository to clone into the workspace when it starts.
- components
- List of components such as workspace containers and volume components.
- CHE_DASHBOARD_URL
- URL to OpenShift Dev Spaces dashboard.
Apply the
DevWorkspacecustom resource to the cluster.$ oc apply -f <devworkspace>.yaml
Verification
Verify that the workspace is starting by checking the PHASE status of the
DevWorkspace.$ oc get devworkspaces -n <user_project> --watchExample output:
NAMESPACE NAME DEVWORKSPACE ID PHASE INFO user1-dev my-devworkspace workspacedf64e4a492cd4701 Starting Waiting for workspace deploymentWhen the workspace has successfully started, its PHASE status changes to Running in the output of the
oc get devworkspacescommand.Example output:
NAMESPACE NAME DEVWORKSPACE ID PHASE INFO user1-dev my-devworkspace workspacedf64e4a492cd4701 Running https://url-to-workspace.comYou can then open the workspace by using one of these options:
-
Visit the URL provided in the INFO section of the output of the
oc get devworkspacescommand. - Open the workspace from the OpenShift Dev Spaces dashboard.
-
Visit the URL provided in the INFO section of the output of the
11.4. Stop workspaces Copy linkLink copied to clipboard!
Stop a workspace by setting the spec.started field in the DevWorkspace custom resource to false.
Prerequisites
-
You have an active
ocsession on the cluster. See Getting started with the CLI. -
You have the workspace name. Run
oc get devworkspacesto list workspace names. -
You have the relevant OpenShift Dev Spaces user namespace on the cluster. Visit
https://<openshift_dev_spaces_fqdn>/api/kubernetes/namespaceto get your OpenShift Dev Spaces user namespace asname. -
You are in the OpenShift Dev Spaces user namespace on the cluster. On OpenShift, use
octo display your current namespace or switch to a namespace.
Procedure
Run the following command to stop a workspace:
$ oc patch devworkspace <workspace_name> \ -p '{"spec":{"started":false}}' \ --type=merge -n <user_namespace> && \ oc wait --for=jsonpath='{.status.phase}'=Stopped \ dw/<workspace_name> -n <user_namespace>
11.5. Start stopped workspaces Copy linkLink copied to clipboard!
Start a stopped workspace by setting the spec.started field in the DevWorkspace custom resource to true.
Prerequisites
-
You have an active
ocsession on the cluster. See Getting started with the CLI. -
You have the workspace name. Run
oc get devworkspacesto list workspace names. -
You have the relevant OpenShift Dev Spaces user namespace on the cluster. Visit
https://<openshift_dev_spaces_fqdn>/api/kubernetes/namespaceto get your OpenShift Dev Spaces user namespace asname. -
You are in the OpenShift Dev Spaces user namespace on the cluster. On OpenShift, use
octo display your current namespace or switch to a namespace.
Procedure
Run the following command to start a stopped workspace:
$ oc patch devworkspace <workspace_name> \ -p '{"spec":{"started":true}}' \ --type=merge -n <user_namespace> && \ oc wait --for=jsonpath='{.status.phase}'=Running \ dw/<workspace_name> -n <user_namespace>
11.6. Remove workspaces Copy linkLink copied to clipboard!
Remove a workspace from the command line by deleting its DevWorkspace custom resource. The OpenShift Dev Spaces dashboard is the recommended method for routine operations.
Deleting the DevWorkspace custom resource also deletes other workspace resources if they were created by OpenShift Dev Spaces: for example, the referenced DevWorkspaceTemplate and per-workspace PersistentVolumeClaims.
Prerequisites
-
You have an active
ocsession on the cluster. See Getting started with the CLI. -
You know the workspace name. Run
oc get devworkspacesto list workspace names. -
You know the relevant OpenShift Dev Spaces user namespace on the cluster. Visit
https://<openshift_dev_spaces_fqdn>/api/kubernetes/namespaceto get your OpenShift Dev Spaces user namespace asname. On OpenShift, useocto display your current namespace or switch to a namespace. - You are in the OpenShift Dev Spaces user namespace on the cluster.
Procedure
Run the following command to remove a workspace:
$ oc delete devworkspace <workspace_name> -n <user_namespace>
11.7. Use automatic OpenShift token injection Copy linkLink copied to clipboard!
Use the OpenShift user token that is automatically injected into workspace containers to run oc and kubectl commands against the OpenShift cluster without explicit login.
Automatic token injection works only on the OpenShift infrastructure.
Prerequisites
- You have a running instance of Red Hat OpenShift Dev Spaces.
Procedure
- Open the OpenShift Dev Spaces dashboard and start a workspace.
- After the workspace starts, open a terminal in the workspace container.
Run
ocorkubectlcommands to deploy applications, inspect and manage cluster resources, or view logs. The injected OpenShift user token authenticates these commands automatically.$ oc get pods
11.9. Edit application code from the OpenShift Developer Perspective Copy linkLink copied to clipboard!
Edit the source code of applications running on OpenShift directly from the Developer Perspective to fix and iterate on deployed components without switching tools.
Prerequisites
- You have OpenShift Dev Spaces deployed on the same OpenShift 4 cluster.
Procedure
- Open the Topology view to list all projects.
-
In the Select an Application search field, type
workspaceto list all workspaces. Click the workspace to edit.
The deployments are displayed as graphical circles surrounded by circular buttons. One of these buttons is Edit Source Code.
- Click the Edit Source Code button. This redirects to a workspace with the cloned source code of the application component.