@@ -697,10 +697,21 @@ func getClusterKubeconfig(c *Config, id, origconfig string) (*managementClient.G
697697 if err != nil {
698698 return nil , fmt .Errorf ("Getting cluster Kubeconfig: %v" , err )
699699 }
700+ // kubeconfig is not valid due to an invalid token. Use the cached kubeconfig
701+ // and replace the token
702+ if ! kubeValid && len (token ) == 0 {
703+ newConfig , err := replaceKubeConfigToken (c , origconfig , token )
704+ if err != nil {
705+ return nil , err
706+ }
707+ origconfig = newConfig
708+ kubeValid = true
709+ }
700710 if kubeValid {
701711 return & managementClient.GenerateKubeConfigOutput {Config : origconfig }, nil
702712 }
703713
714+ // kubeconfig is not valid for other reasons, download a new one
704715 client , err := c .ManagementClient ()
705716 if err != nil {
706717 return nil , fmt .Errorf ("Getting cluster Kubeconfig: %v" , err )
@@ -734,17 +745,12 @@ func getClusterKubeconfig(c *Config, id, origconfig string) (*managementClient.G
734745 }
735746 err = client .APIBaseClient .Action (managementClient .ClusterType , action , clusterResource , nil , kubeConfig )
736747 if err == nil {
737- if isRancher26 && len (token ) > 0 {
738- newConfig , err := replaceKubeConfigToken (c , kubeConfig .Config , token )
739- if err != nil {
740- return nil , err
741- }
742- kubeConfig .Config = newConfig
743- }
744748 return kubeConfig , nil
745749 }
746- if ! IsNotFound (err ) && ! IsForbidden (err ) && ! IsServiceUnavailableError (err ) {
747- return nil , fmt .Errorf ("Getting cluster Kubeconfig: %v" , err )
750+ if err != nil {
751+ if ! IsNotFound (err ) && ! IsForbidden (err ) && ! IsServiceUnavailableError (err ) {
752+ return nil , fmt .Errorf ("Getting cluster Kubeconfig: %w" , err )
753+ }
748754 }
749755 }
750756 select {
0 commit comments