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

First step for flux plugin: switch to using flux native data types instead of unstructured #4112 #4175

Merged

Conversation

gfichtenholt
Copy link
Contributor

@gfichtenholt gfichtenholt commented Jan 27, 2022

This is the first step toward implementing flux plugin: switch to using flux native data types instead of unstructured #4112 . In this PR I am using Flux's native data types up until the point I need to interact with k8s API server, which is done via dynamic.Interface so requires me to convert to/from unstructured. This should bring flux plug-in up-to parity with kapp-controller plugin in terms of handling native types and conversion to/from unstructured.

Next step for me will be getting rid of the dependency on dynamic.Interface probably in favor of https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.11.0/pkg/client#Client so I don't have to convert back and forth between unstructured and typed and just pass the typed objects all the way through the entire stack.
I am a little exhausted at the moment. This first step was very arduous and pain-staking, so I would like to take a little break, switch gears for a little while and work on other outstanding issues before coming back to do the next step

I am not sure who to ask to review this. Antonio can do this, since he's done similar work in kapp-controlller plugin. Michael is welcome to review as well. I always look forward to his feedback

@gfichtenholt gfichtenholt self-assigned this Jan 27, 2022
@gfichtenholt gfichtenholt added this to In progress in Kubeapps via automation Jan 27, 2022
Copy link
Contributor

@antgamdia antgamdia left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the effort refactoring tons of lines. Tedious, but really worthy for devs.
+1, but before merging, please take into account this comment below:

It seems you (I mean, the IDE/git/whatever) reverted back somehow the changes in the code-generator submodule. I guess playing around with git submodule update --remote or similar commands should make the trick.

I personally also use Gitkraken and this is what you should view (note it is in v1.22):
image
If using vscode, you can get this info in the repo view (again, note the 1.22.6 branch)
image
So, please, ensure your PR is not changing the code under code-generator. Otherwise, we will be changing alternatively those files in each PR (mine in 1.22, yours in 1.18, back and forth :P)

}

// this is effectively a cache SET operation
func (c *NamespacedResourceWatcherCache) onAddOrModify(checkOldValue bool, unstructuredObj map[string]interface{}) (err error) {
func (c *NamespacedResourceWatcherCache) onAddOrModify(checkOldValue bool, unstructuredObj unstructured.Unstructured) (err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking forward to Go 1.18 and start using generics

}
}

func NamespacedNameForUnstructured(unstructuredObj map[string]interface{}) (*types.NamespacedName, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if using the built-in decoder in k8s UniversalDecoder() + DecodeInto(...) could simplify the logic [1]. I mean, I feel doing things like foo, found, err := unstructured.NestedString(unstructuredObj, "my", "path", "to, "foo") is not the best approach, but I don't have a more precise suggestion atm.

[1] https://stackoverflow.com/a/63338697

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wouldn't worry about it. This function is only called once in the entire code and even this 1 usage will go away (and the function foo), when I am done with the next step

Comment on lines 26 to 31
// TODO: (gfichtenholt) we might want to consider some kind of
// type Clients struct {
// kubernetes.Interface
// dynamic.Interface
// apiext.Interface
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, good idea. As we continue to add more and more clients, it's becoming quite misleading just to rely on the number of returned items (especially because of the differences between every plugin).

@@ -106,7 +117,7 @@ func NewBackgroundClientGetter() ClientGetterWithApiExtFunc {
func clientGetterHelper(config *rest.Config) (kubernetes.Interface, dynamic.Interface, apiext.Interface, error) {
typedClient, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, nil, status.Errorf(codes.FailedPrecondition, "unable to get typed client : %v", err)
return nil, nil, nil, status.Errorf(codes.FailedPrecondition, "unable to get typed client dur to: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

typo?

Suggested change
return nil, nil, nil, status.Errorf(codes.FailedPrecondition, "unable to get typed client dur to: %v", err)
return nil, nil, nil, status.Errorf(codes.FailedPrecondition, "unable to get typed client due to: %v", err)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, will fix

go.mod Outdated
Comment on lines 89 to 93
k8s.io/api v0.23.0
k8s.io/apiextensions-apiserver v0.23.1
k8s.io/apimachinery v0.23.1
k8s.io/cli-runtime v0.22.6
k8s.io/client-go v0.22.6
k8s.io/client-go v0.23.0
Copy link
Contributor

Choose a reason for hiding this comment

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

As I mentioned in one of my PRs, it is just an automatic update when running go mod tidy (or similar) commands. However, given that we have fixed the version to 0.22.6 on the replace section, the effective dependency is not being upgraded though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure if you're asking me to do something here?

@@ -353,123 +377,121 @@ func (s *Server) newRelease(ctx context.Context, packageRef *corev1.AvailablePac
var values map[string]interface{}
if valuesString != "" {
values = make(map[string]interface{})
err = yaml.Unmarshal([]byte(valuesString), &values)
if err != nil {
if err = json.Unmarshal([]byte(valuesString), &values); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious, why did you replace the yaml dep with the json one here (and in updateRelease(...))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't remember exact reason. It's been a few days. This business with "Values" took especially long due to how Flux expects it (some kind of v1.JSON) and how we handle it. So somewhere in the (very long debugging process) I arrived at this. Possibly, I saw when I was working on this "values" business that it was JSON I was unmarshaling not YAML and to use encoding/json was clearer and it also happened to work :-)

Comment on lines +525 to +542
func (s *Server) newFluxHelmRelease(chart *models.Chart, targetName types.NamespacedName, versionRef *corev1.VersionReference, reconcile *corev1.ReconciliationOptions, values map[string]interface{}) (*helmv2.HelmRelease, error) {
fluxRelease := &helmv2.HelmRelease{
TypeMeta: metav1.TypeMeta{
Kind: helmv2.HelmReleaseKind,
APIVersion: helmv2.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: targetName.Name,
Namespace: targetName.Namespace,
},
Spec: helmv2.HelmReleaseSpec{
Chart: helmv2.HelmChartTemplate{
Spec: helmv2.HelmChartTemplateSpec{
Chart: chart.Name,
SourceRef: helmv2.CrossNamespaceObjectReference{
Name: chart.Repo.Name,
Kind: sourcev1.HelmRepositoryKind,
Namespace: chart.Repo.Namespace,
Copy link
Contributor

Choose a reason for hiding this comment

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

Excelsior! Way much better than before

@gfichtenholt
Copy link
Contributor Author

Thanks a lot for the effort refactoring tons of lines. Tedious, but really worthy for devs. +1, but before merging, please take into account this comment below:

Thanks Antonio

It seems you (I mean, the IDE/git/whatever) reverted back somehow the changes in the code-generator submodule. I guess playing around with git submodule update --remote or similar commands should make the trick.

argh, I haven't had time to figure out why this keeps on happening, but I will make sure this PR doesn't have these changes

@gfichtenholt
Copy link
Contributor Author

Thanks a lot for the effort refactoring tons of lines. Tedious, but really worthy for devs. +1, but before merging, please take into account this comment below:

Thanks Antonio

It seems you (I mean, the IDE/git/whatever) reverted back somehow the changes in the code-generator submodule. I guess playing around with git submodule update --remote or similar commands should make the trick.

argh, I haven't had time to figure out why this keeps on happening, but I will make sure this PR doesn't have these changes

Update: almost got it. I tried a couple of commands, including the one you sent and well as https://stackoverflow.com/questions/39459467/remove-a-modified-file-from-pull-request

and it got rid of everything except 1 file:
Screen Shot 2022-01-28 at 10 17 00 AM

Any advice how to remove this from my PR would be appreciated

@gfichtenholt gfichtenholt merged commit 1c23836 into vmware-tanzu:main Jan 31, 2022
Kubeapps automation moved this from In progress to Done Jan 31, 2022
@gfichtenholt gfichtenholt deleted the further-fluxv2-plugin-features-21 branch January 31, 2022 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Kubeapps
  
Done
Development

Successfully merging this pull request may close these issues.

flux plugin: switch to using flux native data types instead of unstructured
2 participants