-
Notifications
You must be signed in to change notification settings - Fork 1.6k
✨(feat): add necessary changes to support applyconfiguration gen #5237
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
base: master
Are you sure you want to change the base?
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: yyewolf 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 |
|
Welcome @yyewolf! |
|
Hi @yyewolf. Thanks for your PR. I'm waiting for a github.com member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. 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-sigs/prow repository. |
525e623 to
6ce9be5
Compare
6ce9be5 to
7166a19
Compare
| @@ -0,0 +1,59 @@ | |||
| /* | |||
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.
Thank you for looking into that. 🎉
If it turns out to be a breaking change, we need to find a way to make it backwards compatible. We can’t introduce it right now.
However, if you want to go ahead and implement the changes based on how you think it should work, and then share your findings with us, we can refine it together in a follow-up.
Because of that, I wouldn’t worry about the e2e tests at this stage. Instead, focus on making the changes in a way that, when you run make generate, the testdata samples are regenerated—or at least show how a project would look with the new behavior.
| // GroupVersion is group version used to register these objects. | ||
| GroupVersion = schema.GroupVersion{Group: "{{ .Resource.QualifiedGroup }}", Version: "{{ .Resource.Version }}"} | ||
| // SchemeGroupVersion is group version used to register these objects. | ||
| SchemeGroupVersion = schema.GroupVersion{Group: "{{ .Resource.QualifiedGroup }}", Version: "{{ .Resource.Version }}"} |
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.
Why would those changes?
We did not change the values. Why would we need to change the exported variable name?
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.
The generated code for applyconfiguration uses this variable name, it's referenced here: https://github.com/kubernetes/code-generator/blob/master/cmd/applyconfiguration-gen/generators/util.go#L129
However in order to make it backward compatible, we can easily go back and do
| SchemeGroupVersion = schema.GroupVersion{Group: "{{ .Resource.QualifiedGroup }}", Version: "{{ .Resource.Version }}"} | |
| GroupVersion = schema.GroupVersion{Group: "{{ .Resource.QualifiedGroup }}", Version: "{{ .Resource.Version }}"} | |
| SchemeGroupVersion = GroupVersion |
| //nolint:lll | ||
| const typesTemplate = `{{ .Boilerplate }} | ||
| // +kubebuilder:ac:generate=true |
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.
I think the feature must be optional.
We should not promote it by default, right?
By default, people should still be working as they do today.
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.
You're right, I'll make it optional
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.
But if you want to add it in a way that we can first see it working, then we can evaluate it.
Do you have a project that uses this configuration? I’ve never tried to use it myself, so looking at an example would be helpful and could give us some ideas on how we might add it.
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.
I don't have a project that uses this configuration, however I ran the generate testdata using it: 537f54e
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.
I added the flag, made it true by default (for now so we can see the generated code) and also generated the code 👍
| {{- else if not .Resource.IsRegularPlural }} | ||
| // +kubebuilder:resource:path={{ .Resource.Plural }} | ||
| {{- else }} | ||
| // +kubebuilder:resource |
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.
What does this marker do?
Have you an example of a project using the option for we give a look?
Could you share?
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.
controller-tools uses this marker to mark the type as a resource type:
Marker: https://github.com/kubernetes-sigs/controller-tools/blob/main/pkg/applyconfiguration/gen.go#L45C1-L45C127
Help message associated: https://github.com/kubernetes-sigs/controller-tools/blob/main/pkg/applyconfiguration/gen.go#L80
camilamacedo86
left a comment
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.
That is great 🎉start.
Thank you for looking on that.
We need ApplyConfigs to be optional. It must NOT be part of the default scaffold.
The idea is:
- Add a new flag to
kubebuilder create api, for example--applyconfiguration. - Only when this flag is used, we scaffold the Makefile target and the templates.
- To do that, we add a new scaffold marker in the Makefile:
+kubebuilder:scaffold:applyconfiguration - All projects will contain this marker by default, but it is only replaced when the API is created with the flag. ( that would be a change in the Makefile template of the init command. All new project would need to have the marker for we wire or not things up )
How scaffold markers work:
https://book.kubebuilder.io/reference/markers/scaffold
When the user runs:
kubebuilder create api --applyconfiguration
we replace the marker with something like:
.PHONY: generate-applyconfigs
generate-applyconfigs: controller-gen
"$(CONTROLLER_GEN)" \
applyconfiguration:headerFile="hack/boilerplate.go.txt" \
paths="./apis/..."
Also, the scaffolds for the APIs specific files need to be conditinal. I mean, by default nothing changes and will only change when we use create api first ( for all that is common base ) and only for the specific API scaffold for all that can be unique to that api
-
Add the new flag here:
https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/golang/v4/api.go#L112-L121 -
Conditionally write the scaffold here (same as webhook logic):
https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/golang/v4/scaffolds/api.go#L101-L102
Reference for conditional scaffolding (webhooks):
https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/plugins/golang/v4/scaffolds/webhook.go#L106-L143
| if err := scaffold.Execute( | ||
| &api.Types{Force: s.force}, | ||
| &api.Group{}, | ||
| &api.Doc{}, |
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.
We should only generate the new files if the/when the flag is informed
| "$(CONTROLLER_GEN)" applyconfiguration:headerFile="hack/boilerplate.go.txt" object:headerFile={{printf "%q" .BoilerplatePath}} paths="./..." | ||
| {{- else -}} | ||
| "$(CONTROLLER_GEN)" object paths="./..." | ||
| "$(CONTROLLER_GEN)" applyconfiguration object paths="./..." |
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.
So, here we should not do this change as well.
We should not change the default scaffold to be applyconfiguration
That would mean all would only work with.
So, the first question is:
In a project, can I have both?
Apis created with applyconfiguration and others not?
OR if we use applyconfiguration all APIs will need to generate with it?
| "if set, generate the resource without prompting the user") | ||
| p.resourceFlag = fs.Lookup("resource") | ||
| fs.BoolVar(&p.options.Namespaced, "namespaced", true, "resource is namespaced") | ||
| fs.BoolVar(&p.options.GenerateApplyConfiguration, "generate-apply-configuration", true, "if set, generate applyconfiguration code for the resource") |
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.
Can it be done per API or the whole project need to be changed to adopted this?
I mean, can I have 3 APIs that uses applyconfiguration and 3 that do not use them in the same project?
Is that possible?
| @@ -0,0 +1,47 @@ | |||
| /* | |||
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.
IN the testdata we should have no changes.
We should have all projects generate as before.
See that we have test/generate.sh
So, we would need to have a new mock using it and then yes we would only have the scaffold change for the specific API created with the flag. If we cannot have for example X APIs using it and X APIs using not then the change/flag cannot be in the API and it need to be in the edit and init commands ( such as we have multi-group option )
bc37134 to
c91bfa9
Compare
I'm not sure why this issue has been closed: #3692
But I haven't seen anyone working on this.
I marked the PR as breaking though I am not sure of the semantics, since I renamedGroupVersiontoSchemeGroupVersionI feel like this is a breaking change to the generated code, even though it's not a breaking change to the CLI.I'm also working on modifying the Makefile and E2E tests which is gonna be in another PR.