Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.iml
k8test
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ lint:
golangci-lint run

test:
go test -coverpkg=./pkg/... ./pkg/...
go test -race -coverpkg=./pkg/... ./pkg/...
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ Provides the following features:
* Automatically provisions load balancers for [`LoadBalancer` services](https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer).
* Enriches `Node` metadata with information from our cloud.
* Updates `Node` state depending on the state of the underlying VM.

## Test Cluster

To test the code on a vanilla Kubernetes cluster, you can use `helpers/run-in-test-cluster`. Note that you need a `CLOUDSCALE_API_TOKEN` for this to work, and this may incur costs on your side.

To clean the cluster up, run `helpers/cleanup`.

This is based on [k8test](https://github.com/cloudscale-ch/k8test), our in-house Kubernetes integration test utility.
3 changes: 3 additions & 0 deletions cmd/cloudscale-cloud-controller-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,8 @@ func cloudInitializer(config *config.CompletedConfig) cloudprovider.Interface {
"allow-untagged-cloud option")
}
}

klog.Info("cloudscale-cloud-controller-manager initialized")

return cloud
}
11 changes: 11 additions & 0 deletions helpers/cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
#
# Ensures that any Kubernetes cluster is cleaned up
#
set -euo pipefail

if ! test -f k8test/cluster/inventory.yml; then
exit 0
fi

k8test/playbooks/destroy-cluster.yml -i k8test/cluster/inventory.yml
87 changes: 87 additions & 0 deletions helpers/run-in-test-cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env bash
#
# Ensures that a Kubernetes test cluster is present and updated
#
set -euo pipefail

# Prepares k8test with an existing virtual env, or a newly created on
function ensure-k8test() {
if ! test -d k8test; then
git clone git@github.com:cloudscale-ch/k8test.git
git -C k8test switch denis/init
fi

if [[ "${VIRTUAL_ENV:-}" == "" ]]; then

if ! test -d k8test/venv; then
python3 -m venv k8test/venv
fi

# shellcheck source=/dev/null
source k8test/venv/bin/activate
fi

if ! command -v poetry > /dev/null; then
pip install poetry
fi

if ! command -v ansible > /dev/null; then
poetry install --directory k8test
fi
}

# Prints a random zone
function random-zone() {
arr[0]="rma1"
arr[1]="lpg1"

rand=$((RANDOM % 2))
echo "${arr[$rand]}"
}

# Launches the test cluster, if there's no inventory yet
function ensure-inventory() {
if ! test -d k8test/cluster; then
mkdir k8test/cluster
fi

if ! test -f k8test/cluster/ssh.pub; then
ssh-keygen -t ed25519 -N '' -f k8test/cluster/ssh
fi

if ! test -f k8test/cluster/inventory.yml; then
k8test/playbooks/create-cluster.yml \
-e zone="$(random-zone)" \
-e ssh_key=k8test/cluster/ssh.pub \
-e control_count=2 \
-e worker_count=2

# Those won't really change between runs, so update them during install
k8test/playbooks/update-secrets.yml \
-i k8test/cluster/inventory.yml
fi
}

# Build the latest container each time
function build-container() {
k8test/playbooks/build-container.yml \
-i k8test/cluster/inventory.yml \
-e dockerfile=./Dockerfile \
-e tag=quay.io/cloudscalech/cloudscale-cloud-controller-manager:test \
-e extra='--build-arg=VERSION=test'
}

# Re-apply the resources each time
function apply-resources() {
sed 's/controller-manager:latest/controller-manager:test/g' \
deploy/kubernetes/releases/latest.yml > /tmp/ccm.yml

export KUBECONFIG=k8test/cluster/admin.conf
kubectl delete -f /tmp/ccm.yml || true
kubectl apply -f /tmp/ccm.yml
}

ensure-k8test
ensure-inventory
build-container
apply-resources