|
8 | 8 | "os" |
9 | 9 | "path/filepath" |
10 | 10 |
|
| 11 | + "github.com/covexo/devspace/pkg/devspace/config/configutil" |
| 12 | + "github.com/covexo/devspace/pkg/devspace/config/v1" |
11 | 13 | "github.com/covexo/devspace/pkg/util/kubeconfig" |
12 | 14 | "github.com/covexo/devspace/pkg/util/yamlutil" |
13 | 15 |
|
@@ -37,7 +39,7 @@ type DevSpaceCloudConfig struct { |
37 | 39 | Token string `yaml:"token"` |
38 | 40 | } |
39 | 41 |
|
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 |
41 | 43 | func CheckAuth() (string, *api.Cluster, *api.AuthInfo, error) { |
42 | 44 | homedir, err := homedir.Dir() |
43 | 45 | if err != nil { |
@@ -80,6 +82,13 @@ func GetClusterConfig(cfg *DevSpaceCloudConfig) (string, *api.Cluster, *api.Auth |
80 | 82 | return "", nil, nil, err |
81 | 83 | } |
82 | 84 |
|
| 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 | + |
83 | 92 | var objmap map[string]*json.RawMessage |
84 | 93 | err = json.Unmarshal(body, &objmap) |
85 | 94 | if err != nil { |
@@ -107,7 +116,7 @@ func GetClusterConfig(cfg *DevSpaceCloudConfig) (string, *api.Cluster, *api.Auth |
107 | 116 | return namespace, cluster, authInfo, nil |
108 | 117 | } |
109 | 118 |
|
110 | | -// Login loggs the user into the devspace cloud |
| 119 | +// Login logs the user into the devspace cloud |
111 | 120 | func Login() (string, *api.Cluster, *api.AuthInfo, error) { |
112 | 121 | tokenChannel := make(chan string) |
113 | 122 | homedir, err := homedir.Dir() |
@@ -147,6 +156,41 @@ func Login() (string, *api.Cluster, *api.AuthInfo, error) { |
147 | 156 | return GetClusterConfig(&cfg) |
148 | 157 | } |
149 | 158 |
|
| 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 | + |
150 | 194 | // UpdateKubeConfig adds the devspace-cloud context if necessary and switches the current context |
151 | 195 | func UpdateKubeConfig(cluster *api.Cluster, authInfo *api.AuthInfo, switchContext bool) error { |
152 | 196 | config, err := kubeconfig.ReadKubeConfig(clientcmd.RecommendedHomeFile) |
|
0 commit comments