-
Notifications
You must be signed in to change notification settings - Fork 263
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
feat: Add experimental filters to trigger describe cmd #1794
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dsimansk: 0 warnings.
In response to this:
Description
Changes
- π feat: Add cesql filter field to trigger describe cmd
Preview:
β client git:(pr/desc-cesql-filter) β kn trigger describe broker-display Name: broker-display Namespace: default Labels: eventing.knative.dev/broker=mybroker Annotations: eventing.knative.dev/creator=kubernetes-admin, eventing.knative.dev/lastModifier=ku ... Age: 12h Broker: mybroker Filter: type: dev.knative.demo.event.processed Filters (experimental): CESQL: LOWER(type) = 'my-event-type' Sink: Name: event-display Resource: Service (serving.knative.dev/v1) Conditions: OK TYPE AGE REASON
Reference
Fixes #1785
Release Note
Add CESQL filter field to `trigger describe` cmd
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #1794 +/- ##
==========================================
+ Coverage 79.87% 79.94% +0.06%
==========================================
Files 174 174
Lines 13593 13639 +46
==========================================
+ Hits 10858 10904 +46
Misses 1992 1992
Partials 743 743
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. β View full report in Codecov by Sentry. |
bb94097
to
b22cbc0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good to me overall, I agree with your comment that more unit tests would be necessary
pkg/kn/commands/trigger/describe.go
Outdated
// writeNestedFilters goes through SubscriptionsAPIFilter and writes its content accordingly | ||
func writeNestedFilters(dw printers.PrefixWriter, filter v1beta1.SubscriptionsAPIFilter) { | ||
v := reflect.ValueOf(filter) | ||
for i := 0; i < v.NumField(); i++ { | ||
field := v.Type().Field(i) | ||
fieldValue := v.Field(i) | ||
|
||
// Write if it's non-zero string, fields: CESQL | ||
if fieldValue.Kind() == reflect.String && !fieldValue.IsZero() { | ||
dw.WriteAttribute(field.Name, fieldValue.String()) | ||
} | ||
// Write map[string]string key:value pairs of field: Exact, Prefix, Suffix | ||
if fieldValue.Kind() == reflect.Map && fieldValue.Len() > 0 { | ||
for k, v := range fieldValue.Interface().(map[string]string) { | ||
dw.WriteAttribute(k, v) | ||
} | ||
} | ||
|
||
// iterate through []SubscriptionsAPIFilter of fields: All, Any | ||
if fieldValue.Kind() == reflect.Slice { | ||
for j := 0; j < fieldValue.Len(); j++ { | ||
element := fieldValue.Index(j) | ||
// Write filter field name only and create next indentation | ||
dw = dw.WriteAttribute(field.Name, "") | ||
// Call write recursively for struct SubscriptionsAPIFilter | ||
if element.Kind() == reflect.Struct { | ||
writeNestedFilters(dw, element.Interface().(v1beta1.SubscriptionsAPIFilter)) | ||
} | ||
} | ||
} | ||
|
||
// Call write recursively for struct SubscriptionsAPIFilter of field: Not | ||
if fieldValue.Kind() == reflect.Struct { | ||
writeNestedFilters(dw, fieldValue.Interface().(v1beta1.SubscriptionsAPIFilter)) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you have the best context here whether reflection is the best way to go or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intuition was that reflection will make it a bit shorter and cleaner than hardcoded fields. Plus hopefully it will be more future proof any of the fields is added/changed.
At the end it got a bit out of hand and it's not much shorter etc., but the benefit of having it a bit more dynamic hopefully stayed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be also my question, whether it wouldn't be easier to use the fields directly and make it easier to comprehend. When new fields come up, one probably needs to check whether they fit in the layout anyway. Not sure how it looks like, sorry, I'm a bit disconnected. But let me ask GPT-4 ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhuss here's the struct to show: https://github.com/knative/eventing/blob/main/pkg/apis/eventing/v1/trigger_types.go#L124
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func writeNestedFilters(dw printers.PrefixWriter, filter v1beta1.SubscriptionsAPIFilter) {
if filter.CESQL != "" {
dw.WriteAttribute("CESQL", filter.CESQL)
}
if len(filter.Exact) > 0 {
for k, v := range filter.Exact {
dw.WriteAttribute(k, v)
}
}
if len(filter.Prefix) > 0 {
for k, v := range filter.Prefix {
dw.WriteAttribute(k, v)
}
}
if len(filter.Suffix) > 0 {
for k, v := range filter.Suffix {
dw.WriteAttribute(k, v)
}
}
if len(filter.All) > 0 {
dw = dw.WriteAttribute("All", "")
for _, nestedFilter := range filter.All {
writeNestedFilters(dw, nestedFilter)
}
}
if len(filter.Any) > 0 {
dw = dw.WriteAttribute("Any", "")
for _, nestedFilter := range filter.Any {
writeNestedFilters(dw, nestedFilter)
}
}
if filter.Not != nil {
dw = dw.WriteAttribute("Not", "")
writeNestedFilters(dw, *filter.Not)
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This version doesn't entirely cut it, but I think I would prefer the simpler version that is more hardcoded as the reflect-based version, while flexible, is probably a bit over the top as I think one would need to adjust the output anyway if the filter gets more complex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll refactor it. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed revamped version with exact fields + unit test. 0e2e461
@rhuss a gentle ping, the PR is ready! :) |
@pierDipi would you mind getting back to the PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me!
/lgtm
/approve
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: dsimansk, pierDipi The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Description
Changes
Preview:
β client git:(pr/desc-cesql-filter) β kn trigger describe broker-display Name: broker-display Namespace: default Labels: eventing.knative.dev/broker=mybroker Annotations: eventing.knative.dev/creator=kubernetes-admin, eventing.knative.dev/lastModifier=ku ... Age: 12h Broker: mybroker Filter: type: dev.knative.demo.event.processed Filters (experimental): CESQL: LOWER(type) = 'my-event-type' Sink: Name: event-display Resource: Service (serving.knative.dev/v1) Conditions: OK TYPE AGE REASON
Reference
Fixes #1785
Release Note