From f35ee02b04e5eac621e416024c66ad1b43259205 Mon Sep 17 00:00:00 2001 From: Federico Nafria Date: Sat, 14 Dec 2024 20:48:55 +0100 Subject: [PATCH] Fix: switch context fails after failed connection Fixes https://github.com/derailed/k9s/issues/3032 It should not fail to invalidate the cache when there is not cached connection. --- internal/client/client.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index e43a561aa6..67cf12655e 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -458,10 +458,12 @@ func (a *APIClient) RestConfig() (*restclient.Config, error) { return a.config.RESTConfig() } +var NoConnectionToCachedDial = errors.New("no connection to cached dial") + // CachedDiscovery returns a cached discovery client. func (a *APIClient) CachedDiscovery() (*disk.CachedDiscoveryClient, error) { if !a.getConnOK() { - return nil, errors.New("no connection to cached dial") + return nil, NoConnectionToCachedDial } if c := a.getCachedClient(); c != nil { @@ -545,7 +547,10 @@ func (a *APIClient) SwitchContext(name string) error { if err := a.config.SwitchContext(name); err != nil { return err } - if err := a.invalidateCache(); err != nil { + err := a.invalidateCache() + if err == NoConnectionToCachedDial { + log.Warn().Err(err).Msgf("No cache to invalidate") + } else if err != nil { return err } a.reset()