Skip to content

Commit

Permalink
Revert "private_key expl (#33)"
Browse files Browse the repository at this point in the history
This reverts commit 268ac84.
  • Loading branch information
JWDobken committed Oct 31, 2022
1 parent 5bdc6ac commit dd85ed4
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 62 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ Create a Kubernetes cluster:

```hcl
module "hcloud_kubernetes_cluster" {
source = "JWDobken/kubernetes/hcloud"
cluster_name = "demo-cluster"
hcloud_token = var.hcloud_token
hcloud_ssh_keys = [hcloud_ssh_key.demo_cluster.id]
private_key = file("~/.ssh/hcloud")
control_plane_type = "cx11" # optional
worker_type = "cx21" # optional
worker_count = 3
source = "JWDobken/kubernetes/hcloud"
cluster_name = "demo-cluster"
hcloud_token = var.hcloud_token
hcloud_ssh_keys = [hcloud_ssh_key.demo_cluster.id]
control_plane_type = "cx11" # optional
worker_type = "cx21" # optional
worker_count = 3
}
output "kubeconfig" {
Expand Down Expand Up @@ -123,7 +122,7 @@ provider "kubernetes" {

## Acknowledgements

This module came about when I was looking for an affordable Kubernetes cluster. There are a couple of Terraform projects on which the current is heavily based:
This module came about when I was looking for an affordable Kubernetes cluster. There is an [article from Christian Beneke](https://community.hetzner.com/tutorials/install-kubernetes-cluster) and there are a couple of Terraform projects on which the current is heavily based:

- Patrick Stadler's [hobby-kube provisioning](https://github.com/hobby-kube/provisioning)
- Niclas Mietz's [terraform-k8s-hcloud](https://github.com/solidnerd/terraform-k8s-hcloud)
Expand Down
3 changes: 0 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module "cluster" {
source = "./modules/cluster"
hcloud_token = var.hcloud_token
hcloud_ssh_keys = var.hcloud_ssh_keys
private_key = var.private_key
cluster_name = var.cluster_name
location = var.location
image = var.image
Expand All @@ -21,7 +20,6 @@ module "firewall" {
source = "./modules/firewall"
connections = module.cluster.all_nodes.*.ipv4_address
subnet_ip_range = var.subnet_ip_range
private_key = var.private_key
}

module "kubernetes" {
Expand All @@ -33,5 +31,4 @@ module "kubernetes" {
worker_nodes = module.cluster.worker_nodes
private_ips = module.cluster.private_ips
kubernetes_version = var.kubernetes_version
private_key = var.private_key
}
14 changes: 8 additions & 6 deletions modules/cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ resource "hcloud_server" "control_plane_node" {
}

connection {
host = self.ipv4_address
type = "ssh"
private_key = var.private_key
user = "root"
type = "ssh"
timeout = "2m"
host = self.ipv4_address
}

provisioner "file" {
Expand Down Expand Up @@ -50,9 +51,10 @@ resource "hcloud_server" "worker_node" {
}

connection {
host = self.ipv4_address
type = "ssh"
private_key = var.private_key
user = "root"
type = "ssh"
timeout = "2m"
host = self.ipv4_address
}

provisioner "file" {
Expand Down
5 changes: 0 additions & 5 deletions modules/cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ variable "hcloud_ssh_keys" {
type = list(any)
}

variable "private_key" {
type = string
sensitive = true
}

variable "cluster_name" {
type = string
}
Expand Down
11 changes: 3 additions & 8 deletions modules/firewall/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ variable "subnet_ip_range" {
type = string
}

variable "private_key" {
type = string
sensitive = true
}

resource "null_resource" "firewall" {
count = length(var.connections)

Expand All @@ -21,9 +16,9 @@ resource "null_resource" "firewall" {
}

connection {
host = element(var.connections, count.index)
type = "ssh"
private_key = var.private_key
host = element(var.connections, count.index)
user = "root"
agent = true
}

provisioner "remote-exec" {
Expand Down
10 changes: 3 additions & 7 deletions modules/kubernetes/kubeadm_join.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ resource "null_resource" "kubeadm_join" {
depends_on = [null_resource.install]

connection {
host = element(var.worker_nodes.*.ipv4_address, count.index)
type = "ssh"
private_key = var.private_key
host = element(var.worker_nodes.*.ipv4_address, count.index)
user = "root"
agent = true
}

provisioner "local-exec" {
command = <<EOT
eval "$(ssh-agent -s)"
echo "${var.private_key}" | tr -d '\r' | ssh-add -
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
root@${local.control_plane_ip} 'echo $(kubeadm token create) > /tmp/kubeadm_token'
EOT
}

provisioner "local-exec" {
command = <<EOT
eval "$(ssh-agent -s)"
echo "${var.private_key}" | tr -d '\r' | ssh-add -
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
root@${local.control_plane_ip}:/tmp/kubeadm_token \
/tmp/kubeadm_token
Expand Down
17 changes: 4 additions & 13 deletions modules/kubernetes/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ resource "null_resource" "install" {
count = length(local.connections)

connection {
host = element(local.connections, count.index)
type = "ssh"
private_key = var.private_key
type = "ssh"
host = element(local.connections, count.index)
user = "root"
agent = true
}

provisioner "remote-exec" {
Expand Down Expand Up @@ -110,8 +111,6 @@ module "kubeconfig" {
trigger = element(var.control_plane_nodes.*.ipv4_address, 0)

command = <<EOT
eval "$(ssh-agent -s)" > /dev/null
echo "${var.private_key}" | tr -d '\r' | ssh-add - > /dev/null
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
root@${local.control_plane_ip} 'cat /root/.kube/config'
EOT
Expand All @@ -124,8 +123,6 @@ module "endpoint" {
trigger = element(var.control_plane_nodes.*.ipv4_address, 0)

command = <<EOT
eval "$(ssh-agent -s)" > /dev/null
echo "${var.private_key}" | tr -d '\r' | ssh-add - > /dev/null
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
root@${local.control_plane_ip} 'kubectl config --kubeconfig /root/.kube/config view -o jsonpath='{.clusters[0].cluster.server}''
EOT
Expand All @@ -138,8 +135,6 @@ module "certificate_authority_data" {
trigger = element(var.control_plane_nodes.*.ipv4_address, 0)

command = <<EOT
eval "$(ssh-agent -s)" > /dev/null
echo "${var.private_key}" | tr -d '\r' | ssh-add - > /dev/null
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
root@${local.control_plane_ip} 'kubectl config --kubeconfig /root/.kube/config view --flatten -o jsonpath='{.clusters[0].cluster.certificate-authority-data}''
EOT
Expand All @@ -152,8 +147,6 @@ module "client_certificate_data" {
trigger = element(var.control_plane_nodes.*.ipv4_address, 0)

command = <<EOT
eval "$(ssh-agent -s)" > /dev/null
echo "${var.private_key}" | tr -d '\r' | ssh-add - > /dev/null
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
root@${local.control_plane_ip} 'kubectl config --kubeconfig /root/.kube/config view --flatten -o jsonpath='{.users[0].user.client-certificate-data}''
EOT
Expand All @@ -166,8 +159,6 @@ module "client_key_data" {
trigger = element(var.control_plane_nodes.*.ipv4_address, 0)

command = <<EOT
eval "$(ssh-agent -s)" > /dev/null
echo "${var.private_key}" | tr -d '\r' | ssh-add - > /dev/null
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
root@${local.control_plane_ip} 'kubectl config --kubeconfig /root/.kube/config view --flatten -o jsonpath='{.users[0].user.client-key-data}''
EOT
Expand Down
5 changes: 0 additions & 5 deletions modules/kubernetes/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ variable "private_ips" {
type = list(any)
}

variable "private_key" {
type = string
sensitive = true
}

# CONTROL-PLANE NODES
variable "control_plane_nodes" {
type = list(any)
Expand Down
6 changes: 0 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ variable "hcloud_ssh_keys" {
type = list(any)
}

variable "private_key" {
description = "(Required) - content of an SSH key to use for the connection. These can be loaded from a file on disk using the `file` function."
type = string
sensitive = true
}

variable "location" {
description = "(Optional) - Location, e.g. 'nbg1' (Neurenberg)."
type = string
Expand Down

0 comments on commit dd85ed4

Please sign in to comment.