Skip to content

Commit 685e7bc

Browse files
authored
Merge pull request #2240 from FabianKramm/main
fix: run helm registry login
2 parents ffad499 + f0d67f6 commit 685e7bc

9 files changed

Lines changed: 77 additions & 22 deletions

File tree

cmd/root.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"flag"
66
"fmt"
77
"github.com/loft-sh/devspace/pkg/devspace/config/loader"
8+
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/expression"
89
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
910
"github.com/loft-sh/devspace/pkg/devspace/env"
1011
"github.com/loft-sh/devspace/pkg/util/log"
@@ -185,11 +186,18 @@ func BuildRoot(f factory.Factory, excludePlugins bool) *cobra.Command {
185186
}
186187

187188
// try to parse the raw config
188-
rawConfig, err := parseConfig(f)
189-
if err != nil {
190-
f.GetLog().Debugf("error parsing raw config: %v", err)
191-
} else {
192-
env.GlobalGetEnv = rawConfig.GetEnv
189+
var rawConfig *RawConfig
190+
191+
// This check is necessary to avoid process loops where a variable inside
192+
// the devspace.yaml would execute another devspace command which would again
193+
// load the config and execute DevSpace config parsing etc.
194+
if os.Getenv(expression.DEVSPACE_SKIP_PRELOAD_ENV) == "" {
195+
rawConfig, err = parseConfig(f)
196+
if err != nil {
197+
f.GetLog().Debugf("error parsing raw config: %v", err)
198+
} else {
199+
env.GlobalGetEnv = rawConfig.GetEnv
200+
}
193201
}
194202

195203
// build the root cmd
@@ -323,7 +331,9 @@ func parseConfig(f factory.Factory) (*RawConfig, error) {
323331
r := &RawConfig{
324332
resolved: map[string]string{},
325333
}
326-
_, err = configLoader.LoadWithParser(timeoutCtx, nil, nil, r, &loader.ConfigOptions{Dry: true}, log.Discard)
334+
_, err = configLoader.LoadWithParser(timeoutCtx, nil, nil, r, &loader.ConfigOptions{
335+
Dry: true,
336+
}, log.Discard)
327337
if r.Resolver != nil {
328338
return r, nil
329339
}

pkg/devspace/config/loader/variable/command_variable.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package variable
33
import (
44
"bytes"
55
"context"
6+
"github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/expression"
67
"github.com/loft-sh/devspace/pkg/devspace/pipeline/engine"
78
"mvdan.cc/sh/v3/expand"
89
"os"
@@ -53,10 +54,13 @@ func execCommand(ctx context.Context, varName string, definition *latest.Variabl
5354
writer := &bytes.Buffer{}
5455
stdErrWriter := &bytes.Buffer{}
5556
var err error
57+
envVars := []string{}
58+
envVars = append(envVars, expression.DEVSPACE_SKIP_PRELOAD_ENV+"=true")
59+
envVars = append(envVars, os.Environ()...)
5660
if args == nil {
57-
err = engine.ExecuteSimpleShellCommand(ctx, dir, expand.ListEnviron(os.Environ()...), writer, stdErrWriter, nil, cmd)
61+
err = engine.ExecuteSimpleShellCommand(ctx, dir, expand.ListEnviron(envVars...), writer, stdErrWriter, nil, cmd)
5862
} else {
59-
err = command.Command(ctx, dir, expand.ListEnviron(os.Environ()...), writer, stdErrWriter, nil, cmd, args...)
63+
err = command.Command(ctx, dir, expand.ListEnviron(envVars...), writer, stdErrWriter, nil, cmd, args...)
6064
}
6165
if err != nil {
6266
errMsg := "fill variable " + varName + " with command '" + cmd + "': " + err.Error()

pkg/devspace/config/loader/variable/default_variable.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package variable
33
import (
44
"context"
55
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
6+
"github.com/sirupsen/logrus"
67
"os"
78
"strconv"
89

@@ -23,6 +24,7 @@ func NewDefaultVariable(name string, workingDirectory string, localCache localca
2324
type defaultVariable struct {
2425
name string
2526
workingDirectory string
27+
dry bool
2628
localCache localcache.Cache
2729
log log.Logger
2830
}
@@ -47,12 +49,22 @@ func (d *defaultVariable) Load(ctx context.Context, definition *latest.Variable)
4749
}
4850
}
4951

50-
// Now ask the question
51-
value, err := askQuestion(definition, d.log)
52-
if err != nil {
53-
return nil, err
52+
// is logger silent
53+
if d.log == log.Discard || d.log.GetLevel() < logrus.InfoLevel {
54+
if definition.Default != nil {
55+
return definition.Default, nil
56+
}
57+
58+
return valueByType(value, definition.Default)
59+
} else {
60+
var err error
61+
value, err = askQuestion(definition, d.log)
62+
if err != nil {
63+
return nil, err
64+
}
5465
}
5566

67+
// Now ask the question
5668
if !definition.NoCache {
5769
d.localCache.SetVar(d.name, value)
5870
}

pkg/devspace/config/loader/variable/expression/expression.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
// ExpressionMatchRegex is the regex to check if a value matches the devspace var format
2222
var ExpressionMatchRegex = regexp.MustCompile(`(?ms)^\$\#?\!?\((.+)\)$`)
2323

24+
const DEVSPACE_SKIP_PRELOAD_ENV = "DEVSPACE_SKIP_PRELOAD"
25+
2426
func expressionMatchFn(key, value string) bool {
2527
return ExpressionMatchRegex.MatchString(value)
2628
}
@@ -95,7 +97,11 @@ func ResolveExpressions(ctx context.Context, value, dir string, variables map[st
9597

9698
stdout := &bytes.Buffer{}
9799
stderr := &bytes.Buffer{}
98-
err := engine.ExecuteSimpleShellCommand(ctx, dir, env.NewVariableEnvProvider(expand.ListEnviron(os.Environ()...), vars), stdout, stderr, nil, match[1], os.Args[1:]...)
100+
101+
envVars := []string{}
102+
envVars = append(envVars, DEVSPACE_SKIP_PRELOAD_ENV+"=true")
103+
envVars = append(envVars, os.Environ()...)
104+
err := engine.ExecuteSimpleShellCommand(ctx, dir, env.NewVariableEnvProvider(expand.ListEnviron(envVars...), vars), stdout, stderr, nil, match[1], os.Args[1:]...)
99105
if err != nil {
100106
if len(strings.TrimSpace(stdout.String())) == 0 && len(strings.TrimSpace(stderr.String())) == 0 {
101107
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 {

pkg/devspace/config/loader/variable/resolver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ func MergeVarsWithFlags(vars map[string]interface{}, flags []string) error {
5555
type resolver struct {
5656
vars map[string]*latest.Variable
5757
memoryCache map[string]interface{}
58-
localCache localcache.Cache
59-
options *PredefinedVariableOptions
60-
log log.Logger
58+
59+
localCache localcache.Cache
60+
options *PredefinedVariableOptions
61+
log log.Logger
6162
}
6263

6364
func varMatchFn(key, value string) bool {

pkg/devspace/config/loader/variable/runtime/runtime_variable.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ var Locations = []string{
2222
"/hooks/*/container/imageSelector",
2323
"/dev/*/imageSelector",
2424
"/dev/*/replaceImage",
25+
"/dev/*/devImage",
2526
"/dev/*/containers/*/replaceImage",
27+
"/dev/*/containers/*/devImage",
2628
"/dev/ports/*/imageSelector",
2729
"/dev/sync/*/imageSelector",
2830
"/dev/logs/*/selectors/*/imageSelector",
2931
"/dev/replacePods/*/imageSelector",
3032
"/dev/replacePods/*/replaceImage",
3133
"/dev/terminal/imageSelector",
3234
"/pipelines/*",
35+
"/pipelines/*/flags/**",
3336
"/pipelines/*/run",
3437
"/commands/*",
3538
"/commands/*/command",

pkg/devspace/config/loader/variable/undefined_variable.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/loft-sh/devspace/pkg/devspace/config/localcache"
7+
"github.com/sirupsen/logrus"
78
"os"
89
"strconv"
910

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

42+
// is logger silent
43+
if u.log == log.Discard || u.log.GetLevel() < logrus.InfoLevel {
44+
return "", nil
45+
}
46+
4147
// Ask for variable
4248
val, err := askQuestion(&latest.Variable{
4349
Question: "Please enter a value for " + u.name,

pkg/devspace/helm/generic/generic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (c *client) Exec(ctx devspacecontext.Context, args []string) ([]byte, error
7474
}
7575

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

pkg/devspace/helm/v3/client.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package v3
33
import (
44
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
55
dependencyutil "github.com/loft-sh/devspace/pkg/devspace/dependency/util"
6+
"github.com/pkg/errors"
67
"github.com/sirupsen/logrus"
78
"os"
89
"path/filepath"
910
"strconv"
11+
"strings"
1012

1113
"github.com/ghodss/yaml"
1214
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
@@ -71,11 +73,22 @@ func (c *client) InstallChart(ctx devspacecontext.Context, releaseName string, r
7173
if helmConfig.Chart.Version != "" {
7274
args = append(args, "--version", helmConfig.Chart.Version)
7375
}
74-
if helmConfig.Chart.Username != "" {
75-
args = append(args, "--username", helmConfig.Chart.Username)
76-
}
77-
if helmConfig.Chart.Password != "" {
78-
args = append(args, "--password", helmConfig.Chart.Password)
76+
77+
// log into OCI registry if specified
78+
if strings.HasPrefix(chartName, "oci://") {
79+
if helmConfig.Chart.Username != "" && helmConfig.Chart.Password != "" {
80+
_, err := c.genericHelm.Exec(ctx, []string{"registry", "login", "--username", helmConfig.Chart.Username, "--password", helmConfig.Chart.Password})
81+
if err != nil {
82+
return nil, errors.Wrap(err, "login oci registry")
83+
}
84+
}
85+
} else {
86+
if helmConfig.Chart.Username != "" {
87+
args = append(args, "--username", helmConfig.Chart.Username)
88+
}
89+
if helmConfig.Chart.Password != "" {
90+
args = append(args, "--password", helmConfig.Chart.Password)
91+
}
7992
}
8093
}
8194

0 commit comments

Comments
 (0)