Skip to content

Commit 21af882

Browse files
committed
refactor: lazy exclude & create namespace
1 parent de14d7c commit 21af882

12 files changed

Lines changed: 102 additions & 30 deletions

File tree

cmd/run_pipeline.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"io/ioutil"
3838
"k8s.io/client-go/kubernetes/fake"
3939
"os"
40-
"strings"
4140
)
4241

4342
// RunPipelineCmd holds the command flags
@@ -168,7 +167,7 @@ func (cmd *RunPipelineCmd) Run(cobraCmd *cobra.Command, args []string, f factory
168167
}
169168

170169
return runWithHooks(ctx, hookName, func() error {
171-
return runPipeline(ctx, true, options)
170+
return runPipeline(ctx, options)
172171
})
173172
}
174173

@@ -259,14 +258,6 @@ func initialize(ctx context.Context, f factory.Factory, allowFailingKubeClient b
259258
WithConfig(configInterface).
260259
WithKubeClient(client)
261260

262-
// create namespace if necessary
263-
if !options.DeployOptions.Render {
264-
err := kubectl.EnsureNamespace(devCtx.Context(), devCtx.KubeClient(), devCtx.KubeClient().Namespace(), devCtx.Log())
265-
if err != nil {
266-
return nil, errors.Errorf("unable to create namespace: %v", err)
267-
}
268-
}
269-
270261
// print config
271262
if devCtx.Log().GetLevel() == logrus.DebugLevel {
272263
out, _ := yaml.Marshal(devCtx.Config().Config())
@@ -291,7 +282,7 @@ func initialize(ctx context.Context, f factory.Factory, allowFailingKubeClient b
291282

292283
func updateLastKubeContext(ctx devspacecontext.Context) error {
293284
// Update generated if we deploy the application
294-
if ctx.Config != nil && ctx.Config().LocalCache() != nil {
285+
if ctx.Config() != nil && ctx.Config().LocalCache() != nil {
295286
ctx.Config().LocalCache().SetLastContext(&localcache.LastContextConfig{
296287
Context: ctx.KubeClient().CurrentContext(),
297288
Namespace: ctx.KubeClient().Namespace(),
@@ -422,7 +413,7 @@ func (cmd *RunPipelineCmd) BuildOptions(configOptions *loader.ConfigOptions) *Co
422413
}
423414
}
424415

425-
func runPipeline(ctx devspacecontext.Context, forceLeader bool, options *CommandOptions) error {
416+
func runPipeline(ctx devspacecontext.Context, options *CommandOptions) error {
426417
var configPipeline *latest.Pipeline
427418
if ctx.Config().Config().Pipelines != nil && ctx.Config().Config().Pipelines[options.Pipeline] != nil {
428419
configPipeline = ctx.Config().Config().Pipelines[options.Pipeline]
@@ -461,15 +452,6 @@ func runPipeline(ctx devspacecontext.Context, forceLeader bool, options *Command
461452
}
462453
dependencyRegistry.SetServer("http://" + serv.Server.Addr)
463454

464-
// exclude ourselves
465-
couldExclude, err := dependencyRegistry.MarkDependencyExcluded(ctx, ctx.Config().Config().Name, forceLeader)
466-
if err != nil {
467-
return err
468-
} else if !couldExclude {
469-
return fmt.Errorf("couldn't execute '%s', because there is another DevSpace instance active in the current namespace right now that uses the same project name (%s)", strings.Join(os.Args, " "), ctx.Config().Config().Name)
470-
}
471-
ctx.Log().Debugf("Marked project excluded: %v", ctx.Config().Config().Name)
472-
473455
// get a stdout writer
474456
stdoutWriter := ctx.Log().Writer(ctx.Log().GetLevel(), true)
475457
defer stdoutWriter.Close()

pkg/devspace/pipeline/engine/pipelinehandler/commands/build_images.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ type BuildImagesOptions struct {
2424

2525
func BuildImages(ctx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
2626
ctx.Log().Debugf("build_images %s", strings.Join(args, " "))
27+
err := pipeline.Exclude(ctx)
28+
if err != nil {
29+
return err
30+
}
31+
2732
options := &BuildImagesOptions{
2833
Options: pipeline.Options().BuildOptions,
2934
}
30-
args, err := flags.ParseArgs(options, args)
35+
args, err = flags.ParseArgs(options, args)
3136
if err != nil {
3237
return errors.Wrap(err, "parse args")
3338
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/create_deployments.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ type CreateDeploymentsOptions struct {
3131

3232
func CreateDeployments(ctx devspacecontext.Context, pipeline types.Pipeline, args []string, stdout io.Writer) error {
3333
ctx.Log().Debugf("create_deployments %s", strings.Join(args, " "))
34+
err := pipeline.Exclude(ctx)
35+
if err != nil {
36+
return err
37+
}
38+
3439
options := &CreateDeploymentsOptions{
3540
Options: pipeline.Options().DeployOptions,
3641
}
37-
args, err := flags.ParseArgs(options, args)
42+
args, err = flags.ParseArgs(options, args)
3843
if err != nil {
3944
return errors.Wrap(err, "parse args")
4045
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/ensure_pull_secrets.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
flags "github.com/jessevdk/go-flags"
66
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
77
"github.com/loft-sh/devspace/pkg/devspace/docker"
8+
"github.com/loft-sh/devspace/pkg/devspace/pipeline/types"
89
"github.com/loft-sh/devspace/pkg/devspace/pullsecrets"
910
"github.com/pkg/errors"
1011
"strings"
@@ -20,10 +21,15 @@ type EnsurePullSecretsOptions struct {
2021
All bool `long:"all" description:"Ensure all pull secrets"`
2122
}
2223

23-
func EnsurePullSecrets(ctx devspacecontext.Context, args []string) error {
24+
func EnsurePullSecrets(ctx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
2425
ctx.Log().Debugf("ensure_pull_secrets %s", strings.Join(args, " "))
26+
err := pipeline.Exclude(ctx)
27+
if err != nil {
28+
return err
29+
}
30+
2531
options := &EnsurePullSecretsOptions{}
26-
args, err := flags.ParseArgs(options, args)
32+
args, err = flags.ParseArgs(options, args)
2733
if err != nil {
2834
return errors.Wrap(err, "parse args")
2935
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/purge_deployments.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ type PurgeDeploymentsOptions struct {
2323

2424
func PurgeDeployments(ctx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
2525
ctx.Log().Debugf("purge_deployments %s", strings.Join(args, " "))
26+
err := pipeline.Exclude(ctx)
27+
if err != nil {
28+
return err
29+
}
30+
2631
options := &PurgeDeploymentsOptions{
2732
PurgeOptions: pipeline.Options().PurgeOptions,
2833
}
29-
args, err := flags.ParseArgs(options, args)
34+
args, err = flags.ParseArgs(options, args)
3035
if err != nil {
3136
return errors.Wrap(err, "parse args")
3237
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/run_default_pipeline.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import (
1313
type NewHandlerFn func(ctx devspacecontext.Context, stdout, stderr io.Writer, pipeline types.Pipeline) enginetypes.ExecHandler
1414

1515
func RunDefaultPipeline(ctx devspacecontext.Context, pipeline types.Pipeline, args []string, newHandler NewHandlerFn) error {
16+
err := pipeline.Exclude(ctx)
17+
if err != nil {
18+
return err
19+
}
20+
1621
hc := interp.HandlerCtx(ctx.Context())
1722
if len(args) != 1 {
1823
return fmt.Errorf("usage: run_default_pipeline [pipeline]")

pkg/devspace/pipeline/engine/pipelinehandler/commands/run_dependency_pipelines.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ type RunDependencyPipelinesOptions struct {
2020

2121
func RunDependencyPipelines(ctx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
2222
ctx.Log().Debugf("run_dependency_pipelines %s", strings.Join(args, " "))
23+
err := pipeline.Exclude(ctx)
24+
if err != nil {
25+
return err
26+
}
27+
2328
options := &RunDependencyPipelinesOptions{
2429
DependencyOptions: pipeline.Options().DependencyOptions,
2530
}
26-
args, err := flags.ParseArgs(options, args)
31+
args, err = flags.ParseArgs(options, args)
2732
if err != nil {
2833
return errors.Wrap(err, "parse args")
2934
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/start_dev.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ type StartDevOptions struct {
2525

2626
func StartDev(ctx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
2727
ctx.Log().Debugf("start_dev %s", strings.Join(args, " "))
28+
err := pipeline.Exclude(ctx)
29+
if err != nil {
30+
return err
31+
}
32+
2833
options := &StartDevOptions{
2934
Options: pipeline.Options().DevOptions,
3035
}
31-
args, err := flags.ParseArgs(options, args)
36+
args, err = flags.ParseArgs(options, args)
3237
if err != nil {
3338
return errors.Wrap(err, "parse args")
3439
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/stop_dev.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ type StopDevOptions struct {
1919

2020
func StopDev(ctx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
2121
ctx.Log().Debugf("stop_dev %s", strings.Join(args, " "))
22+
err := pipeline.Exclude(ctx)
23+
if err != nil {
24+
return err
25+
}
26+
2227
options := &StopDevOptions{
2328
PurgeOptions: pipeline.Options().PurgeOptions,
2429
}
25-
args, err := flags.ParseArgs(options, args)
30+
args, err = flags.ParseArgs(options, args)
2631
if err != nil {
2732
return errors.Wrap(err, "parse args")
2833
}

pkg/devspace/pipeline/engine/pipelinehandler/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ var PipelineCommands = map[string]func(devCtx devspacecontext.Context, pipeline
5959
return commands.RunDependencyPipelines(devCtx, pipeline, args)
6060
},
6161
"ensure_pull_secrets": func(devCtx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
62-
return commands.EnsurePullSecrets(devCtx, args)
62+
return commands.EnsurePullSecrets(devCtx, pipeline, args)
6363
},
6464
"is_dependency": func(devCtx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
6565
return commands.IsDependency(devCtx.Context(), args)

0 commit comments

Comments
 (0)