Skip to content

Commit 201f094

Browse files
committed
Enable option to add generic cloud provider
1 parent 9605c50 commit 201f094

5 files changed

Lines changed: 223 additions & 103 deletions

File tree

cmd/init.go

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -352,29 +352,74 @@ func (cmd *InitCmd) configureTiller() {
352352
}
353353
}
354354

355-
func (cmd *InitCmd) useDevSpaceCloud() bool {
356-
useDevSpaceCloud := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
357-
Question: "Do you want to use the devspace cloud? (free ready-to-use kubernetes) (yes | no)",
358-
DefaultValue: "yes",
359-
ValidationRegexPattern: "^(yes)|(no)$",
360-
}) == "yes"
355+
func (cmd *InitCmd) useCloudProvider() bool {
356+
providerConfig, err := cloud.ParseCloudConfig()
357+
if err != nil {
358+
log.Fatalf("Error loading cloud config: %v", err)
359+
}
360+
361+
if len(providerConfig) > 1 {
362+
cloudProvider := "("
363+
364+
for name, _ := range providerConfig {
365+
if len(cloudProvider) > 1 {
366+
", "
367+
}
368+
369+
cloudProvider += name
370+
}
371+
372+
cloudProvider += ")"
373+
cloudProviderSelected := ""
374+
375+
for _, ok := providerConfig[cloudProviderSelected]; ok == false && cloudProviderSelected != "no"; {
376+
cloudProviderSelected := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
377+
Question: "Do you want to use a cloud provider? (no to skip) " + cloudProvider,
378+
DefaultValue: cloud.DevSpaceCloudProviderName,
379+
})
380+
}
381+
382+
if cloudProviderSelected != "no" {
383+
addToContext := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
384+
Question: "Do you want to add the cloud provider to the $HOME/.kube/config file? (yes | no)",
385+
DefaultValue: "yes",
386+
ValidationRegexPattern: "^(yes)|(no)$",
387+
}) == "yes"
361388

362-
if useDevSpaceCloud {
363-
addToContext := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
364-
Question: "Do you want to add the devspace-cloud to the $HOME/.kube/config file? (yes | no)",
389+
cmd.config.Cluster.CloudProvider = &cloudProviderSelected
390+
cmd.config.Cluster.UseKubeConfig = &addToContext
391+
392+
err := cloud.Update(providerConfig, cmd.config, true)
393+
if err != nil {
394+
log.Fatalf("Couldn't authenticate to devspace cloud: %v", err)
395+
}
396+
397+
return true
398+
}
399+
} else {
400+
useDevSpaceCloud := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
401+
Question: "Do you want to use the devspace cloud? (free ready-to-use kubernetes) (yes | no)",
365402
DefaultValue: "yes",
366403
ValidationRegexPattern: "^(yes)|(no)$",
367404
}) == "yes"
368405

369-
cmd.config.Cluster.DevSpaceCloud = &useDevSpaceCloud
370-
cmd.config.Cluster.UseKubeConfig = &addToContext
406+
if useDevSpaceCloud {
407+
addToContext := *stdinutil.GetFromStdin(&stdinutil.GetFromStdinParams{
408+
Question: "Do you want to add the devspace-cloud to the $HOME/.kube/config file? (yes | no)",
409+
DefaultValue: "yes",
410+
ValidationRegexPattern: "^(yes)|(no)$",
411+
}) == "yes"
371412

372-
err := cloud.Update(cmd.config, true)
373-
if err != nil {
374-
log.Fatalf("Couldn't authenticate to devspace cloud: %v", err)
375-
}
413+
cmd.config.Cluster.CloudProvider = &cloud.DevSpaceCloudProviderName
414+
cmd.config.Cluster.UseKubeConfig = &addToContext
415+
416+
err := cloud.Update(providerConfig, cmd.config, true)
417+
if err != nil {
418+
log.Fatalf("Couldn't authenticate to devspace cloud: %v", err)
419+
}
376420

377-
return true
421+
return true
422+
}
378423
}
379424

380425
return false

pkg/devspace/clients/kubectl/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ func GetClientConfig() (*rest.Config, error) {
5252
}
5353

5454
// Update devspace cloud cluster config
55-
if config.Cluster.DevSpaceCloud != nil && *config.Cluster.DevSpaceCloud {
56-
err := cloud.Update(config, false)
55+
if config.Cluster.CloudProvider != nil && *config.Cluster.CloudProvider != "" {
56+
providerConfig, err := cloud.ParseCloudConfig()
57+
if err != nil {
58+
log.Fatalf("Couldn't load cloud provider config: %v", err)
59+
}
60+
61+
err = cloud.Update(providerConfig, config, false)
5762
if err != nil {
5863
log.Warnf("Couldn't update devspace cloud cluster information: %v", err)
5964
}

pkg/devspace/cloud/config.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package cloud
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
8+
homedir "github.com/mitchellh/go-homedir"
9+
yaml "gopkg.in/yaml.v2"
10+
)
11+
12+
// DevSpaceCloudConfigPath holds the path to the cloud config file
13+
const DevSpaceCloudConfigPath = ".devspace/clouds.yaml"
14+
15+
// DevSpaceKubeContextName is the name for the kube config context
16+
const DevSpaceKubeContextName = "devspace"
17+
18+
// ProviderConfig holds all the different providers and their configuration
19+
type ProviderConfig map[string]*Provider
20+
21+
// Provider describes the struct to hold the cloud configuration
22+
type Provider struct {
23+
Name string `yaml:"name,omitempty"`
24+
KubeContext string `yaml:"kubecontext,omitempty"`
25+
Login string `yaml:"login,omitempty"`
26+
GetConfig string `yaml:"getConfig,omitempty"`
27+
Token string `yaml:"token,omitempty"`
28+
}
29+
30+
// DevSpaceCloudProviderName is the name of the default devspace-cloud provider
31+
const DevSpaceCloudProviderName = "devspace-cloud"
32+
33+
// DevSpaceCloudProviderConfig holds the information for the devspace-cloud
34+
var DevSpaceCloudProviderConfig = &Provider{
35+
Login: "https://cloud.devspace.covexo.com/login",
36+
GetConfig: "https://cloud.devspace.covexo.com/clusterConfig",
37+
KubeContext: DevSpaceKubeContextName,
38+
}
39+
40+
// ParseCloudConfig parses the cloud configuration and returns a map containing the configurations
41+
func ParseCloudConfig() (ProviderConfig, error) {
42+
homedir, err := homedir.Dir()
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
data, err := ioutil.ReadFile(filepath.Join(homedir, DevSpaceCloudConfigPath))
48+
if os.IsNotExist(err) {
49+
return ProviderConfig{
50+
DevSpaceCloudProviderName: DevSpaceCloudProviderConfig,
51+
}, nil
52+
}
53+
54+
cloudConfig := make(ProviderConfig)
55+
err = yaml.Unmarshal(data, cloudConfig)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
if _, ok := cloudConfig[DevSpaceCloudProviderName]; ok {
61+
cloudConfig[DevSpaceCloudProviderName].GetConfig = DevSpaceCloudProviderConfig.GetConfig
62+
cloudConfig[DevSpaceCloudProviderName].Login = DevSpaceCloudProviderConfig.Login
63+
} else {
64+
cloudConfig[DevSpaceCloudProviderName] = DevSpaceCloudProviderConfig
65+
}
66+
67+
for configName, config := range cloudConfig {
68+
config.Name = configName
69+
}
70+
71+
return cloudConfig, nil
72+
}
73+
74+
// SaveCloudConfig saves the provider configuration to file
75+
func SaveCloudConfig(config ProviderConfig) error {
76+
homedir, err := homedir.Dir()
77+
if err != nil {
78+
return err
79+
}
80+
81+
cfgPath := filepath.Join(homedir, DevSpaceCloudConfigPath)
82+
83+
for name, provider := range config {
84+
provider.Name = ""
85+
86+
if name == DevSpaceCloudProviderName {
87+
provider.Login = ""
88+
provider.GetConfig = ""
89+
}
90+
}
91+
92+
out, err := yaml.Marshal(config)
93+
if err != nil {
94+
return err
95+
}
96+
97+
err = os.MkdirAll(filepath.Dir(cfgPath), 0755)
98+
if err != nil {
99+
return err
100+
}
101+
102+
return ioutil.WriteFile(cfgPath, out, 0600)
103+
}

0 commit comments

Comments
 (0)