Skip to content

Commit 2965985

Browse files
authored
Merge pull request #253 from covexo/docker-config
Docker config
2 parents 3c9c3b3 + 9073cba commit 2965985

10 files changed

Lines changed: 252 additions & 79 deletions

File tree

cmd/enter.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package cmd
2+
3+
import (
4+
helmClient "github.com/covexo/devspace/pkg/devspace/clients/helm"
5+
"github.com/covexo/devspace/pkg/devspace/clients/kubectl"
6+
"github.com/covexo/devspace/pkg/devspace/config/configutil"
7+
"github.com/covexo/devspace/pkg/util/log"
8+
"github.com/spf13/cobra"
9+
k8sv1 "k8s.io/api/core/v1"
10+
"k8s.io/client-go/kubernetes"
11+
kubectlExec "k8s.io/client-go/util/exec"
12+
)
13+
14+
// EnterCmd is a struct that defines a command call for "enter"
15+
type EnterCmd struct {
16+
flags *EnterCmdFlags
17+
helm *helmClient.HelmClientWrapper
18+
kubectl *kubernetes.Clientset
19+
pod *k8sv1.Pod
20+
}
21+
22+
// EnterCmdFlags are the flags available for the enter-command
23+
type EnterCmdFlags struct {
24+
container string
25+
}
26+
27+
func init() {
28+
cmd := &EnterCmd{
29+
flags: &EnterCmdFlags{},
30+
}
31+
32+
cobraCmd := &cobra.Command{
33+
Use: "enter",
34+
Short: "Enter your DevSpace",
35+
Long: `
36+
#######################################################
37+
################## devspace enter #####################
38+
#######################################################
39+
Execute a command or start a new terminal in your
40+
devspace:
41+
42+
devspace enter
43+
devspace enter bash
44+
devspace enter -c myContainer
45+
#######################################################`,
46+
Run: cmd.Run,
47+
}
48+
rootCmd.AddCommand(cobraCmd)
49+
50+
cobraCmd.Flags().StringVarP(&cmd.flags.container, "container", "c", "", "Container name within pod where to execute command")
51+
}
52+
53+
// Run executes the command logic
54+
func (cmd *EnterCmd) Run(cobraCmd *cobra.Command, args []string) {
55+
var err error
56+
log.StartFileLogging()
57+
58+
cmd.kubectl, err = kubectl.NewClient()
59+
if err != nil {
60+
log.Fatalf("Unable to create new kubectl client: %v", err)
61+
}
62+
63+
log.StartWait("Initializing helm client")
64+
cmd.helm, err = helmClient.NewClient(cmd.kubectl, false)
65+
log.StopWait()
66+
if err != nil {
67+
log.Fatalf("Error initializing helm client: %s", err.Error())
68+
}
69+
70+
// Check if we find a running release pod
71+
log.StartWait("Find a running devspace pod")
72+
pod, err := getRunningDevSpacePod(cmd.helm, cmd.kubectl)
73+
log.StopWait()
74+
if err != nil {
75+
log.Fatal("Cannot find a running devspace pod")
76+
}
77+
78+
enterTerminal(cmd.kubectl, pod, cmd.flags.container, args)
79+
}
80+
81+
func enterTerminal(client *kubernetes.Clientset, pod *k8sv1.Pod, containerNameOverride string, args []string) {
82+
var command []string
83+
config := configutil.GetConfig(false)
84+
85+
if len(args) == 0 && (config.DevSpace.Terminal.Command == nil || len(*config.DevSpace.Terminal.Command) == 0) {
86+
command = []string{
87+
"sh",
88+
"-c",
89+
"command -v bash >/dev/null 2>&1 && exec bash || exec sh",
90+
}
91+
} else {
92+
if len(args) > 0 {
93+
command = args
94+
} else {
95+
for _, cmd := range *config.DevSpace.Terminal.Command {
96+
command = append(command, *cmd)
97+
}
98+
}
99+
}
100+
101+
containerName := pod.Spec.Containers[0].Name
102+
if containerNameOverride != "" {
103+
containerName = containerNameOverride
104+
} else if config.DevSpace.Terminal.ContainerName != nil {
105+
containerName = *config.DevSpace.Terminal.ContainerName
106+
}
107+
108+
_, _, _, terminalErr := kubectl.Exec(client, pod, containerName, command, true, nil)
109+
if terminalErr != nil {
110+
if _, ok := terminalErr.(kubectlExec.CodeExitError); ok == false {
111+
log.Fatalf("Unable to start terminal session: %v", terminalErr)
112+
}
113+
}
114+
}

cmd/reset.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@ func (cmd *ResetCmd) Run(cobraCmd *cobra.Command, args []string) {
7373
if cmd.kubectl == nil || cmd.helm == nil {
7474
cmd.kubectl, err = kubectl.NewClient()
7575
if err != nil {
76-
log.Failf("Failed to initialize kubectl client: ", err.Error())
76+
log.Failf("Failed to initialize kubectl client: %v", err)
7777
}
7878
}
7979
cmd.determineResetExtent()
8080

8181
if cmd.flags.deleteRelease {
82+
log.StartWait("Deleting devspace release")
8283
err = cmd.deleteRelease()
84+
log.StopWait()
8385

8486
if err != nil {
8587
log.Failf("Error deleting release: %s", err.Error())
@@ -89,7 +91,9 @@ func (cmd *ResetCmd) Run(cobraCmd *cobra.Command, args []string) {
8991
}
9092

9193
if cmd.flags.deleteRegistry {
94+
log.StartWait("Deleting docker registry")
9295
err = cmd.deleteRegistry()
96+
log.StopWait()
9397

9498
if err != nil {
9599
log.Failf("Error deleting docker registry: %s", err.Error())
@@ -103,7 +107,9 @@ func (cmd *ResetCmd) Run(cobraCmd *cobra.Command, args []string) {
103107
}
104108

105109
if cmd.flags.deleteTiller {
110+
log.StartWait("Deleting tiller")
106111
err = cmd.deleteTiller()
112+
log.StopWait()
107113

108114
if err != nil {
109115
log.Failf("Error deleting tiller: %s", err.Error())
@@ -148,7 +154,7 @@ func (cmd *ResetCmd) Run(cobraCmd *cobra.Command, args []string) {
148154
err = cmd.deleteDevspaceFolder()
149155

150156
if err != nil {
151-
log.Failf("Error deleting .devspace folder: ", err.Error())
157+
log.Failf("Error deleting .devspace folder: %v", err)
152158

153159
if cmd.shouldContinue() == false {
154160
return
@@ -159,9 +165,12 @@ func (cmd *ResetCmd) Run(cobraCmd *cobra.Command, args []string) {
159165
}
160166

161167
if cmd.flags.deleteClusterRoleBinding {
168+
log.StartWait("Deleting cluster role bindings")
162169
err = cmd.kubectl.RbacV1beta1().ClusterRoleBindings().Delete(clusterRoleBindingName, &metav1.DeleteOptions{})
170+
log.StopWait()
171+
163172
if err != nil {
164-
log.Failf("Failed to remove ClusterRoleBinding: ", err.Error())
173+
log.Failf("Failed to remove ClusterRoleBinding: %v", err)
165174
} else {
166175
log.Done("Successfully deleted ClusterRoleBinding '" + clusterRoleBindingName + "'")
167176
}

cmd/up.go

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
k8sv1 "k8s.io/api/core/v1"
3939
k8sv1beta1 "k8s.io/api/rbac/v1beta1"
4040
"k8s.io/client-go/kubernetes"
41-
kubectlExec "k8s.io/client-go/util/exec"
4241
)
4342

4443
// UpCmd is a struct that defines a command call for "up"
@@ -70,7 +69,7 @@ var UpFlagsDefault = &UpCmdFlags{
7069
tiller: true,
7170
open: "cmd",
7271
initRegistries: true,
73-
build: true,
72+
build: false,
7473
sync: true,
7574
deploy: false,
7675
portforwarding: true,
@@ -106,13 +105,13 @@ Starts and connects your DevSpace:
106105

107106
cobraCmd.Flags().BoolVar(&cmd.flags.tiller, "tiller", cmd.flags.tiller, "Install/upgrade tiller")
108107
cobraCmd.Flags().BoolVar(&cmd.flags.initRegistries, "init-registries", cmd.flags.initRegistries, "Initialize registries (and install internal one)")
109-
cobraCmd.Flags().BoolVarP(&cmd.flags.build, "build", "b", cmd.flags.build, "Build image if Dockerfile has been modified")
108+
cobraCmd.Flags().BoolVarP(&cmd.flags.build, "build", "b", cmd.flags.build, "Force image build")
110109
cobraCmd.Flags().StringVarP(&cmd.flags.container, "container", "c", cmd.flags.container, "Container name where to open the shell")
111110
cobraCmd.Flags().BoolVar(&cmd.flags.sync, "sync", cmd.flags.sync, "Enable code synchronization")
112111
cobraCmd.Flags().BoolVar(&cmd.flags.verboseSync, "verbose-sync", cmd.flags.verboseSync, "When enabled the sync will log every file change")
113112
cobraCmd.Flags().BoolVar(&cmd.flags.portforwarding, "portforwarding", cmd.flags.portforwarding, "Enable port forwarding")
114-
cobraCmd.Flags().BoolVarP(&cmd.flags.deploy, "deploy", "d", cmd.flags.deploy, "Deploy chart")
115-
cobraCmd.Flags().BoolVar(&cmd.flags.noSleep, "no-sleep", cmd.flags.noSleep, "Enable no-sleep")
113+
cobraCmd.Flags().BoolVarP(&cmd.flags.deploy, "deploy", "d", cmd.flags.deploy, "Force chart deployment")
114+
cobraCmd.Flags().BoolVar(&cmd.flags.noSleep, "no-sleep", cmd.flags.noSleep, "Enable no-sleep (Override the containers.default.command and containers.default.args values with empty strings)")
116115
}
117116

118117
// Run executes the command logic
@@ -155,11 +154,9 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
155154
if cmd.flags.initRegistries {
156155
cmd.initRegistries()
157156
}
158-
mustRedeploy := false
159157

160-
if cmd.flags.build {
161-
mustRedeploy = cmd.buildImages(cobraCmd.Flags().Changed("build"))
162-
}
158+
// Build image if necessary
159+
mustRedeploy := cmd.buildImages()
163160

164161
// Check if we find a running release pod
165162
pod, err := getRunningDevSpacePod(cmd.helm, cmd.kubectl)
@@ -183,7 +180,7 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
183180
}()
184181
}
185182

186-
cmd.enterTerminal(args)
183+
enterTerminal(cmd.kubectl, cmd.pod, cmd.flags.container, args)
187184
}
188185

189186
func (cmd *UpCmd) ensureNamespace() error {
@@ -345,7 +342,7 @@ func (cmd *UpCmd) initRegistries() {
345342
}
346343
}
347344

348-
func (cmd *UpCmd) shouldRebuild(imageConf *v1.ImageConfig, dockerfilePath string, buildFlagChanged bool) bool {
345+
func (cmd *UpCmd) shouldRebuild(imageConf *v1.ImageConfig, dockerfilePath string) bool {
349346
var dockerfileModTime time.Time
350347

351348
mustRebuild := true
@@ -361,7 +358,7 @@ func (cmd *UpCmd) shouldRebuild(imageConf *v1.ImageConfig, dockerfilePath string
361358
dockerfileModTime = dockerfileInfo.ModTime()
362359

363360
// When user has not used -b or --build flags
364-
if buildFlagChanged == false {
361+
if cmd.flags.build == false {
365362
if imageConf.Build.LatestTimestamp != nil {
366363
latestBuildTime, _ := time.Parse(time.RFC3339Nano, *imageConf.Build.LatestTimestamp)
367364

@@ -370,13 +367,13 @@ func (cmd *UpCmd) shouldRebuild(imageConf *v1.ImageConfig, dockerfilePath string
370367
}
371368
}
372369
}
373-
imageConf.Build.LatestTimestamp = configutil.String(dockerfileModTime.Format(time.RFC3339Nano))
374370

371+
imageConf.Build.LatestTimestamp = configutil.String(dockerfileModTime.Format(time.RFC3339Nano))
375372
return mustRebuild
376373
}
377374

378375
// returns true when one of the images had to be rebuild
379-
func (cmd *UpCmd) buildImages(buildFlagChanged bool) bool {
376+
func (cmd *UpCmd) buildImages() bool {
380377
re := false
381378
config := configutil.GetConfig(false)
382379

@@ -391,10 +388,18 @@ func (cmd *UpCmd) buildImages(buildFlagChanged bool) bool {
391388
if imageConf.Build.ContextPath != nil {
392389
contextPath = *imageConf.Build.ContextPath
393390
}
394-
dockerfilePath = filepath.Join(cmd.workdir, strings.TrimPrefix(dockerfilePath, "."))
395-
contextPath = filepath.Join(cmd.workdir, strings.TrimPrefix(contextPath, "."))
396391

397-
if cmd.shouldRebuild(imageConf, dockerfilePath, buildFlagChanged) {
392+
dockerfilePath, err := filepath.Abs(dockerfilePath)
393+
if err != nil {
394+
log.Fatalf("Couldn't determine absolute path for %s", *imageConf.Build.DockerfilePath)
395+
}
396+
397+
contextPath, err = filepath.Abs(contextPath)
398+
if err != nil {
399+
log.Fatalf("Couldn't determine absolute path for %s", *imageConf.Build.ContextPath)
400+
}
401+
402+
if cmd.shouldRebuild(imageConf, dockerfilePath) {
398403
re = true
399404
imageTag, randErr := randutil.GenerateRandomString(7)
400405

@@ -482,6 +487,12 @@ func (cmd *UpCmd) buildImages(buildFlagChanged bool) bool {
482487
if imageConf.Build.Options.BuildArgs != nil {
483488
buildOptions.BuildArgs = *imageConf.Build.Options.BuildArgs
484489
}
490+
if imageConf.Build.Options.Target != nil {
491+
buildOptions.Target = *imageConf.Build.Options.Target
492+
}
493+
if imageConf.Build.Options.Network != nil {
494+
buildOptions.NetworkMode = *imageConf.Build.Options.Network
495+
}
485496
}
486497

487498
err = imageBuilder.BuildImage(contextPath, dockerfilePath, buildOptions)
@@ -783,41 +794,6 @@ func (cmd *UpCmd) startPortForwarding() {
783794
}
784795
}
785796

786-
func (cmd *UpCmd) enterTerminal(args []string) {
787-
var command []string
788-
config := configutil.GetConfig(false)
789-
790-
if len(args) == 0 && (config.DevSpace.Terminal.Command == nil || len(*config.DevSpace.Terminal.Command) == 0) {
791-
command = []string{
792-
"sh",
793-
"-c",
794-
"command -v bash >/dev/null 2>&1 && exec bash || exec sh",
795-
}
796-
} else {
797-
if len(args) > 0 {
798-
command = args
799-
} else {
800-
for _, cmd := range *config.DevSpace.Terminal.Command {
801-
command = append(command, *cmd)
802-
}
803-
}
804-
}
805-
806-
containerName := cmd.pod.Spec.Containers[0].Name
807-
if cmd.flags.container != "" {
808-
containerName = cmd.flags.container
809-
} else if config.DevSpace.Terminal.ContainerName != nil {
810-
containerName = *config.DevSpace.Terminal.ContainerName
811-
}
812-
813-
_, _, _, terminalErr := kubectl.Exec(cmd.kubectl, cmd.pod, containerName, command, true, nil)
814-
if terminalErr != nil {
815-
if _, ok := terminalErr.(kubectlExec.CodeExitError); ok == false {
816-
log.Fatalf("Unable to start terminal session: %v", terminalErr)
817-
}
818-
}
819-
}
820-
821797
func waitForPodReady(kubectl *kubernetes.Clientset, pod *k8sv1.Pod, maxWaitTime time.Duration, checkInterval time.Duration) error {
822798
for maxWaitTime > 0 {
823799
pod, err := kubectl.Core().Pods(pod.Namespace).Get(pod.Name, metav1.GetOptions{})

docs/docs/cli/enter.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: devspace enter
3+
---
4+
5+
Execute a command or start a new terminal in your devspace.
6+
7+
```bash
8+
Usage:
9+
devspace enter [flags]
10+
11+
Flags:
12+
-c, --container string Container name within pod where to execute command
13+
-h, --help help for enter
14+
15+
Examples:
16+
devspace enter
17+
devspace enter bash
18+
devspace enter echo 123
19+
devspace enter -c myContainer
20+
```

0 commit comments

Comments
 (0)