Skip to content

Commit aeb3b81

Browse files
authored
Merge pull request #162 from kloeckner-i/METAL-3782/drop_schema_event
METAL-3782 better indication why database creation failed
2 parents cb7ff83 + 3e3d634 commit aeb3b81

5 files changed

Lines changed: 69 additions & 26 deletions

File tree

controllers/database_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ func (r *DatabaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
190190
case dbPhaseCreate:
191191
err := r.createDatabase(ctx, dbcr)
192192
if err != nil {
193-
logrus.Errorf("DB: namespace=%s, name=%s failed creating database - %s", dbcr.Namespace, dbcr.Name, err)
194193
// when database creation failed, don't requeue request. to prevent exceeding api limit (ex: against google api)
195194
return r.manageError(ctx, dbcr, err, false)
196195
}
@@ -353,7 +352,7 @@ func (r *DatabaseReconciler) createDatabase(ctx context.Context, dbcr *kciv1alph
353352

354353
err = database.Create(db, adminCred)
355354
if err != nil {
356-
logrus.Errorf("DB: namespace=%s, name=%s failed creating database", dbcr.Namespace, dbcr.Name)
355+
357356
return err
358357
}
359358

controllers/database_helper.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,14 @@ func determinDatabaseType(dbcr *kciv1alpha1.Database, dbCred database.Credential
7474
switch engine {
7575
case "postgres":
7676
extList := dbcr.Spec.Extensions
77-
if monitoringEnabled {
78-
extList = append(dbcr.Spec.Extensions, "pg_stat_statements")
79-
}
80-
8177
db := database.Postgres{
8278
Backend: backend,
8379
Host: host,
8480
Port: uint16(port),
8581
Database: dbCred.Name,
8682
User: dbCred.Username,
8783
Password: dbCred.Password,
84+
Monitoring: monitoringEnabled,
8885
Extensions: extList,
8986
SSLEnabled: instance.Spec.SSLConnection.Enabled,
9087
SkipCAVerify: instance.Spec.SSLConnection.SkipVerify,

controllers/database_helper_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,7 @@ func TestMonitoringEnabled(t *testing.T) {
110110
db, _ := determinDatabaseType(postgresDbCr, testDbcred)
111111
postgresInterface, _ := db.(database.Postgres)
112112

113-
found := false
114-
for _, ext := range postgresInterface.Extensions {
115-
if ext == "pg_stat_statements" {
116-
found = true
117-
break
118-
}
119-
}
120-
assert.Equal(t, found, true, "expected pg_stat_statement is included in extension list")
113+
assert.Equal(t, postgresInterface.Monitoring, true, "expected monitoring is true in postgres interface")
121114
}
122115

123116
func TestPsqlDefaultConnectionStringGeneratationWithProxy(t *testing.T) {

pkg/utils/database/postgres.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Postgres struct {
4141
Database string
4242
User string
4343
Password string
44+
Monitoring bool
4445
Extensions []string
4546
SSLEnabled bool
4647
SkipCAVerify bool
@@ -161,10 +162,21 @@ func (p Postgres) createDatabase(admin AdminCredentials) error {
161162
}
162163
}
163164

165+
if p.Monitoring {
166+
err := p.enableMonitoring(admin)
167+
if err != nil {
168+
return fmt.Errorf("can not enable monitoring - %s", err)
169+
}
170+
}
171+
172+
err := p.addExtensions(admin)
173+
if err != nil {
174+
return fmt.Errorf("can not add extension - %s", err)
175+
}
176+
164177
if p.DropPublicSchema {
165178
if err := p.dropPublicSchema(admin); err != nil {
166-
logrus.Errorf("failed dropping the public schema %s", err)
167-
return err
179+
return fmt.Errorf("can not drop public schema - %s", err)
168180
}
169181
if len(p.Schemas) == 0 {
170182
logrus.Info("the public schema is dropped, but no additional schemas are created, schema creation must be handled on the application side now")
@@ -178,12 +190,6 @@ func (p Postgres) createDatabase(admin AdminCredentials) error {
178190
}
179191
}
180192

181-
err := p.addExtensions(admin)
182-
if err != nil {
183-
logrus.Errorf("failed creating postgres extensions %s", err)
184-
return err
185-
}
186-
187193
return nil
188194
}
189195

@@ -223,6 +229,10 @@ func (p Postgres) createUser(admin AdminCredentials) error {
223229
}
224230

225231
func (p Postgres) dropPublicSchema(admin AdminCredentials) error {
232+
if p.Monitoring {
233+
return fmt.Errorf("can not drop public schema when monitoring is enabled on instance level")
234+
}
235+
226236
drop := "DROP SCHEMA IF EXISTS public;"
227237
if err := p.executeExec(p.Database, drop, admin); err != nil {
228238
logrus.Errorf("failed to drop the schema Public: %s", err)
@@ -321,13 +331,24 @@ func (p Postgres) addExtensions(admin AdminCredentials) error {
321331
query := fmt.Sprintf("CREATE EXTENSION IF NOT EXISTS \"%s\";", ext)
322332
err := p.executeExec(p.Database, query, admin)
323333
if err != nil {
324-
logrus.Errorf("failed creating extensions %s - %s", query, err)
325334
return err
326335
}
327336
}
328337
return nil
329338
}
330339

340+
func (p Postgres) enableMonitoring(admin AdminCredentials) error {
341+
monitoringExtension := "pg_stat_statements"
342+
343+
query := fmt.Sprintf("CREATE EXTENSION IF NOT EXISTS \"%s\";", monitoringExtension)
344+
err := p.executeExec(p.Database, query, admin)
345+
if err != nil {
346+
return err
347+
}
348+
349+
return nil
350+
}
351+
331352
func (p Postgres) checkExtensions() error {
332353
for _, ext := range p.Extensions {
333354
query := fmt.Sprintf("SELECT 1 FROM pg_extension WHERE extname = '%s';", ext)

pkg/utils/database/postgres_test.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,20 @@ import (
2424
)
2525

2626
func testPostgres() *Postgres {
27-
return &Postgres{"local", test.GetPostgresHost(), test.GetPostgresPort(), "testdb", "testuser", "testpassword", []string{}, false, false, false, []string{}}
27+
return &Postgres{
28+
Backend: "local",
29+
Host: test.GetPostgresHost(),
30+
Port: test.GetPostgresPort(),
31+
Database: "testdb",
32+
User: "testuser",
33+
Password: "testpassword",
34+
Monitoring: false,
35+
Extensions: []string{},
36+
SSLEnabled: false,
37+
SkipCAVerify: false,
38+
DropPublicSchema: false,
39+
Schemas: []string{},
40+
}
2841
}
2942

3043
func getPostgresAdmin() AdminCredentials {
@@ -78,21 +91,41 @@ func TestDropPublicSchemaFail(t *testing.T) {
7891
assert.Error(t, p.checkSchemas())
7992
}
8093

81-
func TestDropPublicSchema(t *testing.T) {
94+
func TestDropPublicSchemaMonitoringTrue(t *testing.T) {
8295
p := testPostgres()
8396
admin := getPostgresAdmin()
97+
p.Monitoring = true
98+
p.DropPublicSchema = true
99+
p.dropPublicSchema(admin)
100+
assert.Error(t, p.checkSchemas())
101+
}
102+
103+
func TestDropPublicSchemaMonitoringFalse(t *testing.T) {
104+
p := testPostgres()
105+
admin := getPostgresAdmin()
106+
p.Monitoring = false
84107
p.DropPublicSchema = true
85108
p.dropPublicSchema(admin)
86109
assert.NoError(t, p.checkSchemas())
87110

88-
// Schemas is recreated here not to breaks tests for extensions
111+
// Schemas is recreated here not to breaks tests
89112
p.Schemas = []string{"public"}
90113
assert.NoError(t, p.createSchemas(admin))
91114
}
92115

116+
func TestEnableMonitoring(t *testing.T) {
117+
p := testPostgres()
118+
admin := getPostgresAdmin()
119+
p.Monitoring = true
120+
p.enableMonitoring(admin)
121+
p.Extensions = []string{"pg_stat_statements"}
122+
assert.NoError(t, p.checkExtensions())
123+
}
124+
93125
func TestPostgresNoExtensions(t *testing.T) {
94126
admin := getPostgresAdmin()
95127
p := testPostgres()
128+
p.Extensions = []string{}
96129

97130
assert.NoError(t, p.addExtensions(admin))
98131
assert.NoError(t, p.checkExtensions())

0 commit comments

Comments
 (0)