Skip to content

Add field queryProjectID to BigQueryConfiguration - #3821

Merged
ameijer merged 3 commits into
opencost:developfrom
V-3604:fix/bigquery-query-project-split
Jun 8, 2026
Merged

ameijer merged 3 commits into
opencost:developfrom
V-3604:fix/bigquery-query-project-split

Conversation

@V-3604

@V-3604 V-3604 commented May 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #3428

Description

Adds optional queryProjectID field to BigQueryConfiguration so the project that runs and is billed for BigQuery queries can be separated from the project that owns the billing-export table.
GetBigQueryClient uses QueryProjectID when set and falls back to ProjectID when empty, so existing configs keep working unchanged. Query SQL still references ProjectID.Dataset.Table for the source table.

Related Issues

Fixes #3428

User Impact

Not a breaking change. Existing configs without queryProjectID continue to use projectID for both the source table and query execution, matching previous behavior.
Operators whose service account holds bigquery.dataViewer in the data project but bigquery.jobUser in a separate billing project can now express that split in cloud-integration.json:

{
  "gcp": {
    "bigQuery": [
      {
        "projectID": "<DATA_PROJECT_ID>",
        "dataset": "detailedbilling",
        "table": "gcp_billing_export_resource_v1_...",
        "queryProjectID": "<QUERY_BILLING_PROJECT_ID>",
        "authorizer": {
          "authorizerType": "GCPWorkloadIdentity"
        }
      }
    ]
  }
}

Testing

All existing cases pass. New tests:
TestBigQueryConfiguration_Equals/different_queryProjectID exercises inequality on the new field.
TestBigQueryConfiguration_Equals/matching_config now sets QueryProjectID on both sides, exercising the equality true-path.
TestBigQueryConfiguration_JSON round-trip carries queryProjectID through Marshal → Unmarshal → Equals, covering the new UnmarshalJSON branch.

Test command and Results:

go test ./pkg/cloud/gcp/... -run TestBigQueryConfiguration -v
=== RUN   TestBigQueryConfiguration_Validate
=== RUN   TestBigQueryConfiguration_Validate/access_Key_invalid
=== RUN   TestBigQueryConfiguration_Validate/missing_configurer
=== RUN   TestBigQueryConfiguration_Validate/missing_projectID
=== RUN   TestBigQueryConfiguration_Validate/missing_dataset
=== RUN   TestBigQueryConfiguration_Validate/missing_table
=== RUN   TestBigQueryConfiguration_Validate/valid_config_GCP_Key
=== RUN   TestBigQueryConfiguration_Validate/valid_config_WorkloadIdentity
--- PASS: TestBigQueryConfiguration_Validate (0.00s)
    --- PASS: TestBigQueryConfiguration_Validate/access_Key_invalid (0.00s)
    --- PASS: TestBigQueryConfiguration_Validate/missing_configurer (0.00s)
    --- PASS: TestBigQueryConfiguration_Validate/missing_projectID (0.00s)
    --- PASS: TestBigQueryConfiguration_Validate/missing_dataset (0.00s)
    --- PASS: TestBigQueryConfiguration_Validate/missing_table (0.00s)
    --- PASS: TestBigQueryConfiguration_Validate/valid_config_GCP_Key (0.00s)
    --- PASS: TestBigQueryConfiguration_Validate/valid_config_WorkloadIdentity (0.00s)
