Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Added: vm filtering #458

Merged
merged 6 commits into from
Nov 11, 2019
Merged

Conversation

najeal
Copy link
Contributor

@najeal najeal commented Sep 24, 2019

This adds vm filtering to the ignite ps command with the -f or --filter flag.
We can now apply filtering on VM metadata

  • Name
  • UID
  • Labels
  • Annotations

We can use just one filter or multiple.
ex one filter
sudo ignite ps --filter "{{.Name}}=my-vm"
ex multi filters (separating them with a ,):

  • sudo ignite ps --filter "{{.Name}}=my-vm",{{.UID}}=123
  • sudo ignite ps --filter "{{.Name}}=my-vm",{{.Labels.mylabel}}=123

Fixes #228

This adds a way to create Filter objects from a given string. It also adds the filtering mechanism itself that will be used in the `ignite ps` command
This adds the filter flag to the `ignite ps` command
@najeal najeal requested a review from twelho as a code owner September 24, 2019 16:44
@chanwit chanwit requested review from chanwit and stealthybox and removed request for twelho September 25, 2019 17:17
@chanwit chanwit added area/UX Let's make Ignite's UX great! kind/enhancement Categorizes issue or PR as related to improving an existing feature. labels Sep 25, 2019
Copy link
Contributor

@stealthybox stealthybox left a comment

Choose a reason for hiding this comment

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

Great start on this -- I was able to build and start using it.
Here's some feedback from a 1st pass :)

( thanks for adding tests! )

}
metaFilterList := make([]metaFilter, 0, len(filtersInfos))
for _, fInfo := range filtersInfos {
metaFilterList = append(metaFilterList, metaFilter{identifier: fInfo["key"], expectedValue: fInfo["value"]})
Copy link
Contributor

Choose a reason for hiding this comment

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

This conversion is not needed.
extractMultipleKeyValueFiltering() can return a []metaFilter directly.

Did you mean to return a map[string]string of filterKey => value and convert that?
(I wouldn't do that if you want to support negation operators)

return match[1], match[2], nil
}

// extractMultipleKeyValueFiltering extracts all he keys and values to filter
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ he / the /

for _, filter := range filterList {
key, value, err := extractKeyValueFiltering(filter)
if err != nil {
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

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

it would be helpful to include the failing filterList here for the user

const (
filterSeparator = ","
filterApplyFailed = "failed to apply filtering"
regexString = `^(?P<key>{{(?:\.|[a-zA-Z]+)+}})=(?P<value>[a-zA-Z0-9-_]+)$`
Copy link
Contributor

@stealthybox stealthybox Sep 25, 2019

Choose a reason for hiding this comment

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

Can we capture the operator here and support = == != =~ !~?
The operator can then be stored in the metaFilter.
The filter can be executed with a case statement on the op

= ==   Equal
!=     Not Equal
=~     Contains
!~     Not Contains

Also, note that this regex does not support all valid go identifiers for the key:

# the filter can fail to compile with valid identifiers
sudo ~/Repos/ignite/bin/ignite vm ls -f '{{.Name2}}=my-test-vm'
FATA[0000] failed to generate filter 

}
matches := reg.FindAllStringSubmatch(str, -1)
if len(matches) != 1 {
return "", "", fmt.Errorf("failed to generate filter")
Copy link
Contributor

Choose a reason for hiding this comment

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

Update error to indicate filter contained more than one key + op + value

}
match := matches[0]
if len(match) != 3 {
return "", "", fmt.Errorf("failed to generate filter")
Copy link
Contributor

Choose a reason for hiding this comment

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

Update error to indicate filter was missing a key, operator, or value

@stealthybox
Copy link
Contributor

Further U/X could be done in a follow-up:

  • CLI --help Examples
  • CLI --help list of valid fields for this command, or link to API docs -- mention user can see full object with ignite inspect
  • matching on non Metadata fields:
    IMAGE, KERNEL, SIZE, CPUS, MEMORY, CREATED, STATUS, IPS, PORTS

This is also related to the U/X of #229

@najeal
Copy link
Contributor Author

najeal commented Sep 28, 2019

I think I'm ready for the next round

@stealthybox
Copy link
Contributor

Thanks so much for the persistence to get this through.
I'm playing with this locally -- the docs are very helpful.

This is a very high quality addition to ignite.

One tweak we can follow up with is to perform an OR operation when -f is passed multiple times. (this is what docker does)
We can do this by changing the flag type and running the filter multiple times.

We can also document -f '{{.ObjectMeta.UID}}=092b26ee1196a114'

This is great to merge as is 👍

@stealthybox stealthybox merged commit ba63c96 into weaveworks:master Nov 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/UX Let's make Ignite's UX great! kind/enhancement Categorizes issue or PR as related to improving an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Support to Filter VMs
3 participants