26 Commits

Author SHA1 Message Date
140321f714 Migrate the middleware to the new CRD 2024-02-15 14:36:39 +01:00
a77bf1ec51 improve oauth2 ouput 2024-01-29 09:48:07 +01:00
4f306cdacf adding ingress output for secret_name 2024-01-29 09:33:11 +01:00
0a9700ab94 fix 2024-01-28 11:48:34 +01:00
0a92ad09d5 fix 2024-01-28 10:13:10 +01:00
c1aac2424b fix 2024-01-28 10:11:54 +01:00
ccfac82ad4 fix 2024-01-28 10:09:48 +01:00
96714b7186 fix 2024-01-28 10:07:40 +01:00
dd03003ebc Adding service definition output 2024-01-28 09:56:20 +01:00
68d609a022 Adding service name output 2024-01-28 09:52:33 +01:00
bc5f7ff32b Adding redis url output 2024-01-28 09:44:02 +01:00
71b5da2e14 adding MongoDB 2024-01-28 09:26:08 +01:00
2c066b9049 Adding storage modules 2024-01-27 00:03:20 +01:00
c39cc31bc6 Actualiser application/variables.tf 2024-01-26 12:02:00 +01:00
e645aa0060 Merge pull request 'Refacto modules' (#2) from feature/svc_lb into main
Reviewed-on: #2
2024-01-26 09:56:02 +01:00
384fdd7b69 Refacto and add lb 2024-01-26 09:56:02 +01:00
15c5e64ea5 fixes 2024-01-24 16:58:23 +01:00
1c106733e4 fixes 2024-01-24 16:22:26 +01:00
e29893226d fixes 2024-01-24 16:12:35 +01:00
fe67299dfd fixes 2024-01-24 16:10:40 +01:00
c26cd0b7e7 fixes 2024-01-24 15:47:25 +01:00
c13085ba7c fixes 2024-01-24 15:17:07 +01:00
5d41cce866 fixes 2024-01-24 15:16:23 +01:00
5619140b79 fixes 2024-01-24 15:12:02 +01:00
e70e06c929 adding user configuration path as output for oauth2 modules 2024-01-24 15:04:44 +01:00
eb9596d527 adding secrets values as output for oauth2 modules 2024-01-24 14:49:36 +01:00
36 changed files with 889 additions and 74 deletions

View File

@@ -1,5 +1,5 @@
locals {
app_name = var.component == var.instance ? var.instance : format("%s-%s", var.component, var.instance)
app_name = (var.component == var.instance || var.component=="") ? var.instance : format("%s-%s", var.component, var.instance)
main_group = format("app-%s", local.app_name)
}
data "authentik_group" "akadmin" {
@@ -12,7 +12,7 @@ resource "authentik_group" "groups" {
resource "authentik_group" "subgroup" {
count = length(var.sub_groups)
name = format("%s-%s", local.app_name, var.sub_groups[count.index])
name = format("%s-%s", local.main_group, var.sub_groups[count.index])
parent = authentik_group.groups.id
}

View File

@@ -5,3 +5,6 @@ output "application-id" {
output "policy-id" {
value = authentik_policy_expression.policy.id
}
output "main_group" {
value = local.main_group
}

42
backup/backup.tf Normal file
View File

@@ -0,0 +1,42 @@
locals {
app_slug = (var.component == var.instance || var.component=="") ? var.instance : format("%s-%s", var.component, var.instance)
}
resource "kubectl_manifest" "backup_schedule" {
count = var.backups.enable ? 1 : 0
yaml_body = <<-EOF
apiVersion: k8up.io/v1
kind: Schedule
metadata:
name: "${var.app_slug}-backup"
namespace: "${var.namespace}"
labels: ${jsonencode(local.labels)}
spec:
backend:
repoPasswordSecretRef:
key: "${var.backups.restic_key}"
name: "${var.backups.secret_name}"
s3:
accessKeyIDSecretRef:
key: "${var.backups.key_id_key}"
name: "${var.backups.secret_name}"
bucket: "${var.app_slug}-${var.namespace}"
endpoint: "${var.backups.endpoint}/restic"
secretAccessKeySecretRef:
key: "${var.backups.secret_key}"
name: "${var.backups.secret-name}"
backup:
schedule: "${var.backups.schedule.backup}"
failedJobsHistoryLimit: 2
successfulJobsHistoryLimit: 2
check:
schedule: "${var.backups.schedule.check}"
prune:
retention:
keepDaily: ${var.backups.retention.keepDaily}
keepMonthly: ${var.backups.retention.keepMonthly}
keepWeekly: ${var.backups.retention.keepWeekly}
keepYearly: ${var.backups.retention.keepYearly}
schedule: "${var.backups.schedule.prune}"
EOF
}

3
backup/outputs.tf Normal file
View File

@@ -0,0 +1,3 @@
output "schedule_name" {
value = kubectl_manifest.backup_schedule.name
}

8
backup/providers.tf Normal file
View File

@@ -0,0 +1,8 @@
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
}
}
}

55
backup/variables.tf Normal file
View File

@@ -0,0 +1,55 @@
variable "component" {
type = string
}
variable "instance" {
type = string
}
variable "namespace" {
type = string
}
variable "labels" {
type = map(string)
}
variable "backups" {
default = {
"enable" = false
"endpoint" = ""
"key_id_key" = "s3-id"
"restic_key" = "bck-password"
"retention" = {
"keepDaily" = 14
"keepMonthly" = 12
"keepWeekly" = 6
"keepYearly" = 12
}
"schedule" = {
"backup" = "30 3 * * *"
"check" = "30 5 * * 1"
"db" = "30 3 * * *"
"prune" = "30 1 * * 0"
}
"secret_key" = "s3-secret"
"secret_name" = "backup-settings"
"use_barman" = false
}
type = object({
enable = optional(bool),
endpoint = optional(string),
key_id_key = optional(string),
restic_key = optional(string),
retention = optional(object({
keepDaily = optional(number),
keepMonthly = optional(number),
keepWeekly = optional(number),
keepYearly = optional(number)
})),
schedule = optional(object({
backup = optional(string),
check = optional(string),
prune = optional(string)
})),
secret_key = optional(string),
secret_name = optional(string),
use_barman = optional(bool)
})
}

View File

@@ -1,8 +1,11 @@
locals {
forward_outpost_providers = jsondecode(data.http.get_forward_outpost.response_body).results[0].providers
forward_outpost_pk = jsondecode(data.http.get_forward_outpost.response_body).results[0].pk
app_name = var.component == var.instance ? var.instance : format("%s-%s", var.component, var.instance)
main_group = format("app-%s", local.app_name)
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
forward_labels = merge(var.labels, {
"app.kubernetes.io/component" = "authentik-forward"
})
main_group = format("app-%s", var.app_name)
external_url = format("https://%s", var.dns_names[0])
rules_icons = [for v in var.dns_names : {
"host" = "${v}"
@@ -24,9 +27,9 @@ resource "kubectl_manifest" "prj_ingress_icon" {
apiVersion: "networking.k8s.io/v1"
kind: "Ingress"
metadata:
name: "${var.instance}-icons"
name: "${local.app_slug}-icons"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.forward_labels)}
spec:
ingressClassName: "${var.ingress_class}"
rules: ${jsonencode(local.rules_icons)}
@@ -41,7 +44,7 @@ data "authentik_flow" "default-authorization-flow" {
}
resource "authentik_provider_proxy" "prj_forward" {
name = local.app_name
name = local.app_slug
external_host = local.external_url
authorization_flow = data.authentik_flow.default-authorization-flow.id
mode = "forward_single"
@@ -71,12 +74,12 @@ resource "restapi_object" "forward_outpost_binding" {
resource "kubectl_manifest" "prj_middleware" {
yaml_body = <<-EOF
apiVersion: traefik.containo.us/v1alpha1
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: "forward-${local.app_name}"
name: "${local.app_slug}-forward"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.forward_labels)}
spec:
forwardAuth:
address: http://ak-outpost-forward.${var.domain}-auth.svc:9000/outpost.goauthentik.io/auth/traefik

View File

@@ -3,5 +3,9 @@ output "provider-id" {
}
output "sso_logout" {
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/o/${var.component}-${var.instance}/end-session/"
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/o/${local.app_slug}/end-session/"
}
output "middleware" {
value = "${local.app_slug}-forward"
}

View File

@@ -1,33 +1,48 @@
variable "component" {
type = string
}
variable "instance" {
type = string
}
variable "icon" {
type = string
}
variable "domain" {
type = string
}
variable "namespace" {
type = string
}
variable "ingress_class" {
type = string
}
variable "labels" {
type = map(string)
}
variable "dns_names" {
type = list(string)
}
variable "access_token_validity" {
type = string
default = "hours=10" // ;minutes=10
}
variable "app_name" {
type = string
default = ""
}
variable "service" {
}
variable "request_headers" {
type = map(string)
}

View File

@@ -1,6 +1,9 @@
locals {
name = "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
pres_labels = merge(var.labels, {
"app.kubernetes.io/component" = "presentation"
})
rules = [for v in var.dns_names : {
"host" = "${v}"
"http" = {
@@ -15,12 +18,12 @@ locals {
}]
tls = var.enforce_tls ? [
{
secretName = var.cert_name != "" ? var.cert_name : "${local.name}-cert"
secretName = var.cert_name != "" ? var.cert_name : "${local.app_slug}-cert"
hosts = var.dns_names
}
] : []
middlewares = concat(
var.create_redirect ? ["${local.name}-https"] : [],
var.create_redirect ? ["${local.app_slug}-https"] : [],
var.middlewares
)
annotations = merge(
@@ -39,11 +42,11 @@ resource "kubectl_manifest" "prj_certificate" {
apiVersion: "cert-manager.io/v1"
kind: "Certificate"
metadata:
name: "${local.name}"
name: "${local.app_slug}"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.pres_labels)}
spec:
secretName: "${local.name}-cert"
secretName: "${local.app_slug}-cert"
dnsNames: ${jsonencode(var.dns_names)}
issuerRef:
kind: "ClusterIssuer"
@@ -55,12 +58,12 @@ resource "kubectl_manifest" "prj_certificate" {
resource "kubectl_manifest" "prj_https_redirect" {
count = var.create_redirect || var.component == "" ? 1 : 0
yaml_body = <<-EOF
apiVersion: "traefik.containo.us/v1alpha1"
apiVersion: "traefik.io/v1alpha1"
kind: "Middleware"
metadata:
name: "${local.name}-https"
name: "${local.app_slug}-https"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.pres_labels)}
spec:
redirectScheme:
scheme: "https"
@@ -74,9 +77,9 @@ resource "kubectl_manifest" "prj_ingress" {
apiVersion: "networking.k8s.io/v1"
kind: "Ingress"
metadata:
name: "${local.name}"
name: "${local.app_slug}"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.pres_labels)}
annotations: ${jsonencode(local.annotations)}
spec:
ingressClassName: "${var.ingress_class}"

3
ingress/outputs.tf Normal file
View File

@@ -0,0 +1,3 @@
output "secret_name" {
value = var.create_cert ? "${local.app_slug}-cert":""
}

126
mongo/mongo.tf Normal file
View File

@@ -0,0 +1,126 @@
locals {
app_slug = (var.component == var.instance || var.component=="") ? var.instance : format("%s-%s", var.component, var.instance)
mongo-password = data.kubernetes_secret_v1.prj_mongo_secret.data["password"]
username = var.username==""?var.component==""?var.instance:var.component:var.username
db_name = var.db_name==""?var.component==""?var.instance:var.component:var.db_name
mongo-labels = merge(var.labels, {
"app.kubernetes.io/component" = "mongo"
})
}
resource "kubectl_manifest" "prj_mongo_secret" {
ignore_fields = ["metadata.annotations"]
yaml_body = <<-EOF
apiVersion: "secretgenerator.mittwald.de/v1alpha1"
kind: "StringSecret"
metadata:
name: "${local.app_slug}-mongo"
namespace: "${var.namespace}"
labels: ${jsonencode(local.mongo-labels)}
spec:
forceRegenerate: false
fields:
- fieldName: "password"
length: "16"
EOF
}
data "kubernetes_secret_v1" "prj_mongo_secret" {
depends_on = [ kubectl_manifest.prj_mongo_secret ]
metadata {
name = "${local.app_slug}-mongo"
namespace = var.namespace
}
}
resource "kubectl_manifest" "prj_mongo" {
yaml_body = <<-EOF
apiVersion: mongodbcommunity.mongodb.com/v1
kind: MongoDBCommunity
metadata:
name: "${local.app_slug}-mongo"
namespace: "${var.namespace}"
labels: ${jsonencode(local.mongo-labels)}
spec:
members: 1
type: ${var.mongo_type}
version: "${var.mongo_version}"
statefulSet:
spec:
template:
metadata:
annotations:
"k8up.io/backupcommand": "sh -c 'mongodump --username=$MONGODB_USER --password=$MONGODB_PASSWORD mongodb://localhost/$MONGODB_NAME --archive'"
"k8up.io/file-extension": ".archive"
spec:
containers:
- name: mongod
imagePullPolicy: "${var.pullPolicy}"
resources: ${jsonencode(var.resources)}
env:
- name: MONGODB_NAME
value: ${local.db_name}
- name: MONGODB_USER
value: ${local.username}
- name: MONGODB_PASSWORD
valueFrom:
secretKeyRef:
name: "${local.app_slug}-mongo"
key: password
security:
authentication:
modes: ["SCRAM"]
additionalMongodConfig:
storage.wiredTiger.engineConfig.cacheSizeGB: 1
users:
- name: ${local.username}
db: ${local.db_name}
passwordSecretRef:
name: "${local.app_slug}-mongo"
roles:
- db: ${local.db_name}
name: readWrite
scramCredentialsSecretName: "${local.app_slug}-mongo-scram"
EOF
}
resource "kubectl_manifest" "prj_mongo_sa" {
yaml_body = <<-EOF
apiVersion: v1
kind: ServiceAccount
metadata:
name: "mongodb-database"
namespace: "${var.namespace}"
labels: ${jsonencode(local.mongo-labels)}
EOF
}
resource "kubectl_manifest" "prj_mongo_role" {
yaml_body = <<-EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: "mongodb-database"
namespace: "${var.namespace}"
labels: ${jsonencode(local.mongo-labels)}
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["patch", "delete", "get"]
EOF
}
resource "kubectl_manifest" "prj_mongo_rb" {
yaml_body = <<-EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: "mongodb-database"
namespace: "${var.namespace}"
labels: ${jsonencode(local.mongo-labels)}
subjects:
- kind: ServiceAccount
name: mongodb-database
roleRef:
kind: Role
name: mongodb-database
apiGroup: rbac.authorization.k8s.io
EOF
}

21
mongo/outputs.tf Normal file
View File

@@ -0,0 +1,21 @@
output "url" {
value = "mongodb://${local.username}:${local.mongo-password}@${local.app_slug}-mongo-svc.${var.namespace}.svc:27017/${local.db_name}"
}
output "service" {
value = "${local.app_slug}-mongo-svc.${var.namespace}.svc"
}
output "password" {
value = local.mongo-password
}
output "username" {
value = local.username
}
output "db_name" {
value = local.db_name
}
output "secret" {
value = {
name = "${local.app_slug}-mongo"
key = "password"
}
}

8
mongo/providers.tf Normal file
View File

@@ -0,0 +1,8 @@
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
}
}
}

55
mongo/variables.tf Normal file
View File

@@ -0,0 +1,55 @@
variable "component" {
type = string
}
variable "instance" {
type = string
}
variable "namespace" {
type = string
}
variable "labels" {
type = map(string)
}
variable "db_name" {
type = string
default = ""
}
variable "username" {
type = string
default = ""
}
variable "mongo_version" {
type = string
default = "6.0.13"
}
variable "mongo_type" {
type = string
default = "ReplicaSet"
}
variable "pullPolicy" {
type = string
default = "IfNotPresent"
}
variable "resources" {
type = object({
limits = optional(object({
cpu = string
memory = string
}))
requests = optional(object({
cpu = string
memory = string
}))
})
default = {
limits = {
cpu = "1"
memory = "1100M"
}
requests = {
cpu = "0.3"
memory = "400M"
}
}
}

View File

@@ -1,17 +1,21 @@
locals {
mysql_host = "${var.instance}-${var.component}-db.${var.namespace}.svc"
mysql_username = data.kubernetes_secret_v1.prj_mysql_secret.data["rootUser"]
mysql_password = data.kubernetes_secret_v1.prj_mysql_secret.data["rootPassword"]
mysql_labels = merge(var.labels, {
"app.kubernetes.io/component" = "mysql"
})
app_slug = "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
mysql_host = "${local.app_slug}-mysql.${var.namespace}.svc"
mysql_username = data.kubernetes_secret_v1.mysql_secret.data["rootUser"]
mysql_password = data.kubernetes_secret_v1.mysql_secret.data["rootPassword"]
}
resource "kubectl_manifest" "prj_mysql_secret" {
resource "kubectl_manifest" "mysql_secret" {
yaml_body = <<-EOF
apiVersion: "secretgenerator.mittwald.de/v1alpha1"
kind: "StringSecret"
metadata:
name: "${var.instance}-${var.component}-db"
name: "${local.app_slug}-mysql"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.mysql_labels)}
spec:
forceRegenerate: false
data:
@@ -27,19 +31,26 @@ resource "kubectl_manifest" "prj_mysql_secret" {
EOF
}
resource "kubectl_manifest" "prj_mysql" {
depends_on = [kubectl_manifest.prj_mysql_secret]
data "kubernetes_secret_v1" "mysql_secret" {
depends_on = [kubectl_manifest.mysql_secret]
metadata {
name = "${local.app_slug}-mysql"
namespace = var.namespace
labels = local.mysql_labels
}
}
resource "kubectl_manifest" "mysql" {
yaml_body = <<-EOF
apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
name: "${var.instance}-${var.component}-db"
name: "${local.app_slug}-mysql"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.mysql_labels)}
spec:
secretName: ${kubectl_manifest.prj_mysql_secret.name}
secretName: ${data.kubernetes_secret_v1.mysql_secret.metadata[0].name}
tlsUseSelfSigned: true
# tlsSecretName: "${var.instance}-db-cert"
instances: 1
router:
instances: 1
@@ -55,38 +66,29 @@ resource "kubectl_manifest" "prj_mysql" {
}
resource "time_sleep" "wait_mysql_ready" {
depends_on = [kubectl_manifest.prj_mysql_secret, kubectl_manifest.prj_mysql]
depends_on = [kubectl_manifest.mysql]
create_duration = "45s"
}
data "kubernetes_secret_v1" "prj_mysql_secret" {
depends_on = [kubectl_manifest.prj_mysql_secret, kubectl_manifest.prj_mysql, time_sleep.wait_mysql_ready]
metadata {
name = "${var.instance}-${var.component}-db"
namespace = var.namespace
}
}
resource "mysql_database" "app" {
depends_on = [kubectl_manifest.prj_mysql_secret, kubectl_manifest.prj_mysql, time_sleep.wait_mysql_ready]
name = var.database
depends_on = [
kubectl_manifest.mysql,
time_sleep.wait_mysql_ready
]
name = var.database
}
resource "mysql_user" "app_user" {
depends_on = [kubectl_manifest.prj_mysql_secret, kubectl_manifest.prj_mysql, time_sleep.wait_mysql_ready]
host = data.kubernetes_secret_v1.prj_mysql_secret.data["userHost"]
user = data.kubernetes_secret_v1.prj_mysql_secret.data["username"]
plaintext_password = data.kubernetes_secret_v1.prj_mysql_secret.data["password"]
depends_on = [
time_sleep.wait_mysql_ready,
mysql_database.app,
]
host = data.kubernetes_secret_v1.mysql_secret.data["userHost"]
user = data.kubernetes_secret_v1.mysql_secret.data["username"]
plaintext_password = data.kubernetes_secret_v1.mysql_secret.data["password"]
}
resource "mysql_grant" "app_user_grant" {
depends_on = [
kubectl_manifest.prj_mysql_secret,
kubectl_manifest.prj_mysql,
time_sleep.wait_mysql_ready,
mysql_database.app,
mysql_user.app_user
]
user = mysql_user.app_user.user
host = mysql_user.app_user.host
database = mysql_database.app.name

12
mysql/outpost.tf Normal file
View File

@@ -0,0 +1,12 @@
output "dns_names" {
value = [
"${local.app_slug}-mysql",
"${local.app_slug}-mysql-instances"
]
}
output "mysql_host" {
value =" ${local.app_slug}-mysql"
}
output "secret_name" {
value = "${local.app_slug}-db"
}

View File

@@ -1,12 +1,18 @@
locals {
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
oauth2_labels = merge(var.labels, {
"app.kubernetes.io/component" = "authentik-oauth2"
})
}
resource "kubectl_manifest" "oauth2-secret" {
ignore_fields = ["metadata.annotations"]
yaml_body = <<-EOF
apiVersion: "secretgenerator.mittwald.de/v1alpha1"
kind: "StringSecret"
metadata:
name: "${var.component}-${var.instance}-id"
name: "${local.app_slug}-id"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.oauth2_labels)}
spec:
forceRegenerate: false
fields:
@@ -19,6 +25,7 @@ data "kubernetes_secret_v1" "oauth2-client-id" {
metadata {
name = kubectl_manifest.oauth2-secret.name
namespace = var.namespace
labels = local.oauth2_labels
}
}
@@ -41,7 +48,7 @@ data "authentik_flow" "default-authentication-flow" {
}
resource "authentik_provider_oauth2" "oauth2" {
name = "${var.component}-${var.instance}"
name = "${local.app_slug}"
client_id = data.kubernetes_secret_v1.oauth2-client-id.data["client-id"]
authentication_flow = data.authentik_flow.default-authentication-flow.id
authorization_flow = data.authentik_flow.default-authorization-flow.id
@@ -56,15 +63,23 @@ resource "authentik_provider_oauth2" "oauth2" {
resource "kubernetes_secret_v1" "oauth2-client-secret" {
metadata {
name = "${var.component}-${var.instance}-secret"
name = "${local.app_slug}-secret"
namespace = var.namespace
labels = var.labels
labels = local.oauth2_labels
}
data = {
client-secret = authentik_provider_oauth2.oauth2.client_secret
}
}
data "kubernetes_secret_v1" "oauth2-client-secret" {
depends_on = [kubernetes_secret_v1.oauth2-client-secret]
metadata {
name = kubernetes_secret_v1.oauth2-client-secret.metadata[0].name
namespace = var.namespace
}
}
data "kubernetes_ingress_v1" "authentik" {
metadata {
name = "authentik"

View File

@@ -2,6 +2,37 @@ output "provider-id" {
value = authentik_provider_oauth2.oauth2.id
}
output "sso_configuration_url" {
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/${var.component}-${var.instance}"
output "sso_signout_url" {
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/${var.component}-${var.instance}/end-session/"
}
output "sso_configuration_url" {
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/${local.app_slug}/"
}
output "sso_userinfo_url" {
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/userinfo/"
}
output "sso_authorize_url" {
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/authorize/"
}
output "sso_token_url" {
value = "https://${data.kubernetes_ingress_v1.authentik.spec[0].rule[0].host}/application/o/token/"
}
output "client_id" {
value = data.kubernetes_secret_v1.oauth2-client-id.data["client-id"]
}
output "client_secret" {
value = data.kubernetes_secret_v1.oauth2-client-secret.data["client-secret"]
}
output "secret_client_id_name" {
value = kubectl_manifest.oauth2-secret.name
}
output "secret_client_secret_name" {
value = kubernetes_secret_v1.oauth2-client-secret.metadata[0].name
}
output "secret_client_id_key" {
value = "client-id"
}
output "secret_client_secret_key" {
value = "client-secret"
}

3
postgresql/outputs.tf Normal file
View File

@@ -0,0 +1,3 @@
output "host" {
value = "${var.app_slug}-redis.${var.namespace}.svc"
}

87
postgresql/postgresql.tf Normal file
View File

@@ -0,0 +1,87 @@
locals {
app_slug = (var.component == var.instance || var.component=="") ? var.instance : format("%s-%s", var.component, var.instance)
pg-labels = merge(local.labels, {
"app.kubernetes.io/component" = "pg"
})
pool-labels = merge(local.labels, {
"app.kubernetes.io/component" = "pg-pool"
})
}
resource "kubectl_manifest" "prj_pg" {
yaml_body = join("", concat([<<-EOF
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: "${local.app_slug}-pg"
namespace: "${var.namespace}"
labels: ${jsonencode(local.pg-labels)}
annotations:
"k8up.io/backupcommand": "pg_dump -U postgres -d ${var.component} --clean"
"k8up.io/file-extension": ".sql"
spec:
instances: ${var.replicas}
imageName: "${var.images.postgresql.registry}/${var.images.postgresql.repository}:${var.images.postgresql.tag}"
storage:
size: "${var.storage.size}"
bootstrap:
initdb:
database: "${var.component}"
owner: "${var.component}"
monitoring:
enablePodMonitor: true
EOF
], var.backups.enable&&var.backups.use_barman?[<<-EOF
backup:
barmanObjectStore:
destinationPath: "s3://${var.app_slug}-${var.namespace}/"
endpointURL: "${var.backups.endpoint}/barman"
s3Credentials:
accessKeyId:
name: "${var.backups.secret_name}"
key: "${var.backups.key_id_key}"
secretAccessKey:
name: "${var.backups.secret_name}"
key: "${var.backups.secret_key}"
EOF
]:[""]))
}
resource "kubectl_manifest" "prj_pg_backup" {
count = var.backups.enable ? 1:0
yaml_body = <<-EOF
apiVersion: postgresql.cnpg.io/v1
kind: ScheduledBackup
metadata:
name: "${local.app_slug}-pg"
namespace: "${var.namespace}"
labels: ${jsonencode(local.pg-labels)}
spec:
schedule: "${var.backups.schedule.db}"
backupOwnerReference: self
cluster:
name: "${local.app_slug}-pg"
EOF
}
resource "kubectl_manifest" "prj_pg_pool" {
depends_on = [kubectl_manifest.prj_pg]
yaml_body = <<-EOF
apiVersion: postgresql.cnpg.io/v1
kind: Pooler
metadata:
name: "${local.app_slug}-pool"
namespace: "${var.namespace}"
labels: ${jsonencode(local.pool-labels)}
spec:
cluster:
name: "${local.app_slug}-pg"
instances: 1
type: rw
pgbouncer:
poolMode: session
parameters:
max_client_conn: "1000"
default_pool_size: "10"
EOF
}

8
postgresql/providers.tf Normal file
View File

@@ -0,0 +1,8 @@
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
}
}
}

63
postgresql/variables.tf Normal file
View File

@@ -0,0 +1,63 @@
variable "component" {
type = string
}
variable "instance" {
type = string
}
variable "namespace" {
type = string
}
variable "labels" {
type = map(string)
}
variable "backups" {
default = {
"enable" = false
"endpoint" = ""
"key_id_key" = "s3-id"
"restic_key" = "bck-password"
"schedule" = {
"db" = "30 3 * * *"
}
"secret_key" = "s3-secret"
"secret_name" = "backup-settings"
"use_barman" = false
}
type = object({
enable = optional(bool),
endpoint = optional(string),
key_id_key = optional(string),
restic_key = optional(string),
schedule = optional(object({
db = optional(string),
})),
secret_key = optional(string),
secret_name = optional(string),
use_barman = optional(bool)
})
}
variable "images" {
type = object({
postgresql = optional(object({registry = optional(string), repository = optional(string), tag = optional(number)})),
})
default = {
"postgresql" = {
"registry" = "ghcr.io"
"repository" = "cloudnative-pg/postgresql"
"tag" = 15.3
}
}
}
variable "replicas" {
type = number
default = 1
}
variable "storage" {
type = object({
size = optional(string)
})
default = {
"size" = "5Gi"
}
}

3
pvc/outputs.tf Normal file
View File

@@ -0,0 +1,3 @@
output "name" {
value = local.app_slug
}

8
pvc/providers.tf Normal file
View File

@@ -0,0 +1,8 @@
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
}
}
}

27
pvc/pvc.tf Normal file
View File

@@ -0,0 +1,27 @@
locals {
app_slug = (var.component == var.instance || var.component=="") ? var.instance : format("%s-%s", var.component, var.instance)
pvc_spec = merge({
"accessModes" = [var.storage.accessMode]
"volumeMode" = var.storage.type
"resources" = {
"requests" = {
"storage" = "${var.storage.size}"
}
}
}, var.storage.volume.class != "" ?{
"storageClassName" = var.storage.class
}:{})
}
resource "kubectl_manifest" "pvc" {
yaml_body = <<-EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ${local.app_slug}
namespace: "${var.namespace}"
annotations:
k8up.io/backup: "true"
labels: ${jsonencode(local.labels)}
spec: ${jsonencode(local.pvc_spec)}
EOF
}

26
pvc/variables.tf Normal file
View File

@@ -0,0 +1,26 @@
variable "component" {
type = string
}
variable "instance" {
type = string
}
variable "namespace" {
type = string
}
variable "labels" {
type = map(string)
}
variable "storage" {
type = object({
accessMode = optional(string),
class = optional(string),
size = optional(string),
type = optional(string)
})
default = {
"accessMode" = "ReadWriteOnce"
"class" = ""
"size" = "10Gi"
"type" = "Filesystem"
}
}

6
redis/outputs.tf Normal file
View File

@@ -0,0 +1,6 @@
output "host" {
value = "${local.app_slug}-redis.${var.namespace}.svc"
}
output "url" {
value = "redis://${local.app_slug}-redis.${var.namespace}.svc:6379"
}

8
redis/providers.tf Normal file
View File

@@ -0,0 +1,8 @@
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
}
}
}

