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

Move code from asset syncer to pkg #2976

Merged

Conversation

gfichtenholt
Copy link
Contributor

The main goal of this PR is to address one feedback from the last PR I pushed #2915, where Michael asked:
"Is the functionality in this utils file already available in the existing codebase? If so, I'd be keen to import and re-use that?"
So this PR is addressing this by pulling out some (private) code from asset-syncer into a pkg directory where it can be used by both asset-syncer and the fluxv2 plug in, namely:

  • tar utils
  • http-client
  • helm/index
    In the future, as proceed with the implementation of fluxv2 plug-in, I expect to pull out even more code out of asset-syncer into these or new modules.
    Another small change is some code cleanup and an addition of a unit test

@gfichtenholt gfichtenholt added this to In progress in Kubeapps via automation Jun 13, 2021
@gfichtenholt gfichtenholt self-assigned this Jun 13, 2021
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.

Thank you for splitting up the code! Now the fluxv2 code looks simpler :)

log.Infof("+getHelmIndexFileFromURL(%s) 1", indexURL)
// Get the response bytes from the url
response, err := http.Get(indexURL)
func FetchDetailFromTarball(name string, chartTarballURL string, userAgent string, authz string, netClient httpclient.Client) (map[string]string, 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'd rename this fn to sth like FetchChartDetailFromTarball (to highlight it's just for chart tarballs, not any tarball) since it is now publicly available under the tarutil package

Copy link
Contributor

Choose a reason for hiding this comment

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

Idem here about documenting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do. Thanks

@@ -0,0 +1,69 @@
entries:
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: are we using files for testing elsewehere? Probably it will make sense due to the nature of the test, but I'm not 100% sure if it's the way to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, this file is also under asset syncer testdata and is used there for a bunch of tests to test asset syncer-specific features that only exist there. If/when I end up moving more code out of asset syncer into pkg utils there maybe a point where there no longer any tests left there using this file, at which point I will delete it. But not yet

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, until recently it was necessary to create test files like this and ensure your code accepts a director so you can point it to your test data during tests. More recently go added the fstest package (https://golang.org/pkg/testing/fstest/) which I've used in cmd/kubeapps-apis/server/plugins_test.go . But agree, not worth changing this at all, as part of the benefit of this actual file test data is that it's an exact copy of a subset of a real index.yaml (rather than something we construct in memory to match).

return c
}

func ChartsFromIndex(contents []byte, r *models.Repo, shallow bool) ([]models.Chart, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

In exported (at least) functions I'd like to have some documentation around it. Perhaps this PR is just about moving the existing code, but since it's now public, I think it might be worthwhile to add a couple of lines, no?

https://blog.golang.org/godoc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure will do

Do(req *http.Request) (*http.Response, error)
}

func Get(url string, cli Client, headers map[string]string) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Idem here about documenting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

Name, Body string
}

func CreateTestTarball(w io.Writer, files []TarballFile) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Idem here about documenting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

Copy link
Contributor

@absoludity absoludity left a comment

Choose a reason for hiding this comment

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

Thanks Greg. A few comments in line, see what you think but +1 either way (assuming it's not changing current functionality for the existing helm support - wasn't 100% with part of the refactor where you mention just returning the latest entry).

