-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(serverless-spark): Add serverless-spark source with list_batches tool #1690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| --- | ||
| title: "Serverless for Apache Spark" | ||
| type: docs | ||
| weight: 1 | ||
| description: > | ||
| Google Cloud Serverless for Apache Spark lets you run Spark workloads without requiring you to provision and manage your own Spark cluster. | ||
| --- | ||
|
|
||
| ## About | ||
|
|
||
| The [Serverless for Apache | ||
| Spark](https://cloud.google.com/dataproc-serverless/docs/overview) source allows | ||
| Toolbox to interact with Spark batches hosted on Google Cloud Serverless for | ||
| Apache Spark. | ||
|
|
||
| ## Available Tools | ||
|
|
||
| - [`serverless-spark-list-batches`](../tools/serverless-spark/serverless-spark-list-batches.md) | ||
| List and filter Serverless Spark batches. | ||
|
|
||
| ## Requirements | ||
|
|
||
| ### IAM Permissions | ||
|
|
||
| Serverless for Apache Spark uses [Identity and Access Management | ||
| (IAM)](https://cloud.google.com/bigquery/docs/access-control) to control user | ||
| and group access to serverless Spark resources like batches and sessions. | ||
|
|
||
| Toolbox will use your [Application Default Credentials | ||
| (ADC)](https://cloud.google.com/docs/authentication#adc) to authorize and | ||
| authenticate when interacting with Google Cloud Serverless for Apache Spark. | ||
| When using this method, you need to ensure the IAM identity associated with your | ||
| ADC has the correct | ||
| [permissions](https://cloud.google.com/dataproc-serverless/docs/concepts/iam) | ||
| for the actions you intend to perform. Common roles include | ||
| `roles/dataproc.serverlessEditor` (which includes permissions to run batches) or | ||
| `roles/dataproc.serverlessViewer`. Follow this | ||
| [guide](https://cloud.google.com/docs/authentication/provide-credentials-adc) to | ||
| set up your ADC. | ||
|
|
||
| ## Example | ||
|
|
||
| ```yaml | ||
| sources: | ||
| my-serverless-spark-source: | ||
| kind: serverless-spark | ||
| project: my-project-id | ||
| location: us-central1 | ||
| ``` | ||
|
|
||
| ## Reference | ||
|
|
||
| | **field** | **type** | **required** | **description** | | ||
| | --------- | :------: | :----------: | ----------------------------------------------------------------- | | ||
| | kind | string | true | Must be "serverless-spark". | | ||
| | project | string | true | ID of the GCP project with Serverless for Apache Spark resources. | | ||
| | location | string | true | Location containing Serverless for Apache Spark resources. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| title: "Serverless for Apache Spark" | ||
| type: docs | ||
| weight: 1 | ||
| description: > | ||
| Tools that work with Google Cloud Serverless for Apache Spark Sources. | ||
| --- |
74 changes: 74 additions & 0 deletions
74
docs/en/resources/tools/serverless-spark/serverless-spark-list-batches.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| --- | ||
| title: "serverless-spark-list-batches" | ||
| type: docs | ||
| weight: 1 | ||
| description: > | ||
| A "serverless-spark-list-batches" tool returns a list of Spark batches from the source. | ||
| aliases: | ||
| - /resources/tools/serverless-spark-list-batches | ||
| --- | ||
|
|
||
| ## About | ||
|
|
||
| A `serverless-spark-list-batches` tool returns a list of Spark batches from a | ||
| Google Cloud Serverless for Apache Spark source. It's compatible with the | ||
| following sources: | ||
|
|
||
| - [serverless-spark](../../sources/serverless-spark.md) | ||
|
|
||
| `serverless-spark-list-batches` accepts the following parameters: | ||
|
|
||
| - **`filter`** (optional): A filter expression to limit the batches returned. | ||
| Filters are case sensitive and may contain multiple clauses combined with | ||
| logical operators (AND/OR). Supported fields are `batch_id`, `batch_uuid`, | ||
| `state`, `create_time`, and `labels`. For example: `state = RUNNING AND | ||
| create_time < "2023-01-01T00:00:00Z"`. | ||
| - **`pageSize`** (optional): The maximum number of batches to return in a single | ||
| page. | ||
| - **`pageToken`** (optional): A page token, received from a previous call, to | ||
| retrieve the next page of results. | ||
|
|
||
| The tool gets the `project` and `location` from the source configuration. | ||
|
|
||
| ## Example | ||
|
|
||
| ```yaml | ||
| tools: | ||
| list_spark_batches: | ||
| kind: serverless-spark-list-batches | ||
| source: my-serverless-spark-source | ||
| description: Use this tool to list and filter serverless spark batches. | ||
| ``` | ||
|
|
||
| ## Response Format | ||
|
|
||
| ```json | ||
| { | ||
| "batches": [ | ||
| { | ||
| "name": "projects/my-project/locations/us-central1/batches/batch-abc-123", | ||
| "uuid": "a1b2c3d4-e5f6-7890-1234-567890abcdef", | ||
| "state": "SUCCEEDED", | ||
| "creator": "alice@example.com", | ||
| "createTime": "2023-10-27T10:00:00Z" | ||
| }, | ||
| { | ||
| "name": "projects/my-project/locations/us-central1/batches/batch-def-456", | ||
| "uuid": "b2c3d4e5-f6a7-8901-2345-678901bcdefa", | ||
| "state": "FAILED", | ||
| "creator": "alice@example.com", | ||
| "createTime": "2023-10-27T11:30:00Z" | ||
| } | ||
| ], | ||
| "nextPageToken": "abcd1234" | ||
| } | ||
| ``` | ||
|
|
||
| ## Reference | ||
|
|
||
| | **field** | **type** | **required** | **description** | | ||
| | ------------ | :------: | :----------: | -------------------------------------------------- | | ||
| | kind | string | true | Must be "serverless-spark-list-batches". | | ||
| | source | string | true | Name of the source the tool should use. | | ||
| | description | string | true | Description of the tool that is passed to the LLM. | | ||
| | authRequired | string[] | false | List of auth services required to invoke this tool | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| sources: | ||
| serverless-spark-source: | ||
| kind: serverless-spark | ||
| project: ${SERVERLESS_SPARK_PROJECT} | ||
| location: ${SERVERLESS_SPARK_LOCATION} | ||
|
|
||
| tools: | ||
| list_batches: | ||
| kind: serverless-spark-list-batches | ||
| source: serverless-spark-source | ||
|
|
||
| toolsets: | ||
| serverless_spark_tools: | ||
| - list_batches |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package serverlessspark | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| dataproc "cloud.google.com/go/dataproc/v2/apiv1" | ||
| "github.com/goccy/go-yaml" | ||
| "github.com/googleapis/genai-toolbox/internal/sources" | ||
| "github.com/googleapis/genai-toolbox/internal/util" | ||
| "go.opentelemetry.io/otel/trace" | ||
| "google.golang.org/api/option" | ||
| ) | ||
|
|
||
| const SourceKind string = "serverless-spark" | ||
|
|
||
| // validate interface | ||
| var _ sources.SourceConfig = Config{} | ||
|
|
||
| func init() { | ||
| if !sources.Register(SourceKind, newConfig) { | ||
| panic(fmt.Sprintf("source kind %q already registered", SourceKind)) | ||
| } | ||
| } | ||
|
|
||
| func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (sources.SourceConfig, error) { | ||
| actual := Config{Name: name} | ||
| if err := decoder.DecodeContext(ctx, &actual); err != nil { | ||
| return nil, err | ||
| } | ||
| return actual, nil | ||
| } | ||
|
|
||
| type Config struct { | ||
| Name string `yaml:"name" validate:"required"` | ||
| Kind string `yaml:"kind" validate:"required"` | ||
| Project string `yaml:"project" validate:"required"` | ||
| Location string `yaml:"location" validate:"required"` | ||
| } | ||
|
|
||
| func (r Config) SourceConfigKind() string { | ||
| return SourceKind | ||
| } | ||
|
|
||
| func (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) { | ||
| ua, err := util.UserAgentFromContext(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error in User Agent retrieval: %s", err) | ||
| } | ||
| endpoint := fmt.Sprintf("%s-dataproc.googleapis.com:443", r.Location) | ||
| client, err := dataproc.NewBatchControllerClient(ctx, option.WithEndpoint(endpoint), option.WithUserAgent(ua)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create dataproc client: %w", err) | ||
| } | ||
|
|
||
| s := &Source{ | ||
| Name: r.Name, | ||
| Kind: SourceKind, | ||
| Project: r.Project, | ||
| Location: r.Location, | ||
| Client: client, | ||
| } | ||
| return s, nil | ||
| } | ||
|
|
||
| var _ sources.Source = &Source{} | ||
|
|
||
| type Source struct { | ||
| Name string `yaml:"name"` | ||
| Kind string `yaml:"kind"` | ||
| Project string | ||
| Location string | ||
| Client *dataproc.BatchControllerClient | ||
| } | ||
|
|
||
| func (s *Source) SourceKind() string { | ||
| return SourceKind | ||
| } | ||
|
|
||
| func (s *Source) GetBatchControllerClient() *dataproc.BatchControllerClient { | ||
| return s.Client | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.