-
Notifications
You must be signed in to change notification settings - Fork 104
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
Filter unknown custom resources in Delete- and ToggleTask #1552
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 |
---|---|---|
|
@@ -38,33 +38,59 @@ func IsNamespacedObject(r runtime.Object, di discovery.CachedDiscoveryInterface) | |
return isNamespaced(gvk, di) | ||
} | ||
|
||
func IsKnownObjectType(r runtime.Object, di discovery.CachedDiscoveryInterface) (bool, error) { | ||
gvk := r.GetObjectKind().GroupVersionKind() | ||
return isKnownType(gvk, di) | ||
} | ||
|
||
// isNamespaced method return true if given runtime.Object is a namespaced (not cluster-scoped) resource. It uses the | ||
// discovery client to fetch all API resources (with Groups and Versions), searches for a resource with the passed GVK | ||
// and returns true if it's namespaced. Method returns an error if passed GVK wasn't found in the discovered resource list. | ||
func isNamespaced(gvk schema.GroupVersionKind, di discovery.CachedDiscoveryInterface) (bool, error) { | ||
// Fetch namespaced API resources | ||
apiResource, err := getUncachedAPIResource(gvk, di) | ||
if err != nil { | ||
return false, err | ||
} | ||
if apiResource != nil { | ||
return apiResource.Namespaced, nil | ||
} | ||
return false, fmt.Errorf("a resource with GVK %v seems to be missing in API resource list", gvk) | ||
} | ||
|
||
func isKnownType(gvk schema.GroupVersionKind, di discovery.CachedDiscoveryInterface) (bool, error) { | ||
apiResource, err := getUncachedAPIResource(gvk, di) | ||
if err != nil { | ||
return false, err | ||
} | ||
if apiResource != nil { | ||
return true, nil | ||
} | ||
return false, nil | ||
} | ||
|
||
// getUncachedAPIResource tries to invalidate the cache and requery the discovery interface to make sure no stale data is returned | ||
func getUncachedAPIResource(gvk schema.GroupVersionKind, di discovery.CachedDiscoveryInterface) (*metav1.APIResource, error) { | ||
// First try, this may return nil because of the cache | ||
apiResource, err := getAPIResource(gvk, di) | ||
if err != nil { | ||
return false, err | ||
return nil, err | ||
} | ||
if apiResource != nil { | ||
return apiResource.Namespaced, nil | ||
return apiResource, nil | ||
} | ||
|
||
// Second try, now with invalidated cache. If we still get nil, we know it's not there. | ||
log.Printf("Failed to get APIResource for %v, retry with invalidated cache.", gvk) | ||
di.Invalidate() | ||
apiResource, err = getAPIResource(gvk, di) | ||
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. Wdyt about making these next few lines a nested 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. Not sure about the inline func, I tried it and it didn't really help much. But you're right that there's a bit of duplication, so I extracted a |
||
if err != nil { | ||
return false, err | ||
return nil, err | ||
} | ||
if apiResource != nil { | ||
return apiResource.Namespaced, nil | ||
return apiResource, nil | ||
} | ||
|
||
return false, fmt.Errorf("a resource with GVK %v seems to be missing in API resource list", gvk) | ||
return nil, nil | ||
} | ||
|
||
// getAPIResource returns a specific APIResource from the DiscoveryInterface or nil if no resource was found. | ||
|
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 guess, it's a good thing that the discovery client is cached 😉