-
Notifications
You must be signed in to change notification settings - Fork 226
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 KRM func suppport for PackageVariants #3925
Add KRM func suppport for PackageVariants #3925
Conversation
I have to admit, I am running out of creative ideas how to solve the DeepCopy issue (which was the reason why the first CI run failed). Essentially the problem is that kyaml's ResourceMeta/TypeMeta/ObjectMeta do not have DeepCopy funcs implemented on them. But since it is external and we directly embed it, we don't really have control over it. So I thought maybe it would be enough to switch from yaml.ResourceMeta to metav1.TypeMeta and metav1.ObjectMeta as these are DeepCopy compatible. You can see a draft for this in the commit "Switch ktpfile from yaml metav1 Typemeta and Objectmeta". However then the issue is that kyaml handles fields that are missing the At this point, there are only two ideas I have left: Maybe I am missing something obvious, please let me know if you can think of a more elegant solution to fix this. I am all for it :) If not I would proceed with option propose to proceed with option b). |
You can feel free to make this change in kyaml if you'd like. I agree that this is seemingly the most elegant solution. I can review and approve that PR when you make it. Assuming that it is a small and noninvasive change to kyaml, it should be fine. |
So I think the easiest solution here is to just use kyaml to parse and update the Kptfile. It does provide some more control over how each field is added to yaml, it makes sure the formatting is consistent (since other kpt functionality that updates the Kptfile uses kyaml), and it makes sure any comments in the Kptfile is preserved. It is not quite as convenient as reading the content into a go struct. We can look into whether that is possible, but we could try to do that separately from this change. |
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.
Thanks Simon, this is a great start. The main thing that needs to be fixed is to make this idempotent. The first thing that should be done is to remove all the functions added by this PV in past reconciliations. Then you can add them back in. Details are here:
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
@SimonTheLeg I want to add a warning that sometimes upgrading the kyaml dependency in kpt can be a chore (and probably should go in a separate PR), so while you are waiting for that, I think @mortent's suggestion in #3925 (comment) is a good way to work around it for now. |
1c3aa4e
to
b7681b5
Compare
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 this will be WAY easier using KubeObject. See the comments.
porch/controllers/packagevariants/api/v1alpha1/packagevariant_types.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
Note: I found a bit of a bug where errors in "applyMutation" are not really propagated properly. Or they are, but they are overwritten be subsequent incorrect reconciliations (because findAndUpdate was written before applyMutations, for just doing packageupdates, and I didn't correctly update it when I implemented applyMutations). I will be fixing it in #3939. I mention it here in case you come across it. You can see it when doing manual/e2e tests. If you focus on your unit tests and get those working, you should be OK. |
b7681b5
to
84b54f4
Compare
e23deca
to
098871e
Compare
alright, everything should be in order now and I have rebased the PR to make it easier to review. |
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.
Awesome, looking pretty good.
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
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.
Awesome, looking pretty good.
11082cb
to
ab8a5d6
Compare
5e70ed7
to
2107b37
Compare
okay now everything should be resolved and ready for review |
porch/controllers/packagevariants/api/v1alpha1/packagevariant_types.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Outdated
Show resolved
Hide resolved
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Show resolved
Hide resolved
repo: deployments | ||
package: bar | ||
pipeline: | ||
`[1:] |
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.
Is this to remove the leading line break? If so, it is probably easier to just use strings.TrimSpace
: https://pkg.go.dev/strings#TrimSpace
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.
Yes normally strings.TrimSpace works for this. Unfortunately it does not work in our case, as the second line is indented with spaces. This will result in trimming out the spaces of that line:
func Test(t *testing.T) {
s := `
second-line indented:
more indented:
`
fmt.Println(strings.TrimSpace(s))
fmt.Println("------")
fmt.Println(s[1:])
}
// will produce
second-line indented:
more indented:
------
second-line indented:
more indented:
But I am also going to look into the idea of your other comment to make use of kyaml, which maybe makes this obsolete all together.
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.
Not a blocker for this PR. It is just a bit difficult to read with the indentation.
}{ | ||
"add one mutator with existing mutators": { | ||
initialPipeline: ` | ||
mutators: |
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.
Would it be easier to construct the combined yaml manifests here with the yaml libraries (like kyaml) instead of string manipulation? The indentation here is a bit tricky to follow.
porch/controllers/packagevariants/pkg/controllers/packagevariant/packagevariant_controller.go
Show resolved
Hide resolved
repo: deployments | ||
package: bar | ||
pipeline: | ||
`[1:] |
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.
Not a blocker for this PR. It is just a bit difficult to read with the indentation.
* add deepcopy for kptfile funcs and selectors * add KRM func suppport for PackageVariants * add handling for removed mutators/validators * remove comment * allow for mutator/validator name to be empty * switch to getFileKubeObject utility func * edit validators/mutators in single loop * document functionality of ensureKRMFunctions * fix formatting after rebase * add deleting existin pv mutators/validators and pruning of pipeline field * Fix failing PVS unit test --------- Co-authored-by: John Belamaric <jbelamaric@google.com>
This PR adds the KRM func support discussed in https://github.com/johnbelamaric/kpt/blob/pvs-design/docs/design-docs/08-package-variant.md for PackageVariants.
Fixes: nephio-project/nephio#117
Outdated Review Notes
Couple of notes for the reviewer(s) from my side:
Function
struct type ofkptfile.v1
is being used inside PackageVariantSpec. Because PackageVariantSpec is a Kubernetes Object, I think we also need to have DeepCopy on all of its subfields. As a result I believe we need DeepCopy on theFunction
struct type. For now I have just enabled it on the package level. PTAL if that is fine. Another option is to just have it on the Func type and its fields. But then we need to be super careful whenever we use a new struct to not forget the Kubebuilder annotation.add none with existing
testcase.In practice I don't think it makes a difference. The only good solution I was able to come up with would be to have custom Marshaller/Unmarshallers on the PackageRevisionResources that uses then the kyaml as a preprocessor. But I think this is not necessary. Please let me know what you think.
Those are my thoughts so far, let me know if there's anything else :)