@@ -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
189186func (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-
821797func 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 {})
0 commit comments