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

Closes #4949: Label-based filtering for Helm via predicates #4997

Merged
merged 1 commit into from
Aug 2, 2021

Conversation

VenkatRamaraju
Copy link
Contributor

Description of the change:
Added a predicate to filter events based on labels.

Motivation for the change:
Allow Helm operators to only watch CRs with specific labels defined in watches.yaml, as mentioned in #4949.

internal/helm/controller/controller.go Outdated Show resolved Hide resolved
@estroz
Copy link
Member

estroz commented Jul 28, 2021

Needs a changelog fragment (addition) and docs in https://master.sdk.operatorframework.io/docs/building-operators/helm/reference/watches/.

@VenkatRamaraju VenkatRamaraju force-pushed the Watch-Labels branch 2 times, most recently from 1cbf0bc to 658fafa Compare July 28, 2021 20:13
@VenkatRamaraju
Copy link
Contributor Author

VenkatRamaraju commented Jul 29, 2021

Test output:

$ go test -v                                                                                         
=== RUN   TestFilterPredicate
--- PASS: TestFilterPredicate (0.27s)
=== RUN   TestHasAnnotation
--- PASS: TestHasAnnotation (0.00s)
PASS
ok  	github.com/operator-framework/operator-sdk/internal/helm/controller	0.656s

Additionally, go unit tests still seem to be failing on this PR even though they pass locally.

ok  	github.com/operator-framework/operator-sdk/internal/helm/client	5.622s	coverage: 11.8% of statements
ok  	github.com/operator-framework/operator-sdk/internal/helm/controller	5.684s	coverage: 9.7% of statements
ok  	github.com/operator-framework/operator-sdk/internal/helm/flags	4.701s	coverage: 93.8% of statements

@VenkatRamaraju VenkatRamaraju requested a review from estroz July 29, 2021 15:27
@VenkatRamaraju VenkatRamaraju changed the title Closes #4949: Label-based filtering for Helm via predicates [WIP] Closes #4949: Label-based filtering for Helm via predicates Jul 29, 2021
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 29, 2021
Copy link
Member

@estroz estroz left a comment

Choose a reason for hiding this comment

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

/lgtm

internal/helm/controller/controller_test.go Outdated Show resolved Hide resolved
Comment on lines 39 to 40
cfg, _ := config.GetConfig()
mgr, _ := manager.New(cfg, manager.Options{})
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this returns a valid manager. @VenkatRamaraju does this pass locally for you?

Copy link
Member

Choose a reason for hiding this comment

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

Ah it does not pass. I recommend refactoring the predicate parse logic into its own function and writing a test specifically for that function.

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jul 29, 2021
@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Jul 29, 2021
@VenkatRamaraju VenkatRamaraju changed the title [WIP] Closes #4949: Label-based filtering for Helm via predicates Closes #4949: Label-based filtering for Helm via predicates Jul 29, 2021
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 29, 2021
@VenkatRamaraju VenkatRamaraju requested a review from estroz July 29, 2021 18:48
if !reflect.ValueOf(selector).IsZero() {
p, err := ctrlpredicate.LabelSelectorPredicate(selector)
if err != nil {
log.Error(err, "Unable to create predicate from selector.")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
log.Error(err, "Unable to create predicate from selector.")
return fmt.Errorf("error constructing predicate from watches selector: %v", err), nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you mean return nil, fmt.Errorf("error constructing predicate from watches selector: %v", err) since the first return type is predicate and the second return type is error?

Copy link
Member

Choose a reason for hiding this comment

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

Yes sorry.

@@ -93,6 +104,21 @@ func Add(mgr manager.Manager, options WatchOptions) error {
return nil
}

// parsePredicateSelector parses the selector in the WatchOptions and creates a predicate
// that is used to filter resources based on the specified selector
func parsePredicateSelector(selector metav1.LabelSelector) ctrlpredicate.Predicate {
Copy link
Member

Choose a reason for hiding this comment

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

This function should return an error since the user will not want their operator to run if their selectors are not applied.

Suggested change
func parsePredicateSelector(selector metav1.LabelSelector) ctrlpredicate.Predicate {
func parsePredicateSelector(selector metav1.LabelSelector) (ctrlpredicate.Predicate, error) {

Comment on lines 115 to 119
} else {
return p
}
}
return nil
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else {
return p
}
}
return nil
return p, nil
}
return nil, nil

if !reflect.ValueOf(selector).IsZero() {
p, err := ctrlpredicate.LabelSelectorPredicate(selector)
if err != nil {
return nil, fmt.Errorf("Error constructing predicate from watches selector: %v", err)
Copy link
Member

Choose a reason for hiding this comment

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

Error messages must start with a lowercase character if alphabetical.

Suggested change
return nil, fmt.Errorf("Error constructing predicate from watches selector: %v", err)
return nil, fmt.Errorf("error constructing predicate from watches selector: %v", err)

Copy link
Member

@estroz estroz left a comment

Choose a reason for hiding this comment

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

One fix then LGTM

@VenkatRamaraju VenkatRamaraju force-pushed the Watch-Labels branch 2 times, most recently from 94e295b to 42a7d92 Compare July 29, 2021 22:00
@@ -0,0 +1,7 @@
entries:
- description: >
Added a filter predicate for labels based on selectors specified in watches.yaml.
Copy link
Member

Choose a reason for hiding this comment

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

You should make it clear this is for the helm-operator specifically

Copy link
Member

@fabianvf fabianvf left a comment

Choose a reason for hiding this comment

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

/lgtm

Code all looks good, changelog fragment probably needs a slight tweak though

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jul 30, 2021
Signed-off-by: Venkat Ramaraju <venky2063@gmail.com>
@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Jul 30, 2021
@jmrodri
Copy link
Member

jmrodri commented Aug 2, 2021

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Aug 2, 2021
@jmrodri jmrodri merged commit 65480f4 into operator-framework:master Aug 2, 2021
bentito pushed a commit to bentito/operator-sdk that referenced this pull request Aug 24, 2021
Signed-off-by: Venkat Ramaraju <venky2063@gmail.com>
twasyl pushed a commit to twasyl/operator-sdk that referenced this pull request Sep 3, 2021
Signed-off-by: Venkat Ramaraju <venky2063@gmail.com>
Signed-off-by: Thierry Wasylczenko <thierry.wasylczenko@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants