Skip to content

Commit 6caa0b6

Browse files
kwiatekuspPrecel
andauthored
Cover case when the current-context is missing (#2601) (#2613)
Co-authored-by: Filip Strozik <[email protected]>
1 parent b9e297d commit 6caa0b6

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

internal/kubeconfig/kubeconfig.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ func PrepareWithToken(apiBase *api.Config, token string) *api.Config {
3636
}
3737

3838
func Prepare(ctx context.Context, client kube.Client, name, namespace, time, output string, permanent bool) (*api.Config, clierror.Error) {
39-
currentCtx := client.APIConfig().CurrentContext
40-
clusterName := client.APIConfig().Contexts[currentCtx].Cluster
39+
clusterName := getKubeconfigCurrentClusterName(client.APIConfig())
4140
var tokenData authv1.TokenRequestStatus
4241
var certData []byte
4342
var err clierror.Error
@@ -85,22 +84,21 @@ func Prepare(ctx context.Context, client kube.Client, name, namespace, time, out
8584
},
8685
},
8786
Contexts: map[string]*api.Context{
88-
currentCtx: {
87+
clusterName: {
8988
Cluster: clusterName,
9089
Namespace: namespace,
9190
AuthInfo: name,
9291
},
9392
},
94-
CurrentContext: currentCtx,
93+
CurrentContext: clusterName,
9594
Extensions: nil,
9695
}
9796

9897
return kubeconfig, nil
9998
}
10099

101100
func PrepareFromOpenIDConnectorResource(ctx context.Context, client kube.Client, name string) (*api.Config, clierror.Error) {
102-
currentCtx := client.APIConfig().CurrentContext
103-
clusterName := client.APIConfig().Contexts[currentCtx].Cluster
101+
clusterName := getKubeconfigCurrentClusterName(client.APIConfig())
104102

105103
oidcResUnstruct, err := client.Dynamic().Resource(OpenIdConnectGVR).Get(ctx, name, metav1.GetOptions{})
106104
if err != nil {
@@ -137,18 +135,33 @@ func PrepareFromOpenIDConnectorResource(ctx context.Context, client kube.Client,
137135
},
138136
},
139137
Contexts: map[string]*api.Context{
140-
currentCtx: {
138+
clusterName: {
141139
Cluster: clusterName,
142140
AuthInfo: name,
143141
},
144142
},
145-
CurrentContext: currentCtx,
143+
CurrentContext: clusterName,
146144
Extensions: nil,
147145
}
148146

149147
return kubeconfig, nil
150148
}
151149

150+
func getKubeconfigCurrentClusterName(kubeconfig *api.Config) string {
151+
clusterName := kubeconfig.CurrentContext
152+
if clusterName != "" {
153+
// current context exists
154+
return kubeconfig.Contexts[kubeconfig.CurrentContext].Cluster
155+
}
156+
157+
// get first cluster from map
158+
for clusterName = range kubeconfig.Clusters {
159+
break
160+
}
161+
162+
return clusterName
163+
}
164+
152165
func unmarshalOIDCResource(obj map[string]any) (OpenIDConnect, error) {
153166
var oidc OpenIDConnect
154167
b, err := json.Marshal(obj)

internal/kubeconfig/kubeconfig_test.go

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,13 @@ func Test_Prepare(t *testing.T) {
113113
},
114114
},
115115
Contexts: map[string]*api.Context{
116-
"context": {
116+
"cluster": {
117117
Cluster: "cluster",
118118
AuthInfo: "username",
119119
Namespace: "default",
120120
},
121121
},
122-
CurrentContext: "context",
122+
CurrentContext: "cluster",
123123
},
124124
expectedErr: nil,
125125
},
@@ -146,13 +146,13 @@ func Test_Prepare(t *testing.T) {
146146
},
147147
},
148148
Contexts: map[string]*api.Context{
149-
"context": {
149+
"cluster": {
150150
Cluster: "cluster",
151151
AuthInfo: "username",
152152
Namespace: "default",
153153
},
154154
},
155-
CurrentContext: "context",
155+
CurrentContext: "cluster",
156156
},
157157
expectedErr: nil,
158158
},
@@ -194,11 +194,11 @@ func Test_Prepare(t *testing.T) {
194194
},
195195
},
196196
Contexts: map[string]*api.Context{
197-
"context": {
197+
"cluster": {
198198
Cluster: "cluster",
199199
},
200200
},
201-
CurrentContext: "context",
201+
CurrentContext: "cluster",
202202
}
203203

204204
staticClient := k8s_fake.NewSimpleClientset(
@@ -414,13 +414,70 @@ func Test_PrepareFromOpenIDConnectorResource(t *testing.T) {
414414
},
415415
},
416416
},
417+
Contexts: map[string]*api.Context{
418+
"cluster": {
419+
Cluster: "cluster",
420+
AuthInfo: "kyma-oidc",
421+
},
422+
},
423+
CurrentContext: "cluster",
424+
Extensions: nil,
425+
}
426+
require.Equal(t, expected, result)
427+
})
428+
429+
t.Run("kubeconfig without current context", func(t *testing.T) {
430+
apiConfig := &api.Config{
431+
Clusters: map[string]*api.Cluster{
432+
"cluster": {
433+
Server: "https://localhost:8080",
434+
CertificateAuthorityData: []byte("certificate"),
435+
},
436+
},
417437
Contexts: map[string]*api.Context{
418438
"context": {
439+
Cluster: "cluster",
440+
},
441+
},
442+
CurrentContext: "",
443+
}
444+
fakeClient := newFakeClient(baseOIDCRes)
445+
fakeClient.TestAPIConfig = apiConfig
446+
447+
result, clierr := PrepareFromOpenIDConnectorResource(context.Background(), fakeClient, "kyma-oidc")
448+
require.Nil(t, clierr)
449+
450+
expected := &api.Config{
451+
Kind: "Config",
452+
APIVersion: "v1",
453+
Clusters: map[string]*api.Cluster{
454+
"cluster": {
455+
Server: "https://localhost:8080",
456+
CertificateAuthorityData: []byte("certificate"),
457+
},
458+
},
459+
AuthInfos: map[string]*api.AuthInfo{
460+
"kyma-oidc": {
461+
Exec: &api.ExecConfig{
462+
APIVersion: "client.authentication.k8s.io/v1beta1",
463+
Command: "kubectl-oidc_login",
464+
Args: []string{
465+
"get-token",
466+
"--oidc-issuer-url=http://super.issuer.com",
467+
"--oidc-client-id=10033344-6d08-46cd-80b9-37e18cd856c1",
468+
"--oidc-extra-scope=email",
469+
"--oidc-extra-scope=openid",
470+
},
471+
},
472+
},
473+
},
474+
Contexts: map[string]*api.Context{
475+
"cluster": {
419476
Cluster: "cluster",
420477
AuthInfo: "kyma-oidc",
421478
},
422479
},
423-
CurrentContext: "context",
480+
CurrentContext: "cluster",
424481
Extensions: nil,
425482
}
426483
require.Equal(t, expected, result)

0 commit comments

Comments
 (0)