Skip to content

Commit

Permalink
fix(ci): add test with Vagrant
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasfrahm committed Apr 30, 2022
1 parent 0feeedc commit b66f398
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ trim_trailing_whitespace = false
[Makefile]
indent_style = tab
indent_size = 4

[*{D,d}ockerfile]
indent_size = 4
40 changes: 40 additions & 0 deletions .github/workflows/binaries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,46 @@ on:
push:

jobs:
test:
name: Test
runs-on: macos-latest
strategy:
fail-fast: false
matrix:
example:
- examples/ssh-proxy.yml
- examples/standalone.yml
steps:
- name: Check out
uses: actions/checkout@v3

- name: Show Vagrant version
run: vagrant --version

- name: Bring up VMs
run: make vagrant-up

- name: Set up Go compiler
uses: actions/setup-go@v3
with:
check-latest: true

- name: Download dependencies
run: go mod download

- name: Run k3se up
run: go run main.go up ${{ matrix.example }}

- name: Test k3s
working-directory: deploy/vagrant
run: |
vagrant ssh kube1 -c "k3s version"
vagrant ssh kube1 -c "k3s kubectl get nodes"
vagrant ssh kube1 -c "k3s kubectl get nodes"
- name: Run k3se down
run: go run main.go down ${{ matrix.example }}

build:
name: Build
# pull_request_target: Only "dependabot[bot]" is allowed to access secrets.
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Ignore compiled binaries.
bin/

# Ignore Vagrant state.
.vagrant/
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GOARCH := $(shell echo $(PLATFORM) | cut -d "/" -f 2)
SUFFIX := $(GOOS)-$(GOARCH)
VERSION ?= $(shell git describe --always --tags --dirty)
BUILD_FLAGS := -ldflags="-s -w -X github.com/nicklasfrahm/$(TARGET)/cmd.version=$(VERSION)"
VAGRANT := /usr/bin/vagrant

# Adjust the binary name on Windows.
ifeq ($(GOOS),windows)
Expand All @@ -21,6 +22,14 @@ ifdef UPX
upx -qq $(UPX) $@
endif

.PHONY: vagrant-up
vagrant-up: $(VAGRANT)
cd deploy/vagrant; vagrant up

.PHONY: vagrant-down
vagrant-down: $(VAGRANT)
cd deploy/vagrant; vagrant destroy -f

.PHONY: clean
clean:
@rm -rvf bin
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@

A lightweight Kubernetes engine that deploys `k3s` clusters declaratively based on a cluster configuration file. The name is a hommage to the German word for cheese, _Käse [ˈkɛːzə]_.

## Requirements
## Requirements 📝

The nodes have to be accessible via SSH, either directly or via a bastion host. Further, the user on the remote nodes needs to have passwordless `sudo` set up. If this is not yet the case, you may manually do so via the following command:

```bash
$ echo "$(whoami) ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/$(whoami)
```

## Testing 🧪

If you want to test `k3se` you can use [Vagrant][website-vagrant]. All examples in the `examples/` folder can be used with the provided `Vagrantfile` that provisions 3 Ubuntu VMs. To bring up the VMs you can run the following command:

```bash
$ make vagrant-up
```

Once you are done testing, you can destroy the VMs with the following command:

```bash
$ make vagrant-down
```

## License 📄

This project is and will always be licensed under the terms of the [MIT license][file-license].
Expand Down
46 changes: 46 additions & 0 deletions deploy/vagrant/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "ubuntu/focal64"

# Configure SSH public key authentication for SSH.
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_ed25519.pub").first.strip
config.vm.provision 'shell', inline: 'mkdir -p /root/.ssh'
config.vm.provision 'shell', inline: "echo #{ssh_pub_key} >> /root/.ssh/authorized_keys"
config.vm.provision 'shell', inline: "echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys", privileged: false

# Create multiple Vagrant machines.
(1..3).each do |i|
config.vm.define "kube#{i}" do |node|
# Configure the hostname of the machine.
node.vm.hostname = "kube#{i}"

# Create a private network, which allows host-only access to the machine
# using a specific IP.
node.vm.network "private_network", ip: "192.168.56.1#{i}"
node.vm.network "forwarded_port", guest: 22, host: 2221 + i, host_ip: "127.0.0.1", id: "ssh"
node.vm.network "forwarded_port", guest: 6443, host: 6442 + i, host_ip: "127.0.0.1", id: "kubeapi"

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
node.vm.provider "virtualbox" do |vb|
vb.name = "kube#{i}"

# Customize the amount of memory on the VM:
vb.memory = "1024"
vb.cpus = "1"
end
end
end
end
16 changes: 6 additions & 10 deletions examples/ssh-proxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,23 @@ version: stable
# commmand line options of the `k3s` command.
cluster:
tls-san:
- tph99.nicklasfrahm.xyz
disable:
- traefik
- localhost
write-kubeconfig-mode: "644"

# A list of all nodes in the cluster and their connection information.
nodes:
- role: server
ssh:
host: 127.0.0.1
fingerprint: SHA256:t/bwWCelgcAEYmQW9XbM4p31e1Qq70ZPWOKK+FRxBCc
user: nicklasfrahm
host: 192.168.56.11
user: vagrant
key-file: ~/.ssh/id_ed25519
config:
node-label:
- site=tph99
- site=vagrant

# An SSH proxy, also known as jumpbox or a bastion host
# can be used to access nodes in a private network.
ssh-proxy:
host: tph99.nicklasfrahm.xyz
fingerprint: SHA256:t/bwWCelgcAEYmQW9XbM4p31e1Qq70ZPWOKK+FRxBCc
user: nicklasfrahm
host: 192.168.56.11
user: vagrant
key-file: ~/.ssh/id_ed25519
11 changes: 4 additions & 7 deletions examples/standalone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ version: stable
# commmand line options of the `k3s` command.
cluster:
tls-san:
- tph99.nicklasfrahm.xyz
disable:
- traefik
- localhost
write-kubeconfig-mode: "644"

# A list of all nodes in the cluster and their connection information.
nodes:
- role: server
ssh:
host: tph99.nicklasfrahm.xyz
fingerprint: SHA256:t/bwWCelgcAEYmQW9XbM4p31e1Qq70ZPWOKK+FRxBCc
user: nicklasfrahm
host: 192.168.56.11
user: vagrant
key-file: ~/.ssh/id_ed25519
config:
node-label:
- site=tph99
- site=vagrant
8 changes: 4 additions & 4 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ func (e *Engine) Connect() error {

// Get a list of all nodes and connect to them.
for _, node := range e.Spec.Nodes {
if err := node.Connect(WithSSHProxy(sshProxy)); err != nil {
return err
}

// Inject logger into node.
node.Logger = e.Logger.With().Str("host", node.SSH.Host).Logger()

if err := node.Connect(WithSSHProxy(sshProxy), WithLogger(&node.Logger)); err != nil {
return err
}

// Nodes store the connection state so we want to maintain pointers to them.
e.Nodes = append(e.Nodes, &node)
}
Expand Down

0 comments on commit b66f398

Please sign in to comment.