From 33ca8596eeaeea621320092bbb1a3570791344f9 Mon Sep 17 00:00:00 2001 From: Jorge Falcon <22119513+BCTBB@users.noreply.github.com> Date: Wed, 18 Jun 2025 15:42:57 -0400 Subject: [PATCH] Fleet k8s Terraform updates (#58) - Incrementing version of k8s module to 1.1.2 - Adding `component` label for Fleet deployment - Adding `component` label selector for Fleet service - Adding support for `MYSQL_READ_REPLICA` environment variables. - Can now be configured via `database_read_replica` variable in `example/main.tf` - Updating default REDIS URL for example purposes, used in deployment guide, to fix a REDIS READONLY error. --- k8s/README.md | 1 + k8s/cronjob.tf | 57 +++++++++++++++++++++++++++++++++++++++++++++ k8s/deploy.tf | 44 ++++++++++++++++++++++++++++++++++ k8s/example/main.tf | 51 +++++++++++++++++++++++++++++++++------- k8s/locals.tf | 1 + k8s/svc.tf | 3 ++- k8s/variables.tf | 25 ++++++++++++++++++++ 7 files changed, 172 insertions(+), 10 deletions(-) diff --git a/k8s/README.md b/k8s/README.md index 80f8ef1..34626ad 100644 --- a/k8s/README.md +++ b/k8s/README.md @@ -320,6 +320,7 @@ No modules. | [anti\_affinity\_rules](#input\_anti\_affinity\_rules) | Used to configure anti-affinity rules for the fleet deployment, migration job, and vuln-processing cron job. |
object({ 
required_during_scheduling_ignored_during_execution = optional(list(any), [])
preferred_during_scheduling_ignored_during_execution = optional(list(any),
[
{
weight = 100
label_selector = {
match_expressions = [
{
key = "app"
operator = "In"
values = ["fleet"]
}
]
}
topology_key = "kubernetes.io/hostname"
}
])
})
| n/a | yes | | [cache](#input\_cache) | Used to configure redis specific values for use in the Fleet deployment, migration job, and vuln-processing cron job. |
object({
enabled = optional(bool, false)
address = optional(string, "redis:6379")
database = optional(number, 0)
use_password = optional(bool, false)
secret_name = optional(string, "redis")
password_key = optional(string, "password")
})
| n/a | yes | | [database](#input\_database) | Used to configure database specific values for use in the Fleet deployment, migration job, and vuln-processing cron job. |
object({
enabled = optional(bool, false)
secret_name = optional(string, "mysql")
address = optional(string, "mysql:3306")
database = optional(string, "fleet")
username = optional(string, "fleet")
password_key = optional(string, "password")
max_open_conns = optional(number, 50)
max_idle_conns = optional(number, 50)
conn_max_lifetime = optional(number, 0)

tls = object({
enabled = optional(bool, false)
config = optional(string, "")
server_name = optional(string, "")
ca_cert_key = optional(string, "")
cert_key = optional(string, "")
key_key = optional(string, "")
})
})
| n/a | yes | +| [database\_read\_replica](#input\_database\_read\_replica) | Used to configure database\_read\_replica specific values for use in the Fleet deployment and vuln-processing cron job. |
object({
enabled = optional(bool, false)
secret_name = optional(string, "mysql")
address = optional(string, "mysql-ro:3306")
database = optional(string, "fleet")
username = optional(string, "fleet-ro")
password_key = optional(string, "ro-password")
password_path = optional(string,"")
max_open_conns = optional(number, 50)
max_idle_conns = optional(number, 50)
conn_max_lifetime = optional(number, 0)

tls = object({
enabled = optional(bool, false)
config = optional(string, "")
server_name = optional(string, "")
ca_cert_key = optional(string, "")
cert_key = optional(string, "")
key_key = optional(string, "")
})
})
| n/a | yes | | [environment\_from\_config\_maps](#input\_environment\_from\_config\_maps) | Used to configure additional environment variables from a config map for the fleet deployment and vuln-processing cron job. | `list(map(string))` | `[]` | no | | [environment\_from\_secrets](#input\_environment\_from\_secrets) | Used to configure additional environment variables from a secret for the fleet deployment and vuln-processing cron job. | `list(map(string))` | `[]` | no | | [environment\_variables](#input\_environment\_variables) | Used to configure additional environment variables for the fleet deployment and vuln-processing cron job. | `list(map(string))` | `[]` | no | diff --git a/k8s/cronjob.tf b/k8s/cronjob.tf index 998a113..00f490b 100644 --- a/k8s/cronjob.tf +++ b/k8s/cronjob.tf @@ -103,6 +103,63 @@ resource "kubernetes_cron_job_v1" "fleet_vuln_processing_cron_job" { } } + dynamic "env" { + for_each = local.database.tls.enabled ? [ + { name = "FLEET_MYSQL_TLS_CA", value = "/secrets/mysql/${local.database.tls.ca_cert_key}" }, + { name = "FLEET_MYSQL_TLS_CERT", value = "/secrets/mysql/${local.database.tls.cert_secret_key}" }, + { name = "FLEET_MYSQL_TLS_KEY", value = "/secrets/mysql/${local.database.tls.key_key}" }, + { name = "FLEET_MYSQL_TLS_CONFIG", value = local.database.tls.config }, + { name = "FLEET_MYSQL_TLS_SERVER_NAME", value = local.database.tls.server_name } + ] : [] + + content { + name = env.value.name + value = env.value.value + } + } + + dynamic "env" { + for_each = [ + { name = "FLEET_MYSQL_READ_REPLICA_ADDRESS", value = local.database_read_replica.address }, + { name = "FLEET_MYSQL_READ_REPLICA_DATABASE", value = local.database_read_replica.database }, + { name = "FLEET_MYSQL_READ_REPLICA_USERNAME", value = local.database_read_replica.username }, + { name = "FLEET_MYSQL_READ_REPLICA_PASSWORD_PATH", value = local.database_read_replica.password_path }, + { name = "FLEET_MYSQL_READ_REPLICA_MAX_OPEN_CONNS", value = local.database_read_replica.max_open_conns }, + { name = "FLEET_MYSQL_READ_REPLICA_MAX_IDLE_CONNS", value = local.database_read_replica.max_idle_conns }, + { name = "FLEET_MYSQL_READ_REPLICA_CONN_MAX_LIFETIME", value = local.database_read_replica.conn_max_lifetime } + ] + + content { + name = env.value.name + value = env.value.value + } + } + + env { + name = "FLEET_MYSQL_READ_REPLICA_PASSWORD" + value_from { + secret_key_ref { + name = local.database_read_replica.secret_name + key = local.database_read_replica.password_key + } + } + } + + dynamic "env" { + for_each = local.database.tls.enabled ? [ + { name = "FLEET_MYSQL_READ_REPLICA_TLS_CA", value = "/secrets/mysql/${local.database_read_replica.tls.ca_cert_key}" }, + { name = "FLEET_MYSQL_READ_REPLICA_TLS_CERT", value = "/secrets/mysql/${local.database_read_replica.tls.cert_secret_key}" }, + { name = "FLEET_MYSQL_READ_REPLICA_TLS_KEY", value = "/secrets/mysql/${local.database_read_replica.tls.key_key}" }, + { name = "FLEET_MYSQL_READ_REPLICA_TLS_CONFIG", value = local.database_read_replica.tls.config }, + { name = "FLEET_MYSQL_READ_REPLICA_TLS_SERVER_NAME", value = local.database_read_replica.tls.server_name } + ] : [] + + content { + name = env.value.name + value = env.value.value + } + } + dynamic "env" { for_each = [ { name = "FLEET_REDIS_ADDRESS", value = local.cache.address }, diff --git a/k8s/deploy.tf b/k8s/deploy.tf index bac5a30..6189cd8 100644 --- a/k8s/deploy.tf +++ b/k8s/deploy.tf @@ -19,6 +19,7 @@ resource "kubernetes_deployment" "fleet" { selector { match_labels = { app = "fleet" + component = "fleet-server" } } @@ -34,6 +35,7 @@ resource "kubernetes_deployment" "fleet" { metadata { labels = { app = "fleet" + component = "fleet-server" } annotations = local.pod_annotations } @@ -194,6 +196,48 @@ resource "kubernetes_deployment" "fleet" { } } + dynamic "env" { + for_each = [ + { name = "FLEET_MYSQL_READ_REPLICA_ADDRESS", value = local.database_read_replica.address }, + { name = "FLEET_MYSQL_READ_REPLICA_DATABASE", value = local.database_read_replica.database }, + { name = "FLEET_MYSQL_READ_REPLICA_USERNAME", value = local.database_read_replica.username }, + { name = "FLEET_MYSQL_READ_REPLICA_PASSWORD_PATH", value = local.database_read_replica.password_path }, + { name = "FLEET_MYSQL_READ_REPLICA_MAX_OPEN_CONNS", value = local.database_read_replica.max_open_conns }, + { name = "FLEET_MYSQL_READ_REPLICA_MAX_IDLE_CONNS", value = local.database_read_replica.max_idle_conns }, + { name = "FLEET_MYSQL_READ_REPLICA_CONN_MAX_LIFETIME", value = local.database_read_replica.conn_max_lifetime } + ] + + content { + name = env.value.name + value = env.value.value + } + } + + env { + name = "FLEET_MYSQL_READ_REPLICA_PASSWORD" + value_from { + secret_key_ref { + name = local.database_read_replica.secret_name + key = local.database_read_replica.password_key + } + } + } + + dynamic "env" { + for_each = local.database.tls.enabled ? [ + { name = "FLEET_MYSQL_READ_REPLICA_TLS_CA", value = "/secrets/mysql/${local.database_read_replica.tls.ca_cert_key}" }, + { name = "FLEET_MYSQL_READ_REPLICA_TLS_CERT", value = "/secrets/mysql/${local.database_read_replica.tls.cert_secret_key}" }, + { name = "FLEET_MYSQL_READ_REPLICA_TLS_KEY", value = "/secrets/mysql/${local.database_read_replica.tls.key_key}" }, + { name = "FLEET_MYSQL_READ_REPLICA_TLS_CONFIG", value = local.database_read_replica.tls.config }, + { name = "FLEET_MYSQL_READ_REPLICA_TLS_SERVER_NAME", value = local.database_read_replica.tls.server_name } + ] : [] + + content { + name = env.value.name + value = env.value.value + } + } + dynamic "env" { for_each = [ { name = "FLEET_REDIS_ADDRESS", value = local.cache.address }, diff --git a/k8s/example/main.tf b/k8s/example/main.tf index ecd3aa9..06573b8 100644 --- a/k8s/example/main.tf +++ b/k8s/example/main.tf @@ -1,11 +1,11 @@ module "fleet" { - source = "git::https://github.com/fleetdm/fleet-terraform//k8s?depth=1&ref=tf-mod-k8s-v1.1.1" + source = "git::https://github.com/fleetdm/fleet-terraform//k8s?depth=1&ref=tf-mod-k8s-v1.1.2" namespace = "fleet" hostname = "fleet.localhost.local" replicas = "3" image_repository = "fleetdm/fleet" - image_tag = "v4.66.0" + image_tag = "v4.69.0" /* Example: image_pull_secrets = [ @@ -135,12 +135,16 @@ module "fleet" { path_type = "ImplementationSpecific" }] }] - tls = { - secret_name = "chart-example-tls" - hosts = [ - "fleet.localhost.local" - ] - } + /* + # Example + tls = { + secret_name = "chart-example-tls" + hosts = [ + "fleet.localhost.local" + ] + } + */ + tls = {} } fleet = { @@ -309,9 +313,38 @@ module "fleet" { } } + /* + database_read_replica - optional and be configured to enable Fleet to use read replica for + MySQL SELECT statements. + + + database_read_replica = { + enabled = false + address = "fleet-database-mysql-read-replica:3306" + database = "fleet" + username = "fleet-ro" + secret_name = "mysql" + password_key = "ro-password" + password_path = "" + max_open_conns = 50 + max_idle_conns = 50 + conn_max_lifetime = 0 + + tls = { + enabled = false + config = "" + server_name = "" + ca_cert_key = "" + cert_key = "" + key_key = "" + } + } + */ + database_read_replica = {} + cache = { enabled = false - address = "fleet-cache-redis-headless:6379" + address = "fleet-cache-redis-master:6379" database = "0" use_password = true secret_name = "redis" diff --git a/k8s/locals.tf b/k8s/locals.tf index 1f01882..96a0036 100644 --- a/k8s/locals.tf +++ b/k8s/locals.tf @@ -18,6 +18,7 @@ locals { fleet = var.fleet osquery = var.osquery database = var.database + database_read_replica = var.database_read_replica cache = var.cache gke = var.gke environment_variables = var.environment_variables diff --git a/k8s/svc.tf b/k8s/svc.tf index 8b65b16..dac8a6e 100644 --- a/k8s/svc.tf +++ b/k8s/svc.tf @@ -8,6 +8,7 @@ resource "kubernetes_service" "fleet-service" { spec { selector = { app = "fleet" + component = "fleet-server" } dynamic "port" { @@ -31,4 +32,4 @@ resource "kubernetes_service" "fleet-service" { depends_on = [ resource.kubernetes_deployment.fleet ] -} \ No newline at end of file +} diff --git a/k8s/variables.tf b/k8s/variables.tf index a47b5be..dfb9a1f 100644 --- a/k8s/variables.tf +++ b/k8s/variables.tf @@ -324,6 +324,31 @@ variable "database" { description = "Used to configure database specific values for use in the Fleet deployment, migration job, and vuln-processing cron job." } +variable "database_read_replica" { + type = object({ + enabled = optional(bool, false) + secret_name = optional(string, "mysql") + address = optional(string, "mysql-ro:3306") + database = optional(string, "fleet") + username = optional(string, "fleet-ro") + password_key = optional(string, "ro-password") + password_path = optional(string,"") + max_open_conns = optional(number, 50) + max_idle_conns = optional(number, 50) + conn_max_lifetime = optional(number, 0) + + tls = object({ + enabled = optional(bool, false) + config = optional(string, "") + server_name = optional(string, "") + ca_cert_key = optional(string, "") + cert_key = optional(string, "") + key_key = optional(string, "") + }) + }) + description = "Used to configure database_read_replica specific values for use in the Fleet deployment and vuln-processing cron job." +} + variable "cache" { type = object({ enabled = optional(bool, false)