Manage workspaces from the command line
Manage Che workspaces with kubectl and kubectl to list, create, stop, start, and remove workspaces without using the dashboard.
On your organization’s Kubernetes cluster, Che workspaces are represented as DevWorkspace custom resources of the same name.
As a result, if there is a workspace named my-workspace in the Che dashboard, there is a corresponding DevWorkspace custom resource named my-workspace in the user’s namespace on the cluster.
Because each DevWorkspace custom resource on the cluster represents a Che workspace, you can manage Che workspaces by using Kubernetes APIs with clients such as the command-line kubectl.
Each DevWorkspace custom resource contains details derived from the devfile of the Git repository cloned for the workspace. For example, a devfile might provide devfile commands and workspace container configurations.
List all workspaces
List your workspaces from the command line to check their status, identify stopped or failed workspaces, and monitor resource usage across your Che environment.
-
You have an active
kubectlsession with permissions togettheDevWorkspaceresources in your namespace on the cluster. See Overview of kubectl. -
You know the relevant Che user namespace on the cluster. Visit
https://<che_fqdn>/api/kubernetes/namespaceto get your Che user namespace asname. -
You are in the Che user namespace on the cluster. On Kubernetes, use
kubectlto display your current namespace or switch to a namespace.
-
List your workspaces:
$ kubectl get devworkspacesExample 1. OutputNAMESPACE 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 CrashLoopBackOff
-
Optional: Add the
--watchflag to show PHASE changes live:$ kubectl get devworkspaces --watch -
Optional: Add the
--all-namespacesflag to list workspaces from all Che users. This requires administrative permissions on the cluster.$ kubectl get devworkspaces --all-namespaces
Create workspaces
If your use case does not permit use of the Che dashboard, create workspaces with Kubernetes APIs by applying custom resources to the cluster.
|
Creating workspaces through the Che dashboard provides better user experience and configuration benefits compared to using the command line:
|
-
You have an active
kubectlsession with permissions to createDevWorkspaceresources in your namespace on the cluster. See Overview of kubectl. -
You know your Che user namespace on the cluster. Visit
https://<che_fqdn>/api/kubernetes/namespaceto get your Che user namespace asname. -
You are in your Che user namespace on the cluster. On Kubernetes, use
kubectlto display your current namespace or switch to a namespace.Che administrators who intend to create workspaces for other users must create the DevWorkspacecustom resource in a user namespace that is provisioned by Che or by the administrator. See Configuring user namespace provisioning.
-
Copy the contents of the target Git repository’s devfile to prepare the
DevWorkspacecustom resource.Example 2. Copied devfile contents withschemaVersion: 2.2.0components: - 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.Example 3. ADevWorkspacecustom resourcekind: DevWorkspace apiVersion: workspace.devfile.io/v1alpha2 metadata: name: my-devworkspace(1) namespace: user1-dev(2) spec: routingClass: che started: true(3) contributions:(4) - name: ide uri: http://che-dashboard.eclipse-che.svc.cluster.local:8080/dashboard/api/editors/devfile?che-editor=che-incubator/che-code/latest template: projects:(5) - name: my-project-name git: remotes: origin: https://github.com/eclipse-che/che-docs components:(6) - name: tooling-container container: image: quay.io/devfile/universal-developer-image:ubi9-latest env: - name: CHE_DASHBOARD_URL value: https://<che_fqdn>/dashboard/ (7)1 Name of the DevWorkspacecustom resource. This will be the name of the new workspace.2 User namespace, which is the target namespace for the new workspace. 3 Determines whether the workspace must be started when the DevWorkspacecustom resource is created.4 URL reference to the Microsoft Visual Studio Code - Open Source IDE devfile. 5 Details about the Git repository to clone into the workspace when it starts. 6 List of components such as workspace containers and volume components. 7 URL to Che dashboard -
Apply the
DevWorkspacecustom resource to the cluster.
-
Verify that the workspace is starting by checking the PHASE status of the
DevWorkspace.$ kubectl get devworkspaces -n <user_namespace> --watch
Example 4. OutputNAMESPACE NAME DEVWORKSPACE ID PHASE INFO user1-dev my-devworkspace workspacedf64e4a492cd4701 Starting Waiting for workspace deployment
-
When the workspace has successfully started, its PHASE status changes to Running in the output of the
kubectl get devworkspacescommand.Example 5. OutputNAMESPACE NAME DEVWORKSPACE ID PHASE INFO user1-dev my-devworkspace workspacedf64e4a492cd4701 Running https://url-to-workspace.com
You can then open the workspace by using one of these options:
-
Visit the URL provided in the INFO section of the output of the
kubectl get devworkspacescommand. -
Open the workspace from the Che dashboard.
-
Stop workspaces
Stop a workspace by setting the spec.started field in the DevWorkspace custom resource to false.
-
You have an active
kubectlsession on the cluster. See Overview of kubectl. -
You have the workspace name. Run
kubectl get devworkspacesto list workspace names. -
You have the relevant Che user namespace on the cluster. Visit
https://<che_fqdn>/api/kubernetes/namespaceto get your Che user namespace asname. -
You are in the Che user namespace on the cluster. On Kubernetes, use
kubectlto display your current namespace or switch to a namespace.
-
Run the following command to stop a workspace:
$ kubectl patch devworkspace <workspace_name> \ -p '{"spec":{"started":false}}' \ --type=merge -n <user_namespace> && \ kubectl wait --for=jsonpath='{.status.phase}'=Stopped \ dw/<workspace_name> -n <user_namespace>
Start stopped workspaces
Start a stopped workspace by setting the spec.started field in the DevWorkspace custom resource to true.
-
You have an active
kubectlsession on the cluster. See Overview of kubectl. -
You have the workspace name. Run
kubectl get devworkspacesto list workspace names. -
You have the relevant Che user namespace on the cluster. Visit
https://<che_fqdn>/api/kubernetes/namespaceto get your Che user namespace asname. -
You are in the Che user namespace on the cluster. On Kubernetes, use
kubectlto display your current namespace or switch to a namespace.
-
Run the following command to start a stopped workspace:
$ kubectl patch devworkspace <workspace_name> \ -p '{"spec":{"started":true}}' \ --type=merge -n <user_namespace> && \ kubectl wait --for=jsonpath='{.status.phase}'=Running \ dw/<workspace_name> -n <user_namespace>
Remove workspaces
Remove a workspace from the command line by deleting its DevWorkspace custom resource. The Che dashboard is the recommended method for routine operations.
|
Deleting the |
-
You have an active
kubectlsession on the cluster. See Overview of kubectl. -
You know the workspace name. Run
kubectl get devworkspacesto list workspace names. -
You know the relevant Che user namespace on the cluster. Visit
https://<che_fqdn>/api/kubernetes/namespaceto get your Che user namespace asname. On Kubernetes, usekubectlto display your current namespace or switch to a namespace. -
You are in the Che user namespace on the cluster.
-
Run the following command to remove a workspace:
$ kubectl delete devworkspace <workspace_name> -n <user_namespace>