Skip to content

Instantly share code, notes, and snippets.

@MChorfa
Last active October 14, 2022 07:22
Show Gist options
  • Save MChorfa/89655b597e1e3c7db7a2ca317430e598 to your computer and use it in GitHub Desktop.
Save MChorfa/89655b597e1e3c7db7a2ca317430e598 to your computer and use it in GitHub Desktop.
Makefile.kind
export PROJECT_NAMESPACE=kindness
export CLUSTER_NAME=mccluster
CLIENT_PLATFORM ?= $(shell go env GOOS)
ifeq ($(CLIENT_PLATFORM),linux)
export CURRENT_HOST_IP=$(shell hostname -I | awk '{print $1; exit}' | cut -d ' ' -f 1)
else
export CURRENT_HOST_IP=$(shell ifconfig en0 | awk '/inet / {print $2; }' | cut -d ' ' -f 2)
endif
export KIND_CONFIG_FILE_NAME=kind.config.yaml
export METALLB_CONFIG_FILE_NAME=metallb.config.yaml
export DOCKER_IPAM_SUBNET=$(shell docker network inspect bridge | jq ".[].IPAM.Config | .[].Subnet" | xargs)
export DOCKER_IPAM_SUBNET_CLASS_A=$(shell docker network inspect bridge | jq ".[].IPAM.Config | .[].Subnet" | xargs | cut -d'.' -f 1)
export DOCKER_IPAM_SUBNET_CLASS_B=$(shell docker network inspect bridge | jq ".[].IPAM.Config | .[].Subnet" | xargs | cut -d'.' -f 2)
## Create file definition for the kind cluster
define get_kind_config_file
# Remove config file
rm -rf ${KIND_CONFIG_FILE_NAME}
# Define config file
cat << EOF >> ${KIND_CONFIG_FILE_NAME}
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
## Somehow only need for linux
# networking:
# apiServerAddress: "${CURRENT_HOST_IP}"
# apiServerPort: 6443
nodes:
# the control plane node config
- role: control-plane
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "ingress-ready=true"
authorization-mode: "AlwaysAllow"
# the 3 workers
- role: worker
- role: worker
- role: worker
EOF
endef
export KIND_CLUSTER_FILE_CREATOR = $(value get_kind_config_file)
## Create file definition for metallb
define get_metalb_config_file
# Remove config file
rm -rf ${METALLB_CONFIG_FILE_NAME}
# Define config file
cat << EOF >> ${METALLB_CONFIG_FILE_NAME}
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- ${DOCKER_IPAM_SUBNET_CLASS_A}.${DOCKER_IPAM_SUBNET_CLASS_B}.255.1-${DOCKER_IPAM_SUBNET_CLASS_A}.${DOCKER_IPAM_SUBNET_CLASS_B}.255.250
EOF
endef
export METALLB_CONFIG_FILE_CREATOR = $(value get_metalb_config_file)
run-local:
make -f Makefile.kind setup-kind
make -f Makefile build credentials install-local
delete-local:
make -f Makefile uninstall-local
make -f Makefile.kind delete-kind-cluster
install-kind:
curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/download/v0.8.1/kind-$(uname)-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
setup-kind:
make -f Makefile.kind create-kind-cluster
make -f Makefile.kind display-kind-cluster
make -f Makefile.kind deploy-metallb-cluster
create-kind-config-file:; @ eval "$$KIND_CLUSTER_FILE_CREATOR"
create-kind-cluster:
make -f Makefile.kind create-kind-config-file
kind create cluster --name ${CLUSTER_NAME} --config=${KIND_CONFIG_FILE_NAME}
# Remove config file
rm -rf ${KIND_CONFIG_FILE_NAME}
delete-kind-cluster:
kind delete cluster --name ${CLUSTER_NAME}
docker system prune
display-kind-cluster:
kubectl cluster-info --context kind-${CLUSTER_NAME}
create-metallb-config-file:; @ eval "$$METALLB_CONFIG_FILE_CREATOR"
deploy-metallb-cluster:
@echo "Make sure to change addresses range into ${METALLB_CONFIG_FILE_NAME}' accordding to your Docker IPAM Subnet ${DOCKER_IPAM_SUBNET}"
make -f Makefile.kind create-metallb-config-file
kubectl apply -f https://raw.githubusercontent.com/google/metallb/v0.8.3/manifests/metallb.yaml || true
# Apply after the addresses range has been changed
kubectl apply -f ./${METALLB_CONFIG_FILE_NAME} || true
rm -rf ${METALLB_CONFIG_FILE_NAME}
@databasedav
Copy link

thanks for helping me create a kind cluster with a config in a makefile :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment