Skip to content

Commit 712e066

Browse files
simplify kubeconfig retrieval
1 parent bc6e833 commit 712e066

File tree

1 file changed

+7
-8
lines changed
  • internal/cmd/ske/kubeconfig/login

1 file changed

+7
-8
lines changed

internal/cmd/ske/kubeconfig/login/login.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,18 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8484
}
8585

8686
cachedKubeconfig := getCachedKubeConfig(clusterConfig.cacheKey)
87-
8887
if cachedKubeconfig == nil {
89-
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, false, nil)
88+
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, nil)
9089
}
9190

9291
isValid, notAfter := checkKubeconfigExpiry(cachedKubeconfig.CertData)
9392
if !isValid {
9493
// cert is expired or invalid, request new
9594
_ = cache.DeleteObject(clusterConfig.cacheKey)
96-
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, false, nil)
95+
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, nil)
9796
} else if time.Now().Add(refreshBeforeDuration).After(notAfter.UTC()) {
9897
// cert expires within the next 15min, refresh (try to get a new, use cache on failure)
99-
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, true, cachedKubeconfig)
98+
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, cachedKubeconfig)
10099
}
101100
// cert not expired, nor will it expire in the next 15min; therefore, use the cached kubeconfig
102101
return output(params.Printer, clusterConfig.cacheKey, cachedKubeconfig)
@@ -188,25 +187,25 @@ func checkKubeconfigExpiry(certData []byte) (bool, time.Time) {
188187
return true, certificate.NotAfter.UTC()
189188
}
190189

191-
func GetAndOutputKubeconfig(ctx context.Context, p *print.Printer, apiClient *ske.APIClient, clusterConfig *clusterConfig, fallbackToCache bool, cachedKubeconfig *rest.Config) error {
190+
func GetAndOutputKubeconfig(ctx context.Context, p *print.Printer, apiClient *ske.APIClient, clusterConfig *clusterConfig, cachedKubeconfig *rest.Config) error {
192191
req := buildRequest(ctx, apiClient, clusterConfig)
193192
kubeconfigResponse, err := req.Execute()
194193
if err != nil {
195-
if fallbackToCache {
194+
if cachedKubeconfig != nil {
196195
return output(p, clusterConfig.cacheKey, cachedKubeconfig)
197196
}
198197
return fmt.Errorf("request kubeconfig: %w", err)
199198
}
200199

201200
kubeconfig, err := clientcmd.RESTConfigFromKubeConfig([]byte(*kubeconfigResponse.Kubeconfig))
202201
if err != nil {
203-
if fallbackToCache {
202+
if cachedKubeconfig != nil {
204203
return output(p, clusterConfig.cacheKey, cachedKubeconfig)
205204
}
206205
return fmt.Errorf("parse kubeconfig: %w", err)
207206
}
208207
if err = cache.PutObject(clusterConfig.cacheKey, []byte(*kubeconfigResponse.Kubeconfig)); err != nil {
209-
if fallbackToCache {
208+
if cachedKubeconfig != nil {
210209
return output(p, clusterConfig.cacheKey, cachedKubeconfig)
211210
}
212211
return fmt.Errorf("cache kubeconfig: %w", err)

0 commit comments

Comments
 (0)