Skip to content

tolerate discovery and errors better #17195

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions pkg/oc/cli/util/clientcmd/legacy_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
restclient "k8s.io/client-go/rest"
)
Expand All @@ -16,6 +17,7 @@ type legacyDiscoveryClient struct {
}

// ServerResourcesForGroupVersion returns the supported resources for a group and version.
// This can return an error *and* a partial result
func (d *legacyDiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (resources *metav1.APIResourceList, err error) {
parentList, err := d.DiscoveryClient.ServerResourcesForGroupVersion(groupVersion)
if err != nil {
Expand All @@ -37,29 +39,42 @@ func (d *legacyDiscoveryClient) ServerResourcesForGroupVersion(groupVersion stri
if groupVersion == "v1" && (errors.IsNotFound(err) || errors.IsForbidden(err)) {
return parentList, nil
}
return nil, err
return parentList, err
}

parentList.APIResources = append(parentList.APIResources, originResources.APIResources...)
return parentList, nil
}

// ServerResources returns the supported resources for all groups and versions.
// This can return an error *and* a partial result
func (d *legacyDiscoveryClient) ServerResources() ([]*metav1.APIResourceList, error) {
apiGroups, err := d.ServerGroups()
if err != nil {
return nil, err
}
groupVersions := metav1.ExtractGroupVersions(apiGroups)

result := []*metav1.APIResourceList{}
for _, groupVersion := range groupVersions {
resources, err := d.ServerResourcesForGroupVersion(groupVersion)
if err != nil {
return nil, err
failedGroups := make(map[schema.GroupVersion]error)

for _, apiGroup := range apiGroups.Groups {
for _, version := range apiGroup.Versions {
gv := schema.GroupVersion{Group: apiGroup.Name, Version: version.Version}
resources, err := d.ServerResourcesForGroupVersion(version.GroupVersion)
if err != nil {
failedGroups[gv] = err
continue
}

result = append(result, resources)
}
result = append(result, resources)
}
return result, nil

if len(failedGroups) == 0 {
return result, nil
}

return result, &discovery.ErrGroupDiscoveryFailed{Groups: failedGroups}
}

// newLegacyDiscoveryClient creates a new DiscoveryClient for the given RESTClient.
Expand Down