From 86bf4204455336e6af0561391cd463c156aef5f4 Mon Sep 17 00:00:00 2001 From: Tim Schrodi Date: Thu, 6 May 2021 11:51:54 +0200 Subject: [PATCH] add simple local cluster setup script (#186) --- docs/development/local-setup.md | 13 ++++++----- hack/resources/k3d-cluster-config.yaml | 7 ++++++ hack/setup-local-env.sh | 31 +++++++++++++++++--------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/docs/development/local-setup.md b/docs/development/local-setup.md index a583d5aa8..b3fb24285 100644 --- a/docs/development/local-setup.md +++ b/docs/development/local-setup.md @@ -13,10 +13,9 @@ Further details could be found in 1. [Principles of Kubernetes](https://kubernetes.io/docs/concepts/), and its [components](https://kubernetes.io/docs/concepts/overview/components/) 1. [Kubernetes Development Guide](https://github.com/kubernetes/community/tree/master/contributors/devel) -1. [Architecture of Gardener](https://github.com/gardener/documentation/wiki/Architecture) -This setup is based on [kind](https://github.com/kubernetes-sigs/kind). -Docker for Desktop, [minikube](https://github.com/kubernetes/minikube) or [k3d](https://github.com/rancher/k3d) are also supported. +This setup is based on [k3d](https://github.com/rancher/k3d). +Docker for Desktop, [minikube](https://github.com/kubernetes/minikube) or [kind](https://github.com/kubernetes-sigs/kind) are also supported. ## Installing Golang environment @@ -65,11 +64,15 @@ brew install git On other OS, please check the [Git installation documentation](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git). -## Installing Kind +## Installing K3d You'll need to have [Docker](https://docs.docker.com/get-docker/) installed and running. -Follow the [kind quickstart guide](https://kind.sigs.k8s.io/docs/user/quick-start/) to start a kubernetes cluster . +Follow the [k3s installation guide](https://k3d.io/#installation) to start a kubernetes cluster. + +:warning: note that with the default k8s cluster installation some development resources like target may not work as on some os's docker is running in VM with different network access. +We therefore recommend to use our local setup script in [hack/setup-local-env.sh](../../hack/setup-local-env.sh) which automatically setups a local environment. +In that environment controllers running in- and outside of the cluster can access the apiserver. (Note that this script modifies the `/etc/hosts` file which may require root permissions) ## [MacOS only] Install GNU core utilities diff --git a/hack/resources/k3d-cluster-config.yaml b/hack/resources/k3d-cluster-config.yaml index e69de29bb..7b962b62c 100644 --- a/hack/resources/k3d-cluster-config.yaml +++ b/hack/resources/k3d-cluster-config.yaml @@ -0,0 +1,7 @@ +# k3d configuration file, saved as e.g. /home/me/myk3dcluster.yaml +apiVersion: k3d.io/v1alpha2 # this will change in the future as we make everything more stable +kind: Simple # internally, we also have a Cluster config, which is not yet available externally +kubeAPI: # same as `--api-port myhost.my.domain:6445` (where the name would resolve to 127.0.0.1) + host: "host.k3d.internal" # important for the `server` setting in the kubeconfig + hostIP: "0.0.0.0" # where the Kubernetes API will be listening on + hostPort: "6445" # where the Kubernetes API listening port will be mapped to on your host system \ No newline at end of file diff --git a/hack/setup-local-env.sh b/hack/setup-local-env.sh index 4827f82e8..2d210e088 100755 --- a/hack/setup-local-env.sh +++ b/hack/setup-local-env.sh @@ -6,19 +6,30 @@ set -e +CLUSTER_NAME="test" +K3D_INTERNAL_HOST="host.k3d.internal" + CURRENT_DIR=$(dirname $0) PROJECT_ROOT="${CURRENT_DIR}"/.. -if [[ $EFFECTIVE_VERSION == "" ]]; then - EFFECTIVE_VERSION=$(cat $PROJECT_ROOT/VERSION) +echo "Setup local Landscaper development environment" + +if ! which k3d 1>/dev/null; then + echo "K3d is not installed, see https://k3d.io/ how to install it..." + exit 1 +fi + + +found_cluster=$(k3d cluster list -o json | jq ".[] | select(.name==\"${CLUSTER_NAME}\").name") +if [[ -z $found_cluster ]]; then + echo "Creating k3d cluster ..." + k3d cluster create $CLUSTER_NAME --config ${CURRENT_DIR}/resources/k3d-cluster-config.yaml +else + echo "k3d cluster ${CLUSTER_NAME} already exists. Skipping..." fi -echo "> Install $EFFECTIVE_VERSION" +echo "Note: the new cluster context is automatically added to your current scope" -CGO_ENABLED=0 GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) GO111MODULE=on \ - go install -mod=vendor \ - -ldflags "-X github.com/gardener/landscaper/pkg/version.GitVersion=$EFFECTIVE_VERSION \ - -X github.com/gardener/landscaper/pkg/version.gitTreeState=$([ -z git status --porcelain 2>/dev/null ] && echo clean || echo dirty) \ - -X github.com/gardener/landscaper/pkg/version.gitCommit=$(git rev-parse --verify HEAD) \ - -X github.com/gardener/landscaper/pkg/version.buildDate=$(date --rfc-3339=seconds | sed 's/ /T/')" \ - ${PROJECT_ROOT}/cmd/... +echo "Adding k3d host entry ..." +echo "# Local k3d cluster" >> /etc/hosts +echo "0.0.0.0 ${K3D_INTERNAL_HOST}" >> /etc/hosts