Skip to content

Commit fc40a51

Browse files
committed
Update cluster config on every up
1 parent e7c0d24 commit fc40a51

4 files changed

Lines changed: 63 additions & 27 deletions

File tree

cmd/init.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -360,37 +360,18 @@ func (cmd *InitCmd) useDevSpaceCloud() bool {
360360
}) == "yes"
361361

362362
if useDevSpaceCloud {
363-
namespace, cluster, authInfo, err := login.CheckAuth()
364-
if err != nil {
365-
log.Fatalf("Error authenticating to devspace cloud: %v", err)
366-
}
367-
368-
cmd.config.DevSpace.Release.Namespace = &namespace
369-
cmd.config.Services.Tiller.Release.Namespace = &namespace
370-
371363
addToContext := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
372364
Question: "Do you want to add the devspace-cloud to the $HOME/.kube/config file? (yes | no)",
373365
DefaultValue: "yes",
374366
ValidationRegexPattern: "^(yes)|(no)$",
375367
}) == "yes"
376368

377-
if addToContext {
378-
err = login.UpdateKubeConfig(cluster, authInfo, true)
379-
if err != nil {
380-
log.Fatalf("Couldn't update kube config: %v", err)
381-
}
382-
383-
cmd.config.Cluster.UseKubeConfig = &addToContext
384-
cmd.config.Cluster.KubeContext = configutil.String(login.DevSpaceCloudContextName)
385-
} else {
386-
cmd.config.Cluster.UseKubeConfig = &addToContext
387-
cmd.config.Cluster.APIServer = &cluster.Server
388-
cmd.config.Cluster.CaCert = configutil.String(string(cluster.CertificateAuthorityData))
369+
cmd.config.Cluster.DevSpaceCloud = &useDevSpaceCloud
370+
cmd.config.Cluster.UseKubeConfig = &addToContext
389371

390-
cmd.config.Cluster.User = &v1.ClusterUser{
391-
ClientCert: configutil.String(string(authInfo.ClientCertificateData)),
392-
ClientKey: configutil.String(string(authInfo.ClientKeyData)),
393-
}
372+
err := login.Update(cmd.config)
373+
if err != nil {
374+
log.Fatalf("Couldn't authenticate to devspace cloud: %v", err)
394375
}
395376

396377
return true

cmd/up.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/covexo/devspace/pkg/devspace/login"
13+
1214
"github.com/covexo/devspace/pkg/util/hash"
1315
"github.com/covexo/devspace/pkg/util/stdinutil"
1416

@@ -135,6 +137,15 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
135137
initCmd.Run(nil, []string{})
136138
}
137139

140+
// Load config
141+
config := configutil.GetConfig(false)
142+
if config.Cluster.DevSpaceCloud != nil && *config.Cluster.DevSpaceCloud {
143+
err = login.Update(config)
144+
if err != nil {
145+
log.Warnf("Couldn't update devspace cloud cluster information: %v", err)
146+
}
147+
}
148+
138149
cmd.kubectl, err = kubectl.NewClient()
139150
if err != nil {
140151
log.Fatalf("Unable to create new kubectl client: %v", err)
@@ -160,7 +171,6 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
160171
mustRedeploy := cmd.buildImages()
161172

162173
// Check if we find a running release pod
163-
config := configutil.GetConfig(false)
164174
hash, err := hash.Directory("chart")
165175
if err != nil {
166176
log.Fatalf("Error hashing chart directory: %v", err)

pkg/devspace/config/v1/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v1
33
//Cluster is a struct that contains data for a Kubernetes-Cluster
44
type Cluster struct {
55
UseKubeConfig *bool `yaml:"useKubeConfig,omitempty"`
6+
DevSpaceCloud *bool `yaml:"devSpaceCloud,omitempty"`
67
KubeContext *string `yaml:"kubeContext,omitempty"`
78
APIServer *string `yaml:"apiServer,omitempty"`
89
CaCert *string `yaml:"caCert,omitempty"`

pkg/devspace/login/login.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"path/filepath"
1010

11+
"github.com/covexo/devspace/pkg/devspace/config/configutil"
12+
"github.com/covexo/devspace/pkg/devspace/config/v1"
1113
"github.com/covexo/devspace/pkg/util/kubeconfig"
1214
"github.com/covexo/devspace/pkg/util/yamlutil"
1315

@@ -37,7 +39,7 @@ type DevSpaceCloudConfig struct {
3739
Token string `yaml:"token"`
3840
}
3941

40-
// CheckAuth verifies if the user is logged into the devspace cloud and if not loggs the user in
42+
// CheckAuth verifies if the user is logged into the devspace cloud and if not logs the user in
4143
func CheckAuth() (string, *api.Cluster, *api.AuthInfo, error) {
4244
homedir, err := homedir.Dir()
4345
if err != nil {
@@ -80,6 +82,13 @@ func GetClusterConfig(cfg *DevSpaceCloudConfig) (string, *api.Cluster, *api.Auth
8082
return "", nil, nil, err
8183
}
8284

85+
if resp.StatusCode == 401 {
86+
return Login()
87+
}
88+
if resp.StatusCode != 200 {
89+
return "", nil, nil, fmt.Errorf("Couldn't retrieve cluster config: %s", body)
90+
}
91+
8392
var objmap map[string]*json.RawMessage
8493
err = json.Unmarshal(body, &objmap)
8594
if err != nil {
@@ -107,7 +116,7 @@ func GetClusterConfig(cfg *DevSpaceCloudConfig) (string, *api.Cluster, *api.Auth
107116
return namespace, cluster, authInfo, nil
108117
}
109118

110-
// Login loggs the user into the devspace cloud
119+
// Login logs the user into the devspace cloud
111120
func Login() (string, *api.Cluster, *api.AuthInfo, error) {
112121
tokenChannel := make(chan string)
113122
homedir, err := homedir.Dir()
@@ -147,6 +156,41 @@ func Login() (string, *api.Cluster, *api.AuthInfo, error) {
147156
return GetClusterConfig(&cfg)
148157
}
149158

159+
// Update updates the devspace cloud information if necessary
160+
func Update(config *v1.Config) error {
161+
// Don't update anything if we don't use the devspace cloud
162+
if *config.Cluster.DevSpaceCloud == false {
163+
return nil
164+
}
165+
166+
namespace, cluster, authInfo, err := CheckAuth()
167+
if err != nil {
168+
return err
169+
}
170+
171+
config.DevSpace.Release.Namespace = &namespace
172+
config.Services.Tiller.Release.Namespace = &namespace
173+
174+
if *config.Cluster.UseKubeConfig {
175+
err = UpdateKubeConfig(cluster, authInfo, true)
176+
if err != nil {
177+
return err
178+
}
179+
180+
config.Cluster.KubeContext = configutil.String(DevSpaceCloudContextName)
181+
} else {
182+
config.Cluster.APIServer = &cluster.Server
183+
config.Cluster.CaCert = configutil.String(string(cluster.CertificateAuthorityData))
184+
185+
config.Cluster.User = &v1.ClusterUser{
186+
ClientCert: configutil.String(string(authInfo.ClientCertificateData)),
187+
ClientKey: configutil.String(string(authInfo.ClientKeyData)),
188+
}
189+
}
190+
191+
return err
192+
}
193+
150194
// UpdateKubeConfig adds the devspace-cloud context if necessary and switches the current context
151195
func UpdateKubeConfig(cluster *api.Cluster, authInfo *api.AuthInfo, switchContext bool) error {
152196
config, err := kubeconfig.ReadKubeConfig(clientcmd.RecommendedHomeFile)

0 commit comments

Comments
 (0)