Skip to content

Commit

Permalink
[cmd/builder] Allow setting DefaultScheme in builder config (#10296)
Browse files Browse the repository at this point in the history
#### Description

Allows configuring `DefaultScheme` via the builder config.

#### Link to tracking issue
Related to
#10259
Related to
#10290

#### Testing
Local testing and unit tests

---------

Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 4, 2024
1 parent f980592 commit d136b6d
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 6 deletions.
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 `otelcol.CollectorSettings.ResolverSettings.DefaultScheme` via the builder's `conf_resolver.default_uri_scheme` configuration option

# 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 `conf_resolver.default_uri_scheme`:

```yaml
conf_resolver:
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"`

ConfResolver ConfResolver `mapstructure:"conf_resolver"`

downloadModules retry `mapstructure:"-"`
}

type ConfResolver struct {
// When set, will be used to set the CollectorSettings.ConfResolver.DefaultScheme value,
// which determines how the Collector interprets URIs that have no scheme, such as ${ENV}.
// 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: "ConfResolverDefaultURIScheme set",
cfgBuilder: func(t *testing.T) Config {
cfg := newTestConfig()
cfg.ConfResolver = ConfResolver{
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 .ConfResolver.DefaultURIScheme }}
DefaultScheme: "{{ .ConfResolver.DefaultURIScheme }}",
{{- 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.ConfResolver.DefaultURIScheme = cfgFromFile.ConfResolver.DefaultURIScheme

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,
ConfResolver: builder.ConfResolver{
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,
ConfResolver: builder.ConfResolver{
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.ConfResolver.DefaultURIScheme, cfg.ConfResolver.DefaultURIScheme)
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.102.0-dev
otelcol_version: 0.102.0

conf_resolver:
default_uri_scheme: "env"

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

0 comments on commit d136b6d

Please sign in to comment.