Skip to content

Commit a3808f7

Browse files
committed
refactor: improve dev pod context handling
1 parent 69cb5bf commit a3808f7

6 files changed

Lines changed: 40 additions & 26 deletions

File tree

cmd/run_pipeline.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,12 @@ func runPipeline(ctx *devspacecontext.Context, f factory.Factory, forceLeader bo
374374
ctx.Log.Debugf("Run pipeline:\n%s\n", string(configPipelineBytes))
375375
}
376376

377+
// create dev context
378+
devCtxCancel, cancelDevCtx := context.WithCancel(ctx.Context)
379+
ctx = ctx.WithContext(values.WithDevContext(ctx.Context, devCtxCancel))
380+
377381
// create a new base dev pod manager
378-
devPodManager := devpod.NewManager(ctx.Context)
382+
devPodManager := devpod.NewManager(cancelDevCtx)
379383
defer devPodManager.Close()
380384

381385
// create dependency registry

pkg/devspace/context/values/values.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@ const (
1212
commandKey
1313
dependencyKey
1414
rootNameKey
15+
devContextKey
1516
)
1617

18+
// WithDevContext creates a new context with the dev context
19+
func WithDevContext(parent context.Context, devCtx context.Context) context.Context {
20+
return WithValue(parent, devContextKey, devCtx)
21+
}
22+
23+
// DevContextFrom returns a context used to start and stop dev configurations
24+
func DevContextFrom(ctx context.Context) (context.Context, bool) {
25+
devCtx, ok := ctx.Value(devContextKey).(context.Context)
26+
return devCtx, ok
27+
}
28+
1729
// RootNameFrom returns the root name of the devspace config
1830
func RootNameFrom(ctx context.Context) (string, bool) {
1931
user, ok := ctx.Value(rootNameKey).(string)

pkg/devspace/devpod/manager.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
66
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
7+
"github.com/loft-sh/devspace/pkg/devspace/context/values"
78
"github.com/loft-sh/devspace/pkg/devspace/deploy"
89
"github.com/loft-sh/devspace/pkg/devspace/services/podreplace"
910
"github.com/loft-sh/devspace/pkg/util/lockfactory"
@@ -41,9 +42,6 @@ type Manager interface {
4142
// Close will close the manager and wait for all dev pods to stop
4243
Close()
4344

44-
// Context returns the context of the DevManager
45-
Context() context.Context
46-
4745
// Wait will wait until all DevPods are stopped
4846
Wait() error
4947
}
@@ -52,15 +50,12 @@ type devPodManager struct {
5250
lockFactory lockfactory.LockFactory
5351

5452
m sync.Mutex
55-
ctx context.Context
5653
cancels []context.CancelFunc
5754
devPods map[string]*devPod
5855
}
5956

60-
func NewManager(ctx context.Context) Manager {
61-
ctx, cancel := context.WithCancel(ctx)
57+
func NewManager(cancel context.CancelFunc) Manager {
6258
return &devPodManager{
63-
ctx: ctx,
6459
cancels: []context.CancelFunc{cancel},
6560
lockFactory: lockfactory.NewDefaultLockFactory(),
6661
devPods: map[string]*devPod{},
@@ -86,21 +81,18 @@ func (d *devPodManager) Close() {
8681
}
8782
d.cancels = []context.CancelFunc{}
8883
d.m.Unlock()
89-
d.Wait()
90-
}
91-
92-
func (d *devPodManager) Context() context.Context {
93-
return d.ctx
84+
_ = d.Wait()
9485
}
9586

9687
func (d *devPodManager) StartMultiple(ctx *devspacecontext.Context, devPods []string, options Options) error {
88+
devCtx, _ := values.DevContextFrom(ctx.Context)
9789
select {
98-
case <-d.ctx.Done():
99-
return d.ctx.Err()
90+
case <-devCtx.Done():
91+
return devCtx.Err()
10092
default:
10193
}
10294

103-
cancelCtx, cancel := context.WithCancel(d.ctx)
95+
cancelCtx, cancel := context.WithCancel(devCtx)
10496
d.m.Lock()
10597
d.cancels = append(d.cancels, cancel)
10698
d.m.Unlock()
@@ -174,16 +166,17 @@ func (d *devPodManager) Start(originalContext *devspacecontext.Context, devPodCo
174166
var dp *devPod
175167
d.m.Lock()
176168
dp = d.devPods[devPodConfig.Name]
177-
d.m.Unlock()
178-
179-
// check if already running
180169
if dp != nil {
181-
return nil, DevPodAlreadyExists{}
170+
select {
171+
case <-dp.Done():
172+
default:
173+
d.m.Unlock()
174+
return nil, DevPodAlreadyExists{}
175+
}
182176
}
183177

184178
// create a new dev pod
185179
dp = newDevPod()
186-
d.m.Lock()
187180
d.devPods[devPodConfig.Name] = dp
188181
d.m.Unlock()
189182

pkg/devspace/pipeline/pipeline.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pipeline
22

33
import (
4+
"context"
45
"fmt"
56
"github.com/loft-sh/devspace/pkg/devspace/context/values"
67
utilerrors "k8s.io/apimachinery/pkg/util/errors"
@@ -205,7 +206,10 @@ func (p *pipeline) startNewDependency(ctx *devspacecontext.Context, dependency t
205206
pipelineConfig = dependency.Config().Config().Pipelines[executePipeline]
206207
}
207208

208-
dependencyDevPodManager := devpod.NewManager(p.devPodManager.Context())
209+
devCtx, _ := values.DevContextFrom(ctx.Context)
210+
devCtxCancel, cancelDevCtx := context.WithCancel(devCtx)
211+
ctx = ctx.WithContext(values.WithDevContext(ctx.Context, devCtxCancel))
212+
dependencyDevPodManager := devpod.NewManager(cancelDevCtx)
209213
pip := NewPipeline(dependency.Name(), dependencyDevPodManager, p.dependencyRegistry, pipelineConfig, p.options)
210214
pip.(*pipeline).parent = p
211215

pkg/devspace/services/sync/sync.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ func StartSync(ctx *devspacecontext.Context, devPod *latest.DevPod, selector tar
8181
for _, syncConfig := range devContainer.Sync {
8282
// start a new go routine in the tomb
8383
s := syncConfig
84+
syncCtx := ctx
8485
var cancel context.CancelFunc
8586
if s.NoWatch {
8687
var cancelCtx context.Context
87-
cancelCtx, cancel = context.WithCancel(ctx.Context)
88-
defer cancel()
89-
ctx = ctx.WithContext(cancelCtx)
88+
cancelCtx, cancel = context.WithCancel(syncCtx.Context)
89+
syncCtx = syncCtx.WithContext(cancelCtx)
9090
}
9191
initDone := parent.NotifyGo(func() error {
9292
if cancel != nil {
9393
defer cancel()
9494
}
9595

96-
return startSync(ctx, devPod.Name, string(devContainer.Arch), s, selector.WithContainer(devContainer.Container), starter, parent)
96+
return startSync(syncCtx, devPod.Name, string(devContainer.Arch), s, selector.WithContainer(devContainer.Container), starter, parent)
9797
})
9898
initDoneArray = append(initDoneArray, initDone)
9999

pkg/util/yamlutil/yaml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func prettifyError(data []byte, err error) error {
3838
if typeError, ok := err.(*yaml.TypeError); ok {
3939
for i, message := range typeError.Errors {
4040
typeError.Errors[i] = strings.Replace(message, "!!seq", "an array", -1)
41+
typeError.Errors[i] = strings.Replace(message, "!!str", "string", -1)
4142

4243
// add line to error
4344
match := lineRegEx.FindSubmatch([]byte(message))

0 commit comments

Comments
 (0)