Skip to content

Commit 292addd

Browse files
authored
Merge pull request #56 from kbst/kind
Add modules and config to provide a local Kubestack lab based on Kind
2 parents ab7610f + 8dad496 commit 292addd

File tree

22 files changed

+211
-1
lines changed

22 files changed

+211
-1
lines changed

common/cluster_services/kubectl_apply.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
set -e
44

5-
echo "${KUBECONFIG_DATA}" | base64 -d > $KUBECONFIG
5+
if [ "$(uname -s)" == "Darwin" ]; then
6+
echo "${KUBECONFIG_DATA}" | base64 -D > $KUBECONFIG
7+
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
8+
echo "${KUBECONFIG_DATA}" | base64 -d > $KUBECONFIG
9+
fi
610

711
if [ -s $1 ]
812
then
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
data "local_file" "kubeconfig" {
2+
filename = "${data.external.kind_kubeconfig.result["kubeconfig_path"]}"
3+
4+
depends_on = ["null_resource.cluster"]
5+
}
6+
7+
module "cluster_services" {
8+
source = "../../../common/cluster_services"
9+
10+
cluster_type = "kind"
11+
12+
metadata_labels = "${var.metadata_labels}"
13+
14+
template_string = "${data.local_file.kubeconfig.content}"
15+
16+
template_vars = {
17+
# hack, because modules can't have depends_on
18+
# prevent a race between kubernetes provider and cluster services/kustomize
19+
# creating the namespace and the provider erroring out during apply
20+
not_used = "${kubernetes_namespace.current.metadata.0.name}"
21+
}
22+
}

kind/_modules/kind/ingress.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "kubernetes_namespace" "current" {
2+
provider = "kubernetes.kind"
3+
4+
metadata {
5+
name = "ingress-kbst-default"
6+
}
7+
8+
# namespace metadata may change through the manifests
9+
# hence ignoring this for the terraform lifecycle
10+
lifecycle {
11+
ignore_changes = ["metadata"]
12+
}
13+
14+
depends_on = ["null_resource.cluster"]
15+
}

kind/_modules/kind/main.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
data "template_file" "config" {
2+
template = "${file("${path.module}/templates/kind_config.yaml")}"
3+
4+
vars = {
5+
node_roles = "${var.node_roles}"
6+
}
7+
}
8+
9+
locals {
10+
kind_config_path = "${path.cwd}/clusters/${var.metadata_fqdn}/kind_config.yaml"
11+
}
12+
13+
resource "local_file" "config" {
14+
content = "${data.template_file.config.rendered}"
15+
filename = "${local.kind_config_path}"
16+
}
17+
18+
resource "null_resource" "cluster" {
19+
triggers = {
20+
node_roles = "${base64sha256(data.template_file.config.rendered)}"
21+
}
22+
23+
provisioner "local-exec" {
24+
command = "kind create cluster --wait 10m --name ${var.metadata_name} --config ${local.kind_config_path}"
25+
}
26+
27+
provisioner "local-exec" {
28+
when = "destroy"
29+
command = "kind delete cluster --name ${var.metadata_name}"
30+
}
31+
32+
depends_on = ["local_file.config"]
33+
}

kind/_modules/kind/provider.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
data "external" "kind_kubeconfig" {
2+
program = ["sh", "${path.module}/provider_authenticator.sh"]
3+
4+
query {
5+
cluster_name = "${var.metadata_name}"
6+
}
7+
}
8+
9+
provider "kubernetes" {
10+
alias = "kind"
11+
12+
config_path = "${data.external.kind_kubeconfig.result["kubeconfig_path"]}"
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Extract cluster name from STDIN
5+
eval "$(jq -r '@sh "CLUSTER_NAME=\(.cluster_name)"')"
6+
7+
# Retrieve path to kubeconfig from kind
8+
KUBECONFIG_PATH=$(kind get kubeconfig-path --name $CLUSTER_NAME)
9+
10+
# Output path to kubeconfig file as JSON
11+
jq -n --arg kubeconfig_path "$KUBECONFIG_PATH" '{"kubeconfig_path": $kubeconfig_path}'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Cluster
2+
apiVersion: kind.sigs.k8s.io/v1alpha3
3+
nodes:
4+
%{ for role in split(",", node_roles) ~}
5+
- role: ${role}
6+
%{ endfor ~}

kind/_modules/kind/variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "metadata_name" {
2+
type = "string"
3+
description = "Metadata name to use."
4+
}
5+
6+
variable "metadata_fqdn" {
7+
type = "string"
8+
description = "DNS name of the zone. Requires dot at the end. E.g. example.com"
9+
}
10+
11+
variable "metadata_tags" {
12+
type = "list"
13+
description = "Metadata tags to use."
14+
}
15+
16+
variable "metadata_labels" {
17+
type = "map"
18+
description = "Metadata labels to use."
19+
}
20+
21+
variable "node_roles" {
22+
type = "string"
23+
description = "Comma seperated list of node roles. E.g. 'control-plan,worker'"
24+
}

kind/cluster/configuration.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
locals {
2+
# apps config and merged ops config
3+
workspaces = {
4+
apps = "${var.configuration["apps"]}"
5+
ops = "${merge(var.configuration["apps"], var.configuration["ops"])}"
6+
}
7+
8+
# current workspace config
9+
cfg = "${local.workspaces[terraform.workspace]}"
10+
11+
name_prefix = "${lookup(local.cfg, "name_prefix")}"
12+
13+
base_domain = "${lookup(local.cfg, "base_domain")}"
14+
15+
node_roles = "${lookup(local.cfg, "node_roles", "control-plane,worker")}"
16+
}

kind/cluster/main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module "cluster_metadata" {
2+
source = "../../common/metadata"
3+
4+
name_prefix = "${local.name_prefix}"
5+
base_domain = "${local.base_domain}"
6+
7+
provider_name = "kind"
8+
provider_region = "localhost"
9+
}
10+
11+
module "cluster" {
12+
source = "../_modules/kind"
13+
14+
metadata_name = "${module.cluster_metadata.name}"
15+
metadata_fqdn = "${module.cluster_metadata.fqdn}"
16+
metadata_tags = "${module.cluster_metadata.tags}"
17+
metadata_labels = "${module.cluster_metadata.labels}"
18+
19+
node_roles = "${local.node_roles}"
20+
}

0 commit comments

Comments
 (0)