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

convert resources to ResourceGenerator #5228

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

koba1t
Copy link
Member

@koba1t koba1t commented Jun 28, 2023

@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. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 28, 2023
@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch from 785a5f7 to 8a195ab Compare July 5, 2023 17:41
@koba1t
Copy link
Member Author

koba1t commented Jul 9, 2023

I have a problem with the import cycle not allowed error.
The accumurateTarget function is processing for kustomization.yaml, and I think ResourceGenerator needs exec this function due we need to process another kustomozation.yaml in the nested subdirectories.
But the target package contains the accumurateTarget function and imports the builtin package, which contains every generator code. That means if we want to call accumurateTarget in any generator plugins, that happens to cycle import for go codes and can't compile.

So Do you have any idea to fix this? I need your help resolving this code structure problem.

@k8s-ci-robot
Copy link
Contributor

This PR has multiple commits, and the default merge method is: merge.
You can request commits to be squashed using the label: tide/merge-method-squash

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.

@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch from 841e34a to b8f2659 Compare July 11, 2023 14:18
@natasha41575
Copy link
Contributor

natasha41575 commented Jul 21, 2023

@koba1t If I understand correctly, the problem is that when you create a generator for resources, the resourceGenerator is in builtins, and needs to call accumulateTarget which is in target. But to process the kustomization file properly, accumulateTarget in target needs to call the generators in builtins, resulting in the import cycle.

We may have already discussed this, but did you try moving all the files from target (all of these files) to the builtins folder, so that it's all in the same package, to avoid the dependencies? I'm not yet sure if this is the right way to go, but I would like to know if this could potentially resolve the issue (and also help to confirm my understanding of the problem).

@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch from b8f2659 to 42759d6 Compare August 10, 2023 12:11
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 10, 2023
@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch 3 times, most recently from a6b2124 to 2535291 Compare August 12, 2023 15:39
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 12, 2023
@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch 4 times, most recently from fc5268d to f17304e Compare August 12, 2023 17:01
@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch 2 times, most recently from 62aa6ce to e119b01 Compare August 30, 2023 19:29
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 30, 2023
@koba1t
Copy link
Member Author

koba1t commented Aug 30, 2023

I resolved to import cycle problem with adding member variable contain pointer of kt for PluginHelperstruct.
But I can't decide whether this is the right approach for resolving this problem.

If you have any ideas, please let me know.

@koba1t
Copy link
Member Author

koba1t commented Aug 30, 2023

@natasha41575

I have a problem with fields that affect out of the resources directory scope.
I think the effect of vars and crd fields are not limited on once accumulateTarget, but any Generator needs return ResMap type result without ResAccumulator that decided by interface and ResourceGenerator return ResMap.
ResAccumulator can contain these fields' data but the ResMap struct looks like a simple struct and I think I can't store data of these fields

So, If we need to keep current behavior, we may need some other keep of these fields. Do you have any good ideas?

@@ -459,7 +459,7 @@ resources:
t.Fatalf("Expected resource accumulation error")
}
if !strings.Contains(
err.Error(), "already registered id: StatefulSet.v1.apps/my-sts.[noNs]") {
err.Error(), `id resid.ResId{Gvk:resid.Gvk{Group:"apps", Version:"v1", Kind:"StatefulSet", isClusterScoped:false}, Name:"my-sts", Namespace:""} exists`) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This error message was changed due to every resources field in another directory being applied independently.

@@ -391,9 +392,6 @@ resources:
nameSuffix: "-a"
`),
writeC("comp-b", `
resources:
Copy link
Member Author

Choose a reason for hiding this comment

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

No longer permitted to call the same resources two times in each components due to the change to resources was transitioned to ResMap before applying components.

@natasha41575
Copy link
Contributor

natasha41575 commented Sep 8, 2023

@natasha41575

I have a problem with fields that affect out of the resources directory scope. I think the effect of vars and crd fields are not limited on once accumulateTarget, but any Generator needs return ResMap type result without ResAccumulator that decided by interface and ResourceGenerator return ResMap. ResAccumulator can contain these fields' data but the ResMap struct looks like a simple struct and I think I can't store data of these fields

