Skip to content

Commit 60ae979

Browse files
committed
refactor: refactor dev section
1 parent 790a899 commit 60ae979

File tree

26 files changed

+533
-430
lines changed

26 files changed

+533
-430
lines changed

cmd/init.go

Lines changed: 39 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,11 @@ func (cmd *InitCmd) addDevConfig(config *latest.Config, imageName, image string,
491491
}
492492

493493
portMapping := latest.PortMapping{
494-
LocalPort: &port,
494+
Port: fmt.Sprintf("%d", port),
495495
}
496-
497496
if port != localPort {
498497
portMapping = latest.PortMapping{
499-
LocalPort: &localPort,
500-
RemotePort: &port,
498+
Port: fmt.Sprintf("%d:%d", &localPort, &port),
501499
}
502500
}
503501

@@ -514,8 +512,7 @@ func (cmd *InitCmd) addDevConfig(config *latest.Config, imageName, image string,
514512
ImageSelector: image,
515513
}
516514
}
517-
518-
config.Dev["default"].Forward = portMappings
515+
config.Dev["default"].Ports = portMappings
519516

520517
// Add dev.open config
521518
config.Dev["default"].Open = []*latest.OpenConfig{
@@ -674,73 +671,50 @@ func (cmd *InitCmd) addProfileConfig(config *latest.Config, imageName string) er
674671
}
675672

676673
// If image building is disabled, move it to production profile instead of disabling it
677-
if imageConfig.Build != nil {
678-
imageConfig.AppendDockerfileInstructions = []string{}
679-
imageConfig.InjectRestartHelper = false
680-
imageConfig.RebuildStrategy = latest.RebuildStrategyDefault
681-
imageConfig.Entrypoint = []string{}
682-
683-
if imageConfig.Build.Docker != nil && imageConfig.Build.Docker.Options != nil && imageConfig.Build.Docker.Options.Target != "" {
684-
imageConfig.Build.Docker.Options.Target = ""
685-
}
686-
687-
if imageConfig.Build.Docker == nil && imageConfig.Build.BuildKit == nil && imageConfig.Build.Kaniko == nil {
688-
imageConfig.Build = nil
689-
}
690-
691-
profile.Merge = &latest.ProfileConfigStructure{
692-
Images: map[string]interface{}{
693-
imageName: imageConfig,
694-
},
695-
}
696-
697-
delete(config.Images, imageName)
698-
} else {
699-
patchRemoveOp := "remove"
700-
patches := []*latest.PatchConfig{}
701-
702-
if len(imageConfig.AppendDockerfileInstructions) > 0 {
703-
patches = append(patches, &latest.PatchConfig{
704-
Operation: patchRemoveOp,
705-
Path: "images." + imageName + ".appendDockerfileInstructions",
706-
})
707-
}
674+
patchRemoveOp := "remove"
675+
patches := []*latest.PatchConfig{}
708676

709-
if imageConfig.InjectRestartHelper {
710-
patches = append(patches, &latest.PatchConfig{
711-
Operation: patchRemoveOp,
712-
Path: "images." + imageName + ".injectRestartHelper",
713-
})
714-
}
677+
if len(imageConfig.AppendDockerfileInstructions) > 0 {
678+
patches = append(patches, &latest.PatchConfig{
679+
Operation: patchRemoveOp,
680+
Path: "images." + imageName + ".appendDockerfileInstructions",
681+
})
682+
}
715683

716-
if imageConfig.RebuildStrategy != latest.RebuildStrategyDefault {
717-
patches = append(patches, &latest.PatchConfig{
718-
Operation: patchRemoveOp,
719-
Path: "images." + imageName + ".rebuildStrategy",
720-
})
721-
}
684+
if imageConfig.InjectRestartHelper {
685+
patches = append(patches, &latest.PatchConfig{
686+
Operation: patchRemoveOp,
687+
Path: "images." + imageName + ".injectRestartHelper",
688+
})
689+
}
722690

723-
if len(imageConfig.Entrypoint) > 0 {
724-
patches = append(patches, &latest.PatchConfig{
725-
Operation: patchRemoveOp,
726-
Path: "images." + imageName + ".entrypoint",
727-
})
728-
}
691+
if imageConfig.RebuildStrategy != latest.RebuildStrategyDefault {
692+
patches = append(patches, &latest.PatchConfig{
693+
Operation: patchRemoveOp,
694+
Path: "images." + imageName + ".rebuildStrategy",
695+
})
696+
}
729697

730-
if imageConfig.Build != nil && imageConfig.Build.Docker != nil && imageConfig.Build.Docker.Options != nil && imageConfig.Build.Docker.Options.Target != "" {
731-
patches = append(patches, &latest.PatchConfig{
732-
Operation: patchRemoveOp,
733-
Path: "images." + imageName + ".build.docker.options.target",
734-
})
735-
}
698+
if len(imageConfig.Entrypoint) > 0 {
699+
patches = append(patches, &latest.PatchConfig{
700+
Operation: patchRemoveOp,
701+
Path: "images." + imageName + ".entrypoint",
702+
})
703+
}
736704

737-
if len(patches) == 0 {
738-
return nil
739-
}
705+
if imageConfig.Target != "" {
706+
patches = append(patches, &latest.PatchConfig{
707+
Operation: patchRemoveOp,
708+
Path: "images." + imageName + ".build.docker.options.target",
709+
})
710+
}
740711

741-
profile.Patches = patches
712+
if len(patches) == 0 {
713+
return nil
742714
}
743715

716+
profile.Patches = patches
717+
744718
config.Profiles = append(config.Profiles, profile)
745719

746720
}

cmd/open.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,7 @@ func (cmd *OpenCmd) openLocal(ctx *devspacecontext.Context, domain string) error
308308
domain = "http://localhost:" + strconv.Itoa(localPort)
309309
portMappings := []*latest.PortMapping{
310310
{
311-
LocalPort: &localPort,
312-
RemotePort: &servicePort,
311+
Port: fmt.Sprintf("%d:%d", localPort, servicePort),
313312
},
314313
}
315314

@@ -321,7 +320,7 @@ func (cmd *OpenCmd) openLocal(ctx *devspacecontext.Context, domain string) error
321320
devPod := &latest.DevPod{
322321
Name: "open",
323322
LabelSelector: labelSelector,
324-
Forward: portMappings,
323+
Ports: portMappings,
325324
}
326325
fakeConfig := &latest.Config{
327326
Dev: map[string]*latest.DevPod{

cmd/sync.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ type SyncCmd struct {
3939
Wait bool
4040
Polling bool
4141

42-
Exclude []string
43-
ContainerPath string
44-
LocalPath string
42+
Exclude []string
43+
Path string
4544

4645
InitialSync string
4746

@@ -92,8 +91,7 @@ devspace sync --container-path=/my-path
9291
syncCmd.Flags().BoolVar(&cmd.Pick, "pick", true, "Select a pod")
9392

9493
syncCmd.Flags().StringSliceVarP(&cmd.Exclude, "exclude", "e", []string{}, "Exclude directory from sync")
95-
syncCmd.Flags().StringVar(&cmd.LocalPath, "local-path", "", "Local path to use (Default is current directory")
96-
syncCmd.Flags().StringVar(&cmd.ContainerPath, "container-path", "", "Container path to use (Default is working directory)")
94+
syncCmd.Flags().StringVar(&cmd.Path, "path", "", "Path to use (Default is current directory). Example: ./local-path:/remote-path or local-path:.")
9795

9896
syncCmd.Flags().BoolVar(&cmd.DownloadOnInitialSync, "download-on-initial-sync", true, "DEPRECATED: Downloads all locally non existing remote files in the beginning")
9997
syncCmd.Flags().StringVar(&cmd.InitialSync, "initial-sync", "", "The initial sync strategy to use (mirrorLocal, mirrorRemote, preferLocal, preferRemote, preferNewest, keepAll)")
@@ -214,17 +212,18 @@ func (cmd *SyncCmd) Run(f factory.Factory) error {
214212
}
215213
if cmd.GlobalFlags.ConfigPath != "" && configInterface != nil {
216214
devSection := configInterface.Config().Dev
217-
218215
syncConfigs := []nameConfig{}
219216
for _, v := range devSection {
220-
for _, s := range v.Sync {
221-
syncConfigs = append(syncConfigs, fromSyncConfig(v, v.Container, s))
222-
}
223-
for _, c := range v.Containers {
224-
for _, s := range c.Sync {
225-
syncConfigs = append(syncConfigs, fromSyncConfig(v, c.Container, s))
217+
loader.EachDevContainer(v, func(devContainer *latest.DevContainer) bool {
218+
for _, s := range devContainer.Sync {
219+
n, err := fromSyncConfig(v, devContainer.Container, s)
220+
if err != nil {
221+
return true
222+
}
223+
syncConfigs = append(syncConfigs, n)
226224
}
227-
}
225+
return true
226+
})
228227
}
229228
if len(syncConfigs) == 0 {
230229
return fmt.Errorf("no sync config found in %s", cmd.GlobalFlags.ConfigPath)
@@ -277,15 +276,10 @@ func (cmd *SyncCmd) Run(f factory.Factory) error {
277276
return sync.StartSyncFromCmd(ctx, targetselector.NewTargetSelector(options), syncConfig.syncConfig, cmd.NoWatch, cmd.Verbose)
278277
}
279278

280-
func fromSyncConfig(devPod *latest.DevPod, containerName string, sc *latest.SyncConfig) nameConfig {
281-
localPath := sc.LocalSubPath
282-
if localPath == "" {
283-
localPath = "."
284-
}
285-
286-
remotePath := sc.ContainerPath
287-
if remotePath == "" {
288-
remotePath = "."
279+
func fromSyncConfig(devPod *latest.DevPod, containerName string, sc *latest.SyncConfig) (nameConfig, error) {
280+
localPath, remotePath, err := sync.ParseSyncPath(sc.Path)
281+
if err != nil {
282+
return nameConfig{}, err
289283
}
290284

291285
selector := ""
@@ -303,15 +297,12 @@ func fromSyncConfig(devPod *latest.DevPod, containerName string, sc *latest.Sync
303297
devPod: devPod,
304298
containerName: containerName,
305299
syncConfig: sc,
306-
}
300+
}, nil
307301
}
308302

309303
func (cmd *SyncCmd) applyFlagsToSyncConfig(syncConfig *latest.SyncConfig, options targetselector.Options) (targetselector.Options, error) {
310-
if cmd.LocalPath != "" {
311-
syncConfig.LocalSubPath = cmd.LocalPath
312-
}
313-
if cmd.ContainerPath != "" {
314-
syncConfig.ContainerPath = cmd.ContainerPath
304+
if cmd.Path != "" {
305+
syncConfig.Path = cmd.Path
315306
}
316307
if len(cmd.Exclude) > 0 {
317308
syncConfig.ExcludePaths = cmd.Exclude

examples/pipelines/devspace.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ version: v2beta1
22
name: pipelines
33

44
vars:
5-
MELLO: $(echo "TEST")
5+
MELLO: $(echo ${DELLO})
66
DELLO: ${MELLO}-${CHELLO}
77
HELLO: ${devspace.namespace}-${DELLO}
88

99
pipelines:
1010
dev:
1111
steps:
1212
- run: |-
13-
echo ${HELLO}
13+
echo "Hello World!"

pkg/devspace/build/builder/buildkit/buildkit.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ type Builder struct {
4141
// NewBuilder creates a new docker Builder instance
4242
func NewBuilder(ctx *devspacecontext.Context, imageConfigName string, imageConf *latest.Image, imageTags []string, skipPush, skipPushOnLocalKubernetes bool) (*Builder, error) {
4343
// ensure namespace
44-
if imageConf.Build != nil && imageConf.Build.BuildKit != nil && imageConf.Build.BuildKit.InCluster != nil && imageConf.Build.BuildKit.InCluster.Namespace != "" {
45-
err := ctx.KubeClient.EnsureNamespace(ctx.Context, imageConf.Build.BuildKit.InCluster.Namespace, ctx.Log)
44+
if imageConf.BuildKit != nil && imageConf.BuildKit.InCluster != nil && imageConf.BuildKit.InCluster.Namespace != "" {
45+
err := ctx.KubeClient.EnsureNamespace(ctx.Context, imageConf.BuildKit.InCluster.Namespace, ctx.Log)
4646
if err != nil {
4747
return nil, err
4848
}
@@ -65,9 +65,9 @@ func (b *Builder) ShouldRebuild(ctx *devspacecontext.Context, forceRebuild bool)
6565
rebuild, err := b.helper.ShouldRebuild(ctx, forceRebuild)
6666

6767
// Check if image is present in local repository
68-
if !rebuild && err == nil && b.helper.ImageConf.Build.BuildKit.InCluster == nil {
68+
if !rebuild && err == nil && b.helper.ImageConf.BuildKit.InCluster == nil {
6969
if b.skipPushOnLocalKubernetes && ctx.KubeClient != nil && ctx.KubeClient.IsLocalKubernetes() {
70-
dockerClient, err := dockerpkg.NewClientWithMinikube(ctx.KubeClient.CurrentContext(), b.helper.ImageConf.Build.BuildKit.PreferMinikube == nil || *b.helper.ImageConf.Build.BuildKit.PreferMinikube, ctx.Log)
70+
dockerClient, err := dockerpkg.NewClientWithMinikube(ctx.KubeClient.CurrentContext(), b.helper.ImageConf.BuildKit.PreferMinikube == nil || *b.helper.ImageConf.BuildKit.PreferMinikube, ctx.Log)
7171
if err != nil {
7272
return false, err
7373
}
@@ -90,19 +90,17 @@ func (b *Builder) ShouldRebuild(ctx *devspacecontext.Context, forceRebuild bool)
9090
func (b *Builder) BuildImage(ctx *devspacecontext.Context, contextPath, dockerfilePath string, entrypoint []string, cmd []string) error {
9191
// build options
9292
options := &types.ImageBuildOptions{}
93-
if b.helper.ImageConf.Build != nil && b.helper.ImageConf.Build.BuildKit != nil && b.helper.ImageConf.Build.BuildKit.Options != nil {
94-
if b.helper.ImageConf.Build.BuildKit.Options.BuildArgs != nil {
95-
options.BuildArgs = b.helper.ImageConf.Build.BuildKit.Options.BuildArgs
96-
}
97-
if b.helper.ImageConf.Build.BuildKit.Options.Target != "" {
98-
options.Target = b.helper.ImageConf.Build.BuildKit.Options.Target
99-
}
100-
if b.helper.ImageConf.Build.BuildKit.Options.Network != "" {
101-
options.NetworkMode = b.helper.ImageConf.Build.BuildKit.Options.Network
102-
}
93+
if b.helper.ImageConf.BuildArgs != nil {
94+
options.BuildArgs = b.helper.ImageConf.BuildArgs
95+
}
96+
if b.helper.ImageConf.Target != "" {
97+
options.Target = b.helper.ImageConf.Target
98+
}
99+
if b.helper.ImageConf.Network != "" {
100+
options.NetworkMode = b.helper.ImageConf.Network
103101
}
104102

105-
buildKitConfig := b.helper.ImageConf.Build.BuildKit
103+
buildKitConfig := b.helper.ImageConf.BuildKit
106104

107105
// create the builder
108106
builder, err := ensureBuilder(ctx.Context, ctx.WorkingDir, ctx.KubeClient, buildKitConfig, ctx.Log)
@@ -129,14 +127,11 @@ func (b *Builder) BuildImage(ctx *devspacecontext.Context, contextPath, dockerfi
129127
}
130128

131129
// Should we build with cli?
132-
if b.skipPush {
133-
buildKitConfig.SkipPush = b.skipPush
134-
}
135-
136-
return buildWithCLI(ctx.Context, ctx.WorkingDir, body, writer, ctx.KubeClient, builder, buildKitConfig, *buildOptions, useMinikubeDocker, ctx.Log)
130+
skipPush := b.skipPush || b.helper.ImageConf.SkipPush
131+
return buildWithCLI(ctx.Context, ctx.WorkingDir, body, writer, ctx.KubeClient, builder, buildKitConfig, *buildOptions, useMinikubeDocker, skipPush, ctx.Log)
137132
}
138133

139-
func buildWithCLI(ctx context.Context, dir string, context io.Reader, writer io.Writer, kubeClient kubectl.Client, builder string, imageConf *latest.BuildKitConfig, options types.ImageBuildOptions, useMinikubeDocker bool, log logpkg.Logger) error {
134+
func buildWithCLI(ctx context.Context, dir string, context io.Reader, writer io.Writer, kubeClient kubectl.Client, builder string, imageConf *latest.BuildKitConfig, options types.ImageBuildOptions, useMinikubeDocker, skipPush bool, log logpkg.Logger) error {
140135
environ := os.Environ()
141136

142137
command := []string{"docker", "buildx"}
@@ -160,7 +155,7 @@ func buildWithCLI(ctx context.Context, dir string, context io.Reader, writer io.
160155
for _, tag := range options.Tags {
161156
args = append(args, "--tag", tag)
162157
}
163-
if !imageConf.SkipPush {
158+
if !skipPush {
164159
if len(options.Tags) > 0 {
165160
args = append(args, "--push")
166161
}

0 commit comments

Comments
 (0)