Skip to content

Commit 48d0ac1

Browse files
author
Kai Kummerer
committed
rearrange functions and improve error messages
1 parent 3e795de commit 48d0ac1

File tree

1 file changed

+49
-49
lines changed
  • internal/cmd/ske/kubeconfig/login

1 file changed

+49
-49
lines changed

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

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ import (
2727
"k8s.io/client-go/tools/clientcmd"
2828
)
2929

30-
type inputModel struct {
31-
ProjectId string
32-
ClusterName string
33-
CacheKey string
34-
}
35-
3630
const (
3731
expirationSeconds = 30 * 60 // 30 min
3832
refreshBeforeDuration = 15 * time.Minute // 15 min
@@ -54,7 +48,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
5448

5549
model, err := parseInput()
5650
if err != nil {
57-
return fmt.Errorf("login SKE kubeconfig: parseInput: %w", err)
51+
return fmt.Errorf("parseInput: %w", err)
5852
}
5953

6054
// Configure API client
@@ -101,6 +95,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
10195
return cmd
10296
}
10397

98+
type inputModel struct {
99+
ProjectId string
100+
ClusterName string
101+
CacheKey string
102+
}
103+
104+
type SKEClusterConfig struct {
105+
STACKITProjectID string `json:"stackitProjectId"`
106+
ClusterName string `json:"clusterName"`
107+
}
108+
104109
func parseInput() (*inputModel, error) {
105110
obj, _, err := exec.LoadExecCredentialFromEnv()
106111
if err != nil {
@@ -136,38 +141,6 @@ func parseInput() (*inputModel, error) {
136141
}, nil
137142
}
138143

139-
func buildRequest(ctx context.Context, apiClient *ske.APIClient, model *inputModel) ske.ApiCreateKubeconfigRequest {
140-
req := apiClient.CreateKubeconfig(ctx, model.ProjectId, model.ClusterName)
141-
expirationSeconds := strconv.Itoa(expirationSeconds)
142-
143-
return req.CreateKubeconfigPayload(ske.CreateKubeconfigPayload{ExpirationSeconds: &expirationSeconds})
144-
}
145-
146-
func parseKubeConfigToExecCredential(kubeconfig *rest.Config) (*clientauthenticationv1.ExecCredential, error) {
147-
certPem, _ := pem.Decode(kubeconfig.CertData)
148-
if certPem == nil {
149-
return nil, fmt.Errorf("login SKE kubeconfig")
150-
}
151-
152-
certificate, err := x509.ParseCertificate(certPem.Bytes)
153-
if err != nil {
154-
return nil, fmt.Errorf("login SKE kubeconfig: %w", err)
155-
}
156-
157-
outputExecCredential := clientauthenticationv1.ExecCredential{
158-
TypeMeta: v1.TypeMeta{
159-
APIVersion: clientauthenticationv1.SchemeGroupVersion.String(),
160-
Kind: "ExecCredential",
161-
},
162-
Status: &clientauthenticationv1.ExecCredentialStatus{
163-
ExpirationTimestamp: &v1.Time{Time: certificate.NotAfter.Add(-time.Minute * 15)},
164-
ClientCertificateData: string(kubeconfig.CertData),
165-
ClientKeyData: string(kubeconfig.KeyData),
166-
},
167-
}
168-
return &outputExecCredential, nil
169-
}
170-
171144
func getCachedKubeConfig(key string) *rest.Config {
172145
cachedKubeconfig, err := cache.GetObject(key)
173146
if err != nil {
@@ -182,51 +155,78 @@ func getCachedKubeConfig(key string) *rest.Config {
182155
return restConfig
183156
}
184157

185-
type SKEClusterConfig struct {
186-
STACKITProjectID string `json:"stackitProjectId"`
187-
ClusterName string `json:"clusterName"`
188-
}
189-
190158
func GetAndOutputKubeconfig(ctx context.Context, cmd *cobra.Command, apiClient *ske.APIClient, model *inputModel, fallbackToCache bool, cachedKubeconfig *rest.Config) error {
191159
req := buildRequest(ctx, apiClient, model)
192160
kubeconfigResponse, err := req.Execute()
193161
if err != nil {
194162
if fallbackToCache {
195163
return output(cmd, model.CacheKey, cachedKubeconfig)
196164
}
197-
return fmt.Errorf("login SKE kubeconfig: requesting kubeconfig: %w", err)
165+
return fmt.Errorf("request kubeconfig: %w", err)
198166
}
199167

200168
kubeconfig, err := clientcmd.RESTConfigFromKubeConfig([]byte(*kubeconfigResponse.Kubeconfig))
201169
if err != nil {
202170
if fallbackToCache {
203171
return output(cmd, model.CacheKey, cachedKubeconfig)
204172
}
205-
return fmt.Errorf("login SKE kubeconfig: parsing kubeconfig: %w", err)
173+
return fmt.Errorf("parse kubeconfig: %w", err)
206174
}
207175
if err = cache.PutObject(model.CacheKey, []byte(*kubeconfigResponse.Kubeconfig)); err != nil {
208176
if fallbackToCache {
209177
return output(cmd, model.CacheKey, cachedKubeconfig)
210178
}
211-
return fmt.Errorf("login SKE kubeconfig: caching kubeconfig: %w", err)
179+
return fmt.Errorf("cache kubeconfig: %w", err)
212180
}
213181

214182
return output(cmd, model.CacheKey, kubeconfig)
215183
}
216184

185+
func buildRequest(ctx context.Context, apiClient *ske.APIClient, model *inputModel) ske.ApiCreateKubeconfigRequest {
186+
req := apiClient.CreateKubeconfig(ctx, model.ProjectId, model.ClusterName)
187+
expirationSeconds := strconv.Itoa(expirationSeconds)
188+
189+
return req.CreateKubeconfigPayload(ske.CreateKubeconfigPayload{ExpirationSeconds: &expirationSeconds})
190+
}
191+
217192
func output(cmd *cobra.Command, cacheKey string, kubeconfig *rest.Config) error {
218193
outputExecCredential, err := parseKubeConfigToExecCredential(kubeconfig)
219194
if err != nil {
220195
_ = cache.DeleteObject(cacheKey)
221-
return fmt.Errorf("login SKE kubeconfig: converting to ExecCredential: %w", err)
196+
return fmt.Errorf("convert to ExecCredential: %w", err)
222197
}
223198

224199
output, err := json.Marshal(outputExecCredential)
225200
if err != nil {
226201
_ = cache.DeleteObject(cacheKey)
227-
return fmt.Errorf("login SKE kubeconfig: marshal ExecCredential: %w", err)
202+
return fmt.Errorf("marshal ExecCredential: %w", err)
228203
}
229204

230205
cmd.Print(string(output))
231206
return nil
232207
}
208+
209+
func parseKubeConfigToExecCredential(kubeconfig *rest.Config) (*clientauthenticationv1.ExecCredential, error) {
210+
certPem, _ := pem.Decode(kubeconfig.CertData)
211+
if certPem == nil {
212+
return nil, fmt.Errorf("decoded pem is nil")
213+
}
214+
215+
certificate, err := x509.ParseCertificate(certPem.Bytes)
216+
if err != nil {
217+
return nil, fmt.Errorf("parse certificate: %w", err)
218+
}
219+
220+
outputExecCredential := clientauthenticationv1.ExecCredential{
221+
TypeMeta: v1.TypeMeta{
222+
APIVersion: clientauthenticationv1.SchemeGroupVersion.String(),
223+
Kind: "ExecCredential",
224+
},
225+
Status: &clientauthenticationv1.ExecCredentialStatus{
226+
ExpirationTimestamp: &v1.Time{Time: certificate.NotAfter.Add(-time.Minute * 15)},
227+
ClientCertificateData: string(kubeconfig.CertData),
228+
ClientKeyData: string(kubeconfig.KeyData),
229+
},
230+
}
231+
return &outputExecCredential, nil
232+
}

0 commit comments

Comments
 (0)