Skip to content

fix: Allow overriding env variable names, restore CODER_TELEMETRY #4894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
44 changes: 38 additions & 6 deletions cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func newConfig() *codersdk.DeploymentConfig {
Name: "Telemetry Enable",
Usage: "Whether telemetry is enabled or not. Coder collects anonymized usage data to help improve our product.",
Flag: "telemetry",
Env: "CODER_TELEMETRY", // Override default environment.
Default: flag.Lookup("test.v") == nil,
},
Trace: &codersdk.DeploymentConfigField[bool]{
Expand Down Expand Up @@ -294,6 +295,7 @@ func newConfig() *codersdk.DeploymentConfig {
Name: "Trace Enable",
Usage: "Whether application tracing data is collected. It exports to a backend configured by environment variables. See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md",
Flag: "trace",
Env: "CODER_TRACE", // Override default environment.
},
HoneycombAPIKey: &codersdk.DeploymentConfigField[string]{
Name: "Trace Honeycomb API Key",
Expand Down Expand Up @@ -370,7 +372,6 @@ func Config(flagset *pflag.FlagSet, vip *viper.Viper) (*codersdk.DeploymentConfi
return nil, xerrors.Errorf("get global config from flag: %w", err)
}
vip.SetEnvPrefix("coder")
vip.AutomaticEnv()

if flg != "" {
vip.SetConfigFile(flg + "/server.yaml")
Expand All @@ -385,6 +386,18 @@ func Config(flagset *pflag.FlagSet, vip *viper.Viper) (*codersdk.DeploymentConfi
return dc, nil
}

func setEnv(prefix string, vip *viper.Viper, val reflect.Value) {
env := val.FieldByName("Env").String()
if env == "" {
env = formatEnv(prefix)
}
// Ensure the Env field is always populated.
val.FieldByName("Env").SetString(env)

// Ensure the auto-generated or manually named env is bound.
vip.MustBindEnv(prefix, env)
}

func setConfig(prefix string, vip *viper.Viper, target interface{}) {
val := reflect.Indirect(reflect.ValueOf(target))
typ := val.Type()
Expand All @@ -393,21 +406,26 @@ func setConfig(prefix string, vip *viper.Viper, target interface{}) {
typ = val.Type()
}

// Manually bind to env to support CODER_$INDEX_$FIELD format for structured slices.
_ = vip.BindEnv(prefix, formatEnv(prefix))

// Ensure that we only bind env variables to proper fields,
// otherwise Viper will get confused if the parent struct is
// assigned a value.
if strings.HasPrefix(typ.Name(), "DeploymentConfigField[") {
value := val.FieldByName("Value").Interface()
switch value.(type) {
case string:
setEnv(prefix, vip, val)
val.FieldByName("Value").SetString(vip.GetString(prefix))
case bool:
setEnv(prefix, vip, val)
val.FieldByName("Value").SetBool(vip.GetBool(prefix))
case int:
setEnv(prefix, vip, val)
val.FieldByName("Value").SetInt(int64(vip.GetInt(prefix)))
case time.Duration:
setEnv(prefix, vip, val)
val.FieldByName("Value").SetInt(int64(vip.GetDuration(prefix)))
case []string:
setEnv(prefix, vip, val)
// As of October 21st, 2022 we supported delimiting a string
// with a comma, but Viper only supports with a space. This
// is a small hack around it!
Expand All @@ -422,6 +440,7 @@ func setConfig(prefix string, vip *viper.Viper, target interface{}) {
}
val.FieldByName("Value").Set(reflect.ValueOf(value))
case []codersdk.GitAuthConfig:
// Do not bind to CODER_GITAUTH, instead bind to CODER_GITAUTH_0_*, etc.
values := readSliceFromViper[codersdk.GitAuthConfig](vip, prefix, value)
val.FieldByName("Value").Set(reflect.ValueOf(values))
default:
Expand Down Expand Up @@ -471,6 +490,11 @@ func readSliceFromViper[T any](vip *viper.Viper, key string, value any) []T {
prop = fve.Tag.Get("yaml")
}
configKey := fmt.Sprintf("%s.%d.%s", key, entry, prop)

// Ensure the env entry for this key is registered
// before checking value.
vip.MustBindEnv(configKey, formatEnv(configKey))

value := vip.Get(configKey)
if value == nil {
continue
Expand Down Expand Up @@ -502,7 +526,6 @@ func NewViper() *viper.Viper {
dc := newConfig()
vip := viper.New()
vip.SetEnvPrefix("coder")
vip.AutomaticEnv()
vip.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))

setViperDefaults("", vip, dc)
Expand Down Expand Up @@ -560,12 +583,21 @@ func setFlags(prefix string, flagset *pflag.FlagSet, vip *viper.Viper, target in
if flg == "" {
return
}

env := val.FieldByName("Env").String()
if env == "" {
env = formatEnv(prefix)
}
usage := val.FieldByName("Usage").String()
usage = fmt.Sprintf("%s\n%s", usage, cliui.Styles.Placeholder.Render("Consumes $"+formatEnv(prefix)))
usage = fmt.Sprintf("%s\n%s", usage, cliui.Styles.Placeholder.Render("Consumes $"+env))
shorthand := val.FieldByName("Shorthand").String()
hidden := val.FieldByName("Hidden").Bool()
value := val.FieldByName("Default").Interface()

// Allow currently set environment variables
// to override default values in help output.
vip.MustBindEnv(prefix, env)

switch value.(type) {
case string:
_ = flagset.StringP(flg, shorthand, vip.GetString(prefix), usage)
Expand Down
23 changes: 20 additions & 3 deletions cli/deployment/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestConfig(t *testing.T) {
"CODER_PROVISIONER_DAEMONS": "5",
"CODER_SECURE_AUTH_COOKIE": "true",
"CODER_SSH_KEYGEN_ALGORITHM": "potato",
"CODER_TELEMETRY": "false",
"CODER_TELEMETRY": "true",
"CODER_TELEMETRY_TRACE": "false",
"CODER_WILDCARD_ACCESS_URL": "something-wildcard.com",
},
Expand All @@ -50,7 +50,7 @@ func TestConfig(t *testing.T) {
require.Equal(t, config.ProvisionerDaemons.Value, 5)
require.Equal(t, config.SecureAuthCookie.Value, true)
require.Equal(t, config.SSHKeygenAlgorithm.Value, "potato")
require.Equal(t, config.Telemetry.Enable.Value, false)
require.Equal(t, config.Telemetry.Enable.Value, true)
require.Equal(t, config.Telemetry.Trace.Value, false)
require.Equal(t, config.WildcardAccessURL.Value, "something-wildcard.com")
},
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestConfig(t *testing.T) {
}, {
Name: "Trace",
Env: map[string]string{
"CODER_TRACE_ENABLE": "true",
"CODER_TRACE": "true",
"CODER_TRACE_HONEYCOMB_API_KEY": "my-honeycomb-key",
},
Valid: func(config *codersdk.DeploymentConfig) {
Expand Down Expand Up @@ -199,6 +199,23 @@ func TestConfig(t *testing.T) {
Regex: "gitlab.com",
}}, config.GitAuth.Value)
},
}, {
Name: "Wrong env must not break default values",
Env: map[string]string{
"CODER_PROMETHEUS_ENABLE": "true",
"CODER_PROMETHEUS": "true", // Wrong env name, must not break prom addr.
},
Valid: func(config *codersdk.DeploymentConfig) {
require.Equal(t, config.Prometheus.Enable.Value, true)
require.Equal(t, config.Prometheus.Address.Value, config.Prometheus.Address.Default)
},
}, {
Name: "Env fields are populated with manual or automatic name",
Valid: func(config *codersdk.DeploymentConfig) {
require.Equal(t, config.Trace.Enable.Env, "CODER_TRACE") // Manual
require.Equal(t, config.Telemetry.Enable.Env, "CODER_TELEMETRY") // Manual
require.Equal(t, config.Prometheus.Address.Env, "CODER_PROMETHEUS_ADDRESS") // Automatic
},
}} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions codersdk/deploymentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type DeploymentConfigField[T Flaggable] struct {
Name string `json:"name"`
Usage string `json:"usage"`
Flag string `json:"flag"`
Env string `json:"env"`
Shorthand string `json:"shorthand"`
Enterprise bool `json:"enterprise"`
Hidden bool `json:"hidden"`
Expand All @@ -146,6 +147,7 @@ func (f *DeploymentConfigField[T]) MarshalJSON() ([]byte, error) {
Name string `json:"name"`
Usage string `json:"usage"`
Flag string `json:"flag"`
Env string `json:"env"`
Shorthand string `json:"shorthand"`
Enterprise bool `json:"enterprise"`
Hidden bool `json:"hidden"`
Expand All @@ -156,6 +158,7 @@ func (f *DeploymentConfigField[T]) MarshalJSON() ([]byte, error) {
Name: f.Name,
Usage: f.Usage,
Flag: f.Flag,
Env: f.Env,
Shorthand: f.Shorthand,
Enterprise: f.Enterprise,
Hidden: f.Hidden,
Expand Down
1 change: 1 addition & 0 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export interface DeploymentConfigField<T extends Flaggable> {
readonly name: string
readonly usage: string
readonly flag: string
readonly env: string
readonly shorthand: string
readonly enterprise: boolean
readonly hidden: boolean
Expand Down