Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/expression"
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
"github.com/loft-sh/devspace/pkg/devspace/env"
"github.com/loft-sh/devspace/pkg/util/log"
Expand Down Expand Up @@ -185,11 +186,18 @@ func BuildRoot(f factory.Factory, excludePlugins bool) *cobra.Command {
}

// try to parse the raw config
rawConfig, err := parseConfig(f)
if err != nil {
f.GetLog().Debugf("error parsing raw config: %v", err)
} else {
env.GlobalGetEnv = rawConfig.GetEnv
var rawConfig *RawConfig

// This check is necessary to avoid process loops where a variable inside
// the devspace.yaml would execute another devspace command which would again
// load the config and execute DevSpace config parsing etc.
if os.Getenv(expression.DEVSPACE_SKIP_PRELOAD_ENV) == "" {
rawConfig, err = parseConfig(f)
if err != nil {
f.GetLog().Debugf("error parsing raw config: %v", err)
} else {
env.GlobalGetEnv = rawConfig.GetEnv
}
}

// build the root cmd
Expand Down Expand Up @@ -323,7 +331,9 @@ func parseConfig(f factory.Factory) (*RawConfig, error) {
r := &RawConfig{
resolved: map[string]string{},
}
_, err = configLoader.LoadWithParser(timeoutCtx, nil, nil, r, &loader.ConfigOptions{Dry: true}, log.Discard)
_, err = configLoader.LoadWithParser(timeoutCtx, nil, nil, r, &loader.ConfigOptions{
Dry: true,
}, log.Discard)
if r.Resolver != nil {
return r, nil
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/devspace/config/loader/variable/command_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package variable
import (
"bytes"
"context"
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/expression"
"github.com/loft-sh/devspace/pkg/devspace/pipeline/engine"
"mvdan.cc/sh/v3/expand"
"os"
Expand Down Expand Up @@ -53,10 +54,13 @@ func execCommand(ctx context.Context, varName string, definition *latest.Variabl
writer := &bytes.Buffer{}
stdErrWriter := &bytes.Buffer{}
var err error
envVars := []string{}
envVars = append(envVars, expression.DEVSPACE_SKIP_PRELOAD_ENV+"=true")
envVars = append(envVars, os.Environ()...)
if args == nil {
err = engine.ExecuteSimpleShellCommand(ctx, dir, expand.ListEnviron(os.Environ()...), writer, stdErrWriter, nil, cmd)
err = engine.ExecuteSimpleShellCommand(ctx, dir, expand.ListEnviron(envVars...), writer, stdErrWriter, nil, cmd)
} else {
err = command.Command(ctx, dir, expand.ListEnviron(os.Environ()...), writer, stdErrWriter, nil, cmd, args...)
err = command.Command(ctx, dir, expand.ListEnviron(envVars...), writer, stdErrWriter, nil, cmd, args...)
}
if err != nil {
errMsg := "fill variable " + varName + " with command '" + cmd + "': " + err.Error()
Expand Down
20 changes: 16 additions & 4 deletions pkg/devspace/config/loader/variable/default_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package variable
import (
"context"
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
"github.com/sirupsen/logrus"
"os"
"strconv"

Expand All @@ -23,6 +24,7 @@ func NewDefaultVariable(name string, workingDirectory string, localCache localca
type defaultVariable struct {
name string
workingDirectory string
dry bool
localCache localcache.Cache
log log.Logger
}
Expand All @@ -47,12 +49,22 @@ func (d *defaultVariable) Load(ctx context.Context, definition *latest.Variable)
}
}

// Now ask the question
value, err := askQuestion(definition, d.log)
if err != nil {
return nil, err
// is logger silent
if d.log == log.Discard || d.log.GetLevel() < logrus.InfoLevel {
if definition.Default != nil {
return definition.Default, nil
}

return valueByType(value, definition.Default)
} else {
var err error
value, err = askQuestion(definition, d.log)
if err != nil {
return nil, err
}
}

// Now ask the question
if !definition.NoCache {
d.localCache.SetVar(d.name, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
// ExpressionMatchRegex is the regex to check if a value matches the devspace var format
var ExpressionMatchRegex = regexp.MustCompile(`(?ms)^\$\#?\!?\((.+)\)$`)

const DEVSPACE_SKIP_PRELOAD_ENV = "DEVSPACE_SKIP_PRELOAD"

func expressionMatchFn(key, value string) bool {
return ExpressionMatchRegex.MatchString(value)
}
Expand Down Expand Up @@ -95,7 +97,11 @@ func ResolveExpressions(ctx context.Context, value, dir string, variables map[st

stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
err := engine.ExecuteSimpleShellCommand(ctx, dir, env.NewVariableEnvProvider(expand.ListEnviron(os.Environ()...), vars), stdout, stderr, nil, match[1], os.Args[1:]...)

envVars := []string{}
envVars = append(envVars, DEVSPACE_SKIP_PRELOAD_ENV+"=true")
envVars = append(envVars, os.Environ()...)
err := engine.ExecuteSimpleShellCommand(ctx, dir, env.NewVariableEnvProvider(expand.ListEnviron(envVars...), vars), stdout, stderr, nil, match[1], os.Args[1:]...)
if err != nil {
if len(strings.TrimSpace(stdout.String())) == 0 && len(strings.TrimSpace(stderr.String())) == 0 {
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/devspace/config/loader/variable/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ func MergeVarsWithFlags(vars map[string]interface{}, flags []string) error {
type resolver struct {
vars map[string]*latest.Variable
memoryCache map[string]interface{}
localCache localcache.Cache
options *PredefinedVariableOptions
log log.Logger

localCache localcache.Cache
options *PredefinedVariableOptions
log log.Logger
}

func varMatchFn(key, value string) bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ var Locations = []string{
"/hooks/*/container/imageSelector",
"/dev/*/imageSelector",
"/dev/*/replaceImage",
"/dev/*/devImage",
"/dev/*/containers/*/replaceImage",
"/dev/*/containers/*/devImage",
"/dev/ports/*/imageSelector",
"/dev/sync/*/imageSelector",
"/dev/logs/*/selectors/*/imageSelector",
"/dev/replacePods/*/imageSelector",
"/dev/replacePods/*/replaceImage",
"/dev/terminal/imageSelector",
"/pipelines/*",
"/pipelines/*/flags/**",
"/pipelines/*/run",
"/commands/*",
"/commands/*/command",
Expand Down
6 changes: 6 additions & 0 deletions pkg/devspace/config/loader/variable/undefined_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
"github.com/sirupsen/logrus"
"os"
"strconv"

Expand Down Expand Up @@ -38,6 +39,11 @@ func (u *undefinedVariable) Load(ctx context.Context, _ *latest.Variable) (inter
return convertStringValue(v), nil
}

// is logger silent
if u.log == log.Discard || u.log.GetLevel() < logrus.InfoLevel {
return "", nil
}

// Ask for variable
val, err := askQuestion(&latest.Variable{
Question: "Please enter a value for " + u.name,
Expand Down
2 changes: 1 addition & 1 deletion pkg/devspace/helm/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *client) Exec(ctx devspacecontext.Context, args []string) ([]byte, error
}

// disable log for list, because it prints same command multiple times if we've multiple deployments.
if args[0] != "list" {
if args[0] != "list" && args[0] != "registry" && (len(args) == 1 || args[1] != "login") {
c.log.Debugf("Execute '%s %s'", c.helmPath, strings.Join(args, " "))
}

Expand Down
23 changes: 18 additions & 5 deletions pkg/devspace/helm/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package v3
import (
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
dependencyutil "github.com/loft-sh/devspace/pkg/devspace/dependency/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/ghodss/yaml"
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
Expand Down Expand Up @@ -71,11 +73,22 @@ func (c *client) InstallChart(ctx devspacecontext.Context, releaseName string, r
if helmConfig.Chart.Version != "" {
args = append(args, "--version", helmConfig.Chart.Version)
}
if helmConfig.Chart.Username != "" {
args = append(args, "--username", helmConfig.Chart.Username)
}
if helmConfig.Chart.Password != "" {
args = append(args, "--password", helmConfig.Chart.Password)

// log into OCI registry if specified
if strings.HasPrefix(chartName, "oci://") {
if helmConfig.Chart.Username != "" && helmConfig.Chart.Password != "" {
_, err := c.genericHelm.Exec(ctx, []string{"registry", "login", "--username", helmConfig.Chart.Username, "--password", helmConfig.Chart.Password})
if err != nil {
return nil, errors.Wrap(err, "login oci registry")
}
}
} else {
if helmConfig.Chart.Username != "" {
args = append(args, "--username", helmConfig.Chart.Username)
}
if helmConfig.Chart.Password != "" {
args = append(args, "--password", helmConfig.Chart.Password)
}
}
}

Expand Down