Skip to content

Commit 0b6df05

Browse files
committed
Initial commit
0 parents  commit 0b6df05

10 files changed

+136
-0
lines changed

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
volumes
2+
3+
.envrc
4+
5+
node_modules
6+
7+
.terraform
8+
*.tfstate
9+
*.tfstate.tmp
10+
*.tfstate.backup
11+
*.tfstate.*.backup
12+
*.tfvars
13+
plan.json
14+
15+
/config/*
16+
!/config/.gitkeep

.terraform-version

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.3.6

modules/k3s/backend.tf

Whitespace-only changes.

modules/k3s/locals.tf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
servers = {for server in var.servers : server.host => server}
3+
}

modules/k3s/main.tf

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
resource "ssh_resource" "install_k3s" {
2+
for_each = local.servers
3+
host = each.value.host
4+
user = each.value.user
5+
private_key = file(each.value.private_key)
6+
commands = [
7+
"if ! command -v k3s &>/dev/null; then",
8+
" echo 'K3s is not installed, proceeding with installation...'",
9+
" curl -sfL ${var.k3s.download_url} | INSTALL_K3S_VERSION='${var.k3s.version}' sh -s - server --docker --write-kubeconfig-mode 644 --disable=traefik",
10+
"else",
11+
" echo 'K3s is already installed.'",
12+
"fi"
13+
]
14+
timeout = "10m"
15+
}
16+
17+
18+
resource "ssh_resource" "start_k3s" {
19+
for_each = local.servers
20+
host = each.value.host
21+
user = each.value.user
22+
private_key = file(each.value.private_key)
23+
commands = [
24+
"if systemctl is-active --quiet k3s; then",
25+
" echo 'K3s is installed and running, nothing to do.'",
26+
"else",
27+
" echo 'K3s is installed but not running, starting the service...'",
28+
" sudo systemctl start k3s",
29+
"fi"
30+
]
31+
32+
timeout = "10m"
33+
}
34+
35+
data "remote_file" "kubeconfig" {
36+
for_each = local.servers
37+
conn {
38+
host = each.value.host
39+
user = each.value.user
40+
private_key = file(each.value.private_key)
41+
}
42+
path = "/etc/rancher/k3s/k3s.yaml"
43+
depends_on = [
44+
ssh_resource.start_k3s
45+
]
46+
}
47+
48+
49+
50+
51+

modules/k3s/outputs.tf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
output "kubeconfig" {
2+
description = "KubeConfigs for all servers"
3+
value = {
4+
for kubeconfig in values(data.remote_file.kubeconfig)
5+
: kubeconfig.conn[0].host => replace(kubeconfig.content, "127.0.0.1", kubeconfig.conn[0].host)
6+
}
7+
sensitive = true
8+
}

modules/k3s/override.tf

Whitespace-only changes.

modules/k3s/providers.tf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
provider "ssh" {
2+
3+
}
4+
provider "remote" {
5+
6+
}
7+
8+

modules/k3s/terraform.tf

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
terraform {
2+
required_version = ">=1.3.0"
3+
4+
required_providers {
5+
ssh = {
6+
source = "loafoe/ssh"
7+
version = "2.3.0"
8+
}
9+
remote = {
10+
source = "tenstad/remote"
11+
version = "0.1.1"
12+
}
13+
}
14+
15+
}

modules/k3s/variables.tf

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "k3s" {
2+
type = object({
3+
download_url = string,
4+
version = string,
5+
})
6+
description = "K3s instance details"
7+
default = {
8+
download_url = "https://get.k3s.io",
9+
version = "v1.25.5+k3s1"
10+
}
11+
validation {
12+
condition = can(regex("^https?://[a-zA-Z0-9.-]+(?:/[a-zA-Z0-9/-]*)?$", var.k3s.download_url))
13+
error_message = "The provided URL is not valid. It should start with http:// or https:// and be a valid URL format."
14+
}
15+
16+
}
17+
18+
variable "servers" {
19+
type = list(object({
20+
host = string
21+
user = string
22+
private_key = string
23+
}))
24+
default = []
25+
description = "Node IPs"
26+
validation {
27+
condition = alltrue([
28+
for server in var.servers :
29+
can(regex("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", server.host))
30+
])
31+
error_message = "One of the IPs provided are invalid"
32+
}
33+
}
34+

0 commit comments

Comments
 (0)