diff --git a/API.md b/API.md index af8487d..d0fcbe3 100644 --- a/API.md +++ b/API.md @@ -53,6 +53,8 @@ with identical semantics upstream. | `octavia/db/repositories` | `GetAllPools` | `PoolGetAll` | | `octavia/db/repositories` | `GetAllLoadBalancersWithVip` | `LoadBalancerGetAllWithVIP` | | `octavia/db/repositories` | `GetAllAmphora` | `AmphoraGetAll` | +| `octavia/db/repositories` | — | `QuotaGetAll` (`QuotaRepository.GetAll`) | +| `octavia/db/repositories` | — | `QuotaUsageGetAll` (`QuotaRepository.GetUsage`) | | `placement/objects` | `GetResourceMetrics` | `GetResourceProviderInventories` | | `placement/objects` | `GetAllocationsByProject` | `GetAllocationsByProject` | | `placement/objects` | `GetConsumerCountByProject` | `GetConsumerCountByProject` | @@ -80,7 +82,8 @@ with identical semantics upstream. - The filtered APIs support exact membership and lifetime overlap, not every upstream regex, pagination, eager-loading, or authorization option. - Current exporter queries retain their existing deleted/hidden/status predicates; they are not full upstream administrative listings. - Errors are Go/database errors; absent single resources return `sql.ErrNoRows` rather than Python service-specific exceptions. -- Aggregate queries such as `QuotaGetAllWithUsage`, `GetResourceProviderInventories`, and `GetResourceCountsByProject` are explicitly library extensions. +- Aggregate queries such as `QuotaGetAllWithUsage`, `GetResourceProviderInventories`, `GetResourceCountsByProject`, and Octavia `QuotaUsageGetAll` are explicitly library extensions. Octavia `QuotaUsageGetAll` recounts objects with `check_quota_met`'s predicate; Octavia itself enforces against the cached `in_use_*` counters that `QuotaGetAll` returns. +- Owner filtering follows each service: Neutron keeps empty-string project IDs because Neutron creates real objects with them (for example L3 HA networks and ports), while Octavia excludes them because it rejects empty project IDs. - Designate returns canonical zone and recordset UUIDs and excludes soft-deleted zones. `RecordsetCountByZone` includes zero-count zones and can run without reading individual records. `RecordsetGetAllWithStatus` uses the child precedence `ERROR`, `PENDING`, `ACTIVE`, `DELETED` and returns `ACTIVE` for an empty recordset. Recordsets with a NULL `zone_id` have no visible zone and are omitted. - No cross-cell routing or historic schema negotiation is performed. Nova archive access is explicitly selected with `InstanceFilters.Archive`. @@ -99,3 +102,39 @@ lifecycle metadata. Their fixed current-resource queries remain unchanged for exporter consumers. Deleted volume types and legacy type tables are only read when explicitly requested. Legacy table availability is a caller/deployment concern; errors are propagated rather than hidden. + +### Octavia quotas + +| Go operation | Upstream naming source | Scope | +| --- | --- | --- | +| `QuotaGetAll` (`QuotaRepository.GetAll`) | [Octavia `QuotasRepository`](https://github.com/openstack/octavia/blob/master/octavia/db/repositories.py) | Stored limits and cached `in_use_*` counters for every project at once; NULL and empty project IDs excluded. | +| `QuotaUsageGetAll` (`QuotaRepository.GetUsage`) | Octavia `Repositories.check_quota_met` recount predicate | Library extension: recounts all seven quota resources for every project; scans seven unindexed object tables. | + +`NewQuotaRepository(db).GetAll(ctx)` / `QuotaGetAll` returns one `Quota` row per +project with the stored limits for load balancers, listeners, pools, health +monitors, members, L7 policies and L7 rules, plus Octavia's cached `in_use_*` +counters. Each value is a `sql.NullInt32`. A NULL limit means no explicit +override, and zero and -1 remain distinct values; the caller resolves configured +defaults. The `in_use_*` counters are what Octavia's `check_quota_met` and +`decrement_quota` enforce against. A NULL or zero counter means Octavia will +recount on its next quota check for that project. + +`QuotaRepository.GetUsage` / `QuotaUsageGetAll` rebuilds project counts for all +seven quota resources independently, using the same predicate Octavia uses when +it recounts: the object's own `project_id` and `provisioning_status != 'DELETED'`. +Counts include ERROR and pending statuses. Each object uses its own project and +status, including children of deleted parents or parents owned by another project, +and there are no parent joins to multiply counts. Results contain only nonzero +project/resource groups, including projects without quota rows, with int64 usage. + +The two reads answer different questions. `in_use_*` is what Octavia enforces +today, and `GetUsage` is what those counters should be. Compare them to detect +counter drift, which persists until Octavia reconciles it. Octavia defines no +index on `project_id` or `provisioning_status`, so `GetUsage` scans all seven +object tables. `GetAll` reads only the `quotas` table and is cheap enough for +every scrape. Run `GetUsage` as a less frequent drift check on large deployments. + +Both reads exclude NULL and empty project IDs. Octavia rejects empty project IDs +(`MissingProjectID`), so such rows can never hold quota. The schema fixtures +contain only fields needed for these tests, not a complete OpenStack migration. +The MariaDB integration test is `TestOctaviaQuotas` in `tests/` and requires Docker. diff --git a/README.md b/README.md index 46520c4..523f72f 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ repository objects. Placement queries live under `placement/objects`. | `magnum/db` | `GetClusterList` | | `manila/db` | `ShareGetAllWithInstances` | | `neutron/db` | `GetAgents`, `GetHARouterAgentPortBindingsWithAgents`, `GetRouters` | -| `octavia/db/repositories` | `PoolRepository`, `LoadBalancerRepository`, `AmphoraRepository` | +| `octavia/db/repositories` | `PoolRepository`, `LoadBalancerRepository`, `AmphoraRepository`, `QuotaRepository` | | `placement/objects` | Resource provider inventory and allocation projections | ## Example diff --git a/octavia/db/repositories/models.go b/octavia/db/repositories/models.go index 3c5c214..8cfa883 100644 --- a/octavia/db/repositories/models.go +++ b/octavia/db/repositories/models.go @@ -31,6 +31,37 @@ type Amphora struct { ComputeFlavor sql.NullString } +type HealthMonitor struct { + ID string + ProjectID sql.NullString + PoolID sql.NullString + ProvisioningStatus string +} + +type L7policy struct { + ID string + ProjectID sql.NullString + ListenerID sql.NullString + ProvisioningStatus string +} + +type L7rule struct { + ID string + ProjectID sql.NullString + L7policyID sql.NullString + ProvisioningStatus string +} + +type Listener struct { + ID string + ProjectID sql.NullString + Name sql.NullString + Protocol string + ProtocolPort int32 + LoadBalancerID sql.NullString + ProvisioningStatus string +} + type LoadBalancer struct { ProjectID sql.NullString ID string @@ -48,6 +79,13 @@ type LoadBalancer struct { AvailabilityZone sql.NullString } +type Member struct { + ID string + ProjectID sql.NullString + PoolID sql.NullString + ProvisioningStatus string +} + type Pool struct { ProjectID sql.NullString ID string @@ -70,6 +108,24 @@ type Pool struct { AlpnProtocols sql.NullString } +type Quota struct { + ProjectID string + HealthMonitor sql.NullInt32 + Listener sql.NullInt32 + LoadBalancer sql.NullInt32 + Member sql.NullInt32 + Pool sql.NullInt32 + L7policy sql.NullInt32 + L7rule sql.NullInt32 + InUseHealthMonitor sql.NullInt32 + InUseListener sql.NullInt32 + InUseLoadBalancer sql.NullInt32 + InUseMember sql.NullInt32 + InUsePool sql.NullInt32 + InUseL7policy sql.NullInt32 + InUseL7rule sql.NullInt32 +} + type Vip struct { LoadBalancerID string IpAddress sql.NullString diff --git a/octavia/db/repositories/queries.sql b/octavia/db/repositories/queries.sql index 7a598a4..22d69ae 100644 --- a/octavia/db/repositories/queries.sql +++ b/octavia/db/repositories/queries.sql @@ -42,3 +42,73 @@ FROM amphora WHERE status != 'DELETED'; + +-- name: QuotaGetAll :many +-- Per-project Octavia quota limits and the cached in_use_* counters that +-- Octavia's check_quota_met and decrement_quota enforce against. NULL limit +-- means no explicit override; a NULL or 0 counter is recounted by Octavia on +-- the next quota check. Octavia rejects empty project IDs (MissingProjectID), +-- so empty-string rows are excluded, matching QuotaUsageGetAll. +SELECT + project_id, + health_monitor, + listener, + load_balancer, + member, + pool, + l7policy, + l7rule, + in_use_health_monitor, + in_use_listener, + in_use_load_balancer, + in_use_member, + in_use_pool, + in_use_l7policy, + in_use_l7rule +FROM + quotas +WHERE + project_id != '' +ORDER BY + project_id; + +-- name: QuotaUsageGetAll :many +-- Rebuild counts using Octavia check_quota_met's project/status predicate. +-- Each table is counted independently: no parent join can multiply rows. +-- Upstream defines no index on project_id or provisioning_status, so this +-- scans all seven tables; compare against QuotaGetAll's in_use_* counters to +-- detect drift rather than calling it on every scrape. +SELECT project_id, 'load_balancer' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM load_balancer +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'listener' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM listener +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'pool' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM pool +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'member' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM member +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'health_monitor' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM health_monitor +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'l7policy' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM l7policy +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'l7rule' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM l7rule +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id; diff --git a/octavia/db/repositories/queries.sql.go b/octavia/db/repositories/queries.sql.go index 604d8ea..108683b 100644 --- a/octavia/db/repositories/queries.sql.go +++ b/octavia/db/repositories/queries.sql.go @@ -184,3 +184,143 @@ func (q *Queries) PoolGetAll(ctx context.Context) ([]PoolGetAllRow, error) { } return items, nil } + +const QuotaGetAll = `-- name: QuotaGetAll :many +SELECT + project_id, + health_monitor, + listener, + load_balancer, + member, + pool, + l7policy, + l7rule, + in_use_health_monitor, + in_use_listener, + in_use_load_balancer, + in_use_member, + in_use_pool, + in_use_l7policy, + in_use_l7rule +FROM + quotas +WHERE + project_id != '' +ORDER BY + project_id +` + +// Per-project Octavia quota limits and the cached in_use_* counters that +// Octavia's check_quota_met and decrement_quota enforce against. NULL limit +// means no explicit override; a NULL or 0 counter is recounted by Octavia on +// the next quota check. Octavia rejects empty project IDs (MissingProjectID), +// so empty-string rows are excluded, matching QuotaUsageGetAll. +func (q *Queries) QuotaGetAll(ctx context.Context) ([]Quota, error) { + rows, err := q.db.QueryContext(ctx, QuotaGetAll) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Quota + for rows.Next() { + var i Quota + if err := rows.Scan( + &i.ProjectID, + &i.HealthMonitor, + &i.Listener, + &i.LoadBalancer, + &i.Member, + &i.Pool, + &i.L7policy, + &i.L7rule, + &i.InUseHealthMonitor, + &i.InUseListener, + &i.InUseLoadBalancer, + &i.InUseMember, + &i.InUsePool, + &i.InUseL7policy, + &i.InUseL7rule, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const QuotaUsageGetAll = `-- name: QuotaUsageGetAll :many +SELECT project_id, 'load_balancer' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM load_balancer +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'listener' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM listener +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'pool' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM pool +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'member' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM member +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'health_monitor' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM health_monitor +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'l7policy' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM l7policy +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +UNION ALL +SELECT project_id, 'l7rule' AS resource, CAST(COUNT(*) AS SIGNED) AS in_use +FROM l7rule +WHERE provisioning_status != 'DELETED' AND project_id IS NOT NULL AND project_id != '' +GROUP BY project_id +` + +type QuotaUsageGetAllRow struct { + ProjectID sql.NullString + Resource string + InUse int64 +} + +// Rebuild counts using Octavia check_quota_met's project/status predicate. +// Each table is counted independently: no parent join can multiply rows. +// Upstream defines no index on project_id or provisioning_status, so this +// scans all seven tables; compare against QuotaGetAll's in_use_* counters to +// detect drift rather than calling it on every scrape. +func (q *Queries) QuotaUsageGetAll(ctx context.Context) ([]QuotaUsageGetAllRow, error) { + rows, err := q.db.QueryContext(ctx, QuotaUsageGetAll) + if err != nil { + return nil, err + } + defer rows.Close() + var items []QuotaUsageGetAllRow + for rows.Next() { + var i QuotaUsageGetAllRow + if err := rows.Scan(&i.ProjectID, &i.Resource, &i.InUse); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/octavia/db/repositories/quota_usage_test.go b/octavia/db/repositories/quota_usage_test.go new file mode 100644 index 0000000..fcbe887 --- /dev/null +++ b/octavia/db/repositories/quota_usage_test.go @@ -0,0 +1,107 @@ +package octavia + +import ( + "context" + "database/sql" + "database/sql/driver" + "errors" + "regexp" + "testing" + + "github.com/DATA-DOG/go-sqlmock" + "github.com/stretchr/testify/require" +) + +func TestQuotaUsageRepository(t *testing.T) { + for _, scenario := range []string{"rows", "empty", "query error", "scan error", "iteration error"} { + t.Run(scenario, func(t *testing.T) { + db, mock, err := sqlmock.New() + require.NoError(t, err) + defer db.Close() + query := mock.ExpectQuery(regexp.QuoteMeta(QuotaUsageGetAll)) + rows := sqlmock.NewRows([]string{"project_id", "resource", "in_use"}) + readErr := errors.New("read failed") + switch scenario { + case "rows": + rows.AddRow("project-a", "member", int64(1)<<33) + query.WillReturnRows(rows).RowsWillBeClosed() + case "empty": + query.WillReturnRows(rows).RowsWillBeClosed() + case "query error": + query.WillReturnError(sql.ErrConnDone) + case "scan error": + rows.AddRow("project-a", "member", "invalid integer") + query.WillReturnRows(rows).RowsWillBeClosed() + case "iteration error": + rows.AddRow("project-a", "member", 4).RowError(0, readErr) + query.WillReturnRows(rows).RowsWillBeClosed() + } + got, err := NewQuotaRepository(db).GetUsage(context.Background()) + if scenario == "rows" { + require.NoError(t, err) + require.Equal(t, []QuotaUsageGetAllRow{{ProjectID: sql.NullString{String: "project-a", Valid: true}, Resource: "member", InUse: int64(1) << 33}}, got) + } else if scenario == "empty" { + require.NoError(t, err) + require.Empty(t, got) + } else { + require.Error(t, err) + if scenario == "query error" { + require.ErrorIs(t, err, sql.ErrConnDone) + } else if scenario == "iteration error" { + require.ErrorIs(t, err, readErr) + } + } + require.NoError(t, mock.ExpectationsWereMet()) + }) + } +} + +func TestQuotaLimitsRepository(t *testing.T) { + n := func(v int32) sql.NullInt32 { return sql.NullInt32{Int32: v, Valid: true} } + columns := []string{"project_id", "health_monitor", "listener", "load_balancer", "member", "pool", "l7policy", "l7rule", + "in_use_health_monitor", "in_use_listener", "in_use_load_balancer", "in_use_member", "in_use_pool", "in_use_l7policy", "in_use_l7rule"} + values := []driver.Value{"project-a", nil, -1, 0, 0, 17, nil, 23, nil, nil, 4, 999, nil, nil, nil} + want := []Quota{{ProjectID: "project-a", Listener: n(-1), LoadBalancer: n(0), Member: n(0), Pool: n(17), L7rule: n(23), + InUseLoadBalancer: n(4), InUseMember: n(999)}} + for _, scenario := range []string{"rows", "empty", "query error", "scan error", "iteration error"} { + t.Run(scenario, func(t *testing.T) { + db, mock, err := sqlmock.New() + require.NoError(t, err) + defer db.Close() + query := mock.ExpectQuery(regexp.QuoteMeta(QuotaGetAll)) + rows := sqlmock.NewRows(columns) + readErr := errors.New("read failed") + switch scenario { + case "rows": + rows.AddRow(values...) + case "query error": + query.WillReturnError(sql.ErrConnDone) + case "scan error": + bad := append([]driver.Value(nil), values...) + bad[11] = "invalid integer" + rows.AddRow(bad...) + case "iteration error": + rows.AddRow(values...).RowError(0, readErr) + } + if scenario != "query error" { + query.WillReturnRows(rows).RowsWillBeClosed() + } + got, err := NewQuotaRepository(db).GetAll(context.Background()) + switch scenario { + case "rows": + require.NoError(t, err) + require.Equal(t, want, got) + case "empty": + require.NoError(t, err) + require.Empty(t, got) + case "query error": + require.ErrorIs(t, err, sql.ErrConnDone) + case "scan error": + require.ErrorContains(t, err, "invalid integer") + case "iteration error": + require.ErrorIs(t, err, readErr) + } + require.NoError(t, mock.ExpectationsWereMet()) + }) + } +} diff --git a/octavia/db/repositories/repositories.go b/octavia/db/repositories/repositories.go index f3adae7..c00b371 100644 --- a/octavia/db/repositories/repositories.go +++ b/octavia/db/repositories/repositories.go @@ -27,3 +27,21 @@ func NewAmphoraRepository(db DBTX) *AmphoraRepository { return &AmphoraRepositor func (r *AmphoraRepository) GetAll(ctx context.Context) ([]AmphoraGetAllRow, error) { return r.queries.AmphoraGetAll(ctx) } + +// QuotaRepository provides read operations for project quotas. +type QuotaRepository struct{ queries *Queries } + +func NewQuotaRepository(db DBTX) *QuotaRepository { return &QuotaRepository{queries: New(db)} } + +// GetAll returns per-project limits and Octavia's cached in_use_* counters, +// which are what Octavia enforces quota against. +func (r *QuotaRepository) GetAll(ctx context.Context) ([]Quota, error) { + return r.queries.QuotaGetAll(ctx) +} + +// GetUsage rebuilds project counts from non-DELETED objects, independently +// of the cached in_use_* counters. It includes usage-only projects. It scans +// every quota resource table; compare it with GetAll to detect counter drift. +func (r *QuotaRepository) GetUsage(ctx context.Context) ([]QuotaUsageGetAllRow, error) { + return r.queries.QuotaUsageGetAll(ctx) +} diff --git a/schema/octavia/schema.sql b/schema/octavia/schema.sql index b77b9a6..1f6affd 100644 --- a/schema/octavia/schema.sql +++ b/schema/octavia/schema.sql @@ -74,3 +74,71 @@ CREATE TABLE `octavia_owned` tinyint (1) DEFAULT NULL, `vnic_type` varchar(64) NOT NULL DEFAULT 'normal' ); + +-- Octavia quotas: one row per project (octavia/db/models.py Quotas). +-- Integer columns are limits; NULL means the deployment default applies. +-- in_use_* are Octavia's cached usage counters, which check_quota_met and +-- decrement_quota maintain and enforce against. +CREATE TABLE + `quotas` ( + `project_id` varchar(36) NOT NULL PRIMARY KEY, + `health_monitor` int DEFAULT NULL, + `listener` int DEFAULT NULL, + `load_balancer` int DEFAULT NULL, + `member` int DEFAULT NULL, + `pool` int DEFAULT NULL, + `l7policy` int DEFAULT NULL, + `l7rule` int DEFAULT NULL, + `in_use_health_monitor` int DEFAULT NULL, + `in_use_listener` int DEFAULT NULL, + `in_use_load_balancer` int DEFAULT NULL, + `in_use_member` int DEFAULT NULL, + `in_use_pool` int DEFAULT NULL, + `in_use_l7policy` int DEFAULT NULL, + `in_use_l7rule` int DEFAULT NULL + ); + +-- Minimal listener rows for QuotaUsageGetAll's recount, which follows +-- check_quota_met's project_id and provisioning_status predicate. +CREATE TABLE + `listener` ( + `id` varchar(36) NOT NULL PRIMARY KEY, + `project_id` varchar(36) DEFAULT NULL, + `name` varchar(255) DEFAULT NULL, + `protocol` varchar(16) NOT NULL, + `protocol_port` int NOT NULL, + `load_balancer_id` varchar(36) DEFAULT NULL, + `provisioning_status` varchar(16) NOT NULL + ); + +-- Minimal quota-count fixture; fields follow Octavia 2025.2 db/models.py. +CREATE TABLE `member` ( + `id` varchar(36) NOT NULL PRIMARY KEY, + `project_id` varchar(36) DEFAULT NULL, + `pool_id` varchar(36) DEFAULT NULL, + `provisioning_status` varchar(16) NOT NULL +); + +-- Minimal quota-count fixture; fields follow Octavia 2025.2 db/models.py. +CREATE TABLE `health_monitor` ( + `id` varchar(36) NOT NULL PRIMARY KEY, + `project_id` varchar(36) DEFAULT NULL, + `pool_id` varchar(36) DEFAULT NULL UNIQUE, + `provisioning_status` varchar(16) NOT NULL +); + +-- Minimal quota-count fixture; fields follow Octavia 2025.2 db/models.py. +CREATE TABLE `l7policy` ( + `id` varchar(36) NOT NULL PRIMARY KEY, + `project_id` varchar(36) DEFAULT NULL, + `listener_id` varchar(36) DEFAULT NULL, + `provisioning_status` varchar(16) NOT NULL +); + +-- Minimal quota-count fixture; fields follow Octavia 2025.2 db/models.py. +CREATE TABLE `l7rule` ( + `id` varchar(36) NOT NULL PRIMARY KEY, + `project_id` varchar(36) DEFAULT NULL, + `l7policy_id` varchar(36) DEFAULT NULL, + `provisioning_status` varchar(16) NOT NULL +); diff --git a/tests/fixtures/octavia_quota_usage.sql b/tests/fixtures/octavia_quota_usage.sql new file mode 100644 index 0000000..2009a4e --- /dev/null +++ b/tests/fixtures/octavia_quota_usage.sql @@ -0,0 +1,86 @@ +-- Deliberately shared parent IDs catch aggregate join multiplication. +-- Live children of DELETED parents still count by their own project and status. +INSERT INTO load_balancer (id, project_id, provisioning_status, operating_status, enabled) VALUES ('load_balancer-a-0', 'project-a', 'ACTIVE', 'ONLINE', 1); +INSERT INTO load_balancer (id, project_id, provisioning_status, operating_status, enabled) VALUES ('load_balancer-a-1', 'project-a', 'ERROR', 'ONLINE', 1); +INSERT INTO load_balancer (id, project_id, provisioning_status, operating_status, enabled) VALUES ('load_balancer-a-2', 'project-a', 'PENDING_CREATE', 'ONLINE', 1); +INSERT INTO load_balancer (id, project_id, provisioning_status, operating_status, enabled) VALUES ('load_balancer-a-3', 'project-a', 'PENDING_DELETE', 'ONLINE', 1); +INSERT INTO load_balancer (id, project_id, provisioning_status, operating_status, enabled) VALUES ('load_balancer-a-4', 'project-a', 'DELETED', 'ONLINE', 1); +INSERT INTO load_balancer (id, project_id, provisioning_status, operating_status, enabled) VALUES ('load_balancer-b', 'project-b', 'ACTIVE', 'ONLINE', 1); +INSERT INTO load_balancer (id, project_id, provisioning_status, operating_status, enabled) VALUES ('load_balancer-null', NULL, 'ACTIVE', 'ONLINE', 1); +INSERT INTO load_balancer (id, project_id, provisioning_status, operating_status, enabled) VALUES ('load_balancer-empty', '', 'ACTIVE', 'ONLINE', 1); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-a-0', 'project-a', 'ACTIVE', 'HTTP', 80, 'load_balancer-a-4'); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-a-1', 'project-a', 'ERROR', 'HTTP', 80, 'load_balancer-a-0'); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-a-2', 'project-a', 'PENDING_CREATE', 'HTTP', 80, 'load_balancer-a-0'); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-a-3', 'project-a', 'PENDING_DELETE', 'HTTP', 80, 'load_balancer-a-0'); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-a-4', 'project-a', 'DELETED', 'HTTP', 80, 'load_balancer-a-0'); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-b', 'project-b', 'ACTIVE', 'HTTP', 80, 'load_balancer-a-0'); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-null', NULL, 'ACTIVE', 'HTTP', 80, 'load_balancer-a-0'); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-empty', '', 'ACTIVE', 'HTTP', 80, 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-a-0', 'project-a', 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-4'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-a-1', 'project-a', 'ERROR', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-a-2', 'project-a', 'PENDING_CREATE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-a-3', 'project-a', 'PENDING_DELETE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-a-4', 'project-a', 'DELETED', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-b', 'project-b', 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-null', NULL, 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-empty', '', 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-a-0', 'project-a', 'ACTIVE', 'pool-a-4'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-a-1', 'project-a', 'ERROR', 'pool-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-a-2', 'project-a', 'PENDING_CREATE', 'pool-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-a-3', 'project-a', 'PENDING_DELETE', 'pool-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-a-4', 'project-a', 'DELETED', 'pool-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-b', 'project-b', 'ACTIVE', 'pool-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-null', NULL, 'ACTIVE', 'pool-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-empty', '', 'ACTIVE', 'pool-a-0'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-a-0', 'project-a', 'ACTIVE', 'pool-a-4'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-a-1', 'project-a', 'ERROR', 'pool-a-1'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-a-2', 'project-a', 'PENDING_CREATE', 'pool-a-2'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-a-3', 'project-a', 'PENDING_DELETE', 'pool-a-3'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-a-4', 'project-a', 'DELETED', 'pool-a-0'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-b', 'project-b', 'ACTIVE', 'pool-b'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-null', NULL, 'ACTIVE', 'pool-null'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-empty', '', 'ACTIVE', 'pool-empty'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-a-0', 'project-a', 'ACTIVE', 'listener-a-4'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-a-1', 'project-a', 'ERROR', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-a-2', 'project-a', 'PENDING_CREATE', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-a-3', 'project-a', 'PENDING_DELETE', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-a-4', 'project-a', 'DELETED', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-b', 'project-b', 'ACTIVE', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-null', NULL, 'ACTIVE', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-empty', '', 'ACTIVE', 'listener-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-a-0', 'project-a', 'ACTIVE', 'l7policy-a-4'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-a-1', 'project-a', 'ERROR', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-a-2', 'project-a', 'PENDING_CREATE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-a-3', 'project-a', 'PENDING_DELETE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-a-4', 'project-a', 'DELETED', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-b', 'project-b', 'ACTIVE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-null', NULL, 'ACTIVE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-empty', '', 'ACTIVE', 'l7policy-a-0'); +-- Cached counters: in_use_load_balancer matches the recount (4), in_use_member +-- has drifted (999 vs 7). The rest are NULL, as before Octavia's first quota check. +INSERT INTO quotas (project_id, health_monitor, listener, load_balancer, member, pool, l7policy, l7rule, in_use_load_balancer, in_use_member) VALUES ('project-a', NULL, -1, 0, 0, 17, NULL, 23, 4, 999); +INSERT INTO listener (id, project_id, provisioning_status, protocol, protocol_port, load_balancer_id) VALUES ('listener-extra-0', 'project-a', 'ACTIVE', 'HTTP', 80, 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-extra-0', 'project-a', 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-extra-1', 'project-a', 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-extra-0', 'project-a', 'ACTIVE', 'pool-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-extra-1', 'project-a', 'ACTIVE', 'pool-a-0'); +INSERT INTO member (id, project_id, provisioning_status, pool_id) VALUES ('member-extra-2', 'project-a', 'ACTIVE', 'pool-a-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-hm-extra-0', NULL, 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-extra-0', 'project-a', 'ACTIVE', 'pool-hm-extra-0'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-hm-extra-1', NULL, 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-extra-1', 'project-a', 'ACTIVE', 'pool-hm-extra-1'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-hm-extra-2', NULL, 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-extra-2', 'project-a', 'ACTIVE', 'pool-hm-extra-2'); +INSERT INTO pool (id, project_id, provisioning_status, operating_status, enabled, protocol, lb_algorithm, load_balancer_id) VALUES ('pool-hm-extra-3', NULL, 'ACTIVE', 'ONLINE', 1, 'HTTP', 'ROUND_ROBIN', 'load_balancer-a-0'); +INSERT INTO health_monitor (id, project_id, provisioning_status, pool_id) VALUES ('health_monitor-extra-3', 'project-a', 'ACTIVE', 'pool-hm-extra-3'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-extra-0', 'project-a', 'ACTIVE', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-extra-1', 'project-a', 'ACTIVE', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-extra-2', 'project-a', 'ACTIVE', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-extra-3', 'project-a', 'ACTIVE', 'listener-a-0'); +INSERT INTO l7policy (id, project_id, provisioning_status, listener_id) VALUES ('l7policy-extra-4', 'project-a', 'ACTIVE', 'listener-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-extra-0', 'project-a', 'ACTIVE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-extra-1', 'project-a', 'ACTIVE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-extra-2', 'project-a', 'ACTIVE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-extra-3', 'project-a', 'ACTIVE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-extra-4', 'project-a', 'ACTIVE', 'l7policy-a-0'); +INSERT INTO l7rule (id, project_id, provisioning_status, l7policy_id) VALUES ('l7rule-extra-5', 'project-a', 'ACTIVE', 'l7policy-a-0'); diff --git a/tests/octavia_quotas_test.go b/tests/octavia_quotas_test.go new file mode 100644 index 0000000..be05a91 --- /dev/null +++ b/tests/octavia_quotas_test.go @@ -0,0 +1,56 @@ +//go:build integration + +package tests + +import ( + "database/sql" + "os" + "strings" + "testing" + + "github.com/stretchr/testify/require" + "github.com/vexxhost/openstackdb/internal/testutil" + octavia "github.com/vexxhost/openstackdb/octavia/db/repositories" +) + +// The fixture includes DELETED rows, NULL and empty project IDs, and cached +// in_use_* counters that both match and drift from the recount. +func TestOctaviaQuotas(t *testing.T) { + ctx := t.Context() + db := testutil.NewMySQLContainer(t, "octavia-quotas", "../sql/octavia/schema.sql") + fixture, err := os.ReadFile("fixtures/octavia_quota_usage.sql") + require.NoError(t, err) + testutil.SeedSQL(t, db, string(fixture), "INSERT INTO quotas (project_id) VALUES ('')") + r := octavia.NewQuotaRepository(db) + + usage, err := r.GetUsage(ctx) + require.NoError(t, err) + expected := map[string]int64{} + for index, resource := range strings.Fields("load_balancer listener pool member health_monitor l7policy l7rule") { + expected["project-a/"+resource] = int64(index + 4) + expected["project-b/"+resource] = 1 + } + got := map[string]int64{} + for _, row := range usage { + require.True(t, row.ProjectID.Valid) + key := row.ProjectID.String + "/" + row.Resource + require.NotContains(t, got, key) + got[key] = row.InUse + } + require.Equal(t, expected, got) + + // Limits and cached counters come back as stored. The empty-string row is + // excluded, matching GetUsage, because Octavia rejects empty project IDs. + quotas, err := r.GetAll(ctx) + require.NoError(t, err) + n := func(v int32) sql.NullInt32 { return sql.NullInt32{Int32: v, Valid: true} } + require.Equal(t, []octavia.Quota{{ + ProjectID: "project-a", + Listener: n(-1), LoadBalancer: n(0), Member: n(0), Pool: n(17), L7rule: n(23), + InUseLoadBalancer: n(4), InUseMember: n(999), + }}, quotas) + + // Drift is visible by comparing the cached counter with the recount. + require.Equal(t, int64(quotas[0].InUseLoadBalancer.Int32), got["project-a/load_balancer"]) + require.NotEqual(t, int64(quotas[0].InUseMember.Int32), got["project-a/member"]) +} diff --git a/tests/queries_test.go b/tests/queries_test.go index ee83701..f2a3542 100644 --- a/tests/queries_test.go +++ b/tests/queries_test.go @@ -276,6 +276,10 @@ func TestOctaviaQueries(t *testing.T) { require.NoError(t, err, "LoadBalancerGetAllWithVIP") _, err = q.AmphoraGetAll(ctx) require.NoError(t, err, "AmphoraGetAll") + _, err = q.QuotaGetAll(ctx) + require.NoError(t, err, "QuotaGetAll") + _, err = q.QuotaUsageGetAll(ctx) + require.NoError(t, err, "QuotaUsageGetAll") } func TestPlacementQueries(t *testing.T) { ctx := context.Background()