1 /* 2 Copyright 2023 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package apiutil 18 19 import ( 20 "fmt" 21 "sort" 22 "strings" 23 24 apierrors "k8s.io/apimachinery/pkg/api/errors" 25 "k8s.io/apimachinery/pkg/api/meta" 26 27 "k8s.io/apimachinery/pkg/runtime/schema" 28 ) 29 30 // ErrResourceDiscoveryFailed is returned if the RESTMapper cannot discover supported resources for some GroupVersions. 31 // It wraps the errors encountered, except "NotFound" errors are replaced with meta.NoResourceMatchError, for 32 // backwards compatibility with code that uses meta.IsNoMatchError() to check for unsupported APIs. 33 type ErrResourceDiscoveryFailed map[schema.GroupVersion]error 34 35 // Error implements the error interface. 36 func (e *ErrResourceDiscoveryFailed) Error() string { 37 subErrors := []string{} 38 for k, v := range *e { 39 subErrors = append(subErrors, fmt.Sprintf("%s: %v", k, v)) 40 } 41 sort.Strings(subErrors) 42 return fmt.Sprintf("unable to retrieve the complete list of server APIs: %s", strings.Join(subErrors, ", ")) 43 } 44 45 func (e *ErrResourceDiscoveryFailed) Unwrap() []error { 46 subErrors := []error{} 47 for gv, err := range *e { 48 if apierrors.IsNotFound(err) { 49 err = &meta.NoResourceMatchError{PartialResource: gv.WithResource("")} 50 } 51 subErrors = append(subErrors, err) 52 } 53 return subErrors 54 } 55