@@ -27,12 +27,6 @@ import (
27
27
"k8s.io/client-go/tools/clientcmd"
28
28
)
29
29
30
- type inputModel struct {
31
- ProjectId string
32
- ClusterName string
33
- CacheKey string
34
- }
35
-
36
30
const (
37
31
expirationSeconds = 30 * 60 // 30 min
38
32
refreshBeforeDuration = 15 * time .Minute // 15 min
@@ -54,7 +48,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
54
48
55
49
model , err := parseInput ()
56
50
if err != nil {
57
- return fmt .Errorf ("login SKE kubeconfig: parseInput: %w" , err )
51
+ return fmt .Errorf ("parseInput: %w" , err )
58
52
}
59
53
60
54
// Configure API client
@@ -101,6 +95,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
101
95
return cmd
102
96
}
103
97
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
+
104
109
func parseInput () (* inputModel , error ) {
105
110
obj , _ , err := exec .LoadExecCredentialFromEnv ()
106
111
if err != nil {
@@ -136,38 +141,6 @@ func parseInput() (*inputModel, error) {
136
141
}, nil
137
142
}
138
143
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
-
171
144
func getCachedKubeConfig (key string ) * rest.Config {
172
145
cachedKubeconfig , err := cache .GetObject (key )
173
146
if err != nil {
@@ -182,51 +155,78 @@ func getCachedKubeConfig(key string) *rest.Config {
182
155
return restConfig
183
156
}
184
157
185
- type SKEClusterConfig struct {
186
- STACKITProjectID string `json:"stackitProjectId"`
187
- ClusterName string `json:"clusterName"`
188
- }
189
-
190
158
func GetAndOutputKubeconfig (ctx context.Context , cmd * cobra.Command , apiClient * ske.APIClient , model * inputModel , fallbackToCache bool , cachedKubeconfig * rest.Config ) error {
191
159
req := buildRequest (ctx , apiClient , model )
192
160
kubeconfigResponse , err := req .Execute ()
193
161
if err != nil {
194
162
if fallbackToCache {
195
163
return output (cmd , model .CacheKey , cachedKubeconfig )
196
164
}
197
- return fmt .Errorf ("login SKE kubeconfig: requesting kubeconfig: %w" , err )
165
+ return fmt .Errorf ("request kubeconfig: %w" , err )
198
166
}
199
167
200
168
kubeconfig , err := clientcmd .RESTConfigFromKubeConfig ([]byte (* kubeconfigResponse .Kubeconfig ))
201
169
if err != nil {
202
170
if fallbackToCache {
203
171
return output (cmd , model .CacheKey , cachedKubeconfig )
204
172
}
205
- return fmt .Errorf ("login SKE kubeconfig: parsing kubeconfig: %w" , err )
173
+ return fmt .Errorf ("parse kubeconfig: %w" , err )
206
174
}
207
175
if err = cache .PutObject (model .CacheKey , []byte (* kubeconfigResponse .Kubeconfig )); err != nil {
208
176
if fallbackToCache {
209
177
return output (cmd , model .CacheKey , cachedKubeconfig )
210
178
}
211
- return fmt .Errorf ("login SKE kubeconfig: caching kubeconfig: %w" , err )
179
+ return fmt .Errorf ("cache kubeconfig: %w" , err )
212
180
}
213
181
214
182
return output (cmd , model .CacheKey , kubeconfig )
215
183
}
216
184
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
+
217
192
func output (cmd * cobra.Command , cacheKey string , kubeconfig * rest.Config ) error {
218
193
outputExecCredential , err := parseKubeConfigToExecCredential (kubeconfig )
219
194
if err != nil {
220
195
_ = cache .DeleteObject (cacheKey )
221
- return fmt .Errorf ("login SKE kubeconfig: converting to ExecCredential: %w" , err )
196
+ return fmt .Errorf ("convert to ExecCredential: %w" , err )
222
197
}
223
198
224
199
output , err := json .Marshal (outputExecCredential )
225
200
if err != nil {
226
201
_ = cache .DeleteObject (cacheKey )
227
- return fmt .Errorf ("login SKE kubeconfig: marshal ExecCredential: %w" , err )
202
+ return fmt .Errorf ("marshal ExecCredential: %w" , err )
228
203
}
229
204
230
205
cmd .Print (string (output ))
231
206
return nil
232
207
}
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