Skip to content

Add bazel generation of alloydb resources#16569

Merged
SirGitsalot merged 2 commits intoGoogleCloudPlatform:mainfrom
SirGitsalot:bazel
Mar 2, 2026
Merged

Add bazel generation of alloydb resources#16569
SirGitsalot merged 2 commits intoGoogleCloudPlatform:mainfrom
SirGitsalot:bazel

Conversation

@SirGitsalot
Copy link
Member

The rules introduced here are not set in stone. They're inspired by the proto rules, where there's a proto_library that lives at the top level and takes .proto files as srcs, and language-specific rules like go_proto_library that depend on it and perform codegen. Here we have mm_product which takes the product.yaml, and tpg_product which generates TPG source from it (and in the future, tgc_product etc.).

The BUILD rules need to be able to specify exact locations and names of generated files, and the current MM code assumes that it's building the standard provider tree. The changes to the compiler are to allow the rules to do that w/o disrupting the current build system.

It's still early days and this only does generation and not compilation, but you can now do:

> bazel build  //mmv1/products/alloydb/...
INFO: Analyzed 10 targets (0 packages loaded, 0 targets configured).
INFO: Found 10 targets...
INFO: Elapsed time: 0.394s, Critical Path: 0.18s
INFO: 12 processes: 1 internal, 11 darwin-sandbox.
INFO: Build completed successfully, 12 total actions
> ls -l  bazel-bin/mmv1/products/alloydb/ga/
total 608
-r-xr-xr-x  1 hawk  primarygroup    3191 Feb 27 13:29 alloydb_operation.go
-r-xr-xr-x  1 hawk  primarygroup     942 Feb 27 13:29 product.go
-r-xr-xr-x  1 hawk  primarygroup    1120 Feb 27 13:29 resource_alloydb_backup_generated_meta.yaml
-r-xr-xr-x  1 hawk  primarygroup    6972 Feb 27 13:29 resource_alloydb_backup_sweeper.go
-r-xr-xr-x  1 hawk  primarygroup   36534 Feb 27 13:29 resource_alloydb_backup.go
-r-xr-xr-x  1 hawk  primarygroup    3664 Feb 27 13:29 resource_alloydb_cluster_generated_meta.yaml
-r-xr-xr-x  1 hawk  primarygroup  123731 Feb 27 13:29 resource_alloydb_cluster.go
-r-xr-xr-x  1 hawk  primarygroup    2852 Feb 27 13:29 resource_alloydb_instance_generated_meta.yaml
-r-xr-xr-x  1 hawk  primarygroup   96767 Feb 27 13:29 resource_alloydb_instance.go
-r-xr-xr-x  1 hawk  primarygroup     471 Feb 27 13:29 resource_alloydb_user_generated_meta.yaml
-r-xr-xr-x  1 hawk  primarygroup   14868 Feb 27 13:29 resource_alloydb_user.go

@modular-magician
Copy link
Collaborator

Hi there, I'm the Modular magician. I've detected the following information about your changes:

Diff report

Your PR hasn't generated any diffs, but I'll let you know if a future commit does.

@SirGitsalot SirGitsalot requested a review from melinath February 27, 2026 22:53
@SirGitsalot SirGitsalot marked this pull request as ready for review February 27, 2026 22:53
Copy link
Member

@melinath melinath left a comment

Choose a reason for hiding this comment

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

Overall this looks good; I think taking the approach of mmv1 as library vs downstreams makes sense 👍

When I try to run bazel locally I get the error:

Error in read: java.io.FileNotFoundException: /Users/{{username}}/projects/magic-modules/go.work (No such file or directory)

I'm not blocking on it because it may be a misconfiguration on my part and this work is still in early phases, but wanted to flag it in case.

attn @zli82016 @c2thorn FYI since this will be a significant shift in how TPG / TGC generation works in the future.

attn @ScottSuarez since you were interested in this work.

@@ -241,6 +268,20 @@ func (t *Terraform) GenerateResourceSweeper(object api.Resource, templateData Te
templateData.GenerateSweeperFile(targetFilePath, object)
}

// GenerateResourceMetadataFile is used by the Bazel version of the MM compiler to generate the sweeper for
// the specified resource. It panics if the resource does not use a sweeper.
Copy link
Member

Choose a reason for hiding this comment

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

I assume the panic is going to change in the future, to support resources without sweepers?

Copy link
Member Author

Choose a reason for hiding this comment

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

Resources without sweepers are supported now, the compiler won't be called if the mm_resource does not include has_sweeper = True. This is catching the case where the rule is configured with has_sweeper = True but the resource.yaml file is configured with exclude_sweeper: true.

@@ -163,6 +165,20 @@ func (t *Terraform) GenerateResource(object api.Resource, templateData TemplateD
}
}

// GenerateResourceFile is the Bazel counterpart to GenerateResource(), generating *only() the .go file and
// taking the full path to the output file to generate rather than implicitly generating the path.
Copy link
Member

Choose a reason for hiding this comment

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

not a blocker, might be what you're planning to do next, but what would you think about making GenerateResource and friends take a full path so we can merge these implementations?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll take a look at doing it. Despite the all of the Generate* methods being exported from the package, the only one that appears to be called from outside the package is the all encompassing Generate() so it should be doable.

@SirGitsalot
Copy link
Member Author

When I try to run bazel locally I get the error:

Error in read: java.io.FileNotFoundException: /Users/{{username}}/projects/magic-modules/go.work (No such file or directory)

Yeah, that's expected. You need a go.work at the root for module resolution to function. These aren't normally checked in, and it's one of the main drivers to replace all of the various go.mod files floating around the repo with a single go.mod at the root. With that we'd be able to change this to point to that go.mod.

@SirGitsalot SirGitsalot added this pull request to the merge queue Mar 2, 2026
Merged via the queue into GoogleCloudPlatform:main with commit c2a9384 Mar 2, 2026
22 of 23 checks passed
@SirGitsalot SirGitsalot deleted the bazel branch March 2, 2026 19:18
Comment on lines +68 to +93
var product api.Product
api.Compile(*productFlag, &product)
if *productOverrideFlag != "" {
var override api.Product
api.Compile(*productOverrideFlag, &override)
api.Merge(reflect.ValueOf(product), reflect.ValueOf(override), *versionFlag)
}
if !product.ExistsAtVersionOrLower(*versionFlag) {
log.Fatalf("product %q does not support version %q", *productNameFlag, *versionFlag)
}
product.Version = product.VersionObjOrClosest(*versionFlag)

if *resourceFlag != "" {
var resource api.Resource
api.Compile(*resourceFlag, &resource)
if *resourceOverrideFlag != "" {
var override api.Resource
api.Compile(*resourceOverrideFlag, &override)
api.Merge(reflect.ValueOf(resource), reflect.ValueOf(override), *versionFlag)
}
resource.TargetVersionName = *versionFlag
resource.SetDefault(&product)
resource.Properties = resource.AddExtraFields(resource.PropertiesWithExcluded(), nil)
resource.SetDefault(&product)
product.Objects = []*api.Resource{&resource}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Might want to share the code with the loader here and expose the Load single product function. This is going to be a lot of duplicated code and difficult to maintain

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.

4 participants