Skip to content

Commit 971e5e1

Browse files
committed
refactor(internal/provider): extract variables, handle CODER_ envs as extra_env
1 parent 81e9e57 commit 971e5e1

File tree

3 files changed

+44
-50
lines changed

3 files changed

+44
-50
lines changed

internal/provider/helpers.go

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ import (
1212
"github.com/spf13/pflag"
1313
)
1414

15+
const (
16+
envbuilderOptionPrefix = "ENVBUILDER_"
17+
)
18+
19+
// nonOverrideOptions are options that cannot be overridden by extra_env.
20+
var nonOverrideOptions = map[string]struct{}{
21+
"ENVBUILDER_CACHE_REPO": {},
22+
"ENVBUILDER_GIT_URL": {},
23+
}
24+
1525
// optionsFromDataModel converts a CachedImageResourceModel into a corresponding set of
1626
// Envbuilder options. It returns the options and any diagnostics encountered.
1727
func optionsFromDataModel(data CachedImageResourceModel) (eboptions.Options, diag.Diagnostics) {
@@ -154,43 +164,39 @@ func overrideOptionsFromExtraEnv(opts *eboptions.Options, extraEnv map[string]st
154164
optsMap[opt.Env] = opt.Value
155165
}
156166
for key, val := range extraEnv {
157-
switch key {
158-
159-
// These options may not be overridden.
160-
case "ENVBUILDER_CACHE_REPO", "ENVBUILDER_GIT_URL":
167+
if _, found := nonOverrideOptions[key]; found {
161168
diags.AddAttributeWarning(path.Root("extra_env"),
162169
"Cannot override required environment variable",
163170
fmt.Sprintf("The key %q in extra_env cannot be overridden.", key),
164171
)
165172
continue
173+
}
174+
175+
// Check if the option was set on the provider data model and generate a warning if so.
176+
if _, overridden := overrides[key]; overridden {
177+
diags.AddAttributeWarning(path.Root("extra_env"),
178+
"Overriding provider environment variable",
179+
fmt.Sprintf("The key %q in extra_env overrides an option set on the provider.", key),
180+
)
181+
}
166182

167-
default:
168-
// Check if the option was set on the provider data model and generate a warning if so.
169-
if _, overridden := overrides[key]; overridden {
170-
diags.AddAttributeWarning(path.Root("extra_env"),
171-
"Overriding provider environment variable",
172-
fmt.Sprintf("The key %q in extra_env overrides an option set on the provider.", key),
173-
)
174-
}
175-
176-
// XXX: workaround for serpent behaviour where calling Set() on a
177-
// string slice will append instead of replace: set to empty first.
178-
if key == "ENVBUILDER_IGNORE_PATHS" {
179-
_ = optsMap[key].Set("")
180-
}
181-
182-
opt, found := optsMap[key]
183-
if !found {
184-
// ignore unknown keys
185-
continue
186-
}
187-
188-
if err := opt.Set(val); err != nil {
189-
diags.AddAttributeError(path.Root("extra_env"),
190-
"Invalid value for environment variable",
191-
fmt.Sprintf("The key %q in extra_env has an invalid value: %s", key, err),
192-
)
193-
}
183+
// XXX: workaround for serpent behaviour where calling Set() on a
184+
// string slice will append instead of replace: set to empty first.
185+
if key == "ENVBUILDER_IGNORE_PATHS" {
186+
_ = optsMap[key].Set("")
187+
}
188+
189+
opt, found := optsMap[key]
190+
if !found {
191+
// ignore unknown keys
192+
continue
193+
}
194+
195+
if err := opt.Set(val); err != nil {
196+
diags.AddAttributeError(path.Root("extra_env"),
197+
"Invalid value for environment variable",
198+
fmt.Sprintf("The key %q in extra_env has an invalid value: %s", key, err),
199+
)
194200
}
195201
}
196202
return diags
@@ -210,26 +216,17 @@ func computeEnvFromOptions(opts eboptions.Options, extraEnv map[string]string) m
210216
allEnvKeys[opt.Env] = struct{}{}
211217
}
212218

213-
// Only set the environment variables from opts that are not legacy options.
214-
// Legacy options are those that are not prefixed with ENVBUILDER_.
215-
// While we can detect when a legacy option is set, overriding it becomes
216-
// problematic. Erring on the side of caution, we will not override legacy options.
217-
isEnvbuilderOption := func(key string) bool {
218-
switch key {
219-
case "CODER_AGENT_URL", "CODER_AGENT_TOKEN", "CODER_AGENT_SUBSYSTEM":
220-
return true // kinda
221-
default:
222-
return strings.HasPrefix(key, "ENVBUILDER_")
223-
}
224-
}
225-
226219
computed := make(map[string]string)
227220
for _, opt := range opts.CLI() {
228221
if opt.Env == "" {
229222
continue
230223
}
231224
// TODO: remove this check once support for legacy options is removed.
232-
if !isEnvbuilderOption(opt.Env) {
225+
// Only set the environment variables from opts that are not legacy options.
226+
// Legacy options are those that are not prefixed with ENVBUILDER_.
227+
// While we can detect when a legacy option is set, overriding it becomes
228+
// problematic. Erring on the side of caution, we will not override legacy options.
229+
if !strings.HasPrefix(opt.Env, envbuilderOptionPrefix) {
233230
continue
234231
}
235232
var val string
@@ -250,7 +247,7 @@ func computeEnvFromOptions(opts eboptions.Options, extraEnv map[string]string) m
250247
// Merge in extraEnv, which may override values from opts.
251248
// Skip any keys that are envbuilder options.
252249
for key, val := range extraEnv {
253-
if isEnvbuilderOption(key) {
250+
if strings.HasPrefix(key, envbuilderOptionPrefix) {
254251
continue
255252
}
256253
computed[key] = val

internal/provider/provider_internal_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,6 @@ func Test_computeEnvFromOptions(t *testing.T) {
284284
"FOO": "bar", // should be included
285285
},
286286
expectEnv: map[string]string{
287-
"CODER_AGENT_SUBSYSTEM": "one,two",
288-
"CODER_AGENT_TOKEN": "string",
289-
"CODER_AGENT_URL": "string",
290287
"ENVBUILDER_BASE_IMAGE_CACHE_DIR": "string",
291288
"ENVBUILDER_BINARY_PATH": "string",
292289
"ENVBUILDER_BUILD_CONTEXT_PATH": "string",

internal/provider/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func seedCache(ctx context.Context, t testing.TB, deps testDependencies) {
118118
}
119119

120120
for k, v := range deps.ExtraEnv {
121-
if !strings.HasPrefix(k, "ENVBUILDER_") {
121+
if !strings.HasPrefix(k, envbuilderOptionPrefix) {
122122
continue
123123
}
124124
if _, ok := seedEnv[k]; ok {

0 commit comments

Comments
 (0)