Skip to content

Commit 549f799

Browse files
extract kubeconfig retrieval
1 parent 712e066 commit 549f799

File tree

1 file changed

+20
-16
lines changed
  • internal/cmd/ske/kubeconfig/login

1 file changed

+20
-16
lines changed

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8383
return err
8484
}
8585

86-
cachedKubeconfig := getCachedKubeConfig(clusterConfig.cacheKey)
87-
if cachedKubeconfig == nil {
88-
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, nil)
89-
}
90-
91-
isValid, notAfter := checkKubeconfigExpiry(cachedKubeconfig.CertData)
92-
if !isValid {
93-
// cert is expired or invalid, request new
94-
_ = cache.DeleteObject(clusterConfig.cacheKey)
95-
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, nil)
96-
} else if time.Now().Add(refreshBeforeDuration).After(notAfter.UTC()) {
97-
// cert expires within the next 15min, refresh (try to get a new, use cache on failure)
98-
return GetAndOutputKubeconfig(ctx, params.Printer, apiClient, clusterConfig, cachedKubeconfig)
99-
}
100-
// cert not expired, nor will it expire in the next 15min; therefore, use the cached kubeconfig
101-
return output(params.Printer, clusterConfig.cacheKey, cachedKubeconfig)
86+
return outputLoginKubeconfig(ctx, params.Printer, apiClient, clusterConfig)
10287
},
10388
}
10489
return cmd
@@ -155,6 +140,25 @@ func parseClusterConfig(p *print.Printer, cmd *cobra.Command) (*clusterConfig, e
155140
return clusterConfig, nil
156141
}
157142

143+
func outputLoginKubeconfig(ctx context.Context, p *print.Printer, apiClient *ske.APIClient, clusterConfig *clusterConfig) error {
144+
cachedKubeconfig := getCachedKubeConfig(clusterConfig.cacheKey)
145+
if cachedKubeconfig == nil {
146+
return GetAndOutputKubeconfig(ctx, p, apiClient, clusterConfig, nil)
147+
}
148+
149+
isValid, notAfter := checkKubeconfigExpiry(cachedKubeconfig.CertData)
150+
if !isValid {
151+
// cert is expired or invalid, request new
152+
_ = cache.DeleteObject(clusterConfig.cacheKey)
153+
return GetAndOutputKubeconfig(ctx, p, apiClient, clusterConfig, nil)
154+
} else if time.Now().Add(refreshBeforeDuration).After(notAfter.UTC()) {
155+
// cert expires within the next 15min, refresh (try to get a new, use cache on failure)
156+
return GetAndOutputKubeconfig(ctx, p, apiClient, clusterConfig, cachedKubeconfig)
157+
}
158+
// cert not expired, nor will it expire in the next 15min; therefore, use the cached kubeconfig
159+
return output(p, clusterConfig.cacheKey, cachedKubeconfig)
160+
}
161+
158162
func getCachedKubeConfig(key string) *rest.Config {
159163
cachedKubeconfig, err := cache.GetObject(key)
160164
if err != nil {

0 commit comments

Comments
 (0)