Skip to content

Commit

Permalink
contrib/pkg/awstagdeprovision: Allow for OR filters
Browse files Browse the repository at this point in the history
AWS docs for filters are a bit sparse, but [1] has:

  Combining search filters

  In general, multiple filters with the same key field (for example,
  tag:Name, search, Instance State) are automatically joined with OR.
  This is intentional, as the vast majority of filters would not be
  logical if they were joined with AND.  For example, you would get
  zero results for a search on Instance State=running AND Instance
  State=stopped.  In many cases, you can granulate the results by
  using complementary search terms on different key fields, where the
  AND rule is automatically applied instead.  If you search for tag:
  Name:=All values and tag:Instance State=running, you get search
  results that contain both those criteria.  To fine-tune your
  results, simply remove one filter in the string until the results
  fit your requirements.

Unfortunately, there seems to be no way to represent OR filters in
ec2's Filter structure [2].  With the approach I have here, resources
that match multiple filters will be fetched multiple times, and may
have parallel, racing delete attempts.  But we should be robust in the
face of racing delete attempt, and hopefully one of the deletes will
go through before too long ;).

It would be possible to adjust our locally-filtered types (which use
filterObjects) to avoid this issue for those types.  I may do that in
follow-up work, but for now I'm treating all of our types the same
way.

[1]: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Filtering.html#advancedsearch
[2]: https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#Filter
  • Loading branch information
wking committed Oct 19, 2018
1 parent a72a886 commit f59dd98
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions contrib/cmd/hiveutil/awstagdeprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
// NewDeprovisionAWSWithTagsCommand is the entrypoint to create the 'aws-tag-deprovision' subcommand
func NewDeprovisionAWSWithTagsCommand() *cobra.Command {
opt := &awstagdeprovision.ClusterUninstaller{}
opt.Filters = awstagdeprovision.AWSFilter{}
opt.Filters = make([]awstagdeprovision.AWSFilter, 1)
cmd := &cobra.Command{
Use: "aws-tag-deprovision KEY=VALUE ...",
Short: "Deprovision AWS assets (as created by openshift-installer) with the given tag(s)",
Expand All @@ -54,7 +54,7 @@ func NewDeprovisionAWSWithTagsCommand() *cobra.Command {

func completeAWSUninstaller(o *awstagdeprovision.ClusterUninstaller, args []string) error {
for _, arg := range args {
err := parseFilter(o.Filters, arg)
err := parseFilter(o.Filters[0], arg)
if err != nil {
return fmt.Errorf("cannot parse filter %s: %v", arg, err)
}
Expand Down
22 changes: 20 additions & 2 deletions contrib/pkg/awstagdeprovision/awstagdeprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,23 @@ type deleteFunc func(awsClient *session.Session, filters AWSFilter, clusterName

// ClusterUninstaller holds the various options for the cluster we want to delete
type ClusterUninstaller struct {
Filters AWSFilter // filter(s) we will be searching for

// Filters is a slice of filters for matching resources. A
// resources matches the whole slice if it matches any of the
// entries. For example:
//
// filter := []map[string]string{
// {
// "a": "b",
// "c": "d:,
// },
// {
// "d": "e",
// },
// }
//
// will match resources with (a:b and c:d) or d:e.
Filters []AWSFilter // filter(s) we will be searching for
Logger log.FieldLogger
LogLevel string
Region string
Expand Down Expand Up @@ -108,7 +124,9 @@ func (o *ClusterUninstaller) Run() error {

// launch goroutines
for name, function := range deleteFuncs {
go deleteRunner(name, function, awsSession, o.Filters, o.ClusterName, o.Logger, returnChannel)
for _, filter := range o.Filters {
go deleteRunner(name, function, awsSession, filter, o.ClusterName, o.Logger, returnChannel)
}
}

// wait for them to finish
Expand Down

0 comments on commit f59dd98

Please sign in to comment.