Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 20cee43

Browse files
committed
Consolidate postgres and tfstate postgres.
1 parent fe50b8c commit 20cee43

File tree

3 files changed

+118
-56
lines changed

3 files changed

+118
-56
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Install ShapeBlock operator
1818
- Registry credentials secret
1919
- App and project CRDs
20+
- Proper cleanup during uninstall
2021

2122
### Changed
2223

2324
- Fix cron job creation
25+
- Unified postgres and tfstate postgres into single instance
2426

2527
### Removed
2628

2729
- Removed resource constraints on backend and frontend.
2830
- Epinio is no longer installed.
31+
- Celery worker in backend
2932

3033
## [1.0.4] - 2024-12-19
3134

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
apiVersion: source.toolkit.fluxcd.io/v1beta2
2-
kind: HelmRepository
2+
kind: OCIRepository
33
metadata:
44
name: bitnami
55
namespace: shapeblock
66
spec:
77
interval: 1m
8-
url: https://charts.bitnami.com/bitnami
8+
url: oci://registry-1.docker.io/bitnamicharts/mysql
9+
ref:
10+
semver: ">12.0.0"

main.go

Lines changed: 111 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ type BackendConfig struct {
119119
TFStateUsername string
120120
TFStatePassword string
121121
TFStateDatabase string
122-
TFStateRootPW string
123122
LicenseKey string
124123
}
125124

@@ -687,41 +686,39 @@ func generatePassword() string {
687686
return string(b)
688687
}
689688

690-
func installPostgres(name, username, database string, backendConfig *BackendConfig) error {
691-
printStatus(fmt.Sprintf("Installing PostgreSQL instance %s...", name))
689+
func installPostgres(backendConfig *BackendConfig) error {
690+
printStatus("Installing PostgreSQL...")
692691

693-
if resourceExists("statefulset", name+"-postgresql", "shapeblock") {
694-
printStatus(fmt.Sprintf("PostgreSQL instance %s already installed", name))
692+
if resourceExists("statefulset", "db-postgresql", "shapeblock") {
693+
printStatus("PostgreSQL already installed")
695694
return nil
696695
}
697696

698697
if err := addHelmRepo("bitnami", "https://charts.bitnami.com/bitnami"); err != nil {
699698
return err
700699
}
701700

702-
// Generate passwords
703-
password := generatePassword()
701+
// Generate passwords for both users and root
702+
mainPassword := generatePassword()
703+
tfstatePassword := generatePassword()
704704
rootPW := generatePassword()
705705

706-
// Store credentials based on instance name
707-
switch name {
708-
case "db":
709-
backendConfig.PostgresUsername = username
710-
backendConfig.PostgresDatabase = database
711-
backendConfig.PostgresPassword = password
712-
backendConfig.PostgresRootPW = rootPW
713-
case "tfstate":
714-
backendConfig.TFStateUsername = username
715-
backendConfig.TFStateDatabase = database
716-
backendConfig.TFStatePassword = password
717-
backendConfig.TFStateRootPW = rootPW
718-
}
706+
// Store credentials in backend config
707+
backendConfig.PostgresUsername = "shapeblock"
708+
backendConfig.PostgresDatabase = "shapeblock"
709+
backendConfig.PostgresPassword = mainPassword
710+
backendConfig.PostgresRootPW = rootPW
711+
backendConfig.TFStateUsername = "tfstate"
712+
backendConfig.TFStateDatabase = "tfstate"
713+
backendConfig.TFStatePassword = tfstatePassword
719714

720715
// Log the credentials for debugging
721-
logMessage("INFO", fmt.Sprintf("PostgreSQL %s credentials - username: %s, database: %s, password: %s",
722-
name, username, database, password))
716+
logMessage("INFO", fmt.Sprintf("PostgreSQL main credentials - username: %s, database: %s, password: %s",
717+
backendConfig.PostgresUsername, backendConfig.PostgresDatabase, mainPassword))
718+
logMessage("INFO", fmt.Sprintf("PostgreSQL tfstate credentials - username: %s, database: %s, password: %s",
719+
backendConfig.TFStateUsername, backendConfig.TFStateDatabase, tfstatePassword))
723720

724-
// Create values.yaml
721+
// Create values with initialization scripts for both databases
725722
values := fmt.Sprintf(`
726723
auth:
727724
database: %s
@@ -732,13 +729,42 @@ architecture: standalone
732729
primary:
733730
persistence:
734731
size: 2Gi
732+
initdb:
733+
scripts:
734+
init_db_and_users.sql: |
735+
-- Create tfstate user and database
736+
CREATE USER %s WITH PASSWORD '%s';
737+
CREATE DATABASE %s OWNER %s;
738+
GRANT ALL PRIVILEGES ON DATABASE %s TO %s;
739+
740+
-- Create shapeblock user and database
741+
CREATE USER %s WITH PASSWORD '%s';
742+
CREATE DATABASE %s OWNER %s;
743+
GRANT ALL PRIVILEGES ON DATABASE %s TO %s;
735744
tls:
736745
enabled: true
737-
autoGenerated: true
738-
`, database, username, password, rootPW)
746+
autoGenerated: true`,
747+
backendConfig.PostgresDatabase, // Initial database
748+
backendConfig.PostgresUsername, // Initial user
749+
backendConfig.PostgresPassword,
750+
backendConfig.PostgresRootPW,
751+
// tfstate database and user creation
752+
backendConfig.TFStateUsername,
753+
backendConfig.TFStatePassword,
754+
backendConfig.TFStateDatabase,
755+
backendConfig.TFStateUsername,
756+
backendConfig.TFStateDatabase,
757+
backendConfig.TFStateUsername,
758+
// shapeblock database and user creation
759+
backendConfig.PostgresUsername,
760+
backendConfig.PostgresPassword,
761+
backendConfig.PostgresDatabase,
762+
backendConfig.PostgresUsername,
763+
backendConfig.PostgresDatabase,
764+
backendConfig.PostgresUsername)
739765

740766
// Write values to temporary file
741-
tmpfile, err := os.CreateTemp("", fmt.Sprintf("%s-values-*.yaml", name))
767+
tmpfile, err := os.CreateTemp("", "postgres-values-*.yaml")
742768
if err != nil {
743769
printError(fmt.Sprintf("Failed to create temp file: %v", err))
744770
return err
@@ -753,16 +779,16 @@ tls:
753779

754780
// Install PostgreSQL
755781
if err := runCommand("helm", "upgrade", "--install",
756-
name, "bitnami/postgresql",
782+
"db", "bitnami/postgresql",
757783
"--version", "16.2.3",
758784
"--namespace", "shapeblock",
759785
"--values", tmpfile.Name(),
760786
"--timeout", "600s"); err != nil {
761-
printError(fmt.Sprintf("Failed to install PostgreSQL %s: %v", name, err))
787+
printError(fmt.Sprintf("Failed to install PostgreSQL: %v", err))
762788
return err
763789
}
764790

765-
printStatus(fmt.Sprintf("PostgreSQL instance %s installed successfully", name))
791+
printStatus("PostgreSQL installed successfully with both databases")
766792
return nil
767793
}
768794

@@ -775,7 +801,7 @@ func createTerraformSecret(backendConfig *BackendConfig) error {
775801
}
776802

777803
// Create connection string
778-
connStr := fmt.Sprintf("postgres://%s:%s@tfstate-postgresql/%s",
804+
connStr := fmt.Sprintf("postgres://%s:%s@db-postgresql/%s",
779805
backendConfig.TFStateUsername,
780806
backendConfig.TFStatePassword,
781807
backendConfig.TFStateDatabase)
@@ -932,14 +958,9 @@ func install(config *Config, backendConfig *BackendConfig) error {
932958
return fmt.Errorf("failed to create service account: %v", err)
933959
}
934960

935-
// Install main PostgreSQL instance
936-
if err := installPostgres("db", "shapeblock", "shapeblock", backendConfig); err != nil {
937-
return fmt.Errorf("failed to install main PostgreSQL: %v", err)
938-
}
939-
940-
// Install TFState PostgreSQL instance
941-
if err := installPostgres("tfstate", "tfstate", "tfstate", backendConfig); err != nil {
942-
return fmt.Errorf("failed to install TFState PostgreSQL: %v", err)
961+
// Install PostgreSQL instance
962+
if err := installPostgres(backendConfig); err != nil {
963+
return fmt.Errorf("failed to install PostgreSQL: %v", err)
943964
}
944965

945966
// Create Terraform credentials secret
@@ -1768,19 +1789,6 @@ deployments:
17681789
release: backend
17691790
replicas: 1
17701791
1771-
worker:
1772-
containers:
1773-
- envConfigmaps:
1774-
- envs
1775-
envSecrets:
1776-
- secret-envs
1777-
name: worker
1778-
command: ['celery', '-A', 'shapeblock', 'worker', '-l', 'INFO']
1779-
podLabels:
1780-
app: shapeblock
1781-
release: backend
1782-
replicas: 1
1783-
17841792
envs:
17851793
DEBUG: "False"
17861794
DATABASE_URL: "postgres://%s:%s@db-postgresql/%s"
@@ -2279,7 +2287,7 @@ Admin Backend Access:
22792287
- URL: https://api.%s/admin-%s/
22802288
- Username: admin
22812289
- Password: <password provided during installation>
2282-
`, domain, config.AdminEmail, domain, config.AppName)
2290+
`, domain, config.AdminEmail, domain, slugify(config.AppName))
22832291

22842292
// Print to console
22852293
fmt.Println(instructions)
@@ -2411,7 +2419,7 @@ func uninstall() error {
24112419

24122420
// Uninstall PostgreSQL instances
24132421
printStatus("Uninstalling PostgreSQL instances...")
2414-
for _, instance := range []string{"db", "tfstate"} {
2422+
for _, instance := range []string{"db"} {
24152423
if err := runCommand("helm", "uninstall", instance, "-n", "shapeblock"); err != nil {
24162424
printStatus(fmt.Sprintf("Note: PostgreSQL instance %s was not installed or already removed", instance))
24172425
}
@@ -2522,6 +2530,55 @@ func uninstall() error {
25222530
}
25232531
}
25242532

2533+
// Clean up files
2534+
printStatus("Cleaning up installation files...")
2535+
2536+
// Get home directory
2537+
homeDir, err := os.UserHomeDir()
2538+
if err != nil {
2539+
printStatus(fmt.Sprintf("Warning: Failed to get home directory: %v", err))
2540+
} else {
2541+
// Remove SSH keys
2542+
sshFiles := []string{
2543+
filepath.Join(homeDir, "sb"),
2544+
filepath.Join(homeDir, "sb.pub"),
2545+
}
2546+
2547+
for _, file := range sshFiles {
2548+
if err := os.Remove(file); err != nil {
2549+
if !os.IsNotExist(err) {
2550+
printStatus(fmt.Sprintf("Warning: Failed to remove %s: %v", file, err))
2551+
}
2552+
} else {
2553+
printStatus(fmt.Sprintf("Removed %s", file))
2554+
}
2555+
}
2556+
}
2557+
2558+
// Remove installation files from current directory
2559+
installFiles := []string{
2560+
"instructions.txt",
2561+
"kubeconfig",
2562+
}
2563+
2564+
// Add any install*.log files
2565+
matches, err := filepath.Glob("install*.log")
2566+
if err != nil {
2567+
printStatus(fmt.Sprintf("Warning: Failed to find log files: %v", err))
2568+
} else {
2569+
installFiles = append(installFiles, matches...)
2570+
}
2571+
2572+
for _, file := range installFiles {
2573+
if err := os.Remove(file); err != nil {
2574+
if !os.IsNotExist(err) {
2575+
printStatus(fmt.Sprintf("Warning: Failed to remove %s: %v", file, err))
2576+
}
2577+
} else {
2578+
printStatus(fmt.Sprintf("Removed %s", file))
2579+
}
2580+
}
2581+
25252582
printStatus("ShapeBlock has been successfully uninstalled")
25262583
return nil
25272584
}
@@ -3515,7 +3572,7 @@ spec:
35153572
serviceAccountName: shapeblock-admin
35163573
containers:
35173574
- name: sb-operator
3518-
image: shapeblock/sb-operator:20-dec-2024.1
3575+
image: ghcr.io/shapeblock/operator:v2
35193576
imagePullPolicy: Always
35203577
livenessProbe:
35213578
failureThreshold: 3

0 commit comments

Comments
 (0)