Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace null_resource with terraform_data #1548

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions agents.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ locals {
) }
}

resource "null_resource" "agent_config" {
resource "terraform_data" "agent_config" {
for_each = local.agent_nodes

triggers = {
triggers_replace = {
agent_id = module.agents[each.key].id
config = sha1(yamlencode(local.k3s-agent-config[each.key]))
}
Expand All @@ -85,11 +85,15 @@ resource "null_resource" "agent_config" {
inline = [local.k3s_config_update_script]
}
}
moved {
from = null_resource.agent_config
to = terraform_data.agent_config
}

resource "null_resource" "agents" {
resource "terraform_data" "agents" {
for_each = local.agent_nodes

triggers = {
triggers_replace = {
agent_id = module.agents[each.key].id
}

Expand Down Expand Up @@ -123,11 +127,15 @@ resource "null_resource" "agents" {
}

depends_on = [
null_resource.first_control_plane,
null_resource.agent_config,
terraform_data.first_control_plane,
terraform_data.agent_config,
hcloud_network_subnet.agent
]
}
moved {
from = null_resource.agents
to = terraform_data.agents
}

resource "hcloud_volume" "longhorn_volume" {
for_each = { for k, v in local.agent_nodes : k => v if((v.longhorn_volume_size >= 10) && (v.longhorn_volume_size <= 10240) && var.enable_longhorn) }
Expand All @@ -145,10 +153,10 @@ resource "hcloud_volume" "longhorn_volume" {
delete_protection = var.enable_delete_protection.volume
}

resource "null_resource" "configure_longhorn_volume" {
resource "terraform_data" "configure_longhorn_volume" {
for_each = { for k, v in local.agent_nodes : k => v if((v.longhorn_volume_size >= 10) && (v.longhorn_volume_size <= 10240) && var.enable_longhorn) }

triggers = {
triggers_replace = {
agent_id = module.agents[each.key].id
}

Expand All @@ -174,6 +182,10 @@ resource "null_resource" "configure_longhorn_volume" {
hcloud_volume.longhorn_volume
]
}
moved {
from = null_resource.configure_longhorn_volume
to = terraform_data.configure_longhorn_volume
}

resource "hcloud_floating_ip" "agents" {
for_each = { for k, v in local.agent_nodes : k => v if coalesce(lookup(v, "floating_ip"), false) }
Expand All @@ -191,14 +203,14 @@ resource "hcloud_floating_ip_assignment" "agents" {
server_id = module.agents[each.key].id

depends_on = [
null_resource.agents
terraform_data.agents
]
}

resource "null_resource" "configure_floating_ip" {
resource "terraform_data" "configure_floating_ip" {
for_each = { for k, v in local.agent_nodes : k => v if coalesce(lookup(v, "floating_ip"), false) }

triggers = {
triggers_replace = {
agent_id = module.agents[each.key].id
floating_ip_id = hcloud_floating_ip.agents[each.key].id
}
Expand Down Expand Up @@ -234,3 +246,7 @@ resource "null_resource" "configure_floating_ip" {
hcloud_floating_ip_assignment.agents
]
}
moved {
from = null_resource.configure_floating_ip
to = terraform_data.configure_floating_ip
}
18 changes: 13 additions & 5 deletions autoscaler-agents.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ locals {
}
}

resource "null_resource" "configure_autoscaler" {
resource "terraform_data" "configure_autoscaler" {
count = length(var.autoscaler_nodepools) > 0 ? 1 : 0

triggers = {
triggers_replace = {
template = local.autoscaler_yaml
}
connection {
Expand All @@ -79,12 +79,16 @@ resource "null_resource" "configure_autoscaler" {

depends_on = [
hcloud_load_balancer.cluster,
null_resource.control_planes,
terraform_data.control_planes,
random_password.rancher_bootstrap,
hcloud_volume.longhorn_volume,
data.hcloud_image.microos_x86_snapshot
]
}
moved {
from = null_resource.configure_autoscaler
to = terraform_data.configure_autoscaler
}

data "cloudinit_config" "autoscaler_config" {
count = length(var.autoscaler_nodepools)
Expand Down Expand Up @@ -155,9 +159,9 @@ data "hcloud_servers" "autoscaled_nodes" {
with_selector = "hcloud/node-group=${local.cluster_prefix}${each.value}"
}

resource "null_resource" "autoscaled_nodes_registries" {
resource "terraform_data" "autoscaled_nodes_registries" {
for_each = local.autoscaled_nodes
triggers = {
triggers_replace = {
registries = var.k3s_registries
}

Expand All @@ -178,3 +182,7 @@ resource "null_resource" "autoscaled_nodes_registries" {
inline = [local.k3s_registries_update_script]
}
}
moved {
from = null_resource.autoscaled_nodes_registries
to = terraform_data.autoscaled_nodes_registries
}
34 changes: 23 additions & 11 deletions control_planes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ locals {
) }
}

resource "null_resource" "control_plane_config" {
resource "terraform_data" "control_plane_config" {
for_each = local.control_plane_nodes

triggers = {
triggers_replace = {
control_plane_id = module.control_planes[each.key].id
config = sha1(yamlencode(local.k3s-config[each.key]))
}
Expand All @@ -149,16 +149,20 @@ resource "null_resource" "control_plane_config" {
}

depends_on = [
null_resource.first_control_plane,
terraform_data.first_control_plane,
hcloud_network_subnet.control_plane
]
}
moved {
from = null_resource.control_plane_config
to = terraform_data.control_plane_config
}


resource "null_resource" "authentication_config" {
resource "terraform_data" "authentication_config" {
for_each = local.control_plane_nodes

triggers = {
triggers_replace = {
control_plane_id = module.control_planes[each.key].id
authentication_config = sha1(var.authentication_config)
}
Expand All @@ -181,15 +185,19 @@ resource "null_resource" "authentication_config" {
}

depends_on = [
null_resource.first_control_plane,
terraform_data.first_control_plane,
hcloud_network_subnet.control_plane
]
}
moved {
from = null_resource.authentication_config
to = terraform_data.authentication_config
}

resource "null_resource" "control_planes" {
resource "terraform_data" "control_planes" {
for_each = local.control_plane_nodes

triggers = {
triggers_replace = {
control_plane_id = module.control_planes[each.key].id
}

Expand Down Expand Up @@ -226,9 +234,13 @@ resource "null_resource" "control_planes" {
}

depends_on = [
null_resource.first_control_plane,
null_resource.control_plane_config,
null_resource.authentication_config,
terraform_data.first_control_plane,
terraform_data.control_plane_config,
terraform_data.authentication_config,
hcloud_network_subnet.control_plane
]
}
moved {
from = null_resource.control_planes
to = terraform_data.control_planes
}
2 changes: 1 addition & 1 deletion data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data "github_release" "calico" {
# count = local.has_external_load_balancer ? 0 : 1
# name = var.cluster_name

# depends_on = [null_resource.kustomization]
# depends_on = [terraform_data.kustomization]
# }

data "hcloud_ssh_keys" "keys_by_selector" {
Expand Down
26 changes: 13 additions & 13 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@
| [local_file.nginx_values](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
| [local_file.traefik_values](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
| [local_sensitive_file.kubeconfig](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/sensitive_file) | resource |
| [null_resource.agent_config](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.agents](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.authentication_config](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.autoscaled_nodes_registries](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.configure_autoscaler](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.configure_floating_ip](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.configure_longhorn_volume](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.control_plane_config](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.control_planes](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.first_control_plane](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.kustomization](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.kustomization_user](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.kustomization_user_deploy](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.agent_config](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.agents](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.authentication_config](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.autoscaled_nodes_registries](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.configure_autoscaler](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.configure_floating_ip](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.configure_longhorn_volume](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.control_plane_config](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.control_planes](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.first_control_plane](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.kustomization](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.kustomization_user](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [terraform_data.kustomization_user_deploy](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [random_password.k3s_token](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
| [random_password.rancher_bootstrap](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |
| [cloudinit_config.autoscaler_config](https://registry.terraform.io/providers/hashicorp/cloudinit/latest/docs/data-sources/config) | data source |
Expand Down
10 changes: 5 additions & 5 deletions examples/kustomization_user_deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ To check the existing kustomization, you can run the following command:
```
$ terraform state list | grep kustom
...
module.kube-hetzner.null_resource.kustomization
module.kube-hetzner.null_resource.kustomization_user["demo-config-map.yaml.tpl"]
module.kube-hetzner.null_resource.kustomization_user["demo-pod.yaml.tpl"]
module.kube-hetzner.null_resource.kustomization_user["kustomization.yaml.tpl"]
module.kube-hetzner.terraform_data.kustomization
module.kube-hetzner.terraform_data.kustomization_user["demo-config-map.yaml.tpl"]
module.kube-hetzner.terraform_data.kustomization_user["demo-pod.yaml.tpl"]
module.kube-hetzner.terraform_data.kustomization_user["kustomization.yaml.tpl"]
...
```

If you want to rerun just the kustomization part, you can use the following command:

```
terraform apply -replace='module.kube-hetzner.null_resource.kustomization_user["kustomization.yaml.tpl"]' --auto-approve
terraform apply -replace='module.kube-hetzner.terraform_data.kustomization_user["kustomization.yaml.tpl"]' --auto-approve
```
16 changes: 12 additions & 4 deletions init.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ resource "hcloud_load_balancer" "cluster" {
}


resource "null_resource" "first_control_plane" {
resource "terraform_data" "first_control_plane" {
connection {
user = "root"
private_key = var.ssh_private_key
Expand Down Expand Up @@ -102,6 +102,10 @@ resource "null_resource" "first_control_plane" {
hcloud_network_subnet.control_plane
]
}
moved {
from = null_resource.first_control_plane
to = terraform_data.first_control_plane
}

# Needed for rancher setup
resource "random_password" "rancher_bootstrap" {
Expand All @@ -111,8 +115,8 @@ resource "random_password" "rancher_bootstrap" {
}

# This is where all the setup of Kubernetes components happen
resource "null_resource" "kustomization" {
triggers = {
resource "terraform_data" "kustomization" {
triggers_replace = {
# Redeploy helm charts when the underlying values change
helm_values_yaml = join("---\n", [
local.traefik_values,
Expand Down Expand Up @@ -373,8 +377,12 @@ resource "null_resource" "kustomization" {

depends_on = [
hcloud_load_balancer.cluster,
null_resource.control_planes,
terraform_data.control_planes,
random_password.rancher_bootstrap,
hcloud_volume.longhorn_volume
]
}
moved {
from = null_resource.kustomization
to = terraform_data.kustomization
}
2 changes: 1 addition & 1 deletion kubeconfig.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data "remote_file" "kubeconfig" {
}
path = "/etc/rancher/k3s/k3s.yaml"

depends_on = [null_resource.control_planes[0]]
depends_on = [terraform_data.control_planes[0]]
}

locals {
Expand Down
20 changes: 14 additions & 6 deletions kustomization_user.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ locals {
user_kustomization_templates = try(fileset("extra-manifests", "**/*.yaml.tpl"), toset([]))
}

resource "null_resource" "kustomization_user" {
resource "terraform_data" "kustomization_user" {
for_each = local.user_kustomization_templates

connection {
Expand All @@ -24,16 +24,20 @@ resource "null_resource" "kustomization_user" {
destination = replace("/var/user_kustomize/${each.key}", ".yaml.tpl", ".yaml")
}

triggers = {
triggers_replace = {
manifest_sha1 = "${sha1(templatefile("extra-manifests/${each.key}", var.extra_kustomize_parameters))}"
}

depends_on = [
null_resource.kustomization
terraform_data.kustomization
]
}
moved {
from = null_resource.kustomization_user
to = terraform_data.kustomization_user
}

resource "null_resource" "kustomization_user_deploy" {
resource "terraform_data" "kustomization_user_deploy" {
count = length(local.user_kustomization_templates) > 0 ? 1 : 0

connection {
Expand All @@ -57,11 +61,15 @@ resource "null_resource" "kustomization_user_deploy" {

lifecycle {
replace_triggered_by = [
null_resource.kustomization_user
terraform_data.kustomization_user
]
}

depends_on = [
null_resource.kustomization_user
terraform_data.kustomization_user
]
}
moved {
from = null_resource.kustomization_user_deploy
to = terraform_data.kustomization_user_deploy
}
Loading
Loading