Skip to content

Commit

Permalink
Module to get K8S join command from control plane (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
FriedCircuits authored Nov 19, 2021
1 parent 26d9d6a commit eee330e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Hodgepodge of Terraform modules.
* Proxmox
* VM Clone (Create VMs from Templates (created by packer))

* K8S
* Get Join (Get join config from Kuberenetes control plane)
1 change: 1 addition & 0 deletions modules/k8s/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Kuberenetes Related Modules
5 changes: 5 additions & 0 deletions modules/k8s/get-join/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Get Join

Small module that will ssh into a k8s control plan node and run the create join command. This will return a map of info needed to run the join command as well as the full join command.

Intended to be passed to cloud-init of a pre-configured worker node to auto join a cluster.
17 changes: 17 additions & 0 deletions modules/k8s/get-join/get_join.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -e

eval "$(jq -r '@sh "k8s_host=\(.hostname) ssh_user=\(.ssh_user)"')"

out=$(ssh -q ${ssh_user}@${k8s_host} <<'EOF'
kubeadm token create --print-join-command
EOF
)

join_command="${out##*$'\n'}"
join_command="$(echo "$join_command"|tr -d '\n')"

join_command=($join_command)


echo $(jq -n --arg host "${join_command[2]}" --arg token "${join_command[4]}" --arg cacerthash "${join_command[6]}" '{"host":$host,"token":$token,"cacerthash":$cacerthash}')
5 changes: 5 additions & 0 deletions modules/k8s/get-join/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

data "external" "k8s" {
program = ["bash", "${path.module}/get_join.sh"]
query = var.k8s_control_config
}
11 changes: 11 additions & 0 deletions modules/k8s/get-join/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "k8s_join" {
description = "Map of config values needed to join a kuberentes cluster."
value = data.external.k8s.result
senstive = true
}

output "full_join_command" {
description = "Full command to join a worker node to kubernetes cluster."
senstive = true
value = "kubeadm join ${data.external.k8s.result.host} --token ${data.external.k8s.result.token} --discovery-token-ca-cert-hash ${data.external.k8s.result.cacerthash}"
}
7 changes: 7 additions & 0 deletions modules/k8s/get-join/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "k8s_control_config" {
description = "Hostname/SSH User of K8S control plan node to get join command from. Assumes your private is deployed for `ssh_user`."
type = object({
hostname = string
ssh_user = string
})
}
3 changes: 3 additions & 0 deletions modules/k8s/get-join/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 1.0"
}

0 comments on commit eee330e

Please sign in to comment.