Chapter 5. Using Red Hat OpenShift Dev Spaces for application development
Red Hat OpenShift Dev Spaces is a web-based IDE that you can use to develop applications. It provides developers and other IT team members with a consistent, secure, and zero-configuration development environment. OpenShift Dev Spaces includes Microsoft Visual Studio Code, and JetBrains IntelliJ IDEA integrated development environments (IDE).
You can use devfile to define different aspects of your workspace , such as the following:
- Resource allocation
- Pre-defined commands to build, test, debug, and run applications
- Containers with development tools
- Containers or Kubernetes manifests for services required for testing
- Workspace start and stop events
- Source code repositories
The following procedures demonstrate the minimum required OpenShift Dev Spaces configurations to build, run, and debug your application in OpenShift Dev Spaces and also describe how to deploy your application to OpenShift Container Platform. For a complete list of configuration options, see product documentation for Red Hat OpenShift Dev Spaces.
To get started with using OpenShift Dev Spaces for developing JBoss EAP applications, follow these procedures:
Create a workspace in OpenShift Dev Spaces
Build, run, debug, and stop your application in OpenShift Dev Spaces
Deploy your application to, and undeploy your application from, OpenShift Container Platform in OpenShift Dev Spaces
5.1. Creating a workspace in OpenShift Dev Spaces for your application Copy linkLink copied to clipboard!
Create a devfile in your application to configure a Red Hat OpenShift Dev Spaces workspace for your application. The devfile defines different aspects of your workspace such as source code repositories, resource allocation, containers with development tools, and so on.
This procedure uses Visual Studio Code as the integrated development environment (IDE) and defines both innerloop and outerloop commands. Innerloop commands refer to the commands that run within the OpenShift Dev Spaces workspace and outerloop commands are those that deploy your application externally.
The following procedure describes the minimum steps required to get started with developing JBoss EAP applications in OpenShift Dev Spaces. For more information, see product documentation for Red Hat OpenShift Dev Spaces.
Prerequisites
You have configured an application for deployment on OpenShift Container Platform.
For more information, see Preparing an application for deployment on OpenShift Container Platform.
Procedure
-
Create a devfile, named
devfile.yaml, at the root of the repository. Add schema and metadata information to the devfile.
schemaVersion: 2.2.01 metadata:2 name: <workspace-name>3 version: <version>4 displayName: <display-name>5 Define variables. You can use the value of the variable throughout the devfile by referencing the variable.
variables: imageRegistry: 'image-registry.openshift-image-registry.svc:5000'1 imageName: 'helloworld'2 nodeName: 'helloworld'3 target-namespace: 'getting-started'4 - 1
- Define the registry to which you want to deploy your application image. The example uses the internal OpenShift registry.
- 2
- Define a name for the image that will be built for the application.
- 3
- Define a name for the node that will contain the application.
- 4
- The namespace in which the application is to be deployed.
Incorporate tools into the workspace for building your application within the workspace.
NoteWhen running OpenShift Dev Spaces in the developer sandbox, you can only use the predefined namespace. Therefore, you must comment out the following lines inside the
envdefinition:- name: TARGET_NAMESPACE .value: '{{target-namespace}}'components:1 - name: tools container:2 image: registry.redhat.io/devspaces/udi-rhel8:3.11-14 memoryLimit: 1512Mi mountSources: true volumeMounts: - name: m2 path: /home/user/.m2 env:3 - name: JAVA_OPTS value: '-Djava.security.egd=file:/dev/urandom -Djboss.host.name=localhost' - name: DEBUG_PORT value: '5005' - name: NODE_NAME value: '{{nodeName}}' - name: IMAGE_REGISTRY value: '{{imageRegistry}}' - name: IMAGE value: '{{imageName}}' - name: TARGET_NAMESPACE .value: '{{target-namespace}}' - name: VSCODE_DEFAULT_WORKSPACE value: /projects/wildfly-devfile-examples/.code-workspace - name: USE_JAVA17 value: 'true' endpoints:4 - name: debug exposure: internal protocol: tcp targetPort: 5005 - name: 'http' protocol: http targetPort: 8080 exposure: public - name: 'management' targetPort: 9990 protocol: http exposure: internal - name: 'transactions' targetPort: 4172 protocol: tcp exposure: internal - name: m25 volume: size: 3Gi- 1
- Define a component named
toolsof the type container. - 2
- Use the
udi-rhel8:3.11-14UDI container from theregistry.redhat.io/devspacesregistry with the specified configuration. - 3
- Define environment variables.
- 4
- Define the container endpoints as follows:
-
exposure: Define how the endpoint should be exposed on the network. -
protocol: Define the application and transport protocols of the traffic that will go through this endpoint. -
targetPort: The port number to be used within the container component. The same port cannot be used by two different container components.
-
- 5
- Define a volume component. This is used in the tools component to mount the Maven repository.
Add innerloop and outerloop commands.
commands: - id: package1 exec: label: "InnerLoop 01 - Build the application" component: tools commandLine: mvn clean package -Popenshift -Dmaven.wagon.http.ssl.insecure=true workingDir: ${PROJECT_SOURCE}/helloworld group: kind: build isDefault: true - id: run2 exec: label: "InnerLoop 02 - Run the application in dev mode" component: tools commandLine: ./target/server/bin/standalone.sh -Djboss.host.name=${NODE_NAME} workingDir: ${PROJECT_SOURCE}/helloworld group: kind: run isDefault: true - id: debug3 exec: label: "InnerLoop 03 - Debug the application in dev mode" component: tools commandLine: ./target/server/bin/standalone.sh -Djboss.host.name=${NODE_NAME} --debug workingDir: ${PROJECT_SOURCE}/helloworld group: kind: debug isDefault: true - id: shutdown4 exec: label: "InnerLoop 04 - Shutdown the server" component: tools commandLine: ./target/server/bin/jboss-cli.sh -c --command=shutdown workingDir: ${PROJECT_SOURCE}/helloworld hotReloadCapable: false group: kind: run isDefault: false - id: deploy-image5 exec: label: "OuterLoop 01 - Deploy Image to OpenShift" component: tools workingDir: ${PROJECT_SOURCE}/helloworld # When using developer sandbox, prefix the following command with export "IMAGE_REGISTRY_NAMESPACE=$(oc project -q) &&" because you can only use the predefined namespace in the developer sandbox commandLine: "helm repo add jboss https://jbossas.github.io/eap-charts/ && helm install ${IMAGE} --namespace=${IMAGE_REGISTRY_NAMESPACE} -f ./charts/helm.yaml jboss/eap8" group: kind: run isDefault: false - id: undeploy-image6 exec: label: "OuterLoop 02 - Undeploy Image from OpenShift" component: tools workingDir: ${PROJECT_SOURCE}/helloworld commandLine: "helm uninstall ${IMAGE}" group: kind: run isDefault: false- 1
- Define a command to build the application. This executes
mvn clean package -Popenshift -Dmaven.wagon.http.ssl.insecure=truein the workspace to provision a server with the application deployed to the server. - 2
- Define a command to run the application. This executes
./target/server/bin/standalone.sh -Djboss.host.name=${NODE_NAME}to start the provisioned server within the workspace. - 3
- Define a command to debug the application. This starts the server with the
--debugoption. - 4
- Define a command to shut down the running server.
- 5
- Define a command to deploy the application to OpenShift Container Platform with helm.
- 6
- Define a command to undeploy the application from OpenShift Container Platform with helm.
Optionally, configure Visual Studio Code.
-
Create a directory
.vscodefor Visual Studio Code configuration. Navigate to the
.vscodedirectory.$ cd .vscodeCreate a file
extensions.jsonto configure the extensions to be installed in Visual Studio Code.NoteYou might need to configure a registry to install the extensions. For more information, see Managing IDE extensions in the OpenShift Dev Spaces Administration guide guide.
{ // See https://go.microsoft.com/fwlink/?LinkId=827846 // for the documentation about the extensions.json format "recommendations": [ "redhat.java", "vscjava.vscode-java-debug", "vscjava.vscode-java-test", "redhat.fabric8-analytics", "cnshenj.vscode-task-manager"1 ] }- 1
- Task manager extension. Provides a custom activity view for the commands in the devfile.
Create a file
launch.jsonto configure a debug session in Visual Studio Code.{ "version": "0.2.0", "configurations": [ { "type": "java", "name": "Attach to running server", "request": "attach", "hostName": "localhost", "port": 5005 } ] }
-
Create a directory
Create a file
settings.json.{ "java.server.launchMode": "Standard", "editor.codeActionsOnSave": { } }- Push the changes to your repository.
5.2. Building your application in OpenShift Dev Spaces Copy linkLink copied to clipboard!
After you create a workspace in OpenShift Dev Spaces, ensure that your application builds correctly. The InnerLoop 01 - Build the application command defined in the devfile runs the Maven package goal to provision a server with the application deployed.
Prerequisites
You have created a workspace for your application in OpenShift Dev Spaces.
For more information, see Creating a workspace in OpenShift Dev Spaces for your application.
You have started a workspace in OpenShift Dev Spaces.
For more information, see Starting a workspace from a Git repository URL in the OpenShift Dev Spaces User guide.
Procedure
To build the application, use the
InnerLoop 01 - Build the applicationcommand.If you have installed the optional task manager Visual Studio Code extension:
- On the Activity Bar, click the Task Manager icon. The list of commands you defined in the devfile are displayed.
- Select InnerLoop 01 - Build the application and click the Run Task icon that is displayed.
Otherwise:
-
Press the
F1key. - In the search field that is displayed, enter "run task".
- From the results, select Tasks: Run task.
- In the search field, enter "devfile".
From the results, select InnerLoop 01 - Build the application.
The application will start building and the progress will be displayed in the Visual Studio Code terminal.
-
Press the
Verification
Inspect the Visual Studio Code terminal output. You should see a build success message like the following:
[INFO] Copy deployment /projects/test/helloworld/target/helloworld.war to /projects/test/helloworld/target/server/standalone/deployments/ROOT.war [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 27.889 s [INFO] Finished at: 2024-04-29T10:31:45Z [INFO] ------------------------------------------------------------------------
5.3. Running your application in OpenShift Dev Spaces Copy linkLink copied to clipboard!
Ensure that your application runs properly in OpenShift Dev Spaces before you deploy it to OpenShift Container Platform. The InnerLoop 02 - Run the application in dev mode command defined in the devfile starts the provisioned server.
Prerequisites
You built your application in OpenShift Dev Spaces.
For more information, see Building your application in OpenShift Dev Spaces.
Procedure
To run the application, use the
InnerLoop 02 - Run the application in dev modecommand .If you have installed the optional task manager Visual Studio Code extension:
- On the Activity Bar, click the Task Manager icon. The list of commands you defined in the devfile are displayed.
- Select InnerLoop 02 - Run the application in dev mode and click the Run Task icon that is displayed.
Otherwise:
-
Press the
F1key. - In the search field that is displayed, enter "run task".
- From the results, select Tasks: Run task.
- In the search field, enter "devfile".
- From the results, select InnerLoop 02 - Run the application in dev mode.
-
Press the
In the pop-up window that is displayed with the following message, click no:
A new process is now listening on port 4712 but is listening on interface ::ffff:7f00:1 which is internal. You should change to be remotely available. Would you want to add a redirect for this port so it becomes available ?In the pop-up window that is displayed with the following message, click Open In New Tab:
Process http is now listening on port 8080. Open it ?
Verification
- You will see "Hello World!" in the new tab.
5.4. Debugging your application in OpenShift Dev Spaces Copy linkLink copied to clipboard!
Being able to set breakpoints in the source when debugging is necessary to pause the debugger and examine the call stack or values of variables. You can also add breakpoints to specific lines of the application source code in OpenShift Dev Spaces when debugging. The InnerLoop 03 - Debug the application in dev mode command defined in the devfile starts the provisioned server in debug mode.
Prerequisites
You built your application in OpenShift Dev Spaces.
For more information, see Building your application in OpenShift Dev Spaces.
Procedure
- To add a breakpoint, click in the left margin of the editor, next to the required line of code. A red dot is displayed in the margin to mark the breakpoint you added.
To debug the application, use the
InnerLoop 03 - InnerLoop 03 - Debug the application in dev modecommand .If you have installed the optional task manager Visual Studio Code extension:
- On the Activity Bar, click the Task Manager icon. The list of commands you defined in the devfile are displayed.
- Select InnerLoop 03 - InnerLoop 03 - Debug the application in dev mode and click the Run Task icon that is displayed.
Otherwise:
-
Press the
F1key. - In the search field that is displayed, enter "run task".
- From the results, select Tasks: Run task.
- In the search field, enter "devfile".
- From the results, select InnerLoop 03 - Debug the application in dev mode .
-
Press the
5.5. Stopping your application in OpenShift Dev Spaces Copy linkLink copied to clipboard!
To free up resources, stop the application running in OpenShift Dev Spaces.
Prerequisites
Your application is running in OpenShift Dev Spaces.
For more information, see Running your application in OpenShift Dev Spaces.
Procedure
To stop the server, use the
InnerLoop 04 - Shutdown the servercommand.If you have installed the optional task manager Visual Studio Code extension:
- On the Activity Bar, click the Task Manager icon. The list of commands you defined in the devfile are displayed.
- Select InnerLoop 04 - Shutdown the server and click the Run Task icon that is displayed.
Otherwise:
-
Press the
F1key. - In the search field that is displayed, enter "run task".
- From the results, select Tasks: Run task.
- In the search field, enter "devfile".
- From the results, select InnerLoop 04 - Shutdown the server.
-
Press the
5.6. Deploying your application to OpenShift Container Platform in OpenShift Dev Spaces Copy linkLink copied to clipboard!
Deploy your application to OpenShift Container Platform by using helm. The OuterLoop 01 - Deploy Image to OpenShift command defined in the devfile uses helm to deploy the application to OpenShift Container Platform. The helm chart, helm.yaml, defined in your application is used for the deployment.
Prerequisites
You have run your application in OpenShift Dev Spaces.
For more information, see Running your application in OpenShift Dev Spaces.
Procedure
To deploy your application to OpenShift Container Platform, use the
OuterLoop 01 - Deploy Image to OpenShiftcommand .If you have installed the optional task manager Visual Studio Code extension:
- On the Activity Bar, click the Task Manager icon. The list of commands you defined in the devfile are displayed.
- Select OuterLoop 01 - Deploy Image to OpenShift and click the Run Task icon that is displayed.
Otherwise:
-
Press the
F1key. - In the search field that is displayed, enter "run task".
- From the results, select Tasks: Run task.
- In the search field, enter "devfile".
From the results, select OuterLoop 01 - Deploy Image to OpenShift.
You get the following output:
Your application is building! To follow the build, run: $ oc get build -w Note that your Deployment will report "ErrImagePull" and "ImagePullBackOff" until the build is complete. Once the build is complete, your image will be automatically rolled out. To follow the deployment of your application, run: $ oc get deployment helloworld -w * Terminal will be reused by tasks, press any key to close it.
-
Press the
Verification
Obtain the application URL.
$ oc get route helloworldNavigate to the URL.
You get the following output:
Hello World!
5.7. Undeploying your application from OpenShift Container Platform in OpenShift Dev Spaces Copy linkLink copied to clipboard!
You can undeploy your application from OpenShift Container Platform in OpenShift Dev Spaces.
Prerequisites
You have deployed your application to OpenShift Container Platform.
For more information, see Deploying your application to OpenShift Container Platform in OpenShift Dev Spaces.
Procedure
To undeploy your application from OpenShift Container Platform, use the
OuterLoop 02 - Undeploy Image from OpenShiftcommand.If you have installed the optional task manager Visual Studio Code extension:
- On the Activity Bar, click the Task Manager icon. The list of commands you defined in the devfile are displayed.
- Select OuterLoop 02 - Undeploy Image from OpenShift and click the Run Task icon that is displayed.
Otherwise:
-
Press the
F1key. - In the search field that is displayed, enter "run task".
- From the results, select Tasks: Run task.
- In the search field, enter "devfile".
- From the results, select OuterLoop 02 - Undeploy Image from OpenShift.
-
Press the
After you undeploy the application, it is no longer available for clients.
Revised on 2025-05-08 10:45:47 UTC