terraform work

This commit is contained in:
ari committed 2024-03-16 15:34:13 -04:00
1 parent 4c17fdf368
commit 0a799094bb
9 files changed
+176 -2

No files matched your search

+4 -1
View File
@@ -14,5 +14,8 @@ devenv.local.nix
# Terraform
*.tfvars
.terraform
os/routers/openwrt-mesh/.env
.DS_Store
.terraform.lock.hcl
# router
os/routers/openwrt-mesh/.env
+5
View File
@@ -0,0 +1,5 @@
{
"recommendations": [
"hashicorp.terraform"
]
}
+74
View File
@@ -0,0 +1,74 @@
#!/bin/sh
NS=terraforms
TF_VARS_SECRET=homelab-tfvars
TFVAR_FILE_NAME=var.tfvars
LOCAL_TF_VARS=./terraform/$TFVAR_FILE_NAME
save_tfvars() {
echo "$LOCAL_TF_VARS ... > kubectl -n $NS create secret/$TF_VARS_SECRET"
kubectl -n $NS create secret generic $TF_VARS_SECRET \
--save-config \
--dry-run=client \
--from-file=var.tfvars=$LOCAL_TF_VARS \
-o yaml | kubectl apply -f -
}
get_tfvars() {
echo "kubectl -n $NS get secret/$TF_VARS_SECRET ... > $LOCAL_TF_VARS"
kubectl -n $NS get secret/$TF_VARS_SECRET \
-o jsonpath="{.data['var\.tfvars']}" \
| base64 -d > $LOCAL_TF_VARS
}
plan() {
pushd ./terraform || exit 1
terraform plan -var-file=$TFVAR_FILE_NAME
}
apply() {
pushd ./terraform || exit 1
save_tfvars
terraform apply -var-file=$TFVAR_FILE_NAME
}
init() {
pushd ./terraform || exit 1
get_tfvars
terraform init
}
help() {
echo "My Awesome Terraform Wrapper!"
echo ""
echo " a|apply) apply terraform in ./terraform folder"
echo " p|plan) plan terraform in ./terraform folder"
echo " i|init) download latest tfvars and init ./terraform folder"
echo " s|save-tfvars) save tfvars to k8s ns"
echo " g|get-tfvars) downloads tfvars from k8s ns"
echo " h|help) display this help"
echo ""
}
case $1 in
a|apply)
apply
;;
p|plan)
plan
;;
i|init)
init
;;
s|save-tfvars)
save_tfvars
;;
g|get-tfvars)
get_tfvars
;;
h|help)
help
;;
*)
echo "invalid input '$1'!"
help
;;
esac
+2 -1
View File
@@ -6,7 +6,8 @@
pkgs.kubectl
pkgs.kubernetes-helm
pkgs.argocd
# pkgs.terraform
pkgs.yq
pkgs.jq
];
languages.nix.enable = true;
+1
View File
@@ -1,3 +1,4 @@
allowUnfree: true
inputs:
nixpkgs:
url: github:NixOS/nixpkgs/release-23.11
View File
Whitespace-only changes.
+57
View File
@@ -0,0 +1,57 @@
# data "cloudflare_zone" "main" {
# name = var.main_domain
# }
# locals {
# update_to_home = {
# "*.home" : {
# type = "A"
# }
# "ari" : {
# type = "A"
# }
# }
# }
# resource "cloudflare_record" "aboutme" {
# for_each = local.update_to_home
# zone_id = data.cloudflare_zone.main.id
# name = each.key
# value = var.myip
# comment = "Updated by Terraform at ${timestamp()}"
# type = each.value.type
# proxied = false
# }
data "cloudflare_zone" "this" {
for_each = var.domain_mappings
name = each.key
}
locals {
records = flatten([
for zone, records in var.domain_mappings : [
for r, def in records :
{
"zone" : zone,
"record" : r,
"type" : def.type != null ? def.type : "A",
"ip" : def.ip != null ? def.ip : var.myip,
"zone_id" : data.cloudflare_zone.this[zone].id,
}
]
])
current_date = formatdate("YYYY-MM-DD", timestamp())
record_comment = "Updated by Terraform at ${local.current_date}"
}
resource "cloudflare_record" "this" {
for_each = {
for d in local.records :
"${d.record}.${d.zone}" => d
}
zone_id = each.value.zone_id
name = each.value.record
value = each.value.ip
comment = local.record_comment
type = each.value.type
proxied = false
}
+19
View File
@@ -0,0 +1,19 @@
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
provider "cloudflare" {
api_token = var.cloudflare_api_token
}
terraform {
backend "kubernetes" {
secret_suffix = "homelab"
namespace = "terraforms"
}
}
+14
View File
@@ -0,0 +1,14 @@
variable "myip" {}
variable "cloudflare_api_token" {}
variable "main_domain" {
default = "sosnovsky.ca"
}
variable "secondary_domain" {
default = "skyg.ca"
}
variable "domain_mappings" {
type = map(map(object({
ip = optional(string),
type = optional(string, "A"),
})))
}