Skip to content

Golang (Sock Shop)

Estimated Time: 15 minutes
Dependencies: The Go CLI

In this guide, we'll deploy the Sock Shop demo application, and see what it's like to develop Go code with Kelda.

We'll do this in two parts:

  1. Install Kelda.
  2. Develop Sock Shop with Kelda.

Installing Kelda

Kelda runs your code in a remote Kubernetes cluster while allowing you to use the local development tools you're used to. Kelda achieves this by running an agent in your Kubernetes cluster, which you interact with via a local CLI tool.

For this guide, we'll connect to a cluster created by the Kelda team, but if you'd like to deploy to your own infrastructure, see the docs here.

  1. Install the Kelda CLI

    Run the following snippet to download Kelda:

    curl -fsSL 'https://kelda.io/install.sh' | sh
    

    You should see the following output:

    Downloading the latest Kelda release...
    ################################################################# 100.0%
    The latest Kelda release has been downloaded to the current working directory.
    Please install Kelda to your desired location, or use the snippet below to install it to /usr/local/bin.
    
        sudo cp ./kelda /usr/local/bin
    

    Use the provided snippet to install Kelda into /usr/local/bin.

  2. Download the Kelda examples repository

    The repo must be downloaded into your GOPATH or else the code won't build.

    This repository contains a script that we'll use to connect to the demo cluster, as well as the source code and Kelda config for Sock Shop.

    go get github.com/kelda-inc/examples
    

    The command will produce the error no Go files, but that's expected because the Go code is in a subdirectory.

  3. Connect to the Kelda demo cluster

    You must run this command again for each new terminal you open.

    Source the use-demo-cluster.sh script in the root directory of the examples repository. This will modify your local kubectl settings so that it points at the Kelda demo cluster.

    source use-demo-cluster.sh
    

    If the script succeeded, your terminal prompt should now start with [kelda-demo]. If your prompt didn't update, then you might have ran the script directly rather than via the source command.

    [kelda-demo] $
    
  4. Test your connection to the cluster (Optional: If kubectl is installed)

    Try listing the Kelda minion pod with:

    kubectl get pods -n kelda
    

    You should see something like:

    NAME                     READY   STATUS    RESTARTS   AGE
    kelda-59464b9cb6-mkkqz   1/1     Running   0          5d21h
    

Developing with Kelda

Each service that you want to develop must have a kelda.yaml that describes what files to sync, and what Kelda should do after each sync.

For the catalogue service, Kelda will sync all files in the repo, and re-run go run ./cmd/cataloguesvc after each sync.

  1. Configure Kelda to work on the Sock Shop project

    The following command creates a file in your home directory that's required for the kelda commands to work.

    Most users can just use the defaults by hitting [Enter] for each prompt.

    In the root directory of the examples repository, run:

    ./golang-sock-shop/configure-kelda.sh
    

    If the command succeeded, you should see something like:

    Wrote config to /Users/kevin/.kelda.yaml
    
  2. Start development on the catalogue service

    Run the following command from the examples repo. It will boot the catalogue service and its dependencies, start the code syncing, and start the tunnel to the Sock Shop frontend.

    kelda dev ./golang-sock-shop/catalogue
    

    Wait for of all the services to become Ready. The first boot may take several minutes while all the services boot.

    Ready

  3. View the Sock Shop frontend

    Open http://localhost:8080 in your browser. There should be various socks with prices at the bottom of the page.

    Before Catalogue Change

  4. Modify the service code to make all the socks free

    Line 71 in golang-sock-shop/catalogue/service.go implements the List method, which returns all the socks to the frontend.

    Uncomment the code at line 105 that sets the price to always be zero.

    socks[i].Price = 0
    

    Once you've made the change, save the file. Kelda will sync the change into your development environment, and automatically rebuild and restart the process.

  5. View the change in your browser

    Reload http://localhost:8080. The socks on the front page should now all have a price of $0.00.

    After Catalogue Change

Alternative Syncing Approach For Large Projects

If your project has too many source files for Kelda to monitor for changes, you can cross-compile your application locally, and sync the executable over.

The user service has an example of this approach.

  1. cd into the directory for the user code

    cd golang-sock-shop/user
    
  2. Build the executable that will be run

    Run the following command to build a user executable that will be synced by Kelda.

    ./rebuild.sh
    

    The script should output something like

    Building binary with 'CGO_ENABLED=0 GOOS=linux go build -o user .'
    Done. Kelda should automatically sync the 'user' file over now.
    
  3. Start development on the users service

    kelda dev
    
  4. Try out the current login process

    Open the Sock Shop home page at http://localhost:8080.

    Try logging in as the user root with any random password.

    Before User Change

  5. Add a backdoor to the login page

    Uncomment the code at line 50 of api/service.go so that the password is ignored for the root user.

        if username == "root" {
            return users.New(), nil
        }
    
  6. Rebuild the service locally

    Run ./rebuild.sh to build a new version of the user executable.

    Once the executable is built, Kelda will automatically notice the change, sync the file over, and restart the process.

  7. Test the new code

    Try logging in as the root user again. It should succeed this time.

    After User Change

Cleanup

  1. Exit Kelda

    Exit Kelda by entering Ctrl + c into the terminal window where the Kelda CLI is running.

  2. Delete your development namespace

    Run kelda delete to delete your development namespace so that it stops consuming resources in the remote cluster.