-
Notifications
You must be signed in to change notification settings - Fork 67
hooks: improve logging for hooks (#2208) #2373
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,8 +36,14 @@ func getResourcesUsingNameSelector(r client.Reader, hook *kubeobjects.HookSpec, | |
| if isValidK8sName(hook.NameSelector) { | ||
| // use nameSelector for Matching field | ||
| objs, err := getObjectsUsingValidK8sName(r, hook, objList) | ||
| if err != nil { | ||
| return ValidNameSelector, filteredObjs, fmt.Errorf( | ||
| "list resources using valid nameSelector=%q failed (hook=%q namespace=%q): %w", | ||
| hook.NameSelector, hook.Name, hook.Namespace, err, | ||
| ) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this file you are using another pattern to return error: I mean after "using nameSelector" you are printing hook nameselector and after a colon - real error. |
||
|
|
||
| return ValidNameSelector, objs, err | ||
| return ValidNameSelector, objs, nil | ||
| } else if isValidRegex(hook.NameSelector) { | ||
| // after listing without the fields selector, match with the regex for filtering | ||
| listOps := &client.ListOptions{ | ||
|
|
@@ -46,13 +52,19 @@ func getResourcesUsingNameSelector(r client.Reader, hook *kubeobjects.HookSpec, | |
|
|
||
| err = r.List(context.Background(), objList, listOps) | ||
| if err != nil { | ||
| return RegexNameSelector, filteredObjs, err | ||
| return RegexNameSelector, filteredObjs, fmt.Errorf( | ||
| "list resources using regex nameSelector=%q failed (hook=%q namespace=%q): %w", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error listing resources... would be good. |
||
| hook.NameSelector, hook.Name, hook.Namespace, err, | ||
| ) | ||
| } | ||
|
|
||
| return RegexNameSelector, getObjectsBasedOnTypeAndRegex(objList, hook.NameSelector), nil | ||
| } | ||
|
|
||
| return InvalidNameSelector, filteredObjs, fmt.Errorf("nameSelector is neither distinct name nor regex") | ||
| return InvalidNameSelector, filteredObjs, fmt.Errorf( | ||
| "invalid nameSelector=%q (must be valid k8s name or regex)", | ||
| hook.NameSelector, | ||
| ) | ||
| } | ||
|
|
||
| func getObjectsUsingValidK8sName(r client.Reader, hook *kubeobjects.HookSpec, | ||
|
|
@@ -64,7 +76,10 @@ func getObjectsUsingValidK8sName(r client.Reader, hook *kubeobjects.HookSpec, | |
|
|
||
| err := r.List(context.Background(), objList, listOps) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error listing resources using nameSelector: %w", err) | ||
| return nil, fmt.Errorf( | ||
| "list resources using nameSelector=%q failed (hook=%q namespace=%q): %w", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error listing resources... would be good. |
||
| hook.NameSelector, hook.Name, hook.Namespace, err, | ||
| ) | ||
| } | ||
|
|
||
| return getFilteredObjectsBasedOnTypeAndNameSelector(objList, hook.NameSelector), err | ||
|
|
@@ -168,9 +183,18 @@ func getObjectsBasedOnTypeAndRegex(objList client.ObjectList, nameSelector strin | |
| func getResourcesUsingLabelSelector(r client.Reader, hook *kubeobjects.HookSpec, | ||
| objList client.ObjectList, | ||
| ) error { | ||
| if len(hook.LabelSelector.MatchLabels) == 0 && len(hook.LabelSelector.MatchExpressions) == 0 { | ||
| return fmt.Errorf( | ||
| "list resources using labelSelector failed: empty MatchLabels and MatchExpressions "+ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error listing resources... would be good. |
||
| "(hook=%q namespace=%q)", | ||
| hook.Name, hook.Namespace, | ||
| ) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sometimes the error messages include the hook namespace, and sometimes they don’t. It would be better to follow a consistent standard. |
||
|
|
||
| selector, err := metav1.LabelSelectorAsSelector(hook.LabelSelector) | ||
| if err != nil { | ||
| return fmt.Errorf("error converting labelSelector to selector") | ||
| return fmt.Errorf("convert labelSelector to selector failed (hook=%q namespace=%q): %w", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be good |
||
| hook.Name, hook.Namespace, err) | ||
| } | ||
|
|
||
| listOps := &client.ListOptions{ | ||
|
|
@@ -180,7 +204,8 @@ func getResourcesUsingLabelSelector(r client.Reader, hook *kubeobjects.HookSpec, | |
|
|
||
| err = r.List(context.Background(), objList, listOps) | ||
| if err != nil { | ||
| return fmt.Errorf("error listing resources using labelSelector: %w", err) | ||
| return fmt.Errorf("list resources using labelSelector failed (hook=%q namespace=%q): %w", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. error listing resources... would be good. |
||
| hook.Name, hook.Namespace, err) | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
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.
error listing resources.... would be good.