This section describes how to import a Kubernetes application into a Che workspace.

For demonstration purposes, the section uses a sample Kubernetes application having the following two pods:

To run the application on a Kubernetes cluster:

$ node=https://raw.githubusercontent.com/redhat-developer/devfile/master/samples/web-nodejs-with-db-sample/nodejs-app.yaml && \
mongo=https://raw.githubusercontent.com/redhat-developer/devfile/master/samples/web-nodejs-with-db-sample/mongo-db.yaml && \
kubectl apply -f ${mongo} && \
kubectl apply -f ${node}

To deploy a new instance of this application in a Che workspace, use one of the following three scenarios:

Including a Kubernetes application in a workspace devfile definition

This procedure demonstrates how to define the Che 7.3 workspace devfile by Kubernetes application.

Prerequisites

The devfile format is used to define a Che workspace, and its format is described in the Making a workspace portable using a devfile section. The following is an example of the simplest devfile:

apiVersion: 1.0.0
metadata:
 name: minimal-workspace

Only the name (minimal-workspace) is specified. After the Che server processes this devfile, the devfile is converted to a minimal Che workspace that only has the default editor (Che-Theia) and the default editor plug-ins (example: the terminal).

Use the Kubernetes type of components in the devfile to add Kubernetes applications to a workspace.

For example, the user can embed the NodeJS-Mongo application in the minimal-workspace defined in this paragraph by adding a components section.

apiVersion: 1.0.0
metadata:
 name: minimal-workspace
components:
 - type: kubernetes
   reference: https://raw.githubusercontent.com/.../mongo-db.yaml
 - alias: nodejs-app
   type: kubernetes
   reference: https://raw.githubusercontent.com/.../nodejs-app.yaml
   entrypoints:
     - command: ['sleep']
       args: ['infinity']

Note that the sleep infinity command is added as the entrypoint of the Node.js application. This prevents the application from starting at the workspace start phase. It allows the user to start it when needed for testing or debugging purposes.

To make it easier for a developer to test the application, add the commands in the devfile:

apiVersion: 1.0.0
metadata:
 name: minimal-workspace
components:
 - type: kubernetes
   reference: https://raw.githubusercontent.com/.../mongo-db.yaml
 - alias: nodejs-app
   type: kubernetes
   reference: https://raw.githubusercontent.com/.../nodejs-app.yaml
   entrypoints:
     - command: ['sleep']
       args: ['infinity']
commands:
 - name: run
   actions:
     - type: exec
       component: nodejs-app
       command: cd ${CHE_PROJECTS_ROOT}/nodejs-mongo-app/EmployeeDB/ && npm install && sed -i -- ''s/localhost/mongo/g'' app.js && node app.js

Use this devfile to create and start a workspace with the chectl command:

$ chectl worspace:start --devfile <devfile-path>

The run command added to the devfile is available as a task in Che-Theia from the command palette. When executed, the command starts the NodeJS application.

Adding a Kubernetes application to an existing workspace using the dashboard

This procedure demonstrates how to modify an existing workspace and import the Kubernetes application using the newly created devfile.

Prerequisites
Procedure
  1. After the creation of a workspace, use the Workspace menu and then the Configure workspace icon to manage the workspace.

    configure workspace
  2. To modify the workspace details, use the Devfile tab. The workspace details are displayed in this tab in the devfile format.

    configure workspace devfile
  3. To add a Kubernetes component, use the Devfile editor on the dashboard.

  4. For the changes to take effect, save the devfile and restart the Che workspace.

Generating a devfile from an existing Kubernetes application

This procedure demonstrates how to generate a devfile from an existing Kubernetes application using the chectl tool.

Prerequisites
Procedure
  1. Use the chectl devfile:generate command to generate a devfile:

    $ chectl devfile:generate
    • The user can also use the chectl devfile:generate command to generate a devfile from, for example, the NodeJS-MongoDB application.

      The following example generates a devfile that includes the NodeJS component:

      $ chectl devfile:generate --selector="app=nodejs"
      apiVersion: 1.0.0
      metadata:
        name: chectl-generated
      components:
        - type: kubernetes
          alias: app=nodejs
          referenceContent: |
            kind: List
            apiVersion: v1
            metadata:
              name: app=nodejs
            items:
              - apiVersion: apps/v1
                kind: Deployment
                metadata:
                  labels:
                    app: nodejs
                  name: web
      (...)

      The NodeJS application YAML definition is included in the devfile, inline, using the referenceContent attribute.

    • To include support for a language, use the --language parameter:

      $ chectl devfile:generate --selector="app=nodejs" --language="typescript"
      apiVersion: 1.0.0
      metadata:
        name: chectl-generated
      components:
        - type: kubernetes
          alias: app=nodejs
          referenceContent: |
            kind: List
            apiVersion: v1
      (...)
        - type: chePlugin
          alias: typescript-ls
          id: che-incubator/typescript/latest
  2. Use the generated devfile to start a Che workspace with chectl.

Tags: