Skip to content

Commit 52e986b

Browse files
authored
Merge pull request #280 from covexo/config-fix
Implement token handling for kubeconfig
2 parents 57da9d2 + 768a6cb commit 52e986b

3 files changed

Lines changed: 44 additions & 12 deletions

File tree

pkg/devspace/clients/kubectl/client.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"k8s.io/client-go/kubernetes"
2121
"k8s.io/client-go/rest"
2222
"k8s.io/client-go/tools/clientcmd"
23+
"k8s.io/client-go/tools/clientcmd/api"
2324
"k8s.io/client-go/tools/portforward"
2425
"k8s.io/client-go/tools/remotecommand"
2526
"k8s.io/client-go/transport/spdy"
@@ -78,14 +79,39 @@ func GetClientConfig() (*rest.Config, error) {
7879
return clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile)
7980
}
8081

81-
return &rest.Config{
82-
Host: *config.Cluster.APIServer,
83-
TLSClientConfig: rest.TLSClientConfig{
84-
CAData: []byte(*config.Cluster.CaCert),
85-
CertData: []byte(*config.Cluster.User.ClientCert),
86-
KeyData: []byte(*config.Cluster.User.ClientKey),
87-
},
88-
}, nil
82+
// We create a new config object here
83+
kubeAuthInfo := api.NewAuthInfo()
84+
if config.Cluster.User != nil {
85+
if config.Cluster.User.ClientCert != nil {
86+
kubeAuthInfo.ClientCertificateData = []byte(*config.Cluster.User.ClientCert)
87+
}
88+
if config.Cluster.User.ClientKey != nil {
89+
kubeAuthInfo.ClientKeyData = []byte(*config.Cluster.User.ClientKey)
90+
}
91+
if config.Cluster.User.Token != nil {
92+
kubeAuthInfo.Token = *config.Cluster.User.Token
93+
}
94+
}
95+
96+
kubeCluster := api.NewCluster()
97+
if config.Cluster.APIServer != nil {
98+
kubeCluster.Server = *config.Cluster.APIServer
99+
}
100+
if config.Cluster.CaCert != nil {
101+
kubeCluster.CertificateAuthorityData = []byte(*config.Cluster.CaCert)
102+
}
103+
104+
kubeContext := api.NewContext()
105+
kubeContext.Cluster = "devspace"
106+
kubeContext.AuthInfo = "devspace"
107+
108+
kubeConfig := api.NewConfig()
109+
kubeConfig.AuthInfos["devspace"] = kubeAuthInfo
110+
kubeConfig.Clusters["devspace"] = kubeCluster
111+
kubeConfig.Contexts["devspace"] = kubeContext
112+
kubeConfig.CurrentContext = "devspace"
113+
114+
return clientcmd.NewNonInteractiveClientConfig(*kubeConfig, "devspace", &clientcmd.ConfigOverrides{}, clientcmd.NewDefaultClientConfigLoadingRules()).ClientConfig()
89115
}
90116

91117
// IsMinikube returns true if the Kubernetes cluster is a minikube

pkg/devspace/cloud/login.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func Update(providerConfig ProviderConfig, dsConfig *v1.Config, switchKubeContex
141141
kubeContext = provider.KubeContext
142142
}
143143

144-
err = UpdateKubeConfig(kubeContext, cluster, authInfo, switchKubeContext)
144+
err = UpdateKubeConfig(kubeContext, namespace, cluster, authInfo, switchKubeContext)
145145
if err != nil {
146146
return err
147147
}
@@ -154,14 +154,15 @@ func Update(providerConfig ProviderConfig, dsConfig *v1.Config, switchKubeContex
154154
dsConfig.Cluster.User = &v1.ClusterUser{
155155
ClientCert: configutil.String(string(authInfo.ClientCertificateData)),
156156
ClientKey: configutil.String(string(authInfo.ClientKeyData)),
157+
Token: configutil.String(string(authInfo.Token)),
157158
}
158159
}
159160

160161
return err
161162
}
162163

163164
// UpdateKubeConfig adds the devspace-cloud context if necessary and switches the current context
164-
func UpdateKubeConfig(contextName string, cluster *api.Cluster, authInfo *api.AuthInfo, switchContext bool) error {
165+
func UpdateKubeConfig(contextName, namespace string, cluster *api.Cluster, authInfo *api.AuthInfo, switchContext bool) error {
165166
config, err := kubeconfig.ReadKubeConfig(clientcmd.RecommendedHomeFile)
166167
if err != nil {
167168
return err
@@ -172,14 +173,18 @@ func UpdateKubeConfig(contextName string, cluster *api.Cluster, authInfo *api.Au
172173
config.CurrentContext = contextName
173174
}
174175

176+
// We generate a unique auth info name for each devspace
177+
authInfoName := contextName + "-" + namespace
178+
175179
config.Clusters[contextName] = cluster
176-
config.AuthInfos[contextName] = authInfo
180+
config.AuthInfos[authInfoName] = authInfo
177181

178182
// Check if we need to add the context
179183
if _, ok := config.Contexts[contextName]; !ok {
180184
context := api.NewContext()
181185
context.Cluster = contextName
182-
context.AuthInfo = contextName
186+
context.AuthInfo = authInfoName
187+
context.Namespace = namespace
183188

184189
config.Contexts[contextName] = context
185190
}

pkg/devspace/config/v1/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ type Cluster struct {
1414
type ClusterUser struct {
1515
ClientCert *string `yaml:"clientCert,omitempty"`
1616
ClientKey *string `yaml:"clientKey,omitempty"`
17+
Token *string `yaml:"token,omitempty"`
1718
}

0 commit comments

Comments
 (0)