Skip to content

Commit

Permalink
Merge pull request #112 from xunleii/fix/110-111
Browse files Browse the repository at this point in the history
Update providers and fix #110
  • Loading branch information
xunleii authored Mar 18, 2023
2 parents 972cb27 + bffc30d commit 0d9f883
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 115 deletions.
106 changes: 56 additions & 50 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions agent_nodes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ locals {
])
agent_taints = local.managed_taint_enabled ? { for o in local.agent_taints_list : o.key => o.value if o.key != "" } : {}

// Generate a map of all calculed agent fields, used during k3s installation.
// Generate a map of all calculated agent fields, used during k3s installation.
agents_metadata = {
for key, agent in var.agents :
key => {
Expand Down Expand Up @@ -106,7 +106,7 @@ resource "null_resource" "agents_install" {

// Upload k3s install script
provisioner "file" {
content = data.http.k3s_installer.body
content = data.http.k3s_installer.response_body
destination = "/tmp/k3s-installer"
}

Expand Down
49 changes: 4 additions & 45 deletions examples/hcloud-k3s/k3s.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ module "k3s" {
hcloud_server.control_planes[i].name => {
ip = hcloud_server_network.control_planes[i].ip
connection = {
host = hcloud_server.control_planes[i].ipv4_address
host = hcloud_server.control_planes[i].ipv4_address
private_key = trimspace(tls_private_key.ed25519-provisioning.private_key_pem)
}
flags = ["--disable-cloud-controller"]
annotations = { "server_id" : i } // theses annotations will not be managed by this module
Expand All @@ -34,54 +35,12 @@ module "k3s" {
name = hcloud_server.agents[i].name
ip = hcloud_server_network.agents_network[i].ip
connection = {
host = hcloud_server.agents[i].ipv4_address
host = hcloud_server.agents[i].ipv4_address
private_key = trimspace(tls_private_key.ed25519-provisioning.private_key_pem)
}

labels = { "node.kubernetes.io/pool" = hcloud_server.agents[i].labels.nodepool }
taints = { "dedicated" : hcloud_server.agents[i].labels.nodepool == "gpu" ? "gpu:NoSchedule" : null }
}
}
}

provider "kubernetes" {
host = module.k3s.kubernetes.api_endpoint
cluster_ca_certificate = module.k3s.kubernetes.cluster_ca_certificate
client_certificate = module.k3s.kubernetes.client_certificate
client_key = module.k3s.kubernetes.client_key
}

resource "kubernetes_service_account" "bootstrap" {
depends_on = [module.k3s.kubernetes_ready]

metadata {
name = "bootstrap"
namespace = "default"
}
}

resource "kubernetes_cluster_role_binding" "boostrap" {
depends_on = [module.k3s.kubernetes_ready]

metadata {
name = "bootstrap"
}

subject {
kind = "ServiceAccount"
name = "bootstrap"
namespace = "default"
}

role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "admin"
}
}

data "kubernetes_secret" "sa_credentials" {
metadata {
name = kubernetes_service_account.bootstrap.default_secret_name
namespace = "default"
}
}
14 changes: 9 additions & 5 deletions examples/hcloud-k3s/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
provider "hcloud" {}

data "hcloud_image" "ubuntu" {
name = "ubuntu-20.04"
}

resource "tls_private_key" "ed25519-provisioning" {
algorithm = "ED25519"
}

resource "hcloud_ssh_key" "default" {
name = "K3S terraform module - Provisionning SSH key"
public_key = var.ssh_key
public_key = trimspace(tls_private_key.ed25519-provisioning.public_key_openssh)
}

resource "hcloud_network" "k3s" {
Expand All @@ -16,7 +24,3 @@ resource "hcloud_network_subnet" "k3s_nodes" {
network_zone = "eu-central"
ip_range = "10.254.1.0/24"
}

data "hcloud_image" "ubuntu" {
name = "ubuntu-20.04"
}
8 changes: 4 additions & 4 deletions examples/hcloud-k3s/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ output "summary" {
value = module.k3s.summary
}

output "bootstrap_sa" {
description = "Bootstrap ServiceAccount. Can be used by Terraform to provision this cluster."
value = data.kubernetes_secret.sa_credentials.data
output "ssh_private_key" {
description = "Generated SSH private key."
value = tls_private_key.ed25519-provisioning.private_key_openssh
sensitive = true
}
}
5 changes: 0 additions & 5 deletions examples/hcloud-k3s/variables.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
variable "ssh_key" {
description = "SSH public Key content needed to provision the instances."
type = string
}

variable "servers_num" {
description = "Number of control plane nodes."
default = 3
Expand Down
4 changes: 2 additions & 2 deletions k3s_version.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ data "http" "k3s_version" {

// Fetch the k3s installation script
data "http" "k3s_installer" {
url = "https://raw.githubusercontent.com/rancher/k3s/${jsondecode(data.http.k3s_version.body).data[1].latest}/install.sh"
url = "https://raw.githubusercontent.com/rancher/k3s/${jsondecode(data.http.k3s_version.response_body).data[1].latest}/install.sh"
}

locals {
// Use the fetched version if 'lastest' is specified
k3s_version = var.k3s_version == "latest" ? jsondecode(data.http.k3s_version.body).data[1].latest : var.k3s_version
k3s_version = var.k3s_version == "latest" ? jsondecode(data.http.k3s_version.response_body).data[1].latest : var.k3s_version
}
4 changes: 2 additions & 2 deletions server_nodes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ locals {
])
server_taints = local.managed_taint_enabled ? { for o in local.server_taints_list : o.key => o.value if o.key != "" } : {}

// Generate a map of all calculed server fields, used during k3s installation.
// Generate a map of all calculated server fields, used during k3s installation.
servers_metadata = {
for key, server in var.servers :
key => {
Expand Down Expand Up @@ -206,7 +206,7 @@ resource "null_resource" "servers_install" {

// Upload k3s file
provisioner "file" {
content = data.http.k3s_installer.body
content = data.http.k3s_installer.response_body
destination = "/tmp/k3s-installer"
}

Expand Down
1 change: 1 addition & 0 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ terraform {
version = "~> 4.0"
}
}

required_version = "~> 1.0"
}

0 comments on commit 0d9f883

Please sign in to comment.