Skip to content

Commit

Permalink
Merge pull request #19 from tedsteen/flags-per-agent
Browse files Browse the repository at this point in the history
Additional flags per instance

NOTE: because I will rewrote the module, I allow to fail the CI this time.
  • Loading branch information
xunleii authored May 24, 2020
2 parents cec8753 + 4cf3667 commit 48371b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 36 deletions.
5 changes: 2 additions & 3 deletions agent_nodes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ locals {
"--server https://${var.server_node.ip}:6443",
"--token ${random_password.k3s_cluster_secret.result}"
]
agent_install_flags = join(" ", concat(var.additional_flags.agent, local.agent_default_flags))
}

resource null_resource k3s_agents {
for_each = var.agent_nodes

triggers = {
server_init = null_resource.k3s_server.id
install_args = sha1(local.agent_install_flags)
install_args = sha1(join(" ", concat(local.agent_default_flags, each.value.additional_flags)))
agent_ip = each.value.ip
}
depends_on = [null_resource.k3s_server_installer]
Expand Down Expand Up @@ -105,7 +104,7 @@ resource null_resource k3s_agents_installer {
provisioner remote-exec {
inline = [
<<EOT
INSTALL_K3S_VERSION=${local.k3s_version} INSTALL_K3S_EXEC=agent sh /tmp/k3s-installer ${local.agent_install_flags} \
INSTALL_K3S_VERSION=${local.k3s_version} INSTALL_K3S_EXEC=agent sh /tmp/k3s-installer ${join(" ", concat(local.agent_default_flags, each.value.additional_flags))} \
${join(" ", [for label, value in each.value.labels : "--node-label '${label}=${value}'" if value != null])} \
${join(" ", [for key, taint in each.value.taints : "--node-taint '${key}=${taint}'" if taint != null])} \
--node-ip ${each.value.ip} --node-name ${each.value.name}
Expand Down
26 changes: 13 additions & 13 deletions examples/hcloud-k3s/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ module k3s {
}
drain_timeout = "30s"

additional_flags = {
server = [
"--disable-cloud-controller",
"--flannel-iface ens10",
"--kubelet-arg cloud-provider=external" # required to use https://github.com/hetznercloud/hcloud-cloud-controller-manager
]
agent = [
"--flannel-iface ens10",
"--kubelet-arg cloud-provider=external" # required to use https://github.com/hetznercloud/hcloud-cloud-controller-manager
]
}

server_node = {
name = "server"
ip = hcloud_server_network.server_network.ip
Expand All @@ -30,6 +18,12 @@ module k3s {
connection = {
host = hcloud_server.server.ipv4_address
}
additional_flags = [
"--disable-cloud-controller",
"--flannel-iface ens10",
"--kubelet-arg cloud-provider=external", # required to use https://github.com/hetznercloud/hcloud-cloud-controller-manager
"--kubelet-arg provider-id=hcloud://${hcloud_server.server.id}"
]
}

agent_nodes = {
Expand All @@ -48,6 +42,12 @@ module k3s {
connection = {
host = hcloud_server.agents[i].ipv4_address
}

additional_flags = [
"--flannel-iface ens10",
"--kubelet-arg cloud-provider=external", # required to use https://github.com/hetznercloud/hcloud-cloud-controller-manager
"--kubelet-arg provider-id=hcloud://${hcloud_server.agents[i].id}"
]
}
}
}
}
5 changes: 2 additions & 3 deletions server_node.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ locals {
]
server_labels_flags = [for label, value in var.server_node.labels : "--node-label '${label}=${value}'" if value != null]
server_taints_flags = [for key, taint in var.server_node.taints : "--node-taint '${key}=${taint}'" if taint != null]
server_install_flags = join(" ", concat(var.additional_flags.server, local.server_labels_flags, local.server_default_flags))
}

resource null_resource k3s_server {
triggers = {
install_args = sha1(local.server_install_flags)
install_args = sha1(join(" ", concat(local.server_labels_flags, local.server_default_flags, var.server_node.additional_flags)))
}

connection {
Expand Down Expand Up @@ -104,7 +103,7 @@ resource null_resource k3s_server_installer {
# Install K3S server
provisioner "remote-exec" {
inline = [
"INSTALL_K3S_VERSION=${local.k3s_version} sh /tmp/k3s-installer ${local.server_install_flags}",
"INSTALL_K3S_VERSION=${local.k3s_version} sh /tmp/k3s-installer ${join(" ", concat(local.server_default_flags, var.server_node.additional_flags))}",
"until kubectl get nodes | grep -v '[WARN] No resources found'; do sleep 1; done"
]
}
Expand Down
24 changes: 7 additions & 17 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ variable cluster_cidr {
}
}

variable additional_flags {
description = "Add additional flags during the k3s installation (see https://rancher.com/docs/k3s/latest/en/installation/install-options/)."
type = object({
server = list(string)
agent = list(string)
})
default = {
server = []
agent = []
}
}

variable drain_timeout {
description = "The length of time to wait before giving up the node draining. Infinite by default."
type = string
Expand All @@ -48,17 +36,19 @@ variable server_node {
labels = map(string)
taints = map(string)
connection = map(any)
additional_flags = list(string)
})
}

variable agent_nodes {
description = "K3s agent nodes definitions. The key is used as node name during the k3s installation."
type = map(object({
name = string
ip = string
labels = map(string)
taints = map(string)
connection = map(any)
name = string
ip = string
labels = map(string)
taints = map(string)
connection = map(any)
additional_flags = list(string)
}))
default = {}
}

0 comments on commit 48371b1

Please sign in to comment.