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

[WIP] ✨ Generation of typed apply clients using upstream generator #818

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

JoelSpeed
Copy link
Contributor

@JoelSpeed JoelSpeed commented May 19, 2023

This integrates the upstream applyconfig generator into controller-tools to allow generation of ApplyConfig style types from custom API types.

The aim here is to provide the pointer style structs that could then be passed to an Apply style method for using server side apply.

Setting this up as a WIP PR for now, current TODO list:

  • Work out if there's any extra stuff here that's not needed
  • Work out how to test this without the hack adding cronjob testdata to the go.mod
  • Do we need OpenAPI schema/do we need the extract functions?
  • General review of code and cleanup - this was quite hacky when I started the branch
  • How would a user want this integrated into their project?

@k8s-ci-robot
Copy link
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels May 19, 2023
@k8s-ci-robot k8s-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label May 19, 2023
@JoelSpeed JoelSpeed force-pushed the applyconfig-gen-integration branch 2 times, most recently from 18594e4 to 74642c2 Compare May 19, 2023 09:38
"k8s.io/api/": "k8s.io/client-go/applyconfigurations/",
}

const importPathSuffix = "applyconfiguration"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this be configurable via a marker?

Copy link

Choose a reason for hiding this comment

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

This is now available to be customized with the marker kubebuilder:ac:output:package

enableTypeMarker = markers.Must(markers.MakeDefinition("kubebuilder:ac:generate", markers.DescribesType, false))
)

var importMapping = map[string]string{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we allow additional import mappings, I know the upstream applyconfiguration gen does

Copy link
Member

Choose a reason for hiding this comment

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

I think that makes sense, yeah (But we can do that in a follow-up)

@JoelSpeed
Copy link
Contributor Author

/test all

Need to work out how to test this, but want to see what the existing tests do with this so I can fix any failures introduced

@JoelSpeed
Copy link
Contributor Author

/test all

@JoelSpeed JoelSpeed marked this pull request as ready for review May 19, 2023 15:46
filesInMaster := make(map[string][]byte)
masterFileNames := sets.New[string]()
cronJobFS := os.DirFS(".")
masterPath := "applyconfiguration-master"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I can't change the directory to which we are generating the assets currently, this is what I came up with, create a copy that we keep checked in and then diff against that, if there are genuine changes we can update both copies.

Will look at gengo to see if there's a backwards compatible way to allow it to output to different places like OutputRule does for these generators

@JoelSpeed
Copy link
Contributor Author

/assign @alvaroaleman

@EraYaN
Copy link

EraYaN commented May 22, 2023

As an option I would love to have the Extract functions, we use them extensively in our operator (for better or for worse).

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 22, 2023
Comment on lines 198 to 170
genericArgs, _ := generatorargs.NewDefaults()
genericArgs.InputDirs = []string{root.PkgPath}
genericArgs.OutputPackagePath = filepath.Join(root.PkgPath, importPathSuffix)
genericArgs.GoHeaderFilePath = ctx.HeaderFilePath
Copy link
Member

Choose a reason for hiding this comment

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

If I see correctly this currently stores the generated files somewhere below GOPATH/src (which isn't where I have the controller-tools repo). Would be probably good to align this to how we can set output dir in other generators.

Copy link
Member

Choose a reason for hiding this comment

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

I tried to run this in Cluster API with ~ apply paths=./api/... I end up with files generated in $GOPATH/src/sigs.k8s.io/cluster-api/api/v1beta1/applyconfiguration/api/v1beta1

Copy link
Member

Choose a reason for hiding this comment

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

Not sure if it's good if we store the applyconfigurations in a sub-package of the API types. Might be a simple output:apply:dir would be better and then we can store it below there, e.g.

  • output:apply:dir is explicitly set to $GOPATH/src/sigs.k8s.io/cluster-api/applyconfigurations
  • files are then generated in e.g. $GOPATH/src/sigs.k8s.io/cluster-api/applyconfiguration/api/v1beta1

I think this would follow the same pattern as the ones in client-go. (side note: the API folders are not well structured in Cluster API)

Copy link
Member

Choose a reason for hiding this comment

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

(probably this is covered by #818 (comment) feel free to resolve)

@sbueringer
Copy link
Member

@JoelSpeed Can we get rid of the vendor folder? It's probably on my side but the performance of the file tab is pretty bad with 1700 files.

@@ -0,0 +1,46 @@
// Code generated by applyconfiguration-gen. DO NOT EDIT.

package internal
Copy link
Member

Choose a reason for hiding this comment

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

Q: Why this is generated?

Copy link

Choose a reason for hiding this comment

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

applyconfiguration-gen always outputs this. The Parser() item is used in the internal extract implementations. And that schema yaml defines what items can then be parsed I believe.

called like this in extractCrdType

internal.Parser().Type("com.domain.group.api.v1.CrdType")

@alvaroaleman
Copy link
Member

@JoelSpeed Can we get rid of the vendor folder? It's probably on my side but the performance of the file tab is pretty bad with 1700 files.

Second this, I can literally not review this through the GitHub UI

@JoelSpeed
Copy link
Contributor Author

@JoelSpeed Can we get rid of the vendor folder? It's probably on my side but the performance of the file tab is pretty bad with 1700 files.

Second this, I can literally not review this through the GitHub UI

Sorry about that, must have committed during my experimenting, dropped it now, commit history in need of a good clean though

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

For an internal project, I added the +kubebuilder:ac:generate=true comment both on the package and on my type, running controller-gen with apply paths=./pkg/api/v1/ succeeds but doesn't generate anything - any idea what I am am doing wrong?

enableTypeMarker = markers.Must(markers.MakeDefinition("kubebuilder:ac:generate", markers.DescribesType, false))
)

var importMapping = map[string]string{
Copy link
Member

Choose a reason for hiding this comment

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

I think that makes sense, yeah (But we can do that in a follow-up)

Copy link
Member

@alvaroaleman alvaroaleman left a comment

Choose a reason for hiding this comment

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

For an internal project, I added the +kubebuilder:ac:generate=true comment both on the package and on my type, running controller-gen with apply paths=./pkg/api/v1/ succeeds but doesn't generate anything - any idea what I am am doing wrong?

@sbueringer
Copy link
Member

sbueringer commented May 30, 2023

For an internal project, I added the +kubebuilder:ac:generate=true comment both on the package and on my type, running controller-gen with apply paths=./pkg/api/v1/ succeeds but doesn't generate anything - any idea what I am am doing wrong?

Potentially could be: #818 (comment)

@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 18, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: JoelSpeed
Once this PR has been reviewed and has the lgtm label, please ask for approval from alvaroaleman. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@JoelSpeed
Copy link
Contributor Author

@jakobmoellerdev Reached out to me last week and sent me a patch that we think resolves a number of the comments here.

@jakobmoellerdev Anything the reviewers need to know about what you've changed?

"k8s.io/api/": "k8s.io/client-go/applyconfigurations/",
}

const importPathSuffix = "applyconfiguration"
Copy link

Choose a reason for hiding this comment

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

This is now available to be customized with the marker kubebuilder:ac:output:package

// looking into the package and retrieving the first go file it finds, and using that as the output base.
// this is because we cannot rely on gogen calculating the correct output base.
// if we leave this empty, gogen will attempt to use GOPATH to write the files which is not wanted
genericArgs.OutputBase = filepath.Dir(root.GoFiles[0])

Choose a reason for hiding this comment

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

I think with some more invest it may be possible to resolve the directory smoother but there we issues with the output package not having correct import paths. For now this will work fine with the generation of the base directory being in the package path. For the request of having a directory outside of the package path this needs adjustment though. Open to any ideas here

Choose a reason for hiding this comment

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

I believe in general it should now work both within and outside GOPATH and should resolve the issue @sbueringer and @alvaroaleman not seeing the generated files in the right folders

Choose a reason for hiding this comment

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

I believe this file was included due to the patch, should be removed from the final version

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 20, 2023
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@erikgb
Copy link
Contributor

erikgb commented Oct 21, 2023

@JoelSpeed Do you have some bandwidth to follow up on this PR? I just added the upstream generator to one of our projects to get some progress towards SSA, and it was NOT a pleasant experience! I would love to see the ability to generate apply clients with controller-gen.

@JoelSpeed
Copy link
Contributor Author

@erikgb I'm currently waiting on a second round of review from those who previously reviewed. @jakobmoellerdev kindly continued the work and we pushed his updates to my branch, but haven't had a review since then

@jakobmoellerdev
Copy link

jakobmoellerdev commented Jan 18, 2024

New year new bump :) any chance this can get another round of review? I can resolve the merge conflict if necessary @JoelSpeed, please let me know if I should take another look

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 17, 2024
@sbueringer
Copy link
Member

/remove-lifecycle stale
/lifecycle frozen

Apologies for the further delay. Really trying to get around to it, just too much going on.

@k8s-ci-robot k8s-ci-robot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 17, 2024
@k8s-ci-robot
Copy link
Contributor

@sbueringer: The lifecycle/frozen label cannot be applied to Pull Requests.

In response to this:

/remove-lifecycle stale
/lifecycle frozen

Apologies for the further delay. Really trying to get around to it, just too much going on.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants