osrsscrape/main.tf
2024-01-24 20:55:19 -08:00

100 lines
2.3 KiB
HCL

terraform {
backend "local" {}
required_version = ">= 1.5"
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.3.2"
}
}
}
provider "kubernetes" {
config_path = "~/.kube/config"
config_context = "scck8s"
}
variable "players" {
type = map(object({
alias = optional(string)
suspend = optional(bool, false)
image = optional(string, "dockermirror:5000/dpedu/rsscrape")
dryrun = optional(bool, false)
schedule = optional(string)
}))
description = "players to scrape"
}
variable "influx_url" {
type = string
description = "influxdb connection url"
}
variable "namespace" {
type = string
default = "jobs"
}
variable "schedule" {
type = string
description = "kube cron expression"
default = "*/10 * * * *"
}
resource "kubernetes_cron_job" "scraper" {
for_each = var.players
metadata {
name = "rsscrape-${lower(replace(replace(each.key, " ", "-"), "_", "-"))}"
namespace = var.namespace
}
spec {
schedule = coalesce(each.value.schedule, var.schedule)
concurrency_policy = "Replace"
starting_deadline_seconds = 60
suspend = lookup(each.value, "suspend", false)
job_template {
metadata {}
spec {
template {
metadata {}
spec {
restart_policy = "Never"
container {
name = "scrape"
image = each.value.image
image_pull_policy = "Always"
env {
name = "RSSCRAPE_PLAYER_NAME"
value = each.key
}
env {
name = "RSSCRAPE_INFLUX_URL"
value = var.influx_url
}
env {
name = "RSSCRAPE_TIMEOUT"
value = "60"
}
env {
name = "RSSCRAPE_PLAYER_ALIAS"
value = coalesce(each.value.alias, each.key)
}
dynamic "env" {
for_each = each.value.dryrun ? [1] : []
content {
name = "RSSCRAPE_DRYRUN"
value = 1
}
}
}
}
}
}
}
}
}