Skip to content
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

[cmd/builder] Allow setting DefaultScheme in builder config #10296

Merged
25 changes: 25 additions & 0 deletions .chloggen/builder-configure-default-scheme.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: cmd/builder

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow setting `CollectorSettings.ResolverSettings.DefaultScheme` via the builder's `conf_resolver_settings.default_uri_scheme` configuration option
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved

# One or more tracking issues or pull requests related to the change
issues: [10296]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
10 changes: 10 additions & 0 deletions cmd/builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ replaces:
- github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.40.0
```

The builder also allows setting the scheme to use as the default URI scheme via `resolver_settings.default_uri_scheme`:
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved

```yaml
conf_resolver_settings:
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
default_uri_scheme: "env"
```

This tells the builder to produce a Collector that uses the `env` scheme when expanding configuration that does not
provide a scheme, such as `${HOST}` (instead of doing `${env:HOST}`).

## Steps

The builder has 3 steps:
Expand Down
9 changes: 9 additions & 0 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@ type Config struct {
Replaces []string `mapstructure:"replaces"`
Excludes []string `mapstructure:"excludes"`

ConfResolverSettings ConfResolverSettings `mapstructure:"conf_resolver_settings"`
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved

downloadModules retry `mapstructure:"-"`
}

type ConfResolverSettings struct {
// When set, will be used to set the CollectorSettings.ConfResolverSettings.DefaultScheme value,
// which determines how the Collector interprets URIs that have no scheme, such as ${ENV}.
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
// See https://pkg.go.dev/go.opentelemetry.io/collector/confmap#ResolverSettings for more details.
DefaultURIScheme string `mapstructure:"default_uri_scheme"`
}

// Distribution holds the parameters for the final binary
type Distribution struct {
Module string `mapstructure:"module"`
Expand Down
12 changes: 12 additions & 0 deletions cmd/builder/internal/builder/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ func TestGenerateAndCompile(t *testing.T) {
return cfg
},
},
{
testCase: "ResolverSettingsDefaultURIScheme set",
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
cfg.ConfResolverSettings = ConfResolverSettings{
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
DefaultURIScheme: "env",
}
cfg.Distribution.OutputPath = t.TempDir()
cfg.Replaces = append(cfg.Replaces, replaces...)
return cfg
},
},
}

for _, tt := range testCases {
Expand Down
3 changes: 3 additions & 0 deletions cmd/builder/internal/builder/templates/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func main() {
{{.Name}}.NewFactory(),
{{- end}}
},
{{- if .ConfResolverSettings.DefaultURIScheme }}
DefaultScheme: "{{ .ConfResolverSettings.DefaultURIScheme }}",
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
{{- end }}
ConverterFactories: []confmap.ConverterFactory{
expandconverter.NewFactory(),
},
Expand Down
2 changes: 2 additions & 0 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ func applyCfgFromFile(flags *flag.FlagSet, cfgFromFile builder.Config) {
cfg.Replaces = cfgFromFile.Replaces
cfg.Excludes = cfgFromFile.Excludes

cfg.ConfResolverSettings.DefaultURIScheme = cfgFromFile.ConfResolverSettings.DefaultURIScheme
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved

if !flags.Changed(skipGenerateFlag) && cfgFromFile.SkipGenerate {
cfg.SkipGenerate = cfgFromFile.SkipGenerate
}
Expand Down
19 changes: 13 additions & 6 deletions cmd/builder/internal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Test_applyCfgFromFile(t *testing.T) {
wantErr bool
}{
{
name: "distribution, excludes, exporters, receivers, processors, replaces are applied correctly",
name: "distribution, scheme, excludes, exporters, receivers, processors, replaces are applied correctly",
args: args{
flags: flag.NewFlagSet("version=1.0.0", 1),
cfgFromFile: builder.Config{
Expand All @@ -95,16 +95,22 @@ func Test_applyCfgFromFile(t *testing.T) {
Receivers: []builder.Module{testModule},
Exporters: []builder.Module{testModule},
Replaces: testStringTable,
ConfResolverSettings: builder.ConfResolverSettings{
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
DefaultURIScheme: "env",
},
},
},
want: builder.Config{
Logger: zap.NewNop(),
Distribution: testDistribution,
Excludes: testStringTable,
Processors: []builder.Module{testModule},
Receivers: []builder.Module{testModule},
Exporters: []builder.Module{testModule},
Replaces: testStringTable,
ConfResolverSettings: builder.ConfResolverSettings{
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
DefaultURIScheme: "env",
},
Excludes: testStringTable,
Processors: []builder.Module{testModule},
Receivers: []builder.Module{testModule},
Exporters: []builder.Module{testModule},
Replaces: testStringTable,
},
wantErr: false,
},
Expand Down Expand Up @@ -246,6 +252,7 @@ func Test_applyCfgFromFile(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
applyCfgFromFile(tt.args.flags, tt.args.cfgFromFile)
assert.Equal(t, tt.want.ConfResolverSettings.DefaultURIScheme, cfg.ConfResolverSettings.DefaultURIScheme)
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, tt.want.Distribution, cfg.Distribution)
assert.Equal(t, tt.want.SkipGenerate, cfg.SkipGenerate)
assert.Equal(t, tt.want.SkipCompilation, cfg.SkipCompilation)
Expand Down
3 changes: 3 additions & 0 deletions cmd/builder/internal/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ dist:
version: 0.101.0-dev
otelcol_version: 0.101.0

conf_resolver_settings:
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
default_uri_scheme: "env"

receivers:
- gomod: go.opentelemetry.io/collector/receiver/nopreceiver v0.101.0
- gomod: go.opentelemetry.io/collector/receiver/otlpreceiver v0.101.0
Expand Down
Loading