diff --git a/pkg/db/seeds.go b/pkg/db/seeds.go index eef505b..3bbab55 100644 --- a/pkg/db/seeds.go +++ b/pkg/db/seeds.go @@ -1,8 +1,10 @@ package db import ( + "bytes" "database/sql" "fmt" + "text/template" _ "embed" @@ -27,11 +29,8 @@ var appuioCloudPersistentStorageQuery string //go:embed seeds/appuio_managed_openshift_vcpu.promql var appuioManagedOpenShiftvCPUQuery string -//go:embed seeds/appcat_postgresql_vshn_standalone.promql -var appcatPostgresqlVSHNStandalone string - -//go:embed seeds/appcat_redis_vshn_standalone.promql -var appcatRedisVSHNStandalone string +//go:embed seeds/appcat_vshn.promql.tmpl +var appcatVSHNTemplate string // DefaultQueries consists of default starter queries. var DefaultQueries = []Query{ @@ -72,27 +71,45 @@ var DefaultQueries = []Query{ Query: appuioManagedOpenShiftvCPUQuery, Unit: "vCPU", }, +} + +var renderedQueries = []RenderedQuery{ { - Name: "appcat_postgresql_vshn_standalone", - Description: "Number of VSHN managed standalone postgres instances", - Query: appcatPostgresqlVSHNStandalone, - Unit: "Instances", + Query: Query{ + Name: "appcat_postgresql_vshn_standalone", + Description: "Number of VSHN managed standalone postgres instances", + Unit: "Instances", + }, + ProductName: "postgres", + ServiceName: "postgresql-standalone", + Template: appcatVSHNTemplate, }, { - Name: "appcat_redis_vshn_standalone", - Description: "Number of VSHN managed standalone redis instances", - Query: appcatRedisVSHNStandalone, - Unit: "Instances", + Query: Query{ + Name: "appcat_redis_vshn_standalone", + Description: "Number of VSHN managed standalone redis instances", + Unit: "Instances", + }, + ProductName: "redis", + ServiceName: "redis-standalone", + Template: appcatVSHNTemplate, }, } +type RenderedQuery struct { + Query + ProductName string + ServiceName string + Template string +} + // Seed seeds the database with "starter" data. // Is idempotent and thus can be executed multiple times in one database. func Seed(db *sql.DB) error { - return SeedQueries(db, DefaultQueries) + return SeedQueries(db, DefaultQueries, renderedQueries) } -func SeedQueries(db *sql.DB, queries []Query) error { +func SeedQueries(db *sql.DB, queries []Query, RenderedQueries []RenderedQuery) error { dbx := NewDBx(db) tx, err := dbx.Beginx() if err != nil { @@ -100,6 +117,11 @@ func SeedQueries(db *sql.DB, queries []Query) error { } defer tx.Rollback() + queries, err = addRenderedQueries(queries, RenderedQueries) + if err != nil { + return err + } + if err := createQueries(tx, queries); err != nil { return err } @@ -152,3 +174,25 @@ func queryExistsByName(tx *sqlx.Tx, name string) (bool, error) { err := tx.Get(&exists, "SELECT EXISTS(SELECT 1 FROM queries WHERE name = $1)", name) return exists, err } + +func addRenderedQueries(queries []Query, renderenderedQueries []RenderedQuery) ([]Query, error) { + for _, query := range renderenderedQueries { + rendered, err := renderQuery(query) + if err != nil { + return nil, err + } + query.Query.Query = rendered + queries = append(queries, query.Query) + } + return queries, nil +} + +func renderQuery(query RenderedQuery) (string, error) { + tmpl, err := template.New("AppCatService").Parse(query.Template) + if err != nil { + return "", err + } + buffer := &bytes.Buffer{} + err = tmpl.Execute(buffer, query) + return buffer.String(), err +} diff --git a/pkg/db/seeds/appcat_postgresql_vshn_standalone.promql b/pkg/db/seeds/appcat_postgresql_vshn_standalone.promql deleted file mode 100644 index 0bb451f..0000000 --- a/pkg/db/seeds/appcat_postgresql_vshn_standalone.promql +++ /dev/null @@ -1,64 +0,0 @@ -# Sum values over one hour and get mean -sum_over_time( - # Udpate label product: $product:$provider:$tenant_id:$claim_namespace:$architecture - label_join( - # Add label category: $provider:$claim_namespace - label_join( - # Add label architecture: standalone-$SLA, where $SLA is the content of label appcat.vshn.io/sla - label_replace( - # Add label provider: vshn - label_replace( - # Add label product: postgres - label_replace( - # Default appcat.vshn.io/sla to besteffort if it is not set - label_replace( - # Copy label appcat.vshn.io/namespace to label claim_namespace - label_replace( - # Copy label appuio.io/organization to label tenant_id - label_replace( - # Fetch all namespaces with label appcat.vshn.io/servicename="postgresql-standalone" - kube_namespace_labels{label_appuio_io_organization=~".+", label_appcat_vshn_io_servicename="postgresql-standalone"}, - "tenant_id", - "$1", - "label_appuio_io_organization", - "(.*)" - ), - "claim_namespace", - "$1", - "label_appcat_vshn_io_claim_namespace", - "(.*)" - ), - "label_appcat_vshn_io_sla", - "besteffort", - "label_appcat_vshn_io_sla", - "^$" - ), - "product", - "postgres", - "", - "" - ), - "provider", - "vshn", - "", - "" - ), - "architecture", - "standalone-$1", - "label_appcat_vshn_io_sla", - "(.*)" - ), - "category", - ":", - "provider", - "claim_namespace" - ), - "product", - ":", - "product", - "provider", - "tenant_id", - "claim_namespace", - "architecture" - )[59m:1m] -)/60 diff --git a/pkg/db/seeds/appcat_redis_vshn_standalone.promql b/pkg/db/seeds/appcat_vshn.promql.tmpl similarity index 93% rename from pkg/db/seeds/appcat_redis_vshn_standalone.promql rename to pkg/db/seeds/appcat_vshn.promql.tmpl index d6ffe20..3f860f4 100644 --- a/pkg/db/seeds/appcat_redis_vshn_standalone.promql +++ b/pkg/db/seeds/appcat_vshn.promql.tmpl @@ -16,8 +16,8 @@ sum_over_time( label_replace( # Copy label appuio.io/organization to label tenant_id label_replace( - # Fetch all namespaces with label appcat.vshn.io/servicename="redis-standalone" - kube_namespace_labels{label_appuio_io_organization=~".+", label_appcat_vshn_io_servicename="redis-standalone"}, + # Fetch all namespaces with label appcat.vshn.io/servicename="{{.ServiceName}}" + kube_namespace_labels{label_appuio_io_organization=~".+", label_appcat_vshn_io_servicename="{{.ServiceName}}"}, "tenant_id", "$1", "label_appuio_io_organization", @@ -34,7 +34,7 @@ sum_over_time( "^$" ), "product", - "redis", + "{{.ProductName}}", "", "" ), diff --git a/pkg/db/seeds_test.go b/pkg/db/seeds_test.go index 596e51e..a6c7191 100644 --- a/pkg/db/seeds_test.go +++ b/pkg/db/seeds_test.go @@ -22,7 +22,7 @@ func (s *SeedsTestSuite) TestSeedDefaultQueries() { _, err := d.Exec("DELETE FROM queries") require.NoError(t, err) - expQueryNum := 7 + expQueryNum := 8 count := "SELECT COUNT(*) FROM queries" requireQueryEqual(t, d, 0, count) @@ -33,7 +33,7 @@ func (s *SeedsTestSuite) TestSeedDefaultQueries() { Description: "Memory usage (maximum of requested and used memory) aggregated by namespace", Unit: "MiB", }, - })) + }, []db.RenderedQuery{})) t.Log(t, count) requireQueryEqual(t, d, 1, count)