41
redis/redis.tf Normal file
View File

@@ -0,0 +1,41 @@
locals {
app_slug = (var.component == var.instance || var.component=="") ? var.instance : format("%s-%s", var.component, var.instance)
redis-labels = merge(var.labels, {
"app.kubernetes.io/component" = "redis"
})
cfg = merge({
"image" = "${var.images.redis.registry}/${var.images.redis.repository}:${var.images.redis.tag}"
"imagePullPolicy" = "${var.images.redis.pullPolicy}"
},lookup(var.password, "enabled", false)?{
redisSecret = {
name = lookup(var.password, "name", var.component)
key = lookup(var.password, "key", "redis-password")
}
}:{})
}
resource "kubectl_manifest" "redis" {
yaml_body = <<-EOF
apiVersion: "redis.redis.opstreelabs.in/v1beta1"
kind: "Redis"
metadata:
name: "${local.app_slug}-redis"
namespace: "${var.namespace}"
labels: ${jsonencode(local.redis-labels)}
spec:
kubernetesConfig: ${jsonencode(local.cfg)}
storage:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: "${var.storage.size}"
redisExporter:
enabled: ${var.exporter.enabled}
image: "${var.images.redis_exporter.registry}/${var.images.redis_exporter.repository}:${var.images.redis_exporter.tag}"
securityContext:
runAsUser: 1000
fsGroup: 1000
EOF
}

