Skip to content

Commit

Permalink
Merge pull request #278 from tisnik/db-schema-configuration-option
Browse files Browse the repository at this point in the history
Db schema configuration option
  • Loading branch information
tisnik authored Nov 24, 2023
2 parents b74168e + ac55856 commit e066373
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 28 deletions.
69 changes: 49 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@
* [Configuration](#configuration)
* [BDD tests](#bdd-tests)
* [Usage](#usage)
* [Output example](#output-example)
* [Output example](#output-example)
* [Database structure](#database-structure)
* [Table `report`](#table-report)
* [Table `cluster_rule_toggle`](#table-cluster_rule_toggle)
* [Table `cluster_rule_user_feedback`](#table-cluster_rule_user_feedback)
* [Table `cluster_user_rule_disable_feedback`](#table-cluster_user_rule_disable_feedback)
* [Table `consumer_error`](#table-consumer_error)
* [Table `migration_info `](#table-migration_info-)
* [Table `rule_hit`](#table-rule_hit)
* [Table `recommendation`](#table-recommendation)
* [Database tables affected by this service](#database-tables-affected-by-this-service)
* [Database schema `ocp_recommendations`](#database-schema-ocp_recommendations)
* [Table `report`](#table-report)
* [Table `cluster_rule_toggle`](#table-cluster_rule_toggle)
* [Table `cluster_rule_user_feedback`](#table-cluster_rule_user_feedback)
* [Table `cluster_user_rule_disable_feedback`](#table-cluster_user_rule_disable_feedback)
* [Table `consumer_error`](#table-consumer_error)
* [Table `migration_info `](#table-migration_info-)
* [Table `rule_hit`](#table-rule_hit)
* [Table `recommendation`](#table-recommendation)
* [Database tables affected by this service](#database-tables-affected-by-this-service)
* [Database schema `dvo_recommendations`](#database-schema-dvo_recommendations)
* [Table `dvo_report`](#table-dvo_report)
* [Documentation](#documentation-1)
* [Documentation for source files from this repository](#documentation-for-source-files-from-this-repository)
* [Documentation for unit tests from this repository](#documentation-for-unit-tests-from-this-repository)
Expand Down Expand Up @@ -179,6 +182,7 @@ pg_host = "localhost"
pg_port = 5432
pg_db_name = "aggregator"
pg_params = "sslmode=disable"
schema = "ocp_recommendations"
[logging]
debug = true
Expand All @@ -198,11 +202,15 @@ INSIGHTS_RESULTS_CLEANER__STORAGE__PG_HOST
INSIGHTS_RESULTS_CLEANER__STORAGE__PG_PORT
INSIGHTS_RESULTS_CLEANER__STORAGE__PG_DB_NAME
INSIGHTS_RESULTS_CLEANER__STORAGE__PG_PARAMS
INSIGHTS_RESULTS_CLEANER__STORAGE__SCHEMA
INSIGHTS_RESULTS_CLEANER__LOGGING__DEBUG
INSIGHTS_RESULTS_CLEANER__LOGGING__LOG_DEVEL
INSIGHTS_RESULTS_CLEANER__CLEANER__MAX_AGE
```

* `db_driver` can be set to "postgres" or "sqlite3"
* `schema` can be set to "ocp_recommendations" or "dvo_recommendations"

## BDD tests

Behaviour tests for this service are included in [Insights Behavioral
Expand All @@ -224,7 +232,7 @@ Just the service needs to be started:
./insights-results-aggregator-cleaner
```

### Output example
#### Output example

* Logging is set to `true`

Expand Down Expand Up @@ -266,6 +274,8 @@ Just the service needs to be started:

## Database structure

### Database schema `ocp_recommendations`

List of tables:

```
Expand All @@ -285,7 +295,7 @@ List of tables:
```

### Table `report`
#### Table `report`

```
Column | Type | Modifiers
Expand All @@ -304,7 +314,7 @@ Referenced by:
TABLE "cluster_rule_user_feedback" CONSTRAINT "cluster_rule_user_feedback_cluster_id_fkey" FOREIGN KEY (cluster_id) REFERENCES report(cluster) ON DELETE CASCADE
```

### Table `cluster_rule_toggle`
#### Table `cluster_rule_toggle`

```
Column | Type | Modifiers
Expand All @@ -322,7 +332,7 @@ Check constraints:
"cluster_rule_toggle_disabled_check" CHECK (disabled >= 0 AND disabled <= 1)
```

### Table `cluster_rule_user_feedback`
#### Table `cluster_rule_user_feedback`

```
Column | Type | Modifiers
Expand All @@ -340,7 +350,7 @@ Foreign-key constraints:
"cluster_rule_user_feedback_cluster_id_fkey" FOREIGN KEY (cluster_id) REFERENCES report(cluster) ON DELETE CASCADE
```

### Table `cluster_user_rule_disable_feedback`
#### Table `cluster_user_rule_disable_feedback`

```
Column | Type | Modifiers
Expand All @@ -355,7 +365,7 @@ Indexes:
"cluster_user_rule_disable_feedback_pkey" PRIMARY KEY, btree (cluster_id, user_id, rule_id)
```

### Table `consumer_error`
#### Table `consumer_error`

```
Table "public.consumer_error"
Expand All @@ -373,15 +383,15 @@ Indexes:
"consumer_error_pkey" PRIMARY KEY, btree (topic, partition, topic_offset)
```

### Table `migration_info `
#### Table `migration_info `

```
Column | Type | Modifiers
---------+---------+-----------
version | integer | not null
```

### Table `rule_hit`
#### Table `rule_hit`

```
Column | Type | Modifiers
Expand All @@ -395,7 +405,7 @@ Indexes:
"rule_hit_pkey" PRIMARY KEY, btree (cluster_id, org_id, rule_fqdn, error_key)
```

### Table `recommendation`
#### Table `recommendation`

```
Table "public.recommendation"
Expand All @@ -411,7 +421,7 @@ Indexes:
"recommendation_pk" PRIMARY KEY, btree (org_id, cluster_id, rule_fqdn, error_key)
```

### Database tables affected by this service
#### Database tables affected by this service

Figuring out which reports are older than the specified time:
* `report`
Expand All @@ -426,6 +436,25 @@ Actually cleaning the data for given cluster:
* `recommendation` by `cluster_id`


### Database schema `dvo_recommendations`

#### Table `dvo_report`

```
Column | Type | Modifiers
------------------+-----------------------------+-----------
org_id | integer | not null
cluster_id | character varying | not null
namespace_id | character varying | not null
namespace_name | character varying |
report | text varying |
recommendations | integer | not null
objects | integer | not null
reported_at | timestamp without time zone |
last_checked_at | timestamp without time zone |
Indexes:
"report_pkey" PRIMARY KEY, btree (org_id, cluster_id, namespace_id)
```

## Documentation

Expand Down
5 changes: 5 additions & 0 deletions cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@ func main() {
log.Err(err).Msg("Load configuration")
}

err = CheckConfiguration(&config)
if err != nil {
log.Err(err).Msg("Check configuration")
}

if config.Logging.Debug {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
Expand Down
55 changes: 55 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ package main
// pg_port = 5432
// pg_db_name = "aggregator"
// pg_params = "sslmode=disable"
// schema = "ocp_recommendations"
//
// [logging]
// debug = true
Expand All @@ -62,6 +63,7 @@ package main
// INSIGHTS_RESULTS_CLEANER__STORAGE__PG_PORT
// INSIGHTS_RESULTS_CLEANER__STORAGE__PG_DB_NAME
// INSIGHTS_RESULTS_CLEANER__STORAGE__PG_PARAMS
// INSIGHTS_RESULTS_CLEANER__STORAGE__SCHEMA
// INSIGHTS_RESULTS_CLEANER__LOGGING__DEBUG
// INSIGHTS_RESULTS_CLEANER__LOGGING__LOG_DEVEL
// INSIGHTS_RESULTS_CLEANER__CLEANER__MAX_AGE
Expand Down Expand Up @@ -132,6 +134,7 @@ type StorageConfiguration struct {
PGPort int `mapstructure:"pg_port" toml:"pg_port"`
PGDBName string `mapstructure:"pg_db_name" toml:"pg_db_name"`
PGParams string `mapstructure:"pg_params" toml:"pg_params"`
Schema string `mapstructure:"schema" toml:"schema"`
}

// LoadConfiguration function loads configuration from defaultConfigFile, file
Expand Down Expand Up @@ -246,3 +249,55 @@ func updateConfigFromClowder(c *ConfigStruct) error {

return nil
}

// StringSet type is a poor man's implementation of set of strings
type StringSet map[string]struct{}

// allSupportedDrivers constructs set with names of all supported database
// drivers
func allSupportedDrivers() StringSet {
var drivers = make(StringSet)
drivers["sqlite3"] = struct{}{}
drivers["postgres"] = struct{}{}
return drivers
}

// allSupportedSchmeas constructs set with names of all supported database
// schemas
func allSupportedSchemas() StringSet {
var schemas = make(StringSet)
schemas["ocm_recommendations"] = struct{}{}
schemas["dvo_recommendations"] = struct{}{}
return schemas
}

// CheckConfiguration function checks if loaded configuration contains expected
// items
func CheckConfiguration(config *ConfigStruct) error {
drivers := allSupportedDrivers()
schemas := allSupportedSchemas()

storageCfg := GetStorageConfiguration(config)
driver := storageCfg.Driver
schema := storageCfg.Schema

if driver == "" {
return fmt.Errorf("Database driver is not specified in configuration")
}

if schema == "" {
return fmt.Errorf("Database schema is not specified in configuration")
}

_, found := drivers[driver]
if !found {
return fmt.Errorf("Incorrect database driver found in configuration: %s", driver)
}

_, found = schemas[schema]
if !found {
return fmt.Errorf("Incorrect database schema found in configuration: %s", schema)
}

return nil
}
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pg_host = "localhost"
pg_port = 5432
pg_db_name = "aggregator"
pg_params = "sslmode=disable"
schema = "dvo_recommendations"

[logging]
debug = true
Expand Down
68 changes: 68 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func TestLoadStorageConfiguration(t *testing.T) {
assert.Equal(t, 5432, storageCfg.PGPort)
assert.Equal(t, "notifications", storageCfg.PGDBName)
assert.Equal(t, "", storageCfg.PGParams)
assert.Equal(t, "ocp_recommendations", storageCfg.Schema)
}

// TestLoadLoggingConfiguration tests loading the logging configuration
Expand Down Expand Up @@ -174,3 +175,70 @@ func TestLoadConfigurationFromEnvVariableClowderEnabled(t *testing.T) {
dbCfg := main.GetStorageConfiguration(&config)
assert.Equal(t, testDB, dbCfg.PGDBName)
}

// TestCheckConfigurationEmptyConfig tests the function to check loaded configuration
func TestCheckConfigurationEmptyConfig(t *testing.T) {
config := main.ConfigStruct{}
err := main.CheckConfiguration(&config)
assert.Error(t, err, "Error should be thrown for empty configuration")
}

// TestCheckConfigurationPositiveTestCases tests the function to check loaded configuration
func TestCheckConfigurationPositiveTestCases(t *testing.T) {
config1 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "postgres",
Schema: "ocm_recommendations",
},
}
err := main.CheckConfiguration(&config1)
assert.NoError(t, err, "Error should not be thrown")

config2 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "sqlite3",
Schema: "dvo_recommendations",
},
}
err = main.CheckConfiguration(&config2)
assert.NoError(t, err, "Error should not be thrown")
}

// TestCheckConfigurationNegativeTestCases tests the function to check loaded configuration
func TestCheckConfigurationNegativeTestCases(t *testing.T) {
config1 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "unknown",
Schema: "ocm_recommendations",
},
}
err := main.CheckConfiguration(&config1)
assert.Error(t, err, "Error should be thrown for unknown database driver")

config2 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "sqlite3",
Schema: "unknown",
},
}
err = main.CheckConfiguration(&config2)
assert.Error(t, err, "Error should be thrown for unknown database schema")

config3 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "",
Schema: "ocm_recommendations",
},
}
err = main.CheckConfiguration(&config3)
assert.Error(t, err, "Error should be thrown for empty/missing database driver")

config4 := main.ConfigStruct{
Storage: main.StorageConfiguration{
Driver: "sqlite3",
Schema: "",
},
}
err = main.CheckConfiguration(&config4)
assert.Error(t, err, "Error should be thrown for empty/missing database schema")
}
2 changes: 2 additions & 0 deletions deploy/clowdapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ objects:
value: ${PG_PARAMS}
- name: INSIGHTS_RESULTS_CLEANER__STORAGE__DB_DRIVER
value: postgres
- name: INSIGHTS_RESULTS_CLEANER__STORAGE__SCHEMA
value: ocp_recommendations
- name: INSIGHTS_RESULTS_CLEANER__LOGGING__DEBUG
value: "${DEBUG}"
- name: INSIGHTS_RESULTS_CLEANER__LOGGING__LOG_DEVEL
Expand Down
Loading

0 comments on commit e066373

Please sign in to comment.