Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Adding filter options to chart-repo #658

Merged
merged 7 commits into from
Mar 4, 2020
Merged

Conversation

gregsidelinger
Copy link

Signed-off-by: Greg Sidelinger gate@ilive4code.net

Here are my basic filter changes mentioned in #657

Copy link
Member

@prydonius prydonius left a comment

Choose a reason for hiding this comment

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

Thanks @gregsidelinger, I like the idea! cc @andresmgot @absoludity

filter := new(filters)
filter.Names = append(filter.Names, "wordpress")
filter.Names = append(filter.Names, "not-found")
charts = chartsFromIndex(index, r, filter)
Copy link
Member

Choose a reason for hiding this comment

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

can you run gofmt?

Copy link
Author

Choose a reason for hiding this comment

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

done

assert.Equal(t, len(charts), 2, "number of charts")
// filter on name
Copy link
Member

Choose a reason for hiding this comment

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

could we split this up into sub-tests using t.Run? (e.g.

t.Run(tt.name, func(t *testing.T) {
, but without the for-loop)

Copy link
Author

Choose a reason for hiding this comment

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

I broke them out into separate tests but did not use a loop using r.Run

filter.Annotations["sync"] = "true"
filter.Annotations["not-found"] = "missing"
filter.Annotations["no-value"] = ""
assert.Equal(t, len(charts), 1, "number of charts")
Copy link
Member

Choose a reason for hiding this comment

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

what chart does this test pick up? I can't see any annotations in https://github.com/helm/monocular/blob/master/cmd/chart-repo/testdata/valid-index.yaml?

Copy link
Author

Choose a reason for hiding this comment

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

My bad, I was doing some cleanup and the updated test yam l did got get committed. It will be there in the next push.

@absoludity
Copy link

Thanks @gregsidelinger, I like the idea! cc @andresmgot @absoludity

Nice. Yes storing extra data for charts when they are synced, and being able to query based on that data is also something we're needing in Kubeapps (though we're now using a different codebase for sync which supports postgresql as a backend).

@helm-bot helm-bot added size/L and removed size/M labels Feb 12, 2020
if v, ok := entry[0].Annotations[a]; ok {
if len(av) == 0 {
charts = append(charts, newChart(entry, r))
continue ENTRYLOOP
Copy link
Member

Choose a reason for hiding this comment

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

I think we can avoid this and improve the readability here by splitting out these filters into separate functions. Also, there might be a bug here. What if I have a name filter and an annotation filter, only the annotation filter will hold?

Something like:

if !matchesAnnotationsFilter(filter.Annotations, entry) || !matchesNamesFilter(filter.Names, entry) {
  // log skipping chart
  continue
}

charts = append(charts, newChart(entry, r))

Those functions can return true if filter.Annotations or filter.Names is empty.

WDYT?

Copy link
Author

Choose a reason for hiding this comment

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

Done, I was thinking the same thing when using the ENTRYLOOP label but was being lazy. Since my intention was to sync if any of the filters matched I did a single function that returns true if a filter matched.

filter.Annotations["sync-by-name-only"] = ""
charts := chartsFromIndex(index, r, filter)
assert.Equal(t, len(charts), 1, "number of charts")
}
Copy link
Member

Choose a reason for hiding this comment

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

let's add a test for both an annotation and name filter

Copy link
Author

Choose a reason for hiding this comment

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

done

filter := new(filters)
filter.Annotations = make(map[string]string)
filter.Annotations["sync"] = "true"
filter.Annotations["not-found"] = "missing"
Copy link
Member

Choose a reason for hiding this comment

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

Is the expectation here that it only needs to match one of the annotations and not all of the annotations? I would have assumed that all the annotations need to match for the entry to be selected (I believe this is also how kubectl label filters work).

Copy link
Author

@gregsidelinger gregsidelinger Feb 13, 2020

Choose a reason for hiding this comment

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

Good point I did not think about that. My intend was if any of them matched it synced. That is actually how I just rewrote the code as a function.

// return true if entry matches any filter
func filterEntry( entry *helmrepo.ChartVersion, filter *filters) bool {

filter.Annotations = make(map[string]string)
filter.Annotations["sync"] = "true"
filter.Annotations["not-found"] = "missing"
filter.Annotations["sync-by-name-only"] = ""
Copy link
Member

Choose a reason for hiding this comment

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

Is this not testing the same thing as Test_chartsFromIndexFilterByAnnotationDuplicateMatches?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, filter.Annotations["sync-by-name-only"] = "" should not be there, too much copy and pasting.

if len(filter.Annotations) > 0 ||
len(filter.Names) > 0 {
if filterEntry(entry[0], filter){
charts = append(charts, newChart(entry, r))
Copy link
Member

Choose a reason for hiding this comment

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

nitpick: it might be simpler to flip this condition and continue if it enters it, and just fallback to the append on line 291 if it matches the filter. i.e. similar to the deprecation condition above

Copy link
Author

Choose a reason for hiding this comment

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

Done. I ended up adding more test data to valid-index.yaml as I removed the ! from if !filterEntry(entry[0], filter) to make the test fail and realized with only two charts of data the test would pass no mater what when filtering.

if filterEntry(entry[0], filter){
charts = append(charts, newChart(entry, r))
}
log.WithFields(log.Fields{"name": entry[0].GetName()}).Info("skipping chart as filters did not match")
Copy link
Member

Choose a reason for hiding this comment

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

This would need to be in an else block?

cmd/chart-repo/chart_repo.go Outdated Show resolved Hide resolved
cmd/chart-repo/chart_repo.go Outdated Show resolved Hide resolved
gregsidelinger and others added 6 commits February 13, 2020 20:55
Signed-off-by: Greg Sidelinger <gate@ilive4code.net>
Add filter on annoation name only

Signed-off-by: Greg Sidelinger <gate@ilive4code.net>
Updating filter tests

Signed-off-by: Greg Sidelinger <sidelig@nationwide.com>
Signed-off-by: Greg Sidelinger <gate@ilive4code.net>
Co-Authored-By: Adnan Abdulhussein <adnan@prydoni.us>
Signed-off-by: Greg Sidelinger <gate@ilive4code.net>
Co-Authored-By: Adnan Abdulhussein <adnan@prydoni.us>
Signed-off-by: Greg Sidelinger <gate@ilive4code.net>
Adding more test data

Signed-off-by: Greg Sidelinger <gate@ilive4code.net>
Signed-off-by: Greg Sidelinger <gate@ilive4code.net>
Copy link
Member

@prydonius prydonius left a comment

Choose a reason for hiding this comment

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

sorry for the delay - I was on holiday.

This looks great, can you just run gofmt one more time to ensure it's all properly formatted?

@gregsidelinger
Copy link
Author

gofmt looks happy on my end.

@prydonius
Copy link
Member

Thanks!

@prydonius prydonius merged commit bc0c3d4 into helm:master Mar 4, 2020
@prydonius
Copy link
Member

@andresmgot do you want to create a new Monocular release with this change?

@andresmgot
Copy link
Contributor

andresmgot commented Mar 5, 2020

sure, thanks for the review!

https://github.com/helm/monocular/releases/tag/v1.10.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants