-
Notifications
You must be signed in to change notification settings - Fork 293
feat: unify networkservices api, fuzzer and mapper #5417
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
google-oss-prow
merged 2 commits into
GoogleCloudPlatform:master
from
yuwenma:networkservice
Oct 17, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/bash | ||
# 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. | ||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
REPO_ROOT="$(git rev-parse --show-toplevel)" | ||
cd ${REPO_ROOT}/dev/tools/controllerbuilder | ||
|
||
go run . generate-types \ | ||
--service google.cloud.networkservices.v1 \ | ||
--api-version "networkservices.cnrm.cloud.google.com/v1alpha1" \ | ||
--resource NetworkServicesServiceBinding:ServiceBinding | ||
|
||
go run . generate-mapper \ | ||
--service google.cloud.networkservices.v1 \ | ||
--api-version "networkservices.cnrm.cloud.google.com/v1alpha1" | ||
|
||
cd ${REPO_ROOT} | ||
dev/tasks/generate-crds | ||
|
||
go run -mod=readonly golang.org/x/tools/cmd/goimports@latest -w pkg/controller/direct/networkservices/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,111 @@ | ||
# How to Unify API Mappers and Fuzzers for Existing Resources | ||
|
||
This guide provides a step-by-step process for refactoring and unifying the API, mapper, and fuzzer for existing resources. The goal is to leverage the auto-generation tools as much as possible, reduce manual code, and ensure consistency across different API versions. | ||
|
||
## 1. Start with `generate.sh` | ||
|
||
The unification process begins with the `generate.sh` script located in the `apis/<service>/<version>` directory (e.g., `apis/dataproc/v1alpha1/`). This script is the source of truth for the auto-generation process. | ||
|
||
### Verify and Correct `generate.sh` | ||
|
||
1. **Check for Existence**: Ensure a `generate.sh` script exists for each version of the service you are unifying (e.g., `v1alpha1`, `v1beta1`). If it doesn't exist, create one using a known good example like `apis/bigquerybiglake/v1alpha1/generate.sh` as a template. | ||
|
||
2. **Verify Resource List**: Open the `generate.sh` script and check the `--resource` flags in the `go run . generate-types` command. This list should accurately reflect all the resources defined within that API version directory. Add or remove `--resource` flags as necessary. | ||
|
||
3. **Check Output Directory**: Ensure the final `go run ... goimports` command is writing to the correct controller directory, which should be `pkg/controller/direct/<service>/`. | ||
|
||
A corrected `generate.sh` for a `v1alpha1` service might look like this: | ||
|
||
```bash | ||
#!/bin/bash | ||
# ... (license header) | ||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
REPO_ROOT="$(git rev-parse --show-toplevel)" | ||
cd ${REPO_ROOT}/dev/tools/controllerbuilder | ||
|
||
go run . generate-types \ | ||
--service google.cloud.myservice.v1 \ | ||
--api-version "myservice.cnrm.cloud.google.com/v1alpha1" \ | ||
--resource MyResourceOne:ResourceOne \ | ||
--resource MyResourceTwo:ResourceTwo | ||
|
||
go run . generate-mapper \ | ||
--service google.cloud.myservice.v1 \ | ||
--api-version "myservice.cnrm.cloud.google.com/v1alpha1" | ||
|
||
cd ${REPO_ROOT} | ||
dev/tasks/generate-crds | ||
|
||
go run -mod=readonly golang.org/x/tools/cmd/goimports@latest -w pkg/controller/direct/myservice/ | ||
``` | ||
|
||
## 2. Rely on Auto-Generated Mappers | ||
|
||
The primary goal of unification is to remove as much manual mapping logic as possible and rely on the code generated by `generate-mapper`. | ||
|
||
1. **Run `generate.sh`**: Execute the script for each API version you are unifying. This will create or update the `types.mapper.go` file in the controller directory (`pkg/controller/direct/<service>/`). | ||
|
||
2. **Identify Manual Mappers**: Look for manually written mapper functions in files like `<resource>_mapper.go`. These are the functions you want to replace. | ||
|
||
3. **Replace with Generated Functions**: Update your controller and fuzzer code to call the new, auto-generated functions from `types.mapper.go` instead of the old manual ones. | ||
|
||
4. **Delete Old Code**: Once all references to the manual mapper functions have been removed, you can delete the `<resource>_mapper.go` file or remove the now-unused functions from it. | ||
|
||
## 3. Handling Multi-Version Services | ||
|
||
When a service has multiple API versions (e.g., `v1alpha1` and `v1beta1`), the mapper needs to handle potential differences in the resource schemas. | ||
|
||
### Use the `--multiversion` Flag | ||
|
||
For the newer version of the API (e.g., `v1beta1`), add the `--multiversion` flag to the `generate-mapper` command in its `generate.sh` script. | ||
|
||
Example for `apis/myservice/v1beta1/generate.sh`: | ||
```bash | ||
# ... | ||
go run . generate-mapper \ | ||
--multiversion \ | ||
--service google.cloud.myservice.v1 \ | ||
--api-version "myservice.cnrm.cloud.google.com/v1beta1" | ||
# ... | ||
``` | ||
|
||
When this flag is used, the generator will create version-specific mapping functions to avoid conflicts. The function names will have the version injected into their name, for example: | ||
* `MyResourceSpec_v1beta1_ToProto(...)` | ||
* `MyResourceSpec_v1alpha1_FromProto(...)` | ||
|
||
**Note**: If the `--multiversion` flag is *not* used (typically for services with only one version), the generated function names will **not** have a version in them (e.g., `MyResourceSpec_ToProto(...)`). | ||
|
||
## 4. Update Fuzzer and Controller Code | ||
|
||
After re-generating the mappers, you must update your fuzzer and controller files to use the correct function names. | ||
|
||
1. **Ensure Fuzzer File Exists**: Ensure each CRD being unified has a corresponding `_fuzzer.go` file. If one does not exist, create it by copying from a similar resource and adapting it. | ||
|
||
2. **Update `*_fuzzer.go`**: Go to `pkg/controller/direct/<service>/<resource>_fuzzer.go` and update the `fuzztesting.NewKRMTypedFuzzer` call to use the new generated function names. | ||
|
||
* If you used `--multiversion`, the names will be version-specific. | ||
* If you did not use `--multiversion`, the names will be generic. | ||
|
||
**Example with `--multiversion`:** | ||
```go | ||
// For the v1beta1 fuzzer | ||
f := fuzztesting.NewKRMTypedFuzzer(&pb.MyResource{}, | ||
MyResourceSpec_v1beta1_FromProto, MyResourceSpec_v1beta1_ToProto, | ||
MyResourceObservedState_v1beta1_FromProto, MyResourceObservedState_v1beta1_ToProto, | ||
) | ||
``` | ||
|
||
**Example without `--multiversion`:** | ||
```go | ||
f := fuzztesting.NewKRMTypedFuzzer(&pb.MyResource{}, | ||
MyResourceSpec_FromProto, MyResourceSpec_ToProto, | ||
MyResourceObservedState_FromProto, MyResourceObservedState_ToProto, | ||
) | ||
``` | ||
|
||
3. **Update `*_controller.go`**: Search for any remaining usages of the old function names in your controller logic (`pkg/controller/direct/<service>/<resource>_controller.go`) and update them to point to the new generated functions. | ||
|
||
By following these steps, you can systematically unify the API definitions, mappers, and fuzzers for existing resources, leading to a more maintainable and consistent codebase. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
pkg/controller/direct/networkservices/sevicebinding_mappings.go
This file was deleted.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc is in #5365
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong PR, I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be #5395