Skip to content

Commit 362cd9c

Browse files
authored
OIDC: allow providing whole Kubeconfig (#2123)
1 parent 595eec8 commit 362cd9c

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

internal/cmd/oidc/oidc.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
type oidcConfig struct {
1818
*cmdcommon.KymaConfig
19+
cmdcommon.KubeClientConfig
1920

2021
output string
2122
caCertificate string
@@ -33,7 +34,8 @@ type TokenData struct {
3334

3435
func NewOIDCCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
3536
cfg := oidcConfig{
36-
KymaConfig: kymaConfig,
37+
KymaConfig: kymaConfig,
38+
KubeClientConfig: cmdcommon.KubeClientConfig{},
3739
}
3840

3941
cmd := &cobra.Command{
@@ -49,6 +51,8 @@ func NewOIDCCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
4951
},
5052
}
5153

54+
cfg.KubeClientConfig.AddFlag(cmd)
55+
5256
cmd.Flags().StringVar(&cfg.output, "output", "", "Path to the output kubeconfig file")
5357
cmd.Flags().StringVar(&cfg.caCertificate, "ca-certificate", "", "Path to the CA certificate file")
5458
cmd.Flags().StringVar(&cfg.clusterServer, "cluster-server", "", "URL of the cluster server")
@@ -57,8 +61,9 @@ func NewOIDCCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
5761
cmd.Flags().StringVar(&cfg.audience, "audience", "", "Audience of the token")
5862
cmd.Flags().StringVar(&cfg.idTokenRequestURL, "id-token-request-url", "", "URL to request the ID token, defaults to ACTIONS_ID_TOKEN_REQUEST_URL env variable")
5963

60-
_ = cmd.MarkFlagRequired("ca-certificate")
61-
_ = cmd.MarkFlagRequired("cluster-server")
64+
cmd.MarkFlagsOneRequired("kubeconfig", "ca-certificate")
65+
cmd.MarkFlagsRequiredTogether("ca-certificate", "cluster-server")
66+
cmd.MarkFlagsMutuallyExclusive("kubeconfig", "ca-certificate")
6267

6368
cmd.MarkFlagsMutuallyExclusive("token", "id-token-request-url")
6469
cmd.MarkFlagsMutuallyExclusive("token", "audience")
@@ -71,6 +76,10 @@ func (cfg *oidcConfig) complete() clierror.Error {
7176
cfg.idTokenRequestURL = os.Getenv("ACTIONS_ID_TOKEN_REQUEST_URL")
7277
}
7378
cfg.idTokenRequestToken = os.Getenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN")
79+
80+
if cfg.KubeClientConfig.Kubeconfig != "" {
81+
return cfg.KubeClientConfig.Complete()
82+
}
7483
return nil
7584
}
7685

@@ -107,8 +116,15 @@ func runOIDC(cfg *oidcConfig) clierror.Error {
107116
return clierror.Wrap(err, clierror.New("failed to get token"))
108117
}
109118
}
119+
caCertificate := cfg.caCertificate
120+
clusterServer := cfg.clusterServer
121+
if cfg.KubeClientConfig.Kubeconfig != "" {
122+
currentServer := cfg.KubeClient.ApiConfig().Clusters[cfg.KubeClient.ApiConfig().CurrentContext]
123+
caCertificate = string(currentServer.CertificateAuthorityData)
124+
clusterServer = currentServer.Server
125+
}
110126

111-
enrichedKubeconfig, err := createKubeconfig(cfg.caCertificate, cfg.clusterServer, token)
127+
enrichedKubeconfig, err := createKubeconfig(caCertificate, clusterServer, token)
112128
if err != nil {
113129
return clierror.Wrap(err, clierror.New("failed to create kubeconfig"))
114130
}

0 commit comments

Comments
 (0)