Using a custom devfile registry and C++ with Red Hat CodeReady Workspaces
fetched at April 14, 2021

Red Hat CodeReady Workspaces provides teams with predefined workspaces to streamline application development. Out of the box, CodeReady Workspaces supports numerous languages and plugins. However, many organizations want to customize a workspace and make it available to developers across the organization as a standard. In this article, I show you how to use a custom devfile registry to customize a workspace for C++ development. Once that’s done, we will deploy an example application using Docker.

Note: A devfile dictates which project is in the environment, which tools are available in the terminal, and more. For more information, please refer to the CodeReady Workspaces documentation.

Setting up the workspace

To get started with the example in this article, fork the following repository:

https://github.com/redhat-developer/codeready-workspaces

Enter the codeready-workspaces/dependencies/che-devfile-registry/devfiles directory, which holds the configuration files for each stack or plugin. There are two configuration files for each stack or plugin: devfile.yaml and meta.yaml. For the purposes of this example, simply copy the 05_cpp folder to a new folder named 05_custom_cpp. From there, you can edit the files to meet your needs:

$ cd dependencies/che-devfile-registry/devfiles/
$ cp -r 05_cpp 05_custom_cpp
$ cd 05_custom_cpp/
$ ls
devfile.yaml meta.yaml

The meta.yaml file stays largely the same, but you can edit the display name and description. Here is my meta.yaml:

---
displayName: C/C++ Hello
description: C and C++ Developer Tools stack with GCC 8.3.1, cmake 3.11.4 and make 4.2.1
tags: ["UBI8", "C", "C++", "clang", "GCC", "g++", "make", "cmake"]
icon: /images/type-cpp.svg
globalMemoryLimit: 1686Mi

You will make more updates in the devfile.yaml file. For this example, we’ll simply update the project that gets imported to the workspace automatically, like so:

---
apiVersion: 1.0.0
metadata:
  generateName: cpp-custom-
projects:
  -
    name: Hello
    source:
      type: git
      location: 'https://github.com/mmistretta/hello.git'
components:

If you want to change the stack even further, you can update things such as the Docker image, the plugins, and even the commands that developers can run with a click of a button. For example, to install Docker or Podman for use in the stack, edit the Docker image to be a custom one. The Dockerfile for the stacks-cpp-rhel8 used for this stack can be found at the Red Hat CodeReady Workspaces – C/C++ Stack web page and customized from there.

Save your files before proceeding to the next task.

Building your devfile registry image

Next, you need to build the registry image. For this example, we will use Docker and the Red Hat Universal Base Image (UBI). From the codeready-workspaces/dependencies/che-devfile-registry directory, run the following commands to build an image and push it to your registry. Replace the italicized text below with the appropriate values for your environment:

$ ./build --organization your-org --registry your_registry --tag version --rhel
$ docker push your-image

To ensure your image is valid, you can test it by running:

$ docker run your-image

Now, you can utilize your custom devfile registry.

Creating your Che Cluster for CodeReady Workspaces

Now, make sure the CodeReady Workspaces Operator is installed. As an admin user, search for the Operator in the Red Hat OpensShift OperatorHub, as shown in Figure 1.

In the search box provided by the OpenShift OperatorHub, search for the CodeReady Workspaces Operator and select it.

Figure 1: Find and select the CodeReady Workspaces Operator in the OperatorHub.

Install the Operator as shown in Figure 2.

The dialog to install the CodeReady Workspaces Operator.

Figure 2: Install the CodeReady Workspaces Operator.

After installing the Operator, select it. Then, select  Provided APIs—>CodeReady Workspaces Cluster and click the Create Instance link, as shown in Figure 3.

From Provided APIs->CodeReady Workspaces Cluster, click on the Create Instance link to start creating your instance.

Figure 3: Start creating your instance with the Create Instance link.

Select Yaml View as the “Configure via” option, as shown in Figure 4.

The dialog to select YAML View or Form View as the configuration option. The YAML view is selected.

Figure 4: Select the YAML view as the configuration option.

Now, edit the YAML file created by this process. Scroll down to the server section and replace the devfileRegistryImage entry with your image, as shown here:

server:
  pluginRegistryImage: ''
  selfSignedCert: false
  devfileRegistryImage: 'quay.io/mcochran/che-devfile-registry:1.2'
  tlsSupport: true
  cheFlavor: codeready
  cheImageTag: ''

Click Create and wait several minutes for the Che cluster to become available. Figure 5 shows the screen that CodeReady Workspaces displays upon successful creation.

The properties of a CodeReady Workspaces cluster: Name, namespace, status, OpenShift oAuth, TLS mode, and labels.

Figure 5: The displayed properties of a CodeReady Workspaces cluster.

Using the workspace

Click on your CodeReady Workspaces URL and log in to the cluster. If you’ve left the default permissions in place during the cluster configuration, you can log in as any OpenShift user. If prompted, select “Allow the selected permissions.” On the Getting Started landing page, you will then see the custom C++ stack you created (denoted by a red arrow in Figure 6).

The CodeReady Workspaces Getting Started page lists available workspaces, including any that you've created.

Figure 6: Available workspaces, including the one you’ve just created.

Select the workspace and watch it spin up. When it opens, you should see the Hello project that you specified in the devfile. Here, you can inspect some of the files, such as hello-main.cpp:

#include <iostream>

int main(int argc, char const *argv[])
{
   std::cout << "Hello Docker container!" << std::endl;
   return0;
}

Now, you can open a terminal to compile and run the code, as shown in the following example. You can also compile and run counter-main.cpp or any other C++ code:

$ cd Hello/
$ make hello-main
$ ./hello-main

You should see the program print a “Hello Docker container!” message and then terminate. At this point in development, you could make any changes you would like to see in the code, compile it, run it, and push your changes to Git, which could kick off a pipeline to deploy to OpenShift.

Deploying the application as a Docker instance

In this example, you do not have Docker tools installed in your CodeReady Workspaces stack, but Docker is installed using the Dockerfile in your Hello Git project. If you would like to test some C++ code in an environment with Docker tooling, enter commands like the following. In this example, rhsm.secret.yaml contains my credentials for my registry, and I am using quay.io with the organization mcochran as my registry. Be sure to replace these variables with the ones for your own environment:

$ DOCKER_BUILDKIT=1 docker build --progress=plain --secret id=rhsm,src=rhsm.secret.yaml -t quay.io/mcochran/cpp-hello:latest -f Dockerfile .
$ docker push quay.io/mcochran/cpp-hello:latest

Then, create a pod.yaml file to deploy your code using the Docker image:

apiVersion: v1
kind: Pod
metadata:
  labels:
    app: hello-cpp
  name: hello-cpp-exec
spec:
  containers:
  - name: hello-cpp
    image: quay.io/mcochran/cpp-hello
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5
  restartPolicy: Never

Save your file, then log in to OpenShift from the command line and deploy the pod:

$ oc login
$ oc create -f pod.yaml

Because this code simply says “hello,” the pod will run quickly. You can review the logs afterward for pods that have shut down in the workloads—>pods section of the admin console.

Summary

This article has demonstrated how to create a custom devfile registry to make dedicated workspaces available to your team. I showed you how to use a devfile to provide the code and tools appropriate to your environment, and you’ve seen how convenient it is to deploy an application in CodeReady Workspaces using Docker.

Share

The post Using a custom devfile registry and C++ with Red Hat CodeReady Workspaces appeared first on Red Hat Developer.

Visit Link
🤖 CodeKN.com

Follow our Twitter bot for more updates.