Odo 2.0 introduces a configuration file named devfile.yaml
. Odo uses this configuration file to set up cloud-native projects and determine the actions required for events such as building, running, and debugging a project. If you are an Eclipse Che user, devfile.yaml
should sound familiar: Eclipse Che uses devfiles to express developer workspaces, and they have proven to be flexible to accommodate a variety of needs.
Odo 2.0 comes with a built-in catalog of devfiles for various project types, so you do not necessarily need to write or modify a devfile to start a new project. You can also create custom devfiles and contribute them to odo's devfile catalog. This article explores how to create a devfile to adopt an existing development flow to run on a Kubernetes cluster. Our example project is based on Gatsby, a framework for generating websites. Gatsby comes with its own developer tools and recommended development flow, so it presents a good example for adopting existing flows for Kubernetes.
Note: See Kubernetes integration and more in odo 2.0 for more about devfiles and other new features in this latest release.
Anatomy of a devfile
Before we begin working with the example, let’s take a quick look at the anatomy of a devfile.
Other than the schemaVersion
, no other properties are mandatory on a devfile. This flexibility lets developers use devfiles for multiple purposes. A devfile can be generic for a technology base or specific to a project, and projects can inherit and override parts of other devfiles.
Components, commands, and events are the most commonly used devfile properties:
- Components describe the parts of the development environment that need to be created. Examples include runtime containers and Kubernetes resources.
- Commands describe the predefined commands to be used to achieve specific development goals with the provided tools.
- Events bind commands to the lifecycle of the developer environment. Currently, there are four events:
postStart
,postStop
,preStart
, andpreStop
.
Implement a devfile
You've had a quick introduction to devfiles and their three most commonly used properties. Now, let's apply what you've learned. For this example, we will project instructions from the Gatsby documentation into a devfile, which we'll use to develop a website on Kubernetes.
Select a base image
Because the application runs as a container, we'll start by selecting a base image and defining it as a component:
schemaVersion: 2.0.0 components: - name: gatsby container: image: quay.io/eclipse/che-nodejs10-ubi:nightly mountSources: true memoryLimit: 700Mi endpoints: - name: web targetPort: 8000
It’s worth mentioning the container's mountSources
property. Odo uses this value as a hint for synchronizing your local files to the container running on your Kubernetes cluster.
Define the commands
Next, let’s define the commands that we'll use to build and run the application. The two commands that we need to define will run on the application's gatsby
component. The gatsby-develop
command starts the application in development mode. The setup-gatsby-cli
command sets up Gatsby's development tools on the gatsby
component:
commands: - id: gatsby-develop exec: commandLine: "gatsby develop -H 0.0.0.0" component: gatsby group: kind: run attributes: restart: "false" - id: setup-gatsby-cli exec: commandLine: "npm install -g gatsby-cli && npm install" component: gatsby
Define the events
Finally, we define a postStart
event to optimize the setup once a component has started:
events: postStart: - setup-gatsby-cli
Create and push the example application
Assuming you have odo and the Gatsby CLI installed locally, you can put your newly acquired devfile to work. Here are the commands to create and push a simple Gatsby site using odo:
gatsby new hello-world https://github.com/gatsbyjs/gatsby-starter-hello-world cd hello-world ## create or copy devfile.yaml ## from https://gist.github.com/gorkem/78fd17864218a125b2bd9146728a1af8 odo push
Conclusion
While odo comes with a built-in catalog of devfiles, you can also develop your own. Creating custom devfiles lets you integrate the technologies that you work with into the Kubernetes environment. Once you've created a devfile, you can contribute it to the devfile catalog for wider community reach.
Last updated: February 9, 2021