added user generation

This commit is contained in:
ari committed 2023-04-15 05:39:32 +00:00
1 parent 321b723228
commit c872c1d061
12 files changed
+226 -39

No files matched your search

+1
View File
@@ -8,6 +8,7 @@ typeofapp=$(basename $location)
mkdir -p $full_path/templates
touch $full_path/values.yaml
touch $full_path/values-{dev,prod}.yaml
cat <<EOT >> $full_path/Chart.yaml
apiVersion: v2
name: $app
+6
View File
@@ -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 -}}
+46
View File
@@ -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 }}
+51
View File
@@ -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.postgresql.auth.existingSecret }}"
key: "username"
- name: TF_VAR_db_password
valueFrom:
secretKeyRef:
name: "{{ .Values.postgresql.auth.existingSecret }}"
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 }}"
View File
Whitespace-only changes.
View File
Whitespace-only changes.
+6
View File
@@ -0,0 +1,6 @@
userGeneration:
tfVersion: "1.3.7"
users:
- nextcloud
- authelia
- authentik
+6 -4
View File
@@ -4,13 +4,15 @@
{{ range $appName, $appProps := .Values.apps }}
{{- $namespace := (printf "apps-%s" $appName) -}}
{{- $genNamespace := (printf "apps-%s" $appName) -}}
{{ if (not $appProps.namespace) }}
---
apiVersion: v1
kind: Namespace
metadata:
name: "{{ $namespace }}"
name: "{{ $genNamespace }}"
{{ end }}
---
apiVersion: argoproj.io/v1alpha1
@@ -28,11 +30,11 @@ spec:
- values.yaml
- values-{{$.Values.global.deployment}}.yaml
values: |-
{{- tpl $appProps $ | nindent 10 }}
{{- tpl ($appProps.values | default "") $ | nindent 10 }}
global:
{{- $.Values.global | toYaml | nindent 12 }}
releaseName: {{ $appName }}
destination:
server: https://kubernetes.default.svc
namespace: {{ $namespace }}
namespace: {{ $appProps.namespace | default $genNamespace }}
{{ end }}
+7 -35
View File
@@ -1,8 +1,12 @@
gitops:
repo: ""
commit: ""
apps:
ingresses: ""
ingresses: {}
users:
namespace: chart-database
global:
email: "ariel@sosnovsky.ca"
timezone: America/Toronto
@@ -16,6 +20,7 @@ global:
storageClass: "nfs-generic"
server: "changme"
share: "changeme"
infrastructureCharts:
cert-manager:
repoURL: "https://charts.jetstack.io"
@@ -66,40 +71,7 @@ infrastructureCharts:
- ReadWriteMany
readReplicas:
replicaCount: 0
# monitoring:
# chart: prometheus
# repoURL: "https://prometheus-community.github.io/helm-charts"
# targetRevision: "20.2.0"
# values: |-
# configmapReload:
# prometheus:
# enabled: false
# alertmanager:
# persistence:
# enabled: true
# size: 8Gi
# storageClass: "local-path"
# ingress:
# enabled: false
# ingressClassName: traefik
# annotations: {}
# hosts:
# - host: "{{$prometheusHost}}.{{ .Values.global.domain }}"
# paths:
# - path: /alertmanager
# pathType: Prefix
# server:
# {{ if .Values.global.tls.enabled }}
# baseURL: "https://{{$prometheusHost}}.{{ .Values.global.domain }}"
# {{ else }}
# baseURL: "http://{{$prometheusHost}}.{{ .Values.global.domain }}"
# {{ end }}
# persistentVolume:
# enabled: true
# size: 8Gi
# storageClass: "local-path"
# ingress:
# enabled: false
secrets:
database:
namespace: chart-database