Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/add/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func (cmd *providerCmd) RunAddProvider(cobraCmd *cobra.Command, args []string) {
log.Fatalf("Couldn't login to provider: %v", err)
}

// Switch default provider to newly added provider name
providerConfig.Default = providerName

err = config.SaveProviderConfig(providerConfig)
if err != nil {
log.Fatalf("Couldn't save provider config: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/create/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (cmd *spaceCmd) RunCreateSpace(cobraCmd *cobra.Command, args []string) {

// Change kube context
kubeContext := cloud.GetKubeContextNameFromSpace(space.Name, space.ProviderName)
err = cloud.UpdateKubeConfig(kubeContext, serviceAccount, true)
err = cloud.UpdateKubeConfig(kubeContext, serviceAccount, spaceID, provider.Name, true)
if err != nil {
log.Fatalf("Error saving kube config: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ devspace deploy --kube-context=deploy-context
deployCmd.Flags().StringVarP(&cmd.Namespace, "namespace", "n", "", "The namespace to deploy to")
deployCmd.Flags().StringVar(&cmd.KubeContext, "kube-context", "", "The kubernetes context to use for deployment")

deployCmd.Flags().BoolVar(&cmd.SwitchContext, "switch-context", false, "Switches the kube context to the deploy context")
deployCmd.Flags().BoolVar(&cmd.SwitchContext, "switch-context", true, "Switches the kube context to the deploy context")
deployCmd.Flags().BoolVar(&cmd.SkipPush, "skip-push", false, "Skips image pushing, useful for minikube deployment")

deployCmd.Flags().BoolVarP(&cmd.ForceBuild, "force-build", "b", false, "Forces to (re-)build every image")
Expand Down
2 changes: 1 addition & 1 deletion cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Starts your project in development mode:

devCmd.Flags().StringVarP(&cmd.Namespace, "namespace", "n", "", "The namespace to deploy to")

devCmd.Flags().BoolVar(&cmd.SwitchContext, "switch-context", false, "Switch kubectl context to the DevSpace context")
devCmd.Flags().BoolVar(&cmd.SwitchContext, "switch-context", true, "Switch kubectl context to the DevSpace context")
devCmd.Flags().BoolVar(&cmd.ExitAfterDeploy, "exit-after-deploy", false, "Exits the command after building the images and deploying the project")

return devCmd
Expand Down
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (cmd *InitCmd) Run(cobraCmd *cobra.Command, args []string) {
if configExists && cmd.Reconfigure == false {
log.Info("Config already exists. If you want to recreate the config please run `devspace init --reconfigure`")
log.Infof("\r \nIf you want to continue with the existing config, run:\n- `%s` to develop application\n- `%s` to deploy application\n", ansi.Color("devspace dev", "white+b"), ansi.Color("devspace deploy", "white+b"))
os.Exit(0)
return
}

// Delete config & overwrite config
Expand Down
7 changes: 3 additions & 4 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"net/http"
"os"
"strings"
"time"

Expand Down Expand Up @@ -130,7 +129,7 @@ func (cmd *OpenCmd) RunOpen(cobraCmd *cobra.Command, args []string) {
if err != nil {
log.Fatalf("Error retrieving space service account: %v", err)
}
err = cloud.UpdateKubeConfig(kubeContext, serviceAccount, true)
err = cloud.UpdateKubeConfig(kubeContext, serviceAccount, space.SpaceID, space.ProviderName, true)
if err != nil {
log.Fatalf("Error saving kube config: %v", err)
}
Expand Down Expand Up @@ -208,7 +207,7 @@ func (cmd *OpenCmd) RunOpen(cobraCmd *cobra.Command, args []string) {
log.StopWait()
open.Start(domain)
log.Donef("Successfully opened %s", domain)
os.Exit(0)
return
}

// Analyze space for issues
Expand All @@ -219,7 +218,7 @@ func (cmd *OpenCmd) RunOpen(cobraCmd *cobra.Command, args []string) {
if len(report) > 0 {
reportString := analyze.ReportToString(report)
log.WriteString(reportString)
os.Exit(1)
log.Fatal("")
}

time.Sleep(time.Second * 5)
Expand Down
70 changes: 58 additions & 12 deletions cmd/use/space.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package use

import (
"strconv"

"github.com/devspace-cloud/devspace/pkg/devspace/cloud"
cloudpkg "github.com/devspace-cloud/devspace/pkg/devspace/cloud"
"github.com/devspace-cloud/devspace/pkg/devspace/config/configutil"
Expand All @@ -10,7 +12,10 @@ import (
)

type spaceCmd struct {
provider string
Provider string
SpaceID string

GetToken bool
}

func newSpaceCmd() *cobra.Command {
Expand All @@ -30,25 +35,31 @@ devspace use space my-space
devspace use space none // stop using a space
#######################################################
`,
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Run: cmd.RunUseSpace,
}

useSpace.Flags().StringVar(&cmd.provider, "provider", "", "The cloud provider to use")
useSpace.Flags().StringVar(&cmd.Provider, "provider", "", "The cloud provider to use")
useSpace.Flags().StringVar(&cmd.SpaceID, "space-id", "", "The space id to use")
useSpace.Flags().BoolVar(&cmd.GetToken, "get-token", false, "Prints the service token to the console")

return useSpace
}

// RunUseDevSpace executes the functionality "devspace use space"
func (cmd *spaceCmd) RunUseSpace(cobraCmd *cobra.Command, args []string) {
if len(args) == 0 && cmd.SpaceID == "" {
log.Fatal("Either a space name or a space id have to be supplied")
}

// Set config root
configExists, err := configutil.SetDevSpaceRoot()
if err != nil {
log.Fatal(err)
}

// Erase currently used space
if args[0] == "none" {
if len(args) > 0 && args[0] == "none" {
// Set tiller env
err = cloudpkg.SetTillerNamespace(nil)
if err != nil {
Expand Down Expand Up @@ -79,29 +90,63 @@ func (cmd *spaceCmd) RunUseSpace(cobraCmd *cobra.Command, args []string) {

// Check if user has specified a certain provider
var cloudProvider *string
if cmd.provider != "" {
cloudProvider = &cmd.provider
if cmd.Provider != "" {
cloudProvider = &cmd.Provider
}

logger := log.GetInstance()
if cmd.GetToken == true {
logger = log.Discard
}

// Get cloud provider from config
provider, err := cloudpkg.GetProvider(cloudProvider, log.GetInstance())
provider, err := cloudpkg.GetProvider(cloudProvider, logger)
if err != nil {
log.Fatalf("Error getting cloud context: %v", err)
}
if provider == nil {
log.Fatal("No cloud provider specified")
}

if cmd.GetToken == true {
spaceID, err := strconv.Atoi(cmd.SpaceID)
if err != nil {
log.Fatalf("Error parsing space id: %v", err)
}

err = provider.PrintToken(spaceID)
if err != nil {
log.Fatal(err)
}

return
}

log.StartWait("Retrieving Space details")
var (
space *cloud.Space
)

space, err := provider.GetSpaceByName(args[0])
if err != nil {
log.Fatalf("Error retrieving Spaces details: %v", err)
if len(args) > 0 {
space, err = provider.GetSpaceByName(args[0])
if err != nil {
log.Fatalf("Error retrieving Spaces details: %v", err)
}
} else {
spaceID, err := strconv.Atoi(cmd.SpaceID)
if err != nil {
log.Fatalf("Error parsing space id: %v", err)
}

space, err = provider.GetSpace(spaceID)
if err != nil {
log.Fatalf("Error retrieving Spaces details: %v", err)
}
}

log.StopWait()

// Change kube context
// Get kube context name
kubeContext := cloud.GetKubeContextNameFromSpace(space.Name, space.ProviderName)

// Get service account
Expand All @@ -110,7 +155,8 @@ func (cmd *spaceCmd) RunUseSpace(cobraCmd *cobra.Command, args []string) {
log.Fatalf("Error retrieving space service account: %v", err)
}

err = cloud.UpdateKubeConfig(kubeContext, serviceAccount, true)
// Change kube context
err = cloud.UpdateKubeConfig(kubeContext, serviceAccount, space.SpaceID, provider.Name, true)
if err != nil {
log.Fatalf("Error saving kube config: %v", err)
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/devspace/cloud/config/versions/latest/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,25 @@ type Provider struct {
Token string `yaml:"token,omitempty"`

ClusterKey map[int]string `yaml:"clusterKeys,omitempty"`

// These are the cached space tokens
SpaceToken map[int]*SpaceToken `yaml:"spaceTokens,omitempty"`
}

// SpaceToken holds the information for a specific space
type SpaceToken struct {
// Service account information
Token string `yaml:"token"`
Namespace string `yaml:"namespace"`
Server string `yaml:"server"`
CaCert string `yaml:"caCert"`

// Cluster information
ClusterID int `yaml:"clusterID"`
ClusterName string `yaml:"clusterName"`
ClusterEncryptToken bool `yaml:"clusterEncryptToken"`

// Expires specifies when the token will expire
LastResume int64 `yaml:"lastResume,omitempty"`
Expires int64 `yaml:"expires,omitempty"`
}
9 changes: 7 additions & 2 deletions pkg/devspace/cloud/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud
import (
"encoding/base64"
"regexp"
"strconv"
"strings"

"github.com/devspace-cloud/devspace/pkg/devspace/cloud/config"
Expand Down Expand Up @@ -75,7 +76,7 @@ func GetKubeContextNameFromSpace(spaceName string, providerName string) string {
}

// UpdateKubeConfig updates the kube config and adds the spaceConfig context
func UpdateKubeConfig(contextName string, serviceAccount *ServiceAccount, setActive bool) error {
func UpdateKubeConfig(contextName string, serviceAccount *ServiceAccount, spaceID int, providerName string, setActive bool) error {
config, err := kubeconfig.LoadRawConfig()
if err != nil {
return err
Expand All @@ -90,7 +91,11 @@ func UpdateKubeConfig(contextName string, serviceAccount *ServiceAccount, setAct
cluster.CertificateAuthorityData = caCert

authInfo := api.NewAuthInfo()
authInfo.Token = serviceAccount.Token
authInfo.Exec = &api.ExecConfig{
APIVersion: "client.authentication.k8s.io/v1alpha1",
Command: "devspace",
Args: []string{"use", "space", "--provider", providerName, "--space-id", strconv.Itoa(spaceID), "--get-token"},
}

config.Clusters[contextName] = cluster
config.AuthInfos[contextName] = authInfo
Expand Down
6 changes: 3 additions & 3 deletions pkg/devspace/cloud/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestGetKubeContextNameFromSpace(t *testing.T) {
}

func TestUpdateKubeConfig(t *testing.T) {
err := UpdateKubeConfig("", &ServiceAccount{CaCert: "Undecodable"}, false)
err := UpdateKubeConfig("", &ServiceAccount{CaCert: "Undecodable"}, 1, "app.devspace.cloud", false)
assert.Error(t, err, "illegal base64 data at input byte 8", "No or wrong error when trying to update kube config with an undecodable cacert in the serviceaccount")

dir, err := ioutil.TempDir("", "test")
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestUpdateKubeConfig(t *testing.T) {
Namespace: "someNamespace",
Server: "someServer",
Token: "someToken",
}, true)
}, 1, "app.devspace.cloud", true)
assert.NilError(t, err, "Error when updating kube config with a vailid serviceaccount")
config, err := kubeconfig.LoadRawConfig()
assert.NilError(t, err, "Error loading kubeconfig")
Expand All @@ -72,5 +72,5 @@ func TestUpdateKubeConfig(t *testing.T) {
assert.Equal(t, config.Contexts["someContext"].Namespace, "someNamespace", "KubeConfig badly saved")
assert.Equal(t, config.Clusters["someContext"].Server, "someServer", "KubeConfig badly saved")
assert.Equal(t, len(config.Clusters["someContext"].CertificateAuthorityData), 0, "KubeConfig badly saved")
assert.Equal(t, config.AuthInfos["someContext"].Token, "someToken", "KubeConfig badly saved")
assert.Equal(t, config.AuthInfos["someContext"].Exec.Command, "devspace", "KubeConfig badly saved")
}
2 changes: 1 addition & 1 deletion pkg/devspace/cloud/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// GetClusterKey makes sure there is a correct key for the given cluster id
func (p *Provider) GetClusterKey(cluster *Cluster) (string, error) {
if cluster.Owner == nil || cluster.EncryptToken == false {
if cluster.EncryptToken == false {
return "", nil
}

Expand Down
Loading