terraform module

This commit is contained in:
dave 2023-07-12 23:54:48 -07:00
parent 2549ad7ed0
commit 16ce96d044
1 changed files with 120 additions and 0 deletions

120
main.tf Normal file
View File

@ -0,0 +1,120 @@
terraform {
backend "local" {}
required_version = ">= 0.13"
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.3.2"
}
}
experiments = [module_variable_optional_attrs]
}
provider "kubernetes" {
config_path = "~/.kube/config"
config_context = "scck8s"
}
variable "players" {
type = map(object({
alias = optional(string)
suspend = optional(bool)
image = optional(string)
dryrun = optional(bool)
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 * * * *"
}
locals {
players = {
for name, player in defaults(
var.players,
{
suspend = false
image = "dockermirror:5000/dpedu/rsscrape"
dryrun = false
schedule = var.schedule
}
) : name => merge(
player,
{
label = coalesce(lookup(player, "alias", null), name)
}
)
}
}
resource "kubernetes_cron_job" "scraper" {
for_each = local.players
metadata {
name = "rsscrape-${lower(replace(replace(each.value.label, " ", "-"), "_", "-"))}"
namespace = var.namespace
}
spec {
schedule = each.value.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 = each.value.label
}
dynamic "env" {
for_each = each.value.dryrun ? [1] : []
content {
name = "RSSCRAPE_DRYRUN"
value = 1
}
}
}
}
}
}
}
}
}