diff --git a/.envrc b/.envrc index a15fe4f..e65b98e 100644 --- a/.envrc +++ b/.envrc @@ -1,2 +1,3 @@ +echo "HOMELAB NIX -- START" use_flake echo "HOMELAB NIX" \ No newline at end of file diff --git a/k8s/infrastructure/metallb/Chart.yaml b/charts/infrastructure/Chart.yaml similarity index 55% rename from k8s/infrastructure/metallb/Chart.yaml rename to charts/infrastructure/Chart.yaml index 61945e9..ca51876 100644 --- a/k8s/infrastructure/metallb/Chart.yaml +++ b/charts/infrastructure/Chart.yaml @@ -3,8 +3,3 @@ name: cluster description: An infrastructure collection of manifests for homelab type: application version: 0.1.0 -dependencies: - - name: metallb - version: 0.14.8 - repository: https://metallb.github.io/metallb - diff --git a/k8s/infrastructure/metallb/templates/metallb.yml b/charts/infrastructure/templates/ips.yml similarity index 52% rename from k8s/infrastructure/metallb/templates/metallb.yml rename to charts/infrastructure/templates/ips.yml index 5d55e6b..d500934 100644 --- a/k8s/infrastructure/metallb/templates/metallb.yml +++ b/charts/infrastructure/templates/ips.yml @@ -3,13 +3,13 @@ apiVersion: metallb.io/v1beta1 kind: IPAddressPool metadata: name: homelab-pool - namespace: "{{ .Release.Namespace }}" + namespace: "{{ .Values.ns.metallb }}" spec: - addresses: - {{- .Values.homelab.addresses | toYaml | nindent 4 }} + addresses: + - 10.0.100.1-10.0.100.255 --- apiVersion: metallb.io/v1beta1 kind: L2Advertisement metadata: name: l2ad - namespace: "{{ .Release.Namespace }}" + namespace: "{{ .Values.ns.metallb }}" diff --git a/terraform/dns.tf b/dns.tf similarity index 59% rename from terraform/dns.tf rename to dns.tf index 6f2f4d0..6999bad 100644 --- a/terraform/dns.tf +++ b/dns.tf @@ -1,9 +1,19 @@ +data "http" "myip" { + url = "https://api.myip.com/" +} data "cloudflare_zone" "this" { for_each = var.domain_mappings - name = each.key + filter = { + name = each.key + account = { + id = var.cloudflare.account_id + } + } } + locals { + myip = jsondecode(data.http.myip.response_body)["ip"] records = flatten([ for zone, records in var.domain_mappings : [ for r, def in records : @@ -11,8 +21,8 @@ locals { "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, + "ip" : def.ip != null ? def.ip : local.myip, + "zone_id" : data.cloudflare_zone.this[zone].zone_id, } ] ]) @@ -20,15 +30,20 @@ locals { record_comment = "Updated by Terraform at ${local.current_date}" } -resource "cloudflare_record" "this" { +output "myip" { + value = local.myip +} + +resource "cloudflare_dns_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 + content = each.value.ip + name = each.value.record type = each.value.type proxied = false -} \ No newline at end of file + ttl = 1 +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2a68eab --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "futils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1735563628, + "narHash": "sha256-OnSAY7XDSx7CtDoqNh8jwVwh4xNL/2HaJxGjryLWzX8=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b134951a4c9f3c995fd7be05f3243f8ecd65d798", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "futils": "futils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix index ef99b4b..81493dc 100644 --- a/flake.nix +++ b/flake.nix @@ -25,7 +25,7 @@ helm-diff ]; }) - helmfile-wrapped + terraform argocd yq jq @@ -34,6 +34,7 @@ echo "$(pwd)" export PATH="$(pwd)/bin:$PATH" export PROMPT="🏠> $PROMPT" + export KUBE_CONFIG_PATH=~/.kube/config echo "Welcome to HomeLab!" ''; }; diff --git a/helmfile.yaml b/helmfile.yaml index 6afc827..7d79f8a 100644 --- a/helmfile.yaml +++ b/helmfile.yaml @@ -5,21 +5,26 @@ repositories: url: https://grafana.github.io/helm-charts - name: external-secrets url: https://charts.external-secrets.io - - name: kubernetes-dashboard - url: https://kubernetes.github.io/dashboard/ - + - name: metallb + url: https://metallb.github.io/metallb +# helm install metallb releases: - name: external-secrets namespace: plugin-external-secrets chart: external-secrets/external-secrets version: "0.11.0" - - name: kubernetes-dashboard - namespace: kubernetes-dashboard - chart: kubernetes-dashboard/kubernetes-dashboard - # - name: prometheus-operator-crds - # namespace: plugin-monitoring - # chart: prometheus-community/prometheus-operator-crds - # version: "17.0.2" + # - name: metallb + # namespace: plugin-metallb + # chart: metallb/metallb + # version: "0.14.9" + # - name: plugin-infrastructure + # namespace: metallb + # chart: ./k8s/infrastructure + # values: + # - ./k8s/infrastructure/values.yaml + # needs: + # - plugin-metallb/metallb + # - plugin-external-secrets/external-secrets # - name: loki # namespace: plugin-monitoring # chart: grafana/loki-stack diff --git a/k8s/infrastructure/metallb/values-dev.yaml b/k8s/infrastructure/metallb/values-dev.yaml deleted file mode 100644 index e69de29..0000000 diff --git a/k8s/infrastructure/metallb/values-prod.yaml b/k8s/infrastructure/metallb/values-prod.yaml deleted file mode 100644 index e69de29..0000000 diff --git a/k8s/infrastructure/metallb/values.yaml b/k8s/infrastructure/metallb/values.yaml deleted file mode 100644 index 18b11cd..0000000 --- a/k8s/infrastructure/metallb/values.yaml +++ /dev/null @@ -1,4 +0,0 @@ - -homelab: - addresses: - - 10.0.100.1-10.0.100.255 \ No newline at end of file diff --git a/plugins.tf b/plugins.tf new file mode 100644 index 0000000..86faa84 --- /dev/null +++ b/plugins.tf @@ -0,0 +1,30 @@ +resource "helm_release" "plugin-external-secrets" { + name = "external-secrets" + chart = "external-secrets" + repository = "https://charts.external-secrets.io" + version = "0.14.2" + atomic = true + namespace = "plugin-external-secrets" + create_namespace = true +} + + +resource "helm_release" "plugin-metallb" { + name = "metallb" + chart = "metallb" + repository = "https://metallb.github.io/metallb" + version = "0.14.9" + atomic = true + namespace = "plugin-metallb" + create_namespace = true +} + +resource "helm_release" "local-chart-infrastructure" { + name = "infrastructure" + chart = "charts/infrastructure" + namespace = "default" + set = [{ + name = "ns.metallb" + value = helm_release.plugin-metallb.namespace + }] +} diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..4b485e1 --- /dev/null +++ b/provider.tf @@ -0,0 +1,34 @@ +terraform { + backend "kubernetes" { + secret_suffix = "homelab" + namespace = "default" + } +} + +terraform { + required_providers { + helm = { + source = "hashicorp/helm" + version = "3.0.0-pre1" + } + kubernetes = { + source = "hashicorp/kubernetes" + version = "2.35.1" + } + http = { + source = "hashicorp/http" + version = "3.4.5" + } + cloudflare = { + source = "cloudflare/cloudflare" + version = "5.1.0" + } + } +} + +provider "helm" {} +provider "kubernetes" {} +provider "http" {} +provider "cloudflare" { + api_token = var.cloudflare.api_token +} diff --git a/terraform/provider.tf b/terraform/provider.tf deleted file mode 100644 index a9b45be..0000000 --- a/terraform/provider.tf +++ /dev/null @@ -1,19 +0,0 @@ -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" - } -} \ No newline at end of file diff --git a/terraform/update-myip.py b/terraform/update-myip.py deleted file mode 100644 index a696a4e..0000000 --- a/terraform/update-myip.py +++ /dev/null @@ -1,38 +0,0 @@ -import httpx -import json -import os -from pathlib import Path - -MYIP_URL = os.environ.get("MYIP_URL", "https://api.myip.com/") -MYIP_KEY = os.environ.get("MYIP_KEY", "myip") - -def get_auto_vars_file(): - if p := os.environ.get("AUTO_TFVARS", None): - return Path(p) - return Path(__file__).parent / "var.auto.tfvars.json" - -auto_tfvars = get_auto_vars_file() - -print("Starting ip-update with") -print(f" > auto_tfvars = {auto_tfvars}") -print(f" > MYIP_URL = {MYIP_URL}") -print(f" > MYIP_KEY = {MYIP_KEY}") - -resp = httpx.get(MYIP_URL) - -if resp.status_code != 200: - raise AssertionError(f"{MYIP_URL} returned {resp}") - -data = resp.json() -print(f"myip response = {data}") - -myip = data['ip'] - -if not auto_tfvars.exists(): - raise AssertionError(f"{auto_tfvars} does not exists!") - -auto_tfvars_data = json.loads(auto_tfvars.read_text()) -auto_tfvars_data[MYIP_KEY] = myip -auto_tfvars.write_text(json.dumps(auto_tfvars_data, indent=4)) - -print("DONE") \ No newline at end of file diff --git a/terraform/variables.tf b/variables.tf similarity index 70% rename from terraform/variables.tf rename to variables.tf index 009c2cc..4095dde 100644 --- a/terraform/variables.tf +++ b/variables.tf @@ -1,5 +1,9 @@ -variable "myip" {} -variable "cloudflare_api_token" {} +variable "cloudflare" { + type = object({ + api_token = string + account_id = string + }) +} variable "main_domain" { default = "sosnovsky.ca" }