Skip to content
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

Add codegen/build-clients.sh script, update codegen/apis submodule #49

Merged
merged 8 commits into from
Jul 23, 2024

Conversation

austin-denoble
Copy link
Contributor

@austin-denoble austin-denoble commented Jul 22, 2024

Problem

We're getting ready to release v1.0.0 of the Go SDK. Before we do that, we need to make sure we're generating our control and data plane client code from the proper specifications. When the go-pinecone repo was originally set up, the apis submodule was created and points to a private, Go-specific cut of the OpenAPI and proto files from about 4 months ago.

Since then we're made changes to how we approach client generation, and where our upstream spec files live.

Solution

  • Add new codegen/ folder to hold both the apis submodule and the build-clients.sh script.
  • Update .gitmodules to move apis -> codegen/apis.
  • Add a build-clients.sh script. Originally, just gen called a few shell commands for generating from OpenAPI and protos. The new script handles generation in a fashion similar to our other repos, and allows specifying a specific API version.
  • Update client.go / client_test.go to handle some changes to underlying types.

My approach was based on the work @jhamon has done in other repos to align our code generation pipelines.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Infrastructure change (CI configs, etc)
  • Non-code change (docs, etc)
  • None of the above: (explain here)

Test Plan

There weren't changes needed to the actual SDK interface. Make sure CI runs and our unit & integration tests are passing as expected.


…d-clients.sh script for handling building control and data plane clients using a specific version of the API spec, update client.go to match generated internals
Comment on lines +1 to +3
[submodule "codegen/apis"]
path = codegen/apis
url = [email protected]:pinecone-io/apis.git
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moving to new submodule location, and swapping the underlying repo.

@@ -0,0 +1,70 @@
#!/bin/bash
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was modeling this after the work @jhamon had done in other repos, but this diverges a bit in that I'm not using a docker image, and we're doing both OAS and proto files in here.

Let me know if this doesn't make sense - or any other feedback. I didn't want to make too many changes to the dependencies we were using to generate code.

Comment on lines +28 to +32
echo "Verifying spec version has been provided: $version"
if [ -z "$version" ]; then
echo "Version is required"
exit 1
fi
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should maybe do some actual checking against the folder names under /apis/_build/ maybe. 🤔


oapi-codegen --package=control \
--generate types,client \
"${oas_file}" > "${control_destination}/control_plane.oas.go"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a version in the proto / data plane output, maybe should have that here too.

@@ -3,7 +3,6 @@ module github.com/pinecone-io/go-pinecone
go 1.21

require (
github.com/golang/protobuf v1.5.4
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran go mod tidy locally and this was removed... I think the google.golang.org/protobuf v1.34.1 below covers it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we should prefer google.golang.org according to https://protobuf.dev/reference/go/faq/.

justfile Outdated
go install golang.org/x/tools/cmd/godoc@latest
gen:
protoc --experimental_allow_proto3_optional --proto_path=apis/proto --go_opt=module="github.com/pinecone-io/go-pinecone" --go-grpc_opt=module="github.com/pinecone-io/go-pinecone" --go_out=. --go-grpc_out=. apis/proto/pinecone/data/v1/vector_service.proto
oapi-codegen --package=control --generate types,client apis/openapi/control/v1/control_v1.yaml > internal/gen/control/control_plane.oas.go
./codegen/build-clients.sh "2024-07"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling the script with version from here, we could set a global "API_VERSION" constant somewhere and use that here and elsewhere maybe?

Copy link
Contributor

Choose a reason for hiding this comment

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

just can also have variables: https://just.systems/man/en/chapter_29.html

Shards *control.PodSpecShards `json:"shards,omitempty"`
SourceCollection *string `json:"source_collection,omitempty"`
}{
req.Spec = control.IndexSpec{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Generated anonymous type cleaned up a bit, which is nice.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is nice. The previous impl was unsightly.

@austin-denoble austin-denoble marked this pull request as ready for review July 22, 2024 20:32
@@ -19,11 +19,10 @@ test-unit:
bootstrap:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/[email protected]
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest
go install golang.org/x/tools/cmd/godoc@latest
gen:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

just gen now requires just bootstrap so the script has the right dependencies to call... should it call bootstrap itself or is unexpectedly installing dependencies weird?

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, that would be weird. bootstrap shouldn't be a requirement for just gen

Copy link
Contributor

@jhamon jhamon left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me, assuming tests are passing with the newly built output. Is the version being set in X-Pinecone-Api-Version header for control plane actions? For some other languages, I had to augment the generated code by writing the version number somewhere (see python and ts) and then setting up that custom header. The generated java included the version number, but still required some effort to hook it up into headers properly. So it's worth double checking that is being done correctly after this refactoring.

@austin-denoble
Copy link
Contributor Author

Is the version being set in X-Pinecone-Api-Version header for control plane actions? For some other languages, I had to augment the generated code by writing the version number somewhere (see python and ts) and then setting up that custom header.

Good question, I was going to implement the header in a follow up PR, but given what you mentioned I should probably just do it here. Thanks for the heads up, I had wrongly assumed the version number was written in as part of the spec build process and wasn't considering it.

Copy link
Contributor

@haruska haruska left a comment

Choose a reason for hiding this comment

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

Minor comments. Looks good!

.gitignore Outdated
@@ -120,3 +120,4 @@ modules.xml

go.work*
.env
.DS_Store
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably just add this to your dev machine as a global gitignore (similar to .idea, .swp files, etc)

@@ -3,7 +3,6 @@ module github.com/pinecone-io/go-pinecone
go 1.21

require (
github.com/golang/protobuf v1.5.4
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, we should prefer google.golang.org according to https://protobuf.dev/reference/go/faq/.

@@ -19,11 +19,10 @@ test-unit:
bootstrap:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/[email protected]
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest
go install golang.org/x/tools/cmd/godoc@latest
gen:
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, that would be weird. bootstrap shouldn't be a requirement for just gen

@@ -19,11 +19,10 @@ test-unit:
bootstrap:
go install google.golang.org/protobuf/cmd/[email protected]
go install google.golang.org/grpc/cmd/[email protected]
go install github.com/oapi-codegen/oapi-codegen/v2/cmd/[email protected]
Copy link
Contributor

Choose a reason for hiding this comment

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

We pinned this to a version to avoid unnecessary codegen updates. It's fine to update but maybe a specific version instead of latest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch, I mean to pin to v2.3.0 which is the version they swapped to the new github.com location.

justfile Outdated
go install golang.org/x/tools/cmd/godoc@latest
gen:
protoc --experimental_allow_proto3_optional --proto_path=apis/proto --go_opt=module="github.com/pinecone-io/go-pinecone" --go-grpc_opt=module="github.com/pinecone-io/go-pinecone" --go_out=. --go-grpc_out=. apis/proto/pinecone/data/v1/vector_service.proto
oapi-codegen --package=control --generate types,client apis/openapi/control/v1/control_v1.yaml > internal/gen/control/control_plane.oas.go
./codegen/build-clients.sh "2024-07"
Copy link
Contributor

Choose a reason for hiding this comment

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

just can also have variables: https://just.systems/man/en/chapter_29.html

x := minOne(req.Replicas)
return &x
return x
Copy link
Contributor

Choose a reason for hiding this comment

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

is assignment and explicit return necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, lemme clean this up - I ran through here too quick fixing the initial build.

Shards *control.PodSpecShards `json:"shards,omitempty"`
SourceCollection *string `json:"source_collection,omitempty"`
}{
req.Spec = control.IndexSpec{
Copy link
Contributor

Choose a reason for hiding this comment

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

This is nice. The previous impl was unsightly.

@austin-denoble
Copy link
Contributor Author

The generated java included the version number, but still required some effort to hook it up into headers properly. So it's worth double checking that is being done correctly after this refactoring.

Added a generate_version_file() function to the build-clients.sh script. This outputs whichever API version was used to generate the code into a Go file as a constant, which is then imported and used for sending the X-Pinecone-Api-Version with requests.

@austin-denoble austin-denoble merged commit be9fe92 into main Jul 23, 2024
3 checks passed
@austin-denoble austin-denoble deleted the adenoble/update-api-submodule branch July 23, 2024 15:44
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.

3 participants