Skip to content

Commit

Permalink
Tolerate empty discovery response in memcache client
Browse files Browse the repository at this point in the history
Kubernetes-commit: 6e8addd9a0a9e2983e3040e337b1b7ba6df83d87
  • Loading branch information
seans3 authored and k8s-publishing-bot committed Feb 22, 2023
1 parent 62133a9 commit 5dbbc58
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions discovery/cached/memory/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/client-go/openapi"
cachedopenapi "k8s.io/client-go/openapi/cached"
restclient "k8s.io/client-go/rest"
"k8s.io/klog/v2"
)

type cacheEntry struct {
Expand Down Expand Up @@ -61,6 +62,15 @@ var (
ErrCacheNotFound = errors.New("not found")
)

// Server returning empty ResourceList for Group/Version.
type emptyResponseError struct {
gv string
}

func (e *emptyResponseError) Error() string {
return fmt.Sprintf("received empty response for: %s", e.gv)
}

var _ discovery.CachedDiscoveryInterface = &memCacheClient{}

// isTransientConnectionError checks whether given error is "Connection refused" or
Expand Down Expand Up @@ -103,7 +113,13 @@ func (d *memCacheClient) ServerResourcesForGroupVersion(groupVersion string) (*m
if cachedVal.err != nil && isTransientError(cachedVal.err) {
r, err := d.serverResourcesForGroupVersion(groupVersion)
if err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't get resource list for %v: %v", groupVersion, err))
// Don't log "empty response" as an error; it is a common response for metrics.
if _, emptyErr := err.(*emptyResponseError); emptyErr {
// Log at same verbosity as disk cache.
klog.V(3).Infof("%v", err)
} else {
utilruntime.HandleError(fmt.Errorf("couldn't get resource list for %v: %v", groupVersion, err))
}
}
cachedVal = &cacheEntry{r, err}
d.groupToServerResources[groupVersion] = cachedVal
Expand Down Expand Up @@ -252,7 +268,13 @@ func (d *memCacheClient) refreshLocked() error {

r, err := d.serverResourcesForGroupVersion(gv)
if err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't get resource list for %v: %v", gv, err))
// Don't log "empty response" as an error; it is a common response for metrics.
if _, emptyErr := err.(*emptyResponseError); emptyErr {
// Log at same verbosity as disk cache.
klog.V(3).Infof("%v", err)
} else {
utilruntime.HandleError(fmt.Errorf("couldn't get resource list for %v: %v", gv, err))
}
}

resultLock.Lock()
Expand All @@ -274,7 +296,7 @@ func (d *memCacheClient) serverResourcesForGroupVersion(groupVersion string) (*m
return r, err
}
if len(r.APIResources) == 0 {
return r, fmt.Errorf("Got empty response for: %v", groupVersion)
return r, &emptyResponseError{gv: groupVersion}
}
return r, nil
}
Expand Down

0 comments on commit 5dbbc58

Please sign in to comment.