|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + kubernetes = { |
| 4 | + source = "hashicorp/kubernetes" |
| 5 | + version = "2.27.0" |
| 6 | + } |
| 7 | + kubectl = { |
| 8 | + source = "alekc/kubectl" |
| 9 | + version = "2.0.4" |
| 10 | + } |
| 11 | + random = { |
| 12 | + source = "hashicorp/random" |
| 13 | + version = "3.6.0" |
| 14 | + } |
| 15 | + pf = { |
| 16 | + source = "panfactum/pf" |
| 17 | + version = "0.0.3" |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +locals { |
| 23 | + all_ports = merge([for container_name, config in var.containers : config.ports]...) |
| 24 | + service_ports = { for name, config in local.all_ports : name => { |
| 25 | + pod_port = config.port |
| 26 | + service_port = config.service_port |
| 27 | + protocol = config.protocol |
| 28 | + } if config.expose_on_service } |
| 29 | +} |
| 30 | + |
| 31 | +// This is needed b/c this can never |
| 32 | +// change without destroying the deployment first |
| 33 | +resource "random_id" "daemon_set_id" { |
| 34 | + byte_length = 8 |
| 35 | +} |
| 36 | + |
| 37 | +data "pf_kube_labels" "labels" { |
| 38 | + module = "kube_daemon_set" |
| 39 | +} |
| 40 | + |
| 41 | +module "pod_template" { |
| 42 | + source = "../kube_pod" |
| 43 | + |
| 44 | + # Pod metadata |
| 45 | + namespace = var.namespace |
| 46 | + service_account = kubernetes_service_account.service_account.metadata[0].name |
| 47 | + workload_name = var.name |
| 48 | + match_labels = { id = random_id.daemon_set_id.hex } |
| 49 | + dns_policy = var.dns_policy |
| 50 | + host_network = var.host_network |
| 51 | + extra_pod_annotations = var.extra_pod_annotations |
| 52 | + extra_pod_labels = var.extra_pod_labels |
| 53 | + pod_version_labels_enabled = var.pod_version_labels_enabled |
| 54 | + extra_labels = data.pf_kube_labels.labels.labels |
| 55 | + |
| 56 | + # Container configuration |
| 57 | + common_env = var.common_env |
| 58 | + common_secrets = var.common_secrets |
| 59 | + common_env_from_secrets = var.common_env_from_secrets |
| 60 | + common_env_from_config_maps = var.common_env_from_config_maps |
| 61 | + containers = var.containers |
| 62 | + pull_through_cache_enabled = var.pull_through_cache_enabled |
| 63 | + |
| 64 | + # Mount configuration |
| 65 | + config_map_mounts = var.config_map_mounts |
| 66 | + secret_mounts = var.secret_mounts |
| 67 | + tmp_directories = var.tmp_directories |
| 68 | + mount_owner = var.mount_owner |
| 69 | + |
| 70 | + # Scheduling params |
| 71 | + priority_class_name = var.priority_class_name |
| 72 | + burstable_nodes_enabled = var.burstable_nodes_enabled |
| 73 | + spot_nodes_enabled = var.spot_nodes_enabled |
| 74 | + arm_nodes_enabled = var.arm_nodes_enabled |
| 75 | + controller_nodes_enabled = var.controller_nodes_enabled |
| 76 | + instance_type_anti_affinity_required = false |
| 77 | + az_anti_affinity_required = false |
| 78 | + host_anti_affinity_required = false |
| 79 | + extra_tolerations = var.extra_tolerations |
| 80 | + controller_nodes_required = false |
| 81 | + node_requirements = var.node_requirements |
| 82 | + node_preferences = {} |
| 83 | + az_spread_preferred = false |
| 84 | + az_spread_required = false |
| 85 | + panfactum_scheduler_enabled = false |
| 86 | + termination_grace_period_seconds = var.termination_grace_period_seconds |
| 87 | + restart_policy = var.restart_policy |
| 88 | +} |
| 89 | + |
| 90 | +resource "kubernetes_service_account" "service_account" { |
| 91 | + metadata { |
| 92 | + name = random_id.daemon_set_id.hex |
| 93 | + namespace = var.namespace |
| 94 | + labels = module.pod_template.labels |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +resource "kubectl_manifest" "daemon_set" { |
| 99 | + yaml_body = yamlencode({ |
| 100 | + apiVersion = "apps/v1" |
| 101 | + kind = "DaemonSet" |
| 102 | + metadata = { |
| 103 | + namespace = var.namespace |
| 104 | + name = var.name |
| 105 | + labels = module.pod_template.labels |
| 106 | + annotations = { |
| 107 | + "reloader.stakater.com/auto" = "true" |
| 108 | + } |
| 109 | + } |
| 110 | + spec = { |
| 111 | + minReadySeconds = var.min_ready_seconds |
| 112 | + updateStrategy = { |
| 113 | + type = var.update_type |
| 114 | + } |
| 115 | + selector = { |
| 116 | + matchLabels = module.pod_template.match_labels |
| 117 | + } |
| 118 | + template = module.pod_template.pod_template |
| 119 | + } |
| 120 | + }) |
| 121 | + server_side_apply = true |
| 122 | + force_conflicts = true |
| 123 | + wait_for_rollout = var.wait_for_rollout |
| 124 | +} |
| 125 | + |
| 126 | +resource "kubectl_manifest" "vpa" { |
| 127 | + count = var.vpa_enabled ? 1 : 0 |
| 128 | + yaml_body = yamlencode({ |
| 129 | + apiVersion = "autoscaling.k8s.io/v1" |
| 130 | + kind = "VerticalPodAutoscaler" |
| 131 | + metadata = { |
| 132 | + name = var.name |
| 133 | + namespace = var.namespace |
| 134 | + labels = module.pod_template.labels |
| 135 | + } |
| 136 | + spec = { |
| 137 | + targetRef = { |
| 138 | + apiVersion = "apps/v1" |
| 139 | + kind = "DaemonSet" |
| 140 | + name = var.name |
| 141 | + } |
| 142 | + updatePolicy = { |
| 143 | + updateMode = "Auto" |
| 144 | + } |
| 145 | + resourcePolicy = { |
| 146 | + containerPolicies = [for config in var.containers : { |
| 147 | + containerName = config.name |
| 148 | + minAllowed = { |
| 149 | + memory = "${config.minimum_memory}Mi" |
| 150 | + cpu = "${config.minimum_cpu}m" |
| 151 | + } |
| 152 | + maxAllowed = { for k, v in { |
| 153 | + memory = config.maximum_memory != null ? "${config.maximum_memory}Mi" : null |
| 154 | + cpu = config.maximum_cpu != null ? "${config.maximum_cpu}Mi" : null |
| 155 | + } : k => v if v != null } |
| 156 | + }] |
| 157 | + } |
| 158 | + } |
| 159 | + }) |
| 160 | + depends_on = [kubectl_manifest.daemon_set] |
| 161 | +} |
| 162 | + |
| 163 | +resource "kubectl_manifest" "pdb" { |
| 164 | + yaml_body = yamlencode({ |
| 165 | + apiVersion = "policy/v1" |
| 166 | + kind = "PodDisruptionBudget" |
| 167 | + metadata = { |
| 168 | + name = "${var.name}-pdb" |
| 169 | + namespace = var.namespace |
| 170 | + labels = module.pod_template.labels |
| 171 | + } |
| 172 | + spec = { |
| 173 | + selector = { |
| 174 | + matchLabels = module.pod_template.match_labels |
| 175 | + } |
| 176 | + minAvailable = var.min_available # Needs to be minAvailable as daemonset does not implement the scale subresource |
| 177 | + unhealthyPodEvictionPolicy = var.unhealthy_pod_eviction_policy |
| 178 | + } |
| 179 | + }) |
| 180 | + force_conflicts = true |
| 181 | + server_side_apply = true |
| 182 | + depends_on = [kubectl_manifest.daemon_set] |
| 183 | +} |
| 184 | + |
0 commit comments