Skip to content

Commit 7a48b9e

Browse files
committed
Implement #252 & improve docs
1 parent 3c9c3b3 commit 7a48b9e

6 files changed

Lines changed: 104 additions & 39 deletions

File tree

cmd/up.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ var UpFlagsDefault = &UpCmdFlags{
7070
tiller: true,
7171
open: "cmd",
7272
initRegistries: true,
73-
build: true,
73+
build: false,
7474
sync: true,
7575
deploy: false,
7676
portforwarding: true,
@@ -106,13 +106,13 @@ Starts and connects your DevSpace:
106106

107107
cobraCmd.Flags().BoolVar(&cmd.flags.tiller, "tiller", cmd.flags.tiller, "Install/upgrade tiller")
108108
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")
109+
cobraCmd.Flags().BoolVarP(&cmd.flags.build, "build", "b", cmd.flags.build, "Force image build")
110110
cobraCmd.Flags().StringVarP(&cmd.flags.container, "container", "c", cmd.flags.container, "Container name where to open the shell")
111111
cobraCmd.Flags().BoolVar(&cmd.flags.sync, "sync", cmd.flags.sync, "Enable code synchronization")
112112
cobraCmd.Flags().BoolVar(&cmd.flags.verboseSync, "verbose-sync", cmd.flags.verboseSync, "When enabled the sync will log every file change")
113113
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")
114+
cobraCmd.Flags().BoolVarP(&cmd.flags.deploy, "deploy", "d", cmd.flags.deploy, "Force chart deployment")
115+
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)")
116116
}
117117

118118
// Run executes the command logic
@@ -155,11 +155,9 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
155155
if cmd.flags.initRegistries {
156156
cmd.initRegistries()
157157
}
158-
mustRedeploy := false
159158

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

164162
// Check if we find a running release pod
165163
pod, err := getRunningDevSpacePod(cmd.helm, cmd.kubectl)
@@ -345,7 +343,7 @@ func (cmd *UpCmd) initRegistries() {
345343
}
346344
}
347345

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

351349
mustRebuild := true
@@ -361,7 +359,7 @@ func (cmd *UpCmd) shouldRebuild(imageConf *v1.ImageConfig, dockerfilePath string
361359
dockerfileModTime = dockerfileInfo.ModTime()
362360

363361
// When user has not used -b or --build flags
364-
if buildFlagChanged == false {
362+
if cmd.flags.build == false {
365363
if imageConf.Build.LatestTimestamp != nil {
366364
latestBuildTime, _ := time.Parse(time.RFC3339Nano, *imageConf.Build.LatestTimestamp)
367365

@@ -370,13 +368,13 @@ func (cmd *UpCmd) shouldRebuild(imageConf *v1.ImageConfig, dockerfilePath string
370368
}
371369
}
372370
}
373-
imageConf.Build.LatestTimestamp = configutil.String(dockerfileModTime.Format(time.RFC3339Nano))
374371

372+
imageConf.Build.LatestTimestamp = configutil.String(dockerfileModTime.Format(time.RFC3339Nano))
375373
return mustRebuild
376374
}
377375

378376
// returns true when one of the images had to be rebuild
379-
func (cmd *UpCmd) buildImages(buildFlagChanged bool) bool {
377+
func (cmd *UpCmd) buildImages() bool {
380378
re := false
381379
config := configutil.GetConfig(false)
382380

@@ -391,10 +389,18 @@ func (cmd *UpCmd) buildImages(buildFlagChanged bool) bool {
391389
if imageConf.Build.ContextPath != nil {
392390
contextPath = *imageConf.Build.ContextPath
393391
}
394-
dockerfilePath = filepath.Join(cmd.workdir, strings.TrimPrefix(dockerfilePath, "."))
395-
contextPath = filepath.Join(cmd.workdir, strings.TrimPrefix(contextPath, "."))
396392

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

@@ -482,6 +488,12 @@ func (cmd *UpCmd) buildImages(buildFlagChanged bool) bool {
482488
if imageConf.Build.Options.BuildArgs != nil {
483489
buildOptions.BuildArgs = *imageConf.Build.Options.BuildArgs
484490
}
491+
if imageConf.Build.Options.Target != nil {
492+
buildOptions.Target = *imageConf.Build.Options.Target
493+
}
494+
if imageConf.Build.Options.Network != nil {
495+
buildOptions.NetworkMode = *imageConf.Build.Options.Network
496+
}
485497
}
486498

487499
err = imageBuilder.BuildImage(contextPath, dockerfilePath, buildOptions)

docs/docs/cli/up.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,37 @@
22
title: devspace up
33
---
44

5-
With `devspace up`, you build your image, start your DevSpace and connect to it.
5+
With `devspace up`, you build your image, start your DevSpace and connect to it.
6+
7+
The command will do the following:
8+
9+
1. Ensure that a tiller server is available (if not it will automatically deploy one to the specified namespace)
10+
2. Optionally it will deploy a docker registry if this was desired
11+
3. Build the docker image if changed or forced by -b
12+
* Push the built image to the specified registry
13+
5. Redeploy the chart if release was not found, image was rebuilt or -d option was specified
14+
6. Establish port forwarding and sync
15+
7. Execute the specified command in the container (default: open a terminal)
616

717
```bash
818
Usage:
919
devspace up [flags]
1020

1121
Flags:
12-
-b, --build Build image if Dockerfile has been modified (default true)
22+
-b, --build Force image build
1323
-c, --container string Container name where to open the shell
14-
-d, --deploy Deploy chart
24+
-d, --deploy Force chart deployment
1525
-h, --help help for up
1626
--init-registries Initialize registries (and install internal one) (default true)
17-
--no-sleep Enable no-sleep
27+
--no-sleep Enable no-sleep (Override the containers.default.command and containers.default.args values with empty strings)
1828
--portforwarding Enable port forwarding (default true)
1929
--sync Enable code synchronization (default true)
2030
--tiller Install/upgrade tiller (default true)
2131
--verbose-sync When enabled the sync will log every file change
2232

2333
Examples:
2434
devspace up # Start the devspace
25-
devspace up bash # Execute bash command
35+
devspace up bash # Execute bash command after deploying
2636
```
2737

2838
**Note**: Every time you run `devspace up`, your containers will be re-deployed. This way, you will always start with a clean state.

docs/docs/configuration/config.yaml.md

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ This is an example of a [.devspace/config.yaml](#)
77
# Devspace version, currently is always v1
88
version: v1
99
devSpace:
10+
# terminal options for devspace up and devspace enter
11+
terminal:
12+
# the container name within the selected release pod to open a terminal connection to (is also a flag in `devspace up -c CONTAINER`)
13+
containerName: default
14+
# the command to execute within the container when using `devspace up` or `devspace enter`
15+
command:
16+
- sh
17+
- -c
18+
- bash
1019
release:
1120
# Name of helm release that is used for deploying
1221
# the devspace chart (contents of /chart)
@@ -33,6 +42,8 @@ devSpace:
3342
- resourceType: pod
3443
labelSelector:
3544
release: my-app
45+
# The container within the pod to sync to
46+
containerName: default
3647
# Sync the complete local project path
3748
localSubPath: ./
3849
# Into the remote container path /app
@@ -49,7 +60,20 @@ images:
4960
tag: 9u5ye0G
5061
# Registry where the build image will be pushed to
5162
registry: default
63+
# Specifies where the docker context path is
64+
contextPath: ./
65+
# Specifies where the Dockerfile lies
66+
dockerfilePath: ./Dockerfile
67+
# Specifies how to build the image
5268
build:
69+
options:
70+
# Used for multi-stage builds
71+
target: development
72+
# buildArgs passed to docker during build
73+
buildArgs:
74+
myarg1: myvalue1
75+
# network mode (see [network](https://docs.docker.com/network/))
76+
network: bridge
5377
engine:
5478
docker:
5579
# Use docker for image building
@@ -103,7 +127,12 @@ A [.devspace/config.yaml](#) contains any public/shared configuration for runnin
103127
**Note: You can easily re-configure your DevSpace by running `devspace init -r`.**
104128

105129
## devspace
106-
Defines the DevSpace including everything related to portForwarding, sync, and the helm release config.
130+
Defines the DevSpace including everything related to terminal, portForwarding, sync, and the helm release config.
131+
132+
### devspace.terminal
133+
In this section options are defined, what should happen when devspace up or devspace enter connect to the release pod.
134+
- `containerName` *string* the name of the container to connect to within the selected pod (default is the first defined container)
135+
- `command` *string array* the default command that is executed when entering a pod with devspace up or devspace enter (default is: ["sh", "-c", "command -v bash >/dev/null 2>&1 && exec bash || exec sh"])
107136

108137
### devspace.release
109138
Defines how the DevSpace is deployed to your cluster. See [Type: Release](#type-release) for details.
@@ -116,15 +145,16 @@ To access applications running inside a DevSpace, the DevSpace CLI allows to con
116145

117146
### devspace.portForwarding[].portMappings[]
118147
PortMapping:
119-
- `localPort` *string* on localhost
120-
- `remotePort` *string* remote pod port
148+
- `localPort` *string* the local port on the machine
149+
- `remotePort` *string* the remote pod port
121150

122151
In the example above, you could open `localhost:8080` inside your browser to see the output of the application listening on port 80 within your DevSpace.
123152

124153
### devspace.sync
125154
To comfortably sync code to a DevSpace, the DevSpace CLI allows to configure real-time code synchronizations. A sync config consists of the following:
126155
- `resourceType` *string* kubernetes resource type that is selected (currently only `pod` is supported)
127-
- `labelSelector` *map[string]string* usually the release/app name
156+
- `labelSelector` *map[string]string* label selector to select the correct pod (usually the release/app name)
157+
- `containerName` *string* the name of the container within the pod to sync to (default: the first specified container in the pod)
128158
- `localSubPath` *string* relative path to the folder that should be synced (default: path to your local project root)
129159
- `containerPath` *string* absolute path within the container
130160
- `excludePaths` *string array* paths to exclude files/folders from sync in .gitignore syntax
@@ -138,20 +168,29 @@ This section of the config defines a map of images that can be used in the helm
138168

139169
### images[]
140170
An image is defined by:
141-
- `name` *string* of the image that is being pushed to the registry
142-
- `tag` *string* stating the latest tag pushed to the registry (auto-generated)
143-
- `registry` *string* referencing one of the keys defined in the `registries` map
171+
- `name` *string* name of the image that is being pushed to the registry
172+
- `tag` *string* tag indicates the latest tag pushed to the registry (auto-generated)
173+
- `registry` *string* registry references one of the keys defined in the `registries` map
144174
- `build` *BuildConfig* defines the build procedure for this image
145175

146176
### images[].build
147177
BuildConfig:
148-
- `engine` *Engine* The engine that should be used for building the image
178+
- `dockerfilePath` *string* specifies the path where the dockerfile lies (default: ./Dockerfile)
179+
- `contextPath` *string* specifies the context path for docker (default: ./)
180+
- `engine` *Engine* the engine that should be used for building the image
181+
- `options` *BuildOptions* additional options used for building the image
182+
183+
### images[].build.options
184+
BuildOptions:
185+
- `buildArgs` *map[string]string* key-value map used for specifying build arguments passed to docker
186+
- `target` *string* the target used for multi-stage builds (see [multi-stage-build](https://docs.docker.com/develop/develop-images/multistage-build/))
187+
- `network` *string* the network mode used for building the image (see [network](https://docs.docker.com/network/))
149188

150189
### images[].build.engine
151190
Engine:
152191
An image build is mainly defined by the build engine. There are 2 build engines currently supported (choose only one):
153-
- `docker` *DockerConfig* uses the local Docker daemon or a Docker daemon running inside a Minikube cluster (if `preferMinikube` == true)
154-
- `kaniko` *KanikoConfig* builds images in userspace within a build pod running inside the Kubernetes cluster
192+
- `docker` *DockerConfig* use the local Docker daemon or a Docker daemon running inside a Minikube cluster (if `preferMinikube` == true)
193+
- `kaniko` *KanikoConfig* build images in userspace within a build pod running inside the Kubernetes cluster
155194

156195
### images[].build.engine.docker
157196
DockerConfig:
@@ -168,25 +207,25 @@ This section of the config defines a map of image registries. You can use any ex
168207

169208
### registries[]
170209
ImageRegistry:
171-
- `url` *string* of the registry (format: myregistry.com:port)
210+
- `url` *string* the url of the registry (format: myregistry.com:port)
172211
- `insecure` *bool* flag to allow pushing to registries without HTTPS
173212
- `user` *RegistryUser* credentials for pushing to / pulling from the registry
174213

175214
### registries[].user
176215
RegistryUser:
177-
- `username` *string* that should be used for pushing and pulling from the registry
178-
- `password` *string* that should be used for pushing and pulling from the registry
216+
- `username` *string* the user that should be used for pushing and pulling from the registry
217+
- `password` *string* the password should be used for pushing and pulling from the registry
179218

180219
## services
181220
Defines cluster services that the DevSpace uses.
182221

183222
### services.internalRegistry
184223
The `internalRegistry` is used to tell the DevSpace CLI to deploy a private registry inside the Kubernetes cluster:
185-
- `release` *Release* for deploying the registry (see [Type: Release](#type-release))
224+
- `release` *Release* release options for deploying the registry (see [Type: Release](#type-release))
186225

187226
### services.tiller
188227
The `tiller` service is defined by:
189-
- `release` *Release* definition for tiller (see [Type: Release](#type-release))
228+
- `release` *Release* release definition for tiller (see [Type: Release](#type-release))
190229
- `appNamespaces` *string array* defines a list of namespace that tiller may deploy applications to
191230

192231
## cluster
@@ -206,6 +245,6 @@ ClusterUser:
206245

207246
## Type: Release
208247
A `release` is specified through:
209-
- `name` *string* of the release
210-
- `namespace` *string* to deploy the release to
211-
- `values` *map[string] any* that are set during the deployment (contents of the values.yaml in helm)
248+
- `name` *string* name of the release
249+
- `namespace` *string* the namespace to deploy the release to
250+
- `values` *map[string] any* override values that are set during the deployment (contents of the values.yaml in helm)

pkg/devspace/builder/docker/docker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ func (b *Builder) BuildImage(contextPath, dockerfilePath string, options *types.
128128
Tags: []string{b.imageURL},
129129
Dockerfile: relDockerfile,
130130
BuildArgs: options.BuildArgs,
131+
Target: options.Target,
132+
NetworkMode: options.NetworkMode,
131133
AuthConfigs: authConfigs,
132134
})
133135
if err != nil {

pkg/devspace/config/v1/image.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ type DockerBuildEngine struct {
3838
//BuildOptions defines options for building Docker images
3939
type BuildOptions struct {
4040
BuildArgs *map[string]*string `yaml:"buildArgs"`
41+
Target *string `yaml:"target"`
42+
Network *string `yaml:"network"`
4143
}

pkg/devspace/config/v1/terminal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package v1
33
// Terminal describes the terminal options
44
type Terminal struct {
55
ContainerName *string `yaml:"containerName"`
6-
Command *[]*string `yaml:"shell"`
6+
Command *[]*string `yaml:"command"`
77
}

0 commit comments

Comments
 (0)