Skip to content

Commit

Permalink
refactor: separate module for k3s registries.yaml
Browse files Browse the repository at this point in the history
This will allow reuse in hcloud-cloud-controller-manager
  • Loading branch information
apricote committed Jul 3, 2024
1 parent 3e82d9b commit 39b5e75
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 33 deletions.
54 changes: 54 additions & 0 deletions k3s_registry/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
variable "registry_service_ip" {
type = string
default = "10.43.0.2"
}

variable "registry_port" {
type = number
default = 30666
}

variable "private_key" {
type = string
default = ""
}

variable "server" {
type = object({
id = number,
ipv4_address = string,
})
}

resource "null_resource" "k3sup_worker" {
triggers = {
id = var.server.id
}

connection {
host = var.server.ipv4_address
private_key = var.private_key != "" ? var.private_key : null
}

provisioner "remote-exec" {
inline = ["mkdir -p /etc/rancher/k3s"]
}
provisioner "file" {
content = yamlencode({
"mirrors" : {
"localhost:${var.registry_port}" : {
"endpoint" : ["http://${var.registry_service_ip}:5000"]
}
}
})
destination = "/etc/rancher/k3s/registries.yaml"
}
}

output "registry_port" {
value = var.registry_port
}

output "registry_service_ip" {
value = var.registry_service_ip
}
51 changes: 18 additions & 33 deletions main-setup.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ locals {
# network (10.0.0.0/8) but must not overlap with the Subnet (10.0.0.0/24)
cluster_cidr = "10.244.0.0/16"

registry_service_ip = "10.43.0.2"
registry_port = 30666

kubeconfig_path = abspath("${path.root}/files/kubeconfig.yaml")
env_path = abspath("${path.root}/files/env.sh")
}

module "registry_control" {
source = "./k3s_registry"

server = hcloud_server.control
private_key = tls_private_key.ssh.private_key_openssh
}

resource "null_resource" "k3sup_control" {
triggers = {
id = hcloud_server.control.id
Expand All @@ -23,20 +27,6 @@ resource "null_resource" "k3sup_control" {
private_key = tls_private_key.ssh.private_key_openssh
}

provisioner "remote-exec" {
inline = ["mkdir -p /etc/rancher/k3s"]
}
provisioner "file" {
content = yamlencode({
"mirrors" : {
"localhost:${local.registry_port}" : {
"endpoint" : ["http://${local.registry_service_ip}:5000"]
}
}
})
destination = "/etc/rancher/k3s/registries.yaml"
}

provisioner "local-exec" {
command = <<-EOT
k3sup install --print-config=false \
Expand All @@ -59,6 +49,15 @@ resource "null_resource" "k3sup_control" {
}
}

module "registry_worker" {
source = "./k3s_registry"

count = var.worker_count

server = hcloud_server.worker[count.index]
private_key = tls_private_key.ssh.private_key_openssh
}

resource "null_resource" "k3sup_worker" {
count = var.worker_count

Expand All @@ -76,20 +75,6 @@ resource "null_resource" "k3sup_worker" {
private_key = tls_private_key.ssh.private_key_openssh
}

provisioner "remote-exec" {
inline = ["mkdir -p /etc/rancher/k3s"]
}
provisioner "file" {
content = yamlencode({
"mirrors" : {
"localhost:${local.registry_port}" : {
"endpoint" : ["http://${local.registry_service_ip}:5000"]
}
}
})
destination = "/etc/rancher/k3s/registries.yaml"
}

provisioner "local-exec" {
command = <<-EOT
k3sup join \
Expand Down Expand Up @@ -184,7 +169,7 @@ resource "helm_release" "docker_registry" {

set {
name = "service.clusterIP"
value = local.registry_service_ip
value = module.registry_control.registry_service_ip
}
set {
name = "tolerations[0].key"
Expand All @@ -209,7 +194,7 @@ resource "local_file" "env" {
#!/usr/bin/env bash
export KUBECONFIG=${data.local_sensitive_file.kubeconfig.filename}
export SKAFFOLD_DEFAULT_REPO=localhost:${local.registry_port}
export SKAFFOLD_DEFAULT_REPO=localhost:${module.registry_control.registry_port}
EOT
filename = local.env_path
file_permission = "0644"
Expand Down

0 comments on commit 39b5e75

Please sign in to comment.