=== RUN   TestBigQueryConfiguration_Equals
=== RUN   TestBigQueryConfiguration_Equals/matching_config
=== RUN   TestBigQueryConfiguration_Equals/missing_both_configurer
=== RUN   TestBigQueryConfiguration_Equals/missing_left_configurer
=== RUN   TestBigQueryConfiguration_Equals/different_projectID
=== RUN   TestBigQueryConfiguration_Equals/different_table
=== RUN   TestBigQueryConfiguration_Equals/different_location
=== RUN   TestBigQueryConfiguration_Equals/different_configurer
=== RUN   TestBigQueryConfiguration_Equals/missing_right_configurer
=== RUN   TestBigQueryConfiguration_Equals/different_dataset
=== RUN   TestBigQueryConfiguration_Equals/different_config
=== RUN   TestBigQueryConfiguration_Equals/different_queryProjectID
--- PASS: TestBigQueryConfiguration_Equals (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/matching_config (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/missing_both_configurer (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/missing_left_configurer (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/different_projectID (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/different_table (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/different_location (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/different_configurer (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/missing_right_configurer (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/different_dataset (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/different_config (0.00s)
    --- PASS: TestBigQueryConfiguration_Equals/different_queryProjectID (0.00s)
=== RUN   TestBigQueryConfiguration_JSON
=== RUN   TestBigQueryConfiguration_JSON/Empty_Config
{"level":"info","time":"2026-05-27T15:52:35-05:00","message":"{\"projectID\":\"\",\"dataset\":\"\",\"table\":\"\",\"excludePartitionTime\":false,\"location\":\"\",\"queryProjectID\":\"\",\"authorizer\":null}"}
=== RUN   TestBigQueryConfiguration_JSON/Nil_Authorizer
{"level":"info","time":"2026-05-27T15:52:35-05:00","message":"{\"projectID\":\"projectID\",\"dataset\":\"dataset\",\"table\":\"table\",\"excludePartitionTime\":false,\"location\":\"\",\"queryProjectID\":\"\",\"authorizer\":null}"}
=== RUN   TestBigQueryConfiguration_JSON/ServiceAccountKeyConfigurer
{"level":"info","time":"2026-05-27T15:52:35-05:00","message":"{\"projectID\":\"projectID\",\"dataset\":\"dataset\",\"table\":\"table\",\"excludePartitionTime\":false,\"location\":\"\",\"queryProjectID\":\"\",\"authorizer\":{\"authorizerType\":\"GCPServiceAccountKey\",\"key\":{\"Key\":\"Key\",\"key1\":\"key2\"}}}"}
=== RUN   TestBigQueryConfiguration_JSON/WorkLoadIdentityConfigurer
{"level":"info","time":"2026-05-27T15:52:35-05:00","message":"{\"projectID\":\"projectID\",\"dataset\":\"dataset\",\"table\":\"table\",\"excludePartitionTime\":false,\"location\":\"\",\"queryProjectID\":\"\",\"authorizer\":{\"authorizerType\":\"GCPWorkloadIdentity\"}}"}
--- PASS: TestBigQueryConfiguration_JSON (0.00s)
    --- PASS: TestBigQueryConfiguration_JSON/Empty_Config (0.00s)
    --- PASS: TestBigQueryConfiguration_JSON/Nil_Authorizer (0.00s)
    --- PASS: TestBigQueryConfiguration_JSON/ServiceAccountKeyConfigurer (0.00s)
    --- PASS: TestBigQueryConfiguration_JSON/WorkLoadIdentityConfigurer (0.00s)
=== RUN   TestBigQueryConfiguration_Key
--- PASS: TestBigQueryConfiguration_Key (0.00s)
=== RUN   TestBigQueryConfiguration_Provider
--- PASS: TestBigQueryConfiguration_Provider (0.00s)
=== RUN   TestBigQueryConfiguration_GetBillingDataDataset
--- PASS: TestBigQueryConfiguration_GetBillingDataDataset (0.00s)
=== RUN   TestBigQueryConfiguration_Sanitize
--- PASS: TestBigQueryConfiguration_Sanitize (0.00s)
=== RUN   TestBigQueryConfiguration_UnmarshalJSON_Valid
--- PASS: TestBigQueryConfiguration_UnmarshalJSON_Valid (0.00s)
=== RUN   TestBigQueryConfiguration_UnmarshalJSON_InvalidProjectID
--- PASS: TestBigQueryConfiguration_UnmarshalJSON_InvalidProjectID (0.00s)
=== RUN   TestBigQueryConfiguration_UnmarshalJSON_InvalidDataset
--- PASS: TestBigQueryConfiguration_UnmarshalJSON_InvalidDataset (0.00s)
=== RUN   TestBigQueryConfiguration_UnmarshalJSON_InvalidTable
--- PASS: TestBigQueryConfiguration_UnmarshalJSON_InvalidTable (0.00s)
=== RUN   TestBigQueryConfiguration_UnmarshalJSON_MissingAuthorizer
--- PASS: TestBigQueryConfiguration_UnmarshalJSON_MissingAuthorizer (0.00s)
=== RUN   TestBigQueryConfiguration_UnmarshalJSON_InvalidAuthorizer
--- PASS: TestBigQueryConfiguration_UnmarshalJSON_InvalidAuthorizer (0.00s)
PASS
ok      github.com/opencost/opencost/pkg/cloud/gcp      (cached)

Copilot AI review requested due to automatic review settings May 27, 2026 21:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for an optional QueryProjectID in the GCP BigQuery configuration, allowing queries to run under a different project than the dataset/table project.

Changes:

  • Added QueryProjectID to BigQueryConfiguration (JSON + sanitization + equality).
  • Updated BigQuery client creation to use QueryProjectID with fallback to ProjectID.
  • Expanded Equals tests to include QueryProjectID comparisons.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/cloud/gcp/bigqueryconfiguration.go Introduces QueryProjectID, uses it for bigquery.NewClient, and handles JSON unmarshalling/sanitization/equality.
pkg/cloud/gcp/bigqueryconfiguration_test.go Updates equality test fixtures and adds a case for differing QueryProjectID.
Comments suppressed due to low confidence (1)

pkg/cloud/gcp/bigqueryconfiguration.go:184

  • The error message references StorageConfiguration, but this is BigQueryConfiguration. This is misleading for debugging/config validation; update the message to the correct type name.
	authAny, ok := fmap["authorizer"]
	if !ok {
		return fmt.Errorf("StorageConfiguration: UnmarshalJSON: missing authorizer")
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/cloud/gcp/bigqueryconfiguration.go Resolved
Comment thread pkg/cloud/gcp/bigqueryconfiguration.go Resolved

@ameijer ameijer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copilot AI review requested due to automatic review settings June 8, 2026 14:57
@ameijer
ameijer enabled auto-merge June 8, 2026 14:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

Comment on lines +85 to +88
bqcEffective := bqc.QueryProjectID
if bqcEffective == "" {
bqcEffective = bqc.ProjectID
}
Comment on lines +102 to +106
ProjectID: bqc.ProjectID,
Dataset: bqc.Dataset,
Table: bqc.Table,
Location: bqc.Location,
QueryProjectID: bqc.QueryProjectID,
Comment on lines +181 to +185
if _, ok := fmap["queryProjectID"]; ok {
queryProjectID, err := cloud.GetInterfaceValue[string](fmap, "queryProjectID")
if err != nil {
return fmt.Errorf("BigQueryConfiguration: UnmarshalJSON: %w", err)
}
Comment on lines +187 to 191
}

authAny, ok := fmap["authorizer"]
if !ok {
return fmt.Errorf("StorageConfiguration: UnmarshalJSON: missing authorizer")
Comment on lines +404 to +405
expected: true,
},
@ameijer
ameijer added this pull request to the merge queue Jun 8, 2026
@sonarqubecloud

sonarqubecloud Bot commented Jun 8, 2026

Copy link
Copy Markdown

Merged via the queue into opencost:develop with commit ca98c34 Jun 8, 2026
16 of 17 checks passed
cgiraldo pushed a commit to Gradiant/opencost-old that referenced this pull request Jul 2, 2026
Signed-off-by: Varshith <[email protected]>
Co-authored-by: Alex Meijer <[email protected]>
Signed-off-by: Carlos Giraldo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bigquery jobUser project split

3 participants