65
redis/variables.tf Normal file
View File

@@ -0,0 +1,65 @@
variable "component" {
type = string
}
variable "instance" {
type = string
}
variable "namespace" {
type = string
}
variable "labels" {
type = map(string)
}
variable "annotations" {
type = map(string)
default = {}
}
variable "images" {
type = object({
redis = optional(object({pullPolicy = optional(string), registry = optional(string), repository = optional(string), tag = optional(string)})),
redis_exporter = optional(object({pullPolicy = optional(string), registry = optional(string), repository = optional(string), tag = optional(string)}))
})
default = {
"redis" = {
"pullPolicy" = "IfNotPresent"
"registry" = "quay.io"
"repository" = "opstree/redis"
"tag" = "v7.0.12"
}
"redis_exporter" = {
"pullPolicy" = "IfNotPresent"
"registry" = "quay.io"
"repository" = "opstree/redis-exporter"
"tag" = "v1.44.0"
}
}
}
variable "exporter" {
type = object({
enabled = optional(bool)
})
default = {
"enabled" = true
}
}
variable "password" {
type = object({
enabled = optional(bool),
name = optional(string),
key = optional(string)
})
default = {
"enabled" = false
"name" = ""
"key" = ""
}
}
variable "storage" {
type = object({
size = optional(string)
})
default = {
"size" = "2Gi"
}
}

View File

@@ -1,3 +1,9 @@
locals{
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
saml_labels = merge(var.labels, {
"app.kubernetes.io/component" = "authentik-saml"
})
}
data "authentik_flow" "default-authorization-flow" {
slug = "default-provider-authorization-implicit-consent"
}
@@ -30,21 +36,21 @@ resource "kubectl_manifest" "saml_certificate" {
apiVersion: "cert-manager.io/v1"
kind: "Certificate"
metadata:
name: "${var.instance}-${var.component}-saml"
name: "${local.app_slug}-saml"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
labels: ${jsonencode(local.saml_labels)}
spec:
secretName: "${var.instance}-${var.component}-saml"
secretName: "${local.app_slug}-saml"
dnsNames: ${jsonencode(var.dns_names)}
issuerRef:
name: "self-sign"
name: "${var.issuer}"
kind: "ClusterIssuer"
group: "cert-manager.io"
EOF
}
resource "authentik_provider_saml" "prj" {
name = "${var.component}-${var.instance}-saml"
name = "${local.app_slug}-saml"
authentication_flow = data.authentik_flow.default-authentication-flow.id
authorization_flow = data.authentik_flow.default-authorization-flow.id
acs_url = "https://${var.dns_names[0]}/${var.acs_path}"

View File

@@ -4,6 +4,9 @@ variable "component" {
variable "instance" {
type = string
}
variable "issuer" {
type = string
}
variable "dns_names" {
type = list(string)
}

11
service/outputs.tf Normal file
View File

@@ -0,0 +1,11 @@
output "name" {
value = "${local.app_slug}"
}
output "default_definition" {
value = {
"name" = "${local.app_slug}"
"port" = {
"number" = var.ports[0]
}
}
}

View File

@@ -1,15 +1,36 @@
locals {
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
cluster_ports = var.svc_type == "ClusterIP" ? [for idx, target in var.targets : {
"name" = target
"port" = var.ports[idx]
"protocol" = var.protocols[idx]
"targetPort" = target
}] : []
ext_ports = var.svc_type == "ExternalName" ? [for idx, target in var.targets : {
"name" = target
"port" = var.ports[idx]
"protocol" = var.protocols[idx]
"targetPort" = var.ports[idx]
}] : []
lb_ports = var.svc_type == "LoadBalancer" ? [for idx, port in var.lb_ports : {
"port" = port
"targetPort" = var.ports[idx]
}] : []
node_ports = var.svc_type == "NodePort" ? [for idx, port in var.ports : {
"port" = port
"targetPort" = port
"nodePort" = var.node_ports[idx]
}] : []
metadata = merge(
{
"name"= "${local.app_slug}"
"namespace"= var.namespace
"labels"= var.labels
},
length(var.annotations) > 0 ? {
"annotations"= var.annotations
} : {}
)
spec = {
"ClusterIP" = {
type = "ClusterIP"
@@ -19,11 +40,18 @@ locals {
"ExternalName" = {
type = "ExternalName"
externalName = var.target_host
ports = local.ext_ports
},
"NodePort" = {
type = "NodePort"
selector = var.labels
ports = local.node_ports
},
"LoadBalancer" = {
type = "LoadBalancer"
selector = var.labels
ports = local.lb_ports
externalTrafficPolicy = var.lb_policy
}
}
}
@@ -31,10 +59,7 @@ resource "kubectl_manifest" "service" {
yaml_body = <<-EOF
apiVersion: v1
kind: Service
metadata:
name: "${var.instance}-${var.component}"
namespace: "${var.namespace}"
labels: ${jsonencode(var.labels)}
metadata: ${jsonencode(local.metadata)}
spec: ${jsonencode(local.spec[var.svc_type])}
EOF
}

View File

@@ -10,12 +10,16 @@ variable "namespace" {
variable "labels" {
type = map(string)
}
variable "annotations" {
type = map(string)
default = {}
}
variable "svc_type" {
type = string
default = "ClusterIP"
validation {
condition = contains(["ClusterIP", "ExternalName", "NodePort"], var.svc_type)
error_message = "Only ClusterIP or ExternalName is allowed"
condition = contains(["ClusterIP", "ExternalName", "NodePort", "LoadBalancer"], var.svc_type)
error_message = "Only ClusterIP, ExternalName, NodePort or LoadBalancer is allowed"
}
}
variable "ports" {
@@ -46,3 +50,15 @@ variable "node_ports" {
error_message = "The range of valid ports is 30000-32767"
}
}
variable "lb_ports" {
type = list(number)
default = [8080]
}
variable "lb_policy" {
type = string
default = "Cluster"
validation {
condition = contains(["Cluster", "Local"], var.lb_policy)
error_message = "Only Cluster or Local is allowed"
}
}