@@ -214,14 +232,16 @@ func (s *Server) GetPackageMeta(ctx context.Context, request *corev1.GetAvailabl
log.Infof("Found chart url: [%s]", *url)

// unzip and untar .tgz file
meta, err := fetchMetaFromChartTarball(request.AvailablePackageRef.Identifier, *url)
// TODO: userAgent, authz and netClient w/TLS config similar to asset-syncer utils.initNetClient(),
Copy link
Contributor

Choose a reason for hiding this comment

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

If you can add your gh nick to the todo (ie. // TODO (gfichtenholz): ...) it'd be helpful (as the obvious committer from git is a pain to dig for as soon as this is refactored and copied elsewhere). Same for other TODOs in this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

@@ -238,14 +258,21 @@ func (s *Server) pullChartTarball(ctx context.Context, packageRef *corev1.Availa
Resource: fluxHelmCharts,
}

// TODO: use the namespace from the request
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a TODO only because it's a pain to construct a request currently (as we're not yet mapping the namespace to the URL)? Otherwise why not just use request.Namespace or whatever the field is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nothing hard about it. Just didn't get to it yet :-) Will happen soon

// @gfichtenholt As I remember I did try a few things here. The problem is the set of supported
// fields in FieldSelector is very small. things like spec.chart are certainly not supported.
// see kubernetes/client-go#713 and https://github.com/flant/shell-operator/blob/8fa3c3b8cfeb1ddb37b070b7a871561fdffe788b/// HOOKS.md#fieldselector. Nevertheless, I made a TODO comment here just as you suggested to see
// if I can improve later
Copy link
Contributor

Choose a reason for hiding this comment

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

Personally don't think we need the history of the discussion there, I think it's enough to just state the current state - something along the lines of the ListOptions FieldSelector not supporting the fields required (and link to the relevant issue/pr comment for more context, if it's helpful).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok you got it

// passing a results channel (of string urls) to pullChartTarball, so it returns
// immediately and you wait on the results channel at the call-site, which would mean
// you could call it for 20 different charts and just wait for the results to come in
// whatever order they happen to take, rather than serially.
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. For example, state the TODO and link to more context (the gh issue comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

Comment on lines 54 to 58
//
// given a an array of bytes containg the contents of index.yaml from a helm repo it will return
// all Chart models. shallow flag controls whether only the latest version of the charts is kept
// or all versions
//
Copy link
Contributor

Choose a reason for hiding this comment

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

See the link Antonio provided... it's fine as is, but the normal convention would be for something like

Suggested change
//
// given a an array of bytes containg the contents of index.yaml from a helm repo it will return
// all Chart models. shallow flag controls whether only the latest version of the charts is kept
// or all versions
//
// ChartsFromIndex receives an array of bytes containing the contents of index.yaml from a helm repo and returns
// all Chart models from that index. The shallow flag controls whether only the latest version of the charts is returned
// or all versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

np, will make that change. Thanks

// "mariadb" version "9.3.11", "9.3.10", etc. For entry "mariadb", bitnami catalog has
// almost 200 chart versions going all the way back many years to version "2.1.4".
// So for now, let's just keep track of the latest, not to overwhelm the caller with
// all these outdated versions
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment doesn't appear to be related to the GetDeprecated below, but perhaps the following expression appending the new chart? But even there, newChart receives a helm.ChartVersions so not sure which you correctly pass, so unsure where you're using entry[0] instead, or whether you can do so here without breaking the current helm support (which expects all versions).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

disagree on this one. Comment is right after the line
for _, entry := range index.Entries { . and really applies to the entire block on code, including the if statement if entry[0].GetDeprecated() {I immediately following it. I don't believe I broke anything, except possibly the format of the log message. The rest of the code should be exactly as it is today in asset syncer. https://github.com/kubeapps/kubeapps/blob/master/cmd/asset-syncer/utils.go#L801

Copy link
Contributor

Choose a reason for hiding this comment

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

Great. Yes, I was looking at the following line (the one at the same indent), not the previous. The last sentence of the comment sounded as though you'd changed something, but I couldn't identify the change. +1

return &index, nil
}

// Takes an entry from the index and constructs a database representation of the
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Takes an entry from the index and constructs a database representation of the
// Takes an entry from the index and constructs a model representation of the

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@@ -0,0 +1,69 @@
entries:
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, until recently it was necessary to create test files like this and ensure your code accepts a director so you can point it to your test data during tests. More recently go added the fstest package (https://golang.org/pkg/testing/fstest/) which I've used in cmd/kubeapps-apis/server/plugins_test.go . But agree, not worth changing this at all, as part of the benefit of this actual file test data is that it's an exact copy of a subset of a real index.yaml (rather than something we construct in memory to match).

@gfichtenholt gfichtenholt merged commit 11c64cf into vmware-tanzu:master Jun 15, 2021
Kubeapps automation moved this from In progress to Done Jun 15, 2021
@gfichtenholt gfichtenholt deleted the move-code-from-asset-syncer-to-pkg branch June 23, 2021 12:29
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.

None yet

3 participants