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

refactor(api)!: make Kubebuilder types protobuf compatible #1515

Merged
merged 24 commits into from
Mar 8, 2024

Conversation

devholic
Copy link
Contributor

@devholic devholic commented Feb 22, 2024

Resolves #708


🚨 THIS PR CONTAINS BREAKING CHANGES (NON-BACKWARD COMPATIBLE) 🚨

Background

Current implementation of converting between Go types (Kubebuilder) and Protobuf types (API Request / Response) may cause unintended bugs since the conversion code should be generated by hand.

This PR removes hand-written type conversion code by making Kubebuilder types protobuf compatible.

Changes

Now codegen target will generate intermediate protobuf definition from the Kubebuilder types using go-to-protobuf (part of kubernetes/code-generator).

Since we assume Kubebuilder structs as a source of truth, we will not use generated protobuf code from above. Instead, hack/codegen/prototag will extract protobuf tag from the generated code then inject into the Kubebuilder structs. This will help us not to write/manage protobuf tags.

Limitations

  • json support

gogo/protobuf extends google/protobuf to support various features. However these extended feature doesn't work on cross-platform environment - for example, json encoding/decoding for some messages (e.g. metav1.Time, one_of message) fails between API and UI.

To make things simple, this PR makes binary-encoded protobuf as a first-class citizen, and drops json support from API for the consistency.

Sunghoon Kang added 7 commits February 21, 2024 02:15
This commit adds a `deps-tools` target to Makefile to manage tools that
should be managed with Go. It leverages `go.mod` to pin the version of
tools.

Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Copy link

netlify bot commented Feb 22, 2024

Deploy Preview for docs-kargo-akuity-io ready!

Name Link
🔨 Latest commit c88f41b
🔍 Latest deploy log https://app.netlify.com/sites/docs-kargo-akuity-io/deploys/65ea7ad3af9b4300088a18c5
😎 Deploy Preview https://deploy-preview-1515.kargo.akuity.io
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Sunghoon Kang added 4 commits February 22, 2024 15:02
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
@devholic devholic force-pushed the devholic/#708 branch 2 times, most recently from b03b4c8 to f90390f Compare February 22, 2024 08:31
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
@devholic devholic marked this pull request as ready for review February 22, 2024 10:20
@devholic devholic requested a review from a team as a code owner February 22, 2024 10:20
@krancour krancour added this to the v0.5.0 milestone Feb 22, 2024
@krancour krancour changed the title refactor: make Kubebuilder types protobuf compatible refactor(api)!: make Kubebuilder types protobuf compatible Feb 22, 2024
.github/workflows/ci.yaml Outdated Show resolved Hide resolved
Makefile Show resolved Hide resolved
Makefile Outdated Show resolved Hide resolved
@krancour
Copy link
Member

@devholic this is awesome work!

I'm fine with everything I see here, but still want to test-drive it just to make sure I really understand how the new codegen process feels. I will do on Tuesday.

I also just want to check with @jessesuen and @gdsoumya that they have no specific objections to using only binary-encoded protobufs.

@krancour
Copy link
Member

@gdsoumya and @devholic did you connect re: the binary-only change?

@devholic
Copy link
Contributor Author

@krancour Yes, we discussed about the change and it will not be a problem if binary format used between just user client (CLI/UI) and Kargo API server :) (cc @gdsoumya)

Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Copy link

codecov bot commented Mar 4, 2024

Codecov Report

Attention: Patch coverage is 34.52381% with 110 lines in your changes are missing coverage. Please review.

Project coverage is 21.64%. Comparing base (ae411af) to head (c88f41b).

Files Patch % Lines
hack/codegen/prototag/main.go 0.00% 47 Missing ⚠️
internal/proto/codegen/ast.go 73.52% 9 Missing and 9 partials ⚠️
pkg/api/service/v1alpha1/types.go 0.00% 9 Missing ⚠️
internal/cli/cmd/get/freight.go 0.00% 3 Missing ⚠️
internal/api/list_projects_v1alpha1.go 0.00% 2 Missing ⚠️
internal/api/list_promotions_v1alpha1.go 0.00% 2 Missing ⚠️
internal/api/query_freights_v1alpha1.go 60.00% 2 Missing ⚠️
internal/cli/cmd/get/projects.go 0.00% 2 Missing ⚠️
internal/cli/cmd/get/promotions.go 0.00% 2 Missing ⚠️
internal/cli/cmd/get/stages.go 0.00% 2 Missing ⚠️
... and 17 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1515      +/-   ##
==========================================
- Coverage   30.87%   21.64%   -9.24%     
==========================================
  Files         188      187       -1     
  Lines       20162    28040    +7878     
==========================================
- Hits         6226     6068     -158     
- Misses      13652    21701    +8049     
+ Partials      284      271      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@krancour
Copy link
Member

krancour commented Mar 5, 2024

@devholic

When I test-drive this new codegen process using make hack-codegen, I end up with the following permissions issues:

(⎈|orbstack:N/A)➜  kargo git:(devholic/#708) make hack-codegen
docker build  \
 		-f Dockerfile.dev -t kargo:dev-tools .
[+] Building 0.0s (8/8) FINISHED                                                                                            docker:orbstack
 => [internal] load build definition from Dockerfile.dev                                                                               0.0s
 => => transferring dockerfile: 2.58kB                                                                                                 0.0s
 => [internal] load metadata for docker.io/library/golang:1.22.0-bookworm                                                              0.0s
 => [internal] load .dockerignore                                                                                                      0.0s
 => => transferring context: 63B                                                                                                       0.0s
 => [1/4] FROM docker.io/library/golang:1.22.0-bookworm                                                                                0.0s
 => CACHED [2/4] RUN apt update && apt install -y ca-certificates curl gnupg unzip     && export PROTOC_REL=protoc-25.3-linux-$([ $(u  0.0s
 => CACHED [3/4] RUN addgroup --gid 1000 user     && adduser --disabled-password --gecos '' --uid 1000 --gid 1000 user     && mkdir -  0.0s
 => CACHED [4/4] RUN mkdir -p /home/user/gocache                                                                                       0.0s
 => exporting to image                                                                                                                 0.0s
 => => exporting layers                                                                                                                0.0s
 => => writing image sha256:aff2c5a1bee731ba6b4ecea51392eb110c5ce4fab37feebb4074703878ea7012                                           0.0s
 => => naming to docker.io/library/kargo:dev-tools                                                                                     0.0s

View build details: docker-desktop://dashboard/build/orbstack/orbstack/lmulvk5rhtpzuwo4gtq2p1dwz

What's Next?
  View a summary of image vulnerabilities and recommendations → docker scout quickview
docker run -it --rm -v gomodcache:/home/user/gocache -v /Users/krancour/Code/akuity/kargo/:/workspaces/kargo -v /workspaces/kargo/ui/node_modules -w /workspaces/kargo kargo:dev-tools make codegen
./hack/install-tools.sh
go: downloading sigs.k8s.io/controller-tools v0.14.0
go: downloading k8s.io/code-generator v0.29.2
go: downloading golang.org/x/tools v0.16.1
go: downloading k8s.io/klog/v2 v2.120.1
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/gogo/protobuf v1.3.2
go: downloading k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01
go: downloading github.com/spf13/cobra v1.8.0
go: downloading github.com/go-logr/logr v1.4.1
go: downloading github.com/gobuffalo/flect v1.0.2
go: downloading k8s.io/apiextensions-apiserver v0.29.0
go: downloading k8s.io/apimachinery v0.29.2
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/fatih/color v1.16.0
go: downloading k8s.io/api v0.29.2
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading sigs.k8s.io/yaml v1.4.0
go: downloading golang.org/x/mod v0.14.0
go: downloading github.com/mattn/go-colorable v0.1.13
go: downloading github.com/mattn/go-isatty v0.0.20
go: downloading k8s.io/utils v0.0.0-20230726121419-3b25d923346b
go: downloading github.com/google/gofuzz v1.2.0
go: downloading sigs.k8s.io/structured-merge-diff/v4 v4.4.1
go: downloading sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd
go: downloading golang.org/x/sys v0.17.0
go: downloading gopkg.in/inf.v0 v0.9.1
go: downloading golang.org/x/net v0.21.0
go: downloading github.com/json-iterator/go v1.1.12
go: downloading github.com/modern-go/reflect2 v1.0.2
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading golang.org/x/text v0.14.0
Installing golang.org/x/tools/cmd/goimports@v0.16.1...
golang.org/x/mod@v0.14.0: verifying module: golang.org/x/mod@v0.14.0: open /go/pkg/sumdb/sum.golang.org/latest: permission denied
make: *** [Makefile:94: deps-tools] Error 1
make: *** [hack-codegen] Error 2

I know not everyone is as keen as I am about isolating work like this to a container, but especially given the large number of tools involved in code generation, I think it's very important that this work.

@krancour
Copy link
Member

krancour commented Mar 5, 2024

I am also not seeing the vendor/ dir getting cleaned up and its presence is occasionally leading me to receive warnings in VS Code about inconsistent vendoring.

Sunghoon Kang added 3 commits March 8, 2024 02:49
#1515 (comment)

Signed-off-by: Sunghoon Kang <hoon@akuity.io>
#1515 (comment)

Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
@devholic
Copy link
Contributor Author

devholic commented Mar 7, 2024

@krancour

When I test-drive this new codegen process using make hack-codegen, I end up with the following permissions issues:

I tried to fix it by giving dev-tools user permission on /go dir (3ff5df2), could you try after removing gomodcache volume?

I am also not seeing the vendor/ dir getting cleaned up and its presence is occasionally leading me to receive warnings in VS Code about inconsistent vendoring.

Fixed in 8adde4e

@krancour
Copy link
Member

krancour commented Mar 7, 2024

@devholic this works perfectly!

Slower than the old process, but a very small price to pay for no longer having to write type conversions by hand!

🔥 🔥 🔥

@devholic devholic added this pull request to the merge queue Mar 8, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Mar 8, 2024
Signed-off-by: Sunghoon Kang <hoon@akuity.io>
@devholic devholic enabled auto-merge March 8, 2024 02:41
@devholic devholic added this pull request to the merge queue Mar 8, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 8, 2024
@devholic devholic added this pull request to the merge queue Mar 8, 2024
Merged via the queue into main with commit b47454f Mar 8, 2024
15 of 16 checks passed
@devholic devholic deleted the devholic/#708 branch March 8, 2024 03:00
devholic pushed a commit that referenced this pull request Apr 2, 2024
`intstr.IntOrString` relies on `gogo/protobuf` for
JSON encoding/decoding, which is incompatible with
the protobuf dependency used in our UI.

This commit extends `IntOrString` to encode/decode
to/from JSON value to make compatible with
`gogo/protobuf`.

Related:
- #708
- #1515
devholic pushed a commit that referenced this pull request Apr 2, 2024
`intstr.IntOrString` relies on `gogo/protobuf` for
JSON encoding/decoding, which is incompatible with
the protobuf dependency used in our UI.

This commit extends `IntOrString` to encode/decode
to/from JSON value to make compatible with
`gogo/protobuf`.

Related:
- #708
- #1515

Signed-off-by: Sunghoon Kang <hoon@akuity.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return native kube API types instead of a proto conversion
3 participants