Customizing Dapr–preparing my environment image 80

Customizing Dapr–preparing my environment

In other articles I will write about the value of Dapr – as a personal assistant to any application that helps out with state, configuration and secrets, receiving inbound messages, publishing messages and invoking services and that supports microservice architectures through a network of closely collaborating personal assistants to all applications involved. I am quite enthusiastic about the potential of Dapr and I want to share that enthusiasm. For now, I want to prepare myself and my laptop for customizing Dapr. Dapr is an open source project. All code is available from GitHub – and it is all Go code. In order to customize – enhance, extend – the code, I need to set up a local environment that not only can run Dapr but also can edit and make the project.

image

I am starting out with Windows 10 and WSL 2 (Windows Subsystem for Linux). I have installed the Ubuntu 20.4 app from the Windows app store. Of course I am also running VS Code with the Go extension. A Git client is already present in this environment. Also the Make utility is already installed in Ubuntu. If you do need to install it – check these instructions.. I have also already installed the Dapr CLI and the Dapr Quickstarts. I have tested the setup by running the hello-world Daprized application.

In this article I will show the steps I took to prepare the environment for Dapr development. I will show at the end how I make a small silly change to the sources, make the project into a binary and run a Daprized application with my change in place. This gets me to a stage where I feel ready to make a more meaningful change – that I will discuss in a follow up article.

The steps discussed in this article:

  • install Go (really upgrade that consists of remove and install)
  • install GoLangCILint
  • install Dapr source code
  • make (lint, test) the Dapr binaries
  • replace the installed daprd executable with the locally built file
  • run a Daprized application
  • make a change and do the make, lint, replace and run again to see the change take effect

Install Go (really upgrade that consists of remove and install)

Using this article:  Install Go on Ubuntu – https://buildvirtual.net/how-to-upgrade-go-on-ubuntu/  – I quickly got the current version of Go removed and the latest version of Go installed.

image

Go get the URL for the latest release: https://go.dev/dl/ 

image

Download using wget and untar

image

Then move the extracted directory go to the /usr/local directory:

image

I tried to run go at this point, but I was too early. First, a small edit of .profile is in order: add the definition of environment variable GOPATH and add $GOPATH to the PATH environment variable.

image

Once the change is made to .profile and the change has been applied with source ~/,profile the environment is once again ready to run go, the latest version.

Install GoLangCILint

Not strictly necessary for purely local development but a good practice anyway and a requirement for any code I will submit to the Dapr project: installation and use of go linting using GoLangCILint. Installation is quite straightforward, following these instructions:

image

Install Dapr source code

Dapr sources are held in a few repositories: for the CLI, the Dashboard, the main “engine” and the components. At this point, I am interested in the main “engine” – the core executables that make up the heart of the Dapr runtime framework. The repository for this code is at: github.com/dapr/dapr. I clone this GitHub repo locally, in a new directory dapr-dev:

image

At a later moment, I will clone the components repository into this same directory and link the two.

I can check the sources on the command line but even more easily in VS Code. I have linked VS Code with WSL2 and I can simply type “code . ” to open the current directory tree in VS Code:

image

SNAGHTML2aaf91e2

Make (lint, test) the Dapr binaries

With the tools set up and the source code cloned locally, I can now bake – as in build through make – the Dapr executables:

Navigate to the folder that contains the Makefile. The run “make build”:

image

Once the build is complete, new executables will have been created in directory /dapr-dev/dapr/dist/linux_amd64/release:

SNAGHTML2ab17316

To make use of this fresh executable when a Daprized application is ran, I have to replace the installed daprd executable with the locally built file.

First back up the current daprd: cp ~/.dapr/bin/daprd ~/.dapr/bin/daprd.bak

Then copy the freshly baked executable to overwrite it: cp /home/lucas/dapr-dev/dapr/dist/linux_amd64/release/daprd ~/.dapr/bin

image

At this point the active daprd executable is in all likelihood exactly the same as before: same source code after all, but now locally produced. I will run a Daprized application – hello-world – to ensure that everything still works as expected and that my local setup and the build process are in working order.

I am using the Dapr hello-world quickstart for this, with a node application that handles HTTP requests and stores values in the Dapr provided statestore:

image

Then run the application – through dapr:

image

With two invoke actions – one to create an order and persist it to the statestore and the second to retrieve the order – I demonstrate that dapr is functioning as expected.

First execute this statement on the terninal command line in the quickstart directory (/quickstarts/hello-world/node) that contains the sample.json file:

dapr invoke –app-id nodeapp –method neworder –data-file sample.json

Then this next line to verify the effect:

dapr invoke –app-id nodeapp –method order –verb GETimage

Here are the logs from the application (plus Dapr) end of things:

image

Note: alternatively, we can directly inspect the statestore’s contents:

wget  http://localhost:3500/v1.0/state/statestore/order

SNAGHTML2ac0b1ac


Make a change to the Dapr sources and do the make, lint, replace and run again to see the change take effect

To make sure I now truly have the power to change Dapr and see that change take effect, I make a small and fairly silly change to package main in the file main,.go. Note: this file will also be modified when new custom components are added.

Open the file, add a log line to write some message that I hope and expect to the see in the terminal when a daprized application is started:

image

Save the change, make the project, replace the binary as before

SNAGHTML2ac6325d

and run the daprized application as before. Dapr starts and my new pointless log message shows up. I am in business, ready to make real and meaningful changes.

image

Resources

Install Go on Ubuntu – https://buildvirtual.net/how-to-upgrade-go-on-ubuntu/

Download Go Releases – https://go.dev/dl/

Install Make on Ubuntu – https://linuxhint.com/install-make-ubuntu/

Install GoLangCILint – https://golangci-lint.run/usage/install/ 

Dapr Source Code Repo – https://github.com/dapr/dapr

Docs on installing Dapr CLI – https://docs.dapr.io/getting-started/install-dapr-cli/

Dapr Quickstarts GitHub Repo – https://github.com/dapr/quickstarts

Dapr Quickstart Hello World – https://github.com/dapr/quickstarts/tree/v1.5.0/hello-world 

Install nodejs on Ubuntu 20.4 – https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04

Dapr Docs: Setting up local Dapr development environment: https://github.com/dapr/dapr/blob/master/docs/development/setup-dapr-development-env.md

Dapr Docs: Custom Development and local build of Dapr: https://github.com/dapr/dapr/blob/master/docs/development/developing-dapr.md

Dapr Docs:  Development of Custom Component – instructions (also on make build and replace) – https://github.com/dapr/components-contrib/blob/master/docs/developing-component.md

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.