@@ -8,6 +8,10 @@ import (
88 "fmt"
99 "github.com/loft-sh/devspace/pkg/devspace/context/values"
1010 "github.com/loft-sh/devspace/pkg/devspace/kubectl"
11+ "github.com/loft-sh/devspace/pkg/util/downloader"
12+ "github.com/loft-sh/devspace/pkg/util/downloader/commands"
13+ "github.com/loft-sh/devspace/pkg/util/log"
14+ "github.com/sirupsen/logrus"
1115 "os"
1216 "path/filepath"
1317 "strconv"
@@ -30,51 +34,60 @@ type PredefinedVariableOptions struct {
3034}
3135
3236// PredefinedVariableFunction is the definition of a predefined variable
33- type PredefinedVariableFunction func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error )
37+ type PredefinedVariableFunction func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error )
3438
3539// predefinedVars holds all predefined variables that can be used in the config
3640var predefinedVars = map [string ]PredefinedVariableFunction {
37- "DEVSPACE_NAME" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
41+ "DEVSPACE_NAME" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
3842 name , ok := values .NameFrom (ctx )
3943 if ! ok {
4044 return "" , nil
4145 }
4246 return name , nil
4347 },
44- "DEVSPACE_EXECUTABLE" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
48+ "DEVSPACE_EXECUTABLE" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
4549 ex , err := os .Executable ()
4650 if err != nil {
4751 return nil , err
4852 }
4953 return ex , nil
5054 },
51- "DEVSPACE_TMPDIR" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
55+ "DEVSPACE_KUBECTL_EXECUTABLE" : func (ctx context.Context , options * PredefinedVariableOptions , log log.Logger ) (interface {}, error ) {
56+ debugLog := log .WithLevel (logrus .DebugLevel )
57+ path , err := downloader .NewDownloader (commands .NewKubectlCommand (), debugLog ).EnsureCommand (ctx )
58+ if err != nil {
59+ debugLog .Debugf ("Error downloading kubectl: %v" , err )
60+ return "" , nil
61+ }
62+ return path , nil
63+ },
64+ "DEVSPACE_TMPDIR" : func (ctx context.Context , options * PredefinedVariableOptions , log log.Logger ) (interface {}, error ) {
5265 tempFolder , ok := values .TempFolderFrom (ctx )
5366 if ! ok {
5467 return os .TempDir (), nil
5568 }
5669 return tempFolder , nil
5770 },
58- "DEVSPACE_VERSION" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
71+ "DEVSPACE_VERSION" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
5972 return upgrade .GetVersion (), nil
6073 },
61- "DEVSPACE_RANDOM" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
74+ "DEVSPACE_RANDOM" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
6275 return randutil .GenerateRandomString (6 ), nil
6376 },
64- "DEVSPACE_PROFILE" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
77+ "DEVSPACE_PROFILE" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
6578 return options .Profile , nil
6679 },
67- "DEVSPACE_USER_HOME" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
80+ "DEVSPACE_USER_HOME" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
6881 homeDir , err := homedir .Dir ()
6982 if err != nil {
7083 return nil , err
7184 }
7285 return homeDir , nil
7386 },
74- "DEVSPACE_TIMESTAMP" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
87+ "DEVSPACE_TIMESTAMP" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
7588 return strconv .FormatInt (time .Now ().Unix (), 10 ), nil
7689 },
77- "DEVSPACE_GIT_BRANCH" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
90+ "DEVSPACE_GIT_BRANCH" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
7891 configPath := options .ConfigPath
7992 branch , err := git .GetBranch (filepath .Dir (configPath ))
8093 if err != nil {
@@ -83,7 +96,7 @@ var predefinedVars = map[string]PredefinedVariableFunction{
8396
8497 return branch , nil
8598 },
86- "DEVSPACE_GIT_COMMIT" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
99+ "DEVSPACE_GIT_COMMIT" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
87100 configPath := options .ConfigPath
88101 hash , err := git .GetHash (ctx , filepath .Dir (configPath ))
89102 if err != nil {
@@ -92,14 +105,14 @@ var predefinedVars = map[string]PredefinedVariableFunction{
92105
93106 return hash [:8 ], nil
94107 },
95- "DEVSPACE_CONTEXT" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
108+ "DEVSPACE_CONTEXT" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
96109 if options .KubeClient == nil {
97110 return "" , nil
98111 }
99112
100113 return options .KubeClient .CurrentContext (), nil
101114 },
102- "DEVSPACE_NAMESPACE" : func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
115+ "DEVSPACE_NAMESPACE" : func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
103116 if options .KubeClient == nil {
104117 return "" , nil
105118 }
@@ -133,7 +146,7 @@ func AddPredefinedVars(plugins []plugin.Metadata) {
133146 pluginFolder := p .PluginFolder
134147 for _ , variable := range p .Vars {
135148 v := variable
136- predefinedVars [variable .Name ] = func (ctx context.Context , options * PredefinedVariableOptions ) (interface {}, error ) {
149+ predefinedVars [variable .Name ] = func (ctx context.Context , options * PredefinedVariableOptions , log log. Logger ) (interface {}, error ) {
137150 args , err := json .Marshal (os .Args )
138151 if err != nil {
139152 return "" , err
@@ -155,20 +168,22 @@ func AddPredefinedVars(plugins []plugin.Metadata) {
155168
156169// NewPredefinedVariable creates a new predefined variable for the given name or fails if there
157170// is none with the given name
158- func NewPredefinedVariable (name string , options * PredefinedVariableOptions ) (Variable , error ) {
171+ func NewPredefinedVariable (name string , options * PredefinedVariableOptions , log log. Logger ) (Variable , error ) {
159172 if _ , ok := predefinedVars [name ]; ! ok {
160173 return nil , errors .New ("predefined variable " + name + " not found" )
161174 }
162175
163176 return & predefinedVariable {
164177 name : name ,
165178 options : options ,
179+ log : log ,
166180 }, nil
167181}
168182
169183type predefinedVariable struct {
170184 name string
171185 options * PredefinedVariableOptions
186+ log log.Logger
172187}
173188
174189func (p * predefinedVariable ) Load (ctx context.Context , definition * latest.Variable ) (interface {}, error ) {
@@ -177,5 +192,5 @@ func (p *predefinedVariable) Load(ctx context.Context, definition *latest.Variab
177192 return nil , errors .New ("predefined variable " + p .name + " not found" )
178193 }
179194
180- return getVar (ctx , p .options )
195+ return getVar (ctx , p .options , p . log )
181196}
0 commit comments