So, If we need to keep current behavior, we may need some other keep of these fields. Do you have any good ideas?

If I understand the problem correctly, then I can think of a couple suggestions

  1. I think it is OK to add crds and vars to ResMap if that appears to be the cleanest solution. To do this, I think you would need to add methods to the ResMap interface to get the crds and vars, and store them in the ResWrangler.

  2. You could maybe change the generators interface to return a type ResAccumulator instead of ResMap. Then you can add the additional fields to ResAccumulator. For other generators that are not ResourceGenerator, we can leave all the other fields besides ResMap empty if needed. But I think to do this, we will need to take ResAccumulator out of api/internal, so that users can still built their own custom generators if they want to. I think we should try to avoid this though. Edited to add: If we change the generators interface, we might also have to change the transformers/validators interfaces and double check that KRM functions still work, and this might be a breaking change for legacy style functions, so maybe this solution isn't actually feasible.

  3. We can define a new wrapper around ResMap, e.g. ResMapWithGlobalData that contains a ResMap and the crds and vars data.

  4. Another potential option (though I'm not sure if this one will solve your issue) - we do already have a wrapper called GeneratorWithProperties that we use to store origin information. It is used to configure and run the generators:

    var generators []*resmap.GeneratorWithProperties
    . I'm curious if it might be possible to add the crd/vars data there, and have runGenerators return them when applicable.

  5. If it helps in conjunction with any of the other options, we can potentially run resourceGenerator separately from the other generators, e.g. have a separate configureResourceGenerator - separate from the configureBuiltinGenerator. This is a bit hacky though so we should try to avoid this if possible.

Of these options, I'm hoping that (3) or (4) will be feasible. (1) is okay too. (2) and (5) we should try to avoid if we can, but we can consider them if the other options aren't possible. I am also open to other solutions if you can come up with some better ideas.

@natasha41575
Copy link
Contributor

@koba1t I edited my comment for (2) above - I think changing the Generators interface is actually not feasible and will have a lot of consequences, so let's try to avoid that one.

@koba1t
Copy link
Member Author

koba1t commented Sep 16, 2023

Thanks for thinking of a solution and explaining it so clearly!
I'm first testing if (3) or (4) works well!

@antoooks
Copy link
Contributor

Thanks for thinking of a solution and explaining it so clearly! I'm first testing if (3) or (4) works well!

Hi everyone, just a little idea of mine. How about adding crds and vars as optional field and applying Builder pattern on ResMap interface or resWrangler struct? I think putting the function on resWrangler is more appropriate since it has higher point of view on the kustomization.

func (r ResMap) withProperties(crds []kt.Crds, vars []types.Var ) ResMap {
     r.crds = crds
     r.vars = vars
     return r
}

We can call it from Factory

m, err := newResMapFromResourceSlice(ress).withProperties(crds,vars)

Alternatively, instead of adding individual optional fields like crds and vars we can make it more scalable by bundling them under an umbrella field like properties and use variadic arguments, e.g. withProperties(properties ...properties). Let me know if I don't get it correctly and I apologise if I misinterpreted the input type 🙇 .

Ideally speaking, we should separate CRDs and Vars as its own maps and make Generators return bunch of maps :)

Example implementation: https://medium.com/@reetas/clean-ways-of-adding-new-optional-fields-to-a-golang-struct-99ae2fe9719d

@natasha41575
Copy link
Contributor

Thanks @antoooks! I think that's an interesting idea as well. @koba1t please feel free to explore that potential solution as well. It looks similar to my (3) above but I think @antoooks's idea is more elegant if it works.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 2, 2023
@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch from a38f44d to 09b3195 Compare November 29, 2023 21:05
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: koba1t

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

The pull request process is described 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

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Nov 29, 2023
@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch from 09b3195 to 594aaee Compare January 8, 2024 17:18
…unction to ResourceGenerator

fix for failing tests
@koba1t koba1t force-pushed the chore/convert_resources_to_ResourceGenerator branch from 594aaee to 823fe3f Compare January 8, 2024 17:21
@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.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 25, 2024
@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 May 25, 2024
@koba1t
Copy link
Member Author

koba1t commented May 25, 2024

/remove-lifecycle stale

@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 May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. 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/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants