Skip to content

Instantly share code, notes, and snippets.

@rafaeltuelho
Last active October 28, 2024 21:59
Show Gist options
  • Save rafaeltuelho/37657c96828909b3ec5298bec4790a33 to your computer and use it in GitHub Desktop.
Save rafaeltuelho/37657c96828909b3ec5298bec4790a33 to your computer and use it in GitHub Desktop.
Sample Python3 + MongoDB DevWorkspace CR which creates a Openshift Dev Spaces workspace
apiVersion: workspace.devfile.io/v1alpha2
kind: DevWorkspace
metadata:
annotations:
che.eclipse.org/che-editor: che-incubator/che-code/latest
name: pymongo-fastapi-crud
namespace: user1-devspaces
finalizers:
- rbac.controller.devfile.io
spec:
contributions:
- name: ide
uri: http://devspaces-dashboard.openshift-devspaces.svc.cluster.local:8080/dashboard/api/editors/devfile?che-editor=che-incubator/che-code/latest
routingClass: che
started: true
template:
attributes:
controller.devfile.io/devworkspace-config:
name: devworkspace-config
namespace: openshift-devspaces
controller.devfile.io/scc: container-build
controller.devfile.io/storage-type: per-workspace
commands:
- exec:
commandLine: |
. ${HOME}/.venv/bin/activate
python hello-world.py
component: tools
group:
kind: run
label: Run the Hello World Program
workingDir: '${PROJECT_SOURCE}'
id: run-hello-world
- exec:
commandLine: 'mongosh mongodb://localhost --username root'
component: tools
group:
kind: run
label: Start mongosh-cli
workingDir: '${PROJECT_SOURCE}'
id: mongosh-cli
- exec:
commandLine: |
. ${HOME}/.venv/bin/activate
python -m uvicorn main:app --reload
component: tools
group:
kind: run
label: Start pymongo-fastapi-crud app
workingDir: '${PROJECT_SOURCE}/pymongo-fastapi-crud'
id: pymongo-fastapi-app
- exec:
commandLine: |
# init venv
python -m venv ${HOME}/.venv && . ${HOME}/.venv/bin/activate
python -m pip install -r requirements.txt
# setup git default config
git config --local credential.helper store
git config --local pull.rebase true
git config --local fetch.prune true
git config --local diff.colorMoved zebra
# create app env file
ENV_FILE=${PROJECT_SOURCE}/pymongo-fastapi-crud/.env
if [ ! -f $ENV_FILE ]; then
echo 'ATLAS_URI=mongodb://root:mongo123!@localhost:27017' > $ENV_FILE
echo 'DB_NAME=pymongo_tutorial' >> $ENV_FILE
fi
component: tools
label: '99: setup environment'
workingDir: '${PROJECT_SOURCE}'
id: init-env
components:
- container:
cpuRequest: '1'
env:
- name: KUBEDOCK_ENABLED
value: 'true'
- name: KUBEDOCK_TIMEOUT
value: '30'
- name: EXHORT_PYTHON_VIRTUAL_ENV
value: 'true'
- name: VSCODE_DEFAULT_WORKSPACE
value: '${PROJECT_SOURCE}/.code-workspace'
memoryRequest: 1Gi
sourceMapping: /projects
cpuLimit: '2'
volumeMounts:
- name: venv
path: /home/user/.venv
memoryLimit: 2Gi
image: 'quay.io/redhat_na_ssa/python3-developer-image:latest'
mountSources: true
name: tools
- name: venv
volume:
size: 1G
- container:
endpoints:
- exposure: public
name: mongodb
protocol: tcp
targetPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: root
- name: MONGO_INITDB_ROOT_PASSWORD
value: mongo123!
image: 'docker.io/mongodb/mongodb-community-server:6.0.1-ubi8'
memoryLimit: 300Mi
sourceMapping: /projects
volumeMounts:
- name: mongodbdata
path: /data/db
name: mongodb
- name: mongodbdata
volume:
size: 1G
events:
postStart:
- init-env
projects:
- name: python3-mongodb-devfile
git:
remotes:
origin: https://github.com/redhat-na-ssa/python3-mongodb-devfile.git
@rafaeltuelho
Copy link
Author

rafaeltuelho commented May 20, 2024

@rafaeltuelho
Copy link
Author

Remember you need also to create and annotate the user's namespace properly before. Here is an user namespace sample definition.

kind: Namespace
apiVersion: v1
metadata:
  name: user1-devspaces # <-- change username here
  labels:
    app.kubernetes.io/component: workspaces-namespace
    app.kubernetes.io/part-of: che.eclipse.org
    kubernetes.io/metadata.name: user1-devspaces # <-- change username here
  annotations:
    che.eclipse.org/username: user1 # <-- change username here
    openshift.io/description: 'DevSpaces workspace for user1'
spec: {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment