Skip to content

Commit 45fdf0d

Browse files
authored
Merge pull request #2020 from FabianKramm/master
refactor: make ssh port stable if already in .ssh/config
2 parents 2c8c79f + 1ddcce5 commit 45fdf0d

15 files changed

Lines changed: 258 additions & 76 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/build/builder/restart/restart.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ counter=0
7070
while ! [ -f $touchFile ]; do
7171
if [ "$counter" = "0" ]; then
7272
echo "Container started with restart helper."
73-
echo "Waiting for initial sync to complete before starting application..."
73+
echo "Waiting for initial sync to complete or file $touchFile to exist before starting the application..."
7474
else
7575
if [ "$counter" = 10 ]; then
76-
echo "(Still waiting for initial sync to complete)"
76+
echo "(Still waiting...)"
7777
counter=0
7878
fi
7979
fi
@@ -82,7 +82,7 @@ while ! [ -f $touchFile ]; do
8282
done
8383
8484
if ! [ "$counter" = "0" ]; then
85-
echo "Initial sync completed. Starting application..."
85+
echo "Starting application..."
8686
fi
8787
8888
while $restart; do

pkg/devspace/config/versions/latest/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,8 @@ type RestartHelper struct {
856856
// Path defines the path to the restart helper that might be used if certain config
857857
// options are enabled
858858
Path string `yaml:"path,omitempty" json:"path,omitempty"`
859-
// Disable signals DevSpace to not inject the restart helper
860-
Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"`
859+
// Inject signals DevSpace to inject the restart helper
860+
Inject *bool `yaml:"inject,omitempty" json:"inject,omitempty"`
861861
}
862862

863863
type ProxyCommand struct {

pkg/devspace/config/versions/v1beta11/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ func (c *Config) mergeDevConfig(log log.Logger) (map[string]*next.DevPod, error)
774774
if devPods[k].Containers[i].RestartHelper == nil {
775775
devPods[k].Containers[i].RestartHelper = &next.RestartHelper{}
776776
}
777-
devPods[k].Containers[i].RestartHelper.Disable = true
777+
devPods[k].Containers[i].RestartHelper.Inject = ptr.Bool(false)
778778
}
779779
}
780780

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/devpod.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ func needPodReplaceContainer(devContainer *latest.DevContainer) bool {
539539
if len(devContainer.PersistPaths) > 0 {
540540
return true
541541
}
542+
if devContainer.RestartHelper != nil && devContainer.RestartHelper.Inject != nil && *devContainer.RestartHelper.Inject {
543+
return true
544+
}
542545
if devContainer.Terminal != nil && !devContainer.Terminal.DisableReplace && (devContainer.Terminal.Enabled == nil || *devContainer.Terminal.Enabled) {
543546
return true
544547
}
@@ -554,7 +557,7 @@ func needPodReplaceContainer(devContainer *latest.DevContainer) bool {
554557
if devContainer.Args != nil {
555558
return true
556559
}
557-
if devContainer.RestartHelper == nil || !devContainer.RestartHelper.Disable {
560+
if devContainer.RestartHelper == nil || devContainer.RestartHelper.Inject == nil || *devContainer.RestartHelper.Inject {
558561
for _, s := range devContainer.Sync {
559562
if s.OnUpload != nil && s.OnUpload.RestartContainer {
560563
return true

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/podreplace/builder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,16 @@ func replaceWorkingDir(ctx *devspacecontext.Context, devPod *latest.DevPod, devC
242242

243243
func replaceCommand(ctx *devspacecontext.Context, devPod *latest.DevPod, devContainer *latest.DevContainer, podTemplate *corev1.PodTemplateSpec) error {
244244
// replace with DevSpace helper
245-
injectRestartHelper := false
246-
if devContainer.RestartHelper == nil || !devContainer.RestartHelper.Disable {
245+
injectRestartHelper := devContainer.RestartHelper != nil && devContainer.RestartHelper.Inject != nil && *devContainer.RestartHelper.Inject
246+
if devContainer.RestartHelper == nil || devContainer.RestartHelper.Inject == nil || *devContainer.RestartHelper.Inject {
247247
for _, s := range devContainer.Sync {
248248
if s.StartContainer || (s.OnUpload != nil && s.OnUpload.RestartContainer) {
249249
injectRestartHelper = true
250250
}
251251
}
252252
}
253253
if len(devContainer.Command) == 0 && injectRestartHelper {
254-
return fmt.Errorf("dev.%s.sync[*].onUpload.restartContainer is true, please specify the entrypoint that should get restarted in dev.%s.command", devPod.Name, devPod.Name)
254+
return fmt.Errorf("dev.%s.sync[*].onUpload.restartContainer or dev.%s.restartHelper.inject is true, please specify the entrypoint that should get restarted in dev.%s.command", devPod.Name, devPod.Name, devPod.Name)
255255
}
256256
if !injectRestartHelper && len(devContainer.Command) == 0 && devContainer.Args == nil {
257257
return nil

pkg/devspace/services/proxycommands/commands.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func startProxyCommands(ctx *devspacecontext.Context, devContainer *latest.DevCo
5151
}
5252

5353
// get a local port
54-
port, err := ssh.LockPort()
54+
port, err := ssh.GetInstance(ctx.Log).LockPort()
5555
if err != nil {
5656
return errors.Wrap(err, "find port")
5757
}
5858

59-
defer ssh.ReleasePort(port)
59+
defer ssh.GetInstance(ctx.Log).ReleasePort(port)
6060

6161
// get remote port
6262
defaultRemotePort := DefaultRemotePort

0 commit comments

Comments
 (0)