diff --git a/charts/Makefile b/charts/Makefile index 5ca4a3b5..39844abb 100644 --- a/charts/Makefile +++ b/charts/Makefile @@ -1,3 +1,4 @@ +# to be executed on kubernetes control nodes REPO_BASE_DIR := $(shell git rev-parse --show-toplevel) include ${REPO_BASE_DIR}/scripts/common.Makefile @@ -23,40 +24,16 @@ helmfile-lint: .check-helmfile-installed helmfile.yaml ## Lints the helmfile set -a; source $(REPO_CONFIG_LOCATION); set +a; \ helmfile lint -.PHONY: .helmfile-local-post-install -.helmfile-local-post-install: ## Post install steps for local helmfile deployment - @$(MAKE) -s configure-local-hosts - @echo ""; - @echo "Cluster has been deployed locally: https://$(MACHINE_FQDN)"; - @echo " For secure connections self-signed certificates are used."; - @echo ""; - .PHONY: helmfile-apply helmfile-apply: .check-helmfile-installed helmfile.yaml ## Applies the helmfile configuration set -a; source $(REPO_CONFIG_LOCATION); set +a; \ helmfile -f $(REPO_BASE_DIR)/charts/helmfile.yaml apply - @if [ "$(MACHINE_FQDN)" = "osparc.local" ]; then \ - $(MAKE) -s .helmfile-local-post-install; \ - fi - .PHONY: helmfile-sync helmfile-sync: .check-helmfile-installed helmfile.yaml ## Syncs the helmfile configuration (use `helmfile-apply` to deploy the app) set -a; source $(REPO_CONFIG_LOCATION); set +a; \ helmfile -f $(REPO_BASE_DIR)/charts/helmfile.yaml sync - @if [ "$(MACHINE_FQDN)" = "osparc.local" ]; then \ - $(MAKE) -s .helmfile-local-post-install; \ - fi - -.PHONY: configure-local-hosts -configure-local-hosts: $(REPO_CONFIG_LOCATION) ## Adds local hosts entries for the machine - # "Updating /etc/hosts with k8s $(MACHINE_FQDN) hosts ..." - @set -a; source $(REPO_CONFIG_LOCATION); set +a; \ - grep -q "127.0.0.1 $$K8S_MONITORING_FQDN" /etc/hosts || echo "127.0.0.1 $$K8S_MONITORING_FQDN" | sudo tee -a /etc/hosts - @set -a; source $(REPO_CONFIG_LOCATION); set +a; \ - grep -q "127.0.0.1 $$K8S_PRIVATE_FQDN" /etc/hosts || echo "127.0.0.1 $$K8S_PRIVATE_FQDN" | sudo tee -a /etc/hosts - .PHONY: helmfile-diff helmfile-diff: .check-helmfile-installed helmfile.yaml ## Shows the differences that would be applied by helmfile @set -a; source $(REPO_CONFIG_LOCATION); set +a; \ diff --git a/charts/README.md b/charts/README.md index 45d83630..8ee43cc8 100644 --- a/charts/README.md +++ b/charts/README.md @@ -45,7 +45,4 @@ helmfile init ## Running k8s cluster locally -```bash -cd ./osparc-ops-environments -./scripts/create_local_k8s_cluster.bash -``` +Use `./local-k8s.Makefile` targets diff --git a/charts/local-k8s.Makefile b/charts/local-k8s.Makefile new file mode 100644 index 00000000..dd83b852 --- /dev/null +++ b/charts/local-k8s.Makefile @@ -0,0 +1,28 @@ +REPO_BASE_DIR := $(shell git rev-parse --show-toplevel) +K8S_CLUSTER_NAME := osparc-cluster +# Determine this makefile's path. +# Be sure to place this BEFORE `include` directives, if any. +THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST)) + +include ${REPO_BASE_DIR}/scripts/common.Makefile +include $(REPO_CONFIG_LOCATION) + +create-cluster: ## Creates a local Kubernetes cluster + @$(REPO_BASE_DIR)/scripts/create_local_k8s_cluster.bash $(K8S_CLUSTER_NAME) + @$(MAKE) --no-print-directory --file $(THIS_MAKEFILE) configure-local-hosts + @echo ""; + @echo "Cluster has been deployed locally: https://$(MACHINE_FQDN)"; + @echo " For secure connections self-signed certificates are used."; + @echo ""; + +delete-cluster: ## Deletes the local Kubernetes cluster + @kind delete cluster --name $(K8S_CLUSTER_NAME) + @echo "Local Kubernetes cluster $(K8S_CLUSTER_NAME) has been deleted." + +.PHONY: configure-local-hosts +configure-local-hosts: $(REPO_CONFIG_LOCATION) ## Adds local hosts entries for the machine + # "Updating /etc/hosts with k8s $(MACHINE_FQDN) hosts ..." + @set -a; source $(REPO_CONFIG_LOCATION); set +a; \ + grep -q "127.0.0.1 $$K8S_MONITORING_FQDN" /etc/hosts || echo "127.0.0.1 $$K8S_MONITORING_FQDN" | sudo tee -a /etc/hosts + @set -a; source $(REPO_CONFIG_LOCATION); set +a; \ + grep -q "127.0.0.1 $$K8S_PRIVATE_FQDN" /etc/hosts || echo "127.0.0.1 $$K8S_PRIVATE_FQDN" | sudo tee -a /etc/hosts diff --git a/scripts/create_local_k8s_cluster.bash b/scripts/create_local_k8s_cluster.bash index 028f446a..b15c6a4e 100755 --- a/scripts/create_local_k8s_cluster.bash +++ b/scripts/create_local_k8s_cluster.bash @@ -6,7 +6,7 @@ set -o pipefail THIS_SCRIPT_DIR=$(dirname "$0") KIND_CONFIG_FILE="$THIS_SCRIPT_DIR/kind_config.yaml" -KIND_CLUSTER_NAME="kind" +KIND_CLUSTER_NAME="${KIND_CLUSTER_NAME:-osparc-cluster}" if ! command -v kind &> /dev/null; then echo "Error: kind is not installed. Please install kind and try again." @@ -19,7 +19,7 @@ if ! command -v kubectl &> /dev/null; then fi if kind get clusters | grep -q "^$KIND_CLUSTER_NAME$"; then - echo "A cluster is already up." + echo "A cluster '$KIND_CLUSTER_NAME' is already up." exit 0 fi @@ -28,5 +28,25 @@ if [[ ! -f "$KIND_CONFIG_FILE" ]]; then exit 1 fi +# # create a k8s cluster +# + +echo "Creating a local Kubernetes cluster named '$KIND_CLUSTER_NAME' using configuration from '$KIND_CONFIG_FILE'..." + kind create cluster --config "$KIND_CONFIG_FILE" --name "$KIND_CLUSTER_NAME" + +# +# install Calico network CNI +# + +# https://docs.tigera.io/calico/3.30/getting-started/kubernetes/kind + +echo "Installing Calico network CNI ..." + +kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/operator-crds.yaml +kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/tigera-operator.yaml + +kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.30.2/manifests/custom-resources.yaml + +while ! kubectl get pods -A -l k8s-app=calico-node 2>/dev/null | grep -q "Running"; do echo "Waiting for Calico pods to start..."; sleep 1; done diff --git a/scripts/kind_config.yaml b/scripts/kind_config.yaml index a5c9dae2..bee02c23 100644 --- a/scripts/kind_config.yaml +++ b/scripts/kind_config.yaml @@ -13,3 +13,9 @@ nodes: labels: ops: "true" simcore: "true" + +# https://archive-os-3-26.netlify.app/calico/3.26/getting-started/kubernetes/kind/ +networking: + disableDefaultCNI: true + # must match with cidr in https://raw.githubusercontent.com/projectcalico/calico/v3.26.4/manifests/custom-resources.yaml + podSubnet: 192.168.0.0/16