renamed to db-users
This commit is contained in:
1 parent
7529e80f85
commit
ddc23dd65b
10 files changed
+1
-1
No files matched your search
@@ -0,0 +1,6 @@
|
||||
apiVersion: v2
|
||||
name: users
|
||||
description: An apps service for homelab
|
||||
type: application
|
||||
version: 0.1.0
|
||||
dependencies: []
|
||||
@@ -0,0 +1,9 @@
|
||||
{{- define "pg-users.entry" -}}
|
||||
#!/bin/sh
|
||||
|
||||
cp -r /app /tmp/app
|
||||
|
||||
cd /tmp/app && \
|
||||
terraform init && \
|
||||
terraform apply -auto-approve -input=false -var-file="/app/users.tfvars" -lock=false
|
||||
{{- end -}}
|
||||
@@ -0,0 +1,83 @@
|
||||
{{- define "pg-users.terraform" -}}
|
||||
variable "db_host" {}
|
||||
variable "db_username" {}
|
||||
variable "db_password" {}
|
||||
variable "db_users" {
|
||||
type = list(string)
|
||||
}
|
||||
terraform {
|
||||
required_providers {
|
||||
postgresql = {
|
||||
source = "cyrilgdn/postgresql"
|
||||
version = "1.18.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
terraform {
|
||||
backend "kubernetes" {
|
||||
secret_suffix = "{{ .Release.Name }}"
|
||||
in_cluster_config = true
|
||||
namespace = "{{ .Release.Namespace }}"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
provider "postgresql" {
|
||||
host = var.db_host
|
||||
port = 5432
|
||||
username = var.db_username
|
||||
password = var.db_password
|
||||
connect_timeout = 15
|
||||
sslmode = "disable"
|
||||
}
|
||||
|
||||
resource "random_password" "pg_users_pswd" {
|
||||
count = length(var.db_users)
|
||||
|
||||
length = 16
|
||||
special = true
|
||||
override_special = "!#$%&"
|
||||
}
|
||||
|
||||
|
||||
locals {
|
||||
users = {
|
||||
for u, pwd in random_password.pg_users_pswd :
|
||||
var.db_users[u] => pwd.result
|
||||
}
|
||||
}
|
||||
|
||||
resource "kubernetes_secret" "pg_users" {
|
||||
for_each = local.users
|
||||
metadata {
|
||||
name = "password.pg.${each.key}"
|
||||
namespace = "{{ .Release.Namespace }}"
|
||||
annotations = {
|
||||
"managed-by": "job/user-generation"
|
||||
}
|
||||
}
|
||||
|
||||
data = {
|
||||
username = each.key
|
||||
password = each.value
|
||||
}
|
||||
|
||||
type = "Opaque"
|
||||
}
|
||||
|
||||
resource "postgresql_role" "u" {
|
||||
for_each = local.users
|
||||
name = each.key
|
||||
password = each.value
|
||||
login = true
|
||||
}
|
||||
|
||||
resource "postgresql_database" "db" {
|
||||
for_each = local.users
|
||||
name = each.key
|
||||
owner = postgresql_role.u[each.key].id
|
||||
}
|
||||
|
||||
|
||||
{{- end -}}
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: Role
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Release.Namespace }}
|
||||
rules:
|
||||
- apiGroups: [""]
|
||||
resources: ["secrets"]
|
||||
verbs: ["list", "create"]
|
||||
- apiGroups: [""]
|
||||
resources: ["secrets"]
|
||||
verbs: ["get", "update", "create", "patch", "delete"]
|
||||
resourceNames:
|
||||
- "tfstate-default-{{ .Release.Name }}"
|
||||
{{ range $user := .Values.userGeneration.users }}
|
||||
- "password.pg.{{ $user }}"
|
||||
{{ end }}
|
||||
- apiGroups: ["coordination.k8s.io"]
|
||||
resources: ["leases"]
|
||||
verbs: ["get", "update", "create", "patch", "delete"]
|
||||
resourceNames: ["lock-tfstate-default-{{ .Release.Name }}"]
|
||||
- apiGroups: ["coordination.k8s.io"]
|
||||
resources: ["leases"]
|
||||
verbs: ["create"]
|
||||
---
|
||||
apiVersion: v1
|
||||
automountServiceAccountToken: true
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Release.Namespace }}
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: RoleBinding
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Release.Namespace }}
|
||||
roleRef:
|
||||
apiGroup: rbac.authorization.k8s.io
|
||||
kind: Role
|
||||
name: {{ .Release.Name }}
|
||||
subjects:
|
||||
- kind: ServiceAccount
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Release.Namespace }}
|
||||
@@ -0,0 +1,51 @@
|
||||
---
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: "user-generation"
|
||||
namespace: "{{ .Release.Namespace }}"
|
||||
annotations:
|
||||
checksum/run-sh: "{{ include "pg-users.entry" . | sha256sum }}"
|
||||
checksum/main-tf: "{{ include "pg-users.terraform" . | sha256sum }}"
|
||||
checksum/users-tfvars: "{{ (print "db_users = " ( .Values.userGeneration.users | toJson )) | sha256sum }}"
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
serviceAccount: "{{ .Release.Name }}"
|
||||
serviceAccountName: "{{ .Release.Name }}"
|
||||
volumes:
|
||||
- name: tffiles
|
||||
secret:
|
||||
secretName: "user-generation-tf"
|
||||
containers:
|
||||
- name: base
|
||||
image: "hashicorp/terraform:{{ .Values.userGeneration.tfVersion }}"
|
||||
command:
|
||||
- sh
|
||||
- /app/run.sh
|
||||
volumeMounts:
|
||||
- mountPath: /app
|
||||
name: tffiles
|
||||
env:
|
||||
- name: TF_LOG_PATH
|
||||
value: /tmp/terraform.log
|
||||
- name: TF_INPUT
|
||||
value: "0"
|
||||
- name: TF_DATA_DIR
|
||||
value: /tmp/.terraform
|
||||
- name: TF_IN_AUTOMATION
|
||||
value: "1"
|
||||
- name: TF_VAR_db_host
|
||||
value: "postgres.{{ .Release.Namespace }}.svc.cluster.local"
|
||||
- name: TF_VAR_db_username
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: "{{ .Values.postgresSecret }}"
|
||||
key: "username"
|
||||
- name: TF_VAR_db_password
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: "{{ .Values.postgresSecret }}"
|
||||
key: "password"
|
||||
restartPolicy: Never
|
||||
backoffLimit: 2
|
||||
@@ -0,0 +1,11 @@
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Secret
|
||||
type: Opaque
|
||||
metadata:
|
||||
name: "user-generation-tf"
|
||||
namespace: "{{ .Release.Namespace }}"
|
||||
data:
|
||||
run.sh: "{{ include "pg-users.entry" . | b64enc }}"
|
||||
main.tf: "{{ include "pg-users.terraform" . | b64enc }}"
|
||||
users.tfvars: "{{ (print "db_users = " ( .Values.userGeneration.users | toJson )) | b64enc }}"
|
||||
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,8 @@
|
||||
userGeneration:
|
||||
tfVersion: "1.3.7"
|
||||
users:
|
||||
- nextcloud
|
||||
- authelia
|
||||
- authentik
|
||||
|
||||
postgresSecret: ""
|
||||
Reference in new issue
Block a user