《OpenShift / RHEL / DevSecOps 汇总目录》
本文在 OpenShift 4.11 中已验证。
请先根据《OpenShift 4 - 用 OpenShift DevSpaces 构建多租户的在线云原生应用开发环境》一文完成 OpenShift Dev Spaces 环境安装。
OpenShift DevSpaces 提供的 WebIDE 需要提供针对应用代码的界面交互功能和后台处理功能。在 OpenShift DevSpaces 中所有运行的界面、后台组件、处理功能都是运行在 Pod 中。例如要编译应用代码,就需要使用带有编译命令的 Image 然后使用编译命令。
Devfile 是在 OpenShift DevSpaces 中运行的 Workspace 的定义文件,它包含以下各方面的配置:包括应用代码源,IDE 可以使用的插件,Build、Run、Debug 应用代码的操作命令和运行环境等。

在 OpenShift DevSpaces 中提供了部分常用的 Devfile 模板,我们也可以在 https://registry.devfile.io/viewer 上获取其他 Devfile 使用。


schemaVersion: 2.1.0
metadata:
name: hello-world-quarkus
components:
- name: tools
container:
image: quay.io/devfile/universal-developer-image:ubi8-latest
endpoints:
- name: webapp
targetPort: 8080
memoryLimit: 4Gi
memoryRequest: 256Mi
commands:
- exec:
commandLine: './mvnw quarkus:dev'
component: tools
group:
kind: build
workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
id: 1-build-dev
- exec:
commandLine: './mvnw test'
component: tools
group:
kind: test
workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
id: 2-test
- exec:
commandLine: './mvnw package'
component: tools
group:
kind: run
workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
id: 3-package
- exec:
commandLine: 'java -jar target/quarkus-app/quarkus-run.jar'
component: tools
group:
kind: run
workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
id: 4-run-jar
- exec:
commandLine: './mvnw install -Dnative -DskipTests -Dquarkus.native.native-image-xmx=4g'
component: tools
group:
kind: build
workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
id: 5-native
- exec:
commandLine: './target/getting-started-1.0.0-SNAPSHOT-runner'
component: tools
group:
kind: run
workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
id: 6-run-native

$ chmod +x mvnw



$ curl -w "\n" http://localhost:8080/hello

./mvnw install -Dnative -DskipTests -Dquarkus.native.native-image-xmx=4g
./target/getting-started-1.0.0-SNAPSHOT-runner
$ curl -w "\n" http://localhost:8080/hello
用 user1 用户进入 DevWorkspace Operator 的 DevWorkspace 实例界面,有名为 hello-world-quarkus 的实例对应以上 Quarkus 应用。

进入 hello-world-quarkus 实例可以看到 Deployment、ReplicaSet、Pod 等相关资源,其中 Pod 显示 Running 的状态。

进入上图运行的 Pod,可以看到其中运行的 Container 和 对应的 Image,以及使用到的 Volume 配置。 注意:其中名为 tools 的容器既是由前面 Devfile 文件的 commponents 组件定义的。

以上 4 个运行的容器正是在 Web IDE 中 WORKSPACE 显示的 4 个 User Runtimes 项目,可以通过打开 New terminal 进入运行的容器。

https://che.eclipseprojects.io/2021/10/12/@mario.loriedo-devfile-v2-and-the-devworkspace-operator-p1.html
https://che.eclipseprojects.io/2021/06/23/@florent.benoit-devfile-v2-and-ide-plug-ins.html
https://che.eclipseprojects.io/2019/10/02/@florent.benoit-discover-che-7-devfile.html
https://github.com/che-samples/java-spring-petclinic/blob/devfilev2/devfile.yaml
https://github.com/devfile-samples
https://developers.redhat.com/articles/2022/04/01/codeready-workspaces-scales-now-red-hat-openshift-dev-spaces#changes_to_the_workspace_engine