This post shows in steps how to set up a dev environment for k3s project.
Step 1. Preparation
The following list shows my local development environment setup:
For code review & build purpose:
- OS: Mac OS
- Docker: 18.06.1-ce
- JetBrains GoLand IDE
- golang: 1.12.7
For testing deploy purpose:
- VirtualBox
- Ubuntu 18.04 Server
Alternatively, one could use EC2 instances as testing deploy environment, in which case may directly to go step 2.
1.1 VirtualBox Setup
Download Ubuntu 18.04 iso from official source and install it on VirtualBox. Configure the network interface as following:
1 | # Check all available network adapters |
Change the content accordingly (192.168.56.1 is my gateway setting, change it according to your own VirtualBox network setting):
1 | network: |
Save and apply the changes:
1 | sudo netplan apply |
1.2 Golang Environment
For Mac OS users, I recommend use homebrew
to manage go environment. However, go has its own version manager called GVM, refer here for detailed information. To use earlier version of go, add following to your .bashrc
file:
1 | # homebrew go version config |
To use simply pass the version selected. For example, to change to go version 12:
1 | # Switch to go version 12 |
1.3 Source Code Download
With go environment set up, now we go to its workspace to pull the source from github (I have already forked k3s project to my own github account, if that’s not the case for you, simply pull the source from rancher)
1 | cd /Users/yourname/go/src/github.com/yourgithubacct |
Step 2. Build & Test Launch
Open the k3s folder in GoLand IDE, on Mac OS, press ⌘ + ⇧ + f
to search in path for “k3s is up and running”, open the pkg/cli/server/server.go file and add following statement:
1 | func run(app *cli.Context, cfg *cmds.Server) error { |
Now open the terminal within GoLand IDE, then issue make
command and wait for it’s pulling required docker images and build the project for us. After the building procedure, there should be hyperkube, k3s produced in the dist/artifacts/ folder. Simply copy k3s to your deploy environment for testing purpose (for me only need to copy to virtualbox ubuntu server that has been setup):
1 | scp dist/artifacts/k3s main@192.168.56.10:~/k3s/ |
Note that:
- (1) Remember to
chmod 777 k3s_install_path
the install path forscp
to be able to upload k3s executable;- (2) k3s doesn’t support cross compile at current release, refer to issue#530 for detailed discussion regarding this;
- (3) There are two options for developers to build k3s from source. We are adapting docker method with rancher/dapper wrapper in this post. However for those who are working on Linux, may alternatively download the dependencies and directly build the source with
go build
commend described here; unfortunately this compile option is not available for Mac OS users at current release, refer to issue#273 for detailed discussion regarding this.- (4) For Mac OS users, docker tends to accumulate its data file (store at
/Users/yourname/Library/Containers/com.docker.docker/Data/vms/0/Docker.qcow2
) which contains its self os image and downloaded containers. Build k3s project may consume ~10GB space on disk and it keeps growing. The way to get around this is to use docker built-in “reset” tab (Also note thatdocker system prune
won’t save you from this pitfall).
Step 3. Breakpoint & debugging
It is essential for us engineers to be able to set up breakpoint and debug the code in software development. In this section we’ll try to connect to remotely deployed k3s executable and use delve tool to navigate through the debugging process.
3.1 Install go and dlv in deploy env
To install and set up go environment and dlv debugger on Ubuntu 18.04 server VM we created earlier, simply follow these two guides:
- How To Install Go on Ubuntu 18.04
- dlv - Installation on Linux
To get familiar with dlv
basic workflows and combine it with JetBrain GoLand IDE, I recommend to follow these practices:
- Using the Go Delve Debugger from the command line
- Debugging with GoLand – Getting Started
Now let us go back to our k3s project, find and modify the following lines in scripts/build.sh
:
1 | ... |
Go to scripts/package-cli.sh
file and change the following:
1 | ... |
Commit the file changes to our local git repo and rebuild the project, then upload the new k3s executable to our local VM:
1 | # Commit local changes |
Setup a new go remote debug run/debug configuration in GoLand IDE which looks like this:
Toggle some breakpoint at main entry then click debug button to start debugging.
Some of my questions:
- (1)
Use themake
method to build the whole project takes long time (3-5 min for me). I wonder if there’s some “quick” and “convenient” way to speedup this procedure, e.g. build only parts that have been changed? Please leave comments below if you know the answer to this.
(Updated) Please refer my following post for a better way to separate source code edit & build/test environment here at k3s Dev (2) Vagrant + VirtualBox Dev.- (2) The latest stable version of GoLand(2019.2) leave the channel open after the debugging procedure has been stopped within IDE. Simple kill the dlv by PID to get around this issue.
- (3) Run
k3s
command with generate a bunch of code in/var/lib/rancher/k3s/
, these run time generated binaries and configurations contain most important components includingk3s-server
andk3s-agent
. Details about these we’ll explore also in next my post.
Summery
Learning k3s from source is important for us to contribute to its community. In my next post, I’ll try to investigate its scheduling mechanism and explain the related major workflow.