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 configuring confmap providers #9513

Merged
merged 15 commits into from
Apr 22, 2024
Merged
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
31 changes: 31 additions & 0 deletions .chloggen/builder-confmap-providers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 configuring `confmap.Provider`s in the builder.

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

# (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: |
If no providers are specified, the defaults are used.
The default providers are: env, file, http, https, and yaml.

To configure providers, use the `providers` key in your OCB build
manifest with a list of Go modules for your providers.
The modules will work the same as other Collector components.

# 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: [user]
14 changes: 10 additions & 4 deletions cmd/builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ dist:
description: Local OpenTelemetry Collector binary
output_path: /tmp/dist
exporters:
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter v0.86.0
- gomod: go.opentelemetry.io/collector/exporter/debugexporter v0.86.0
- gomod: github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alibabacloudlogserviceexporter v0.99.0
- gomod: go.opentelemetry.io/collector/exporter/debugexporter v0.99.0

receivers:
- gomod: go.opentelemetry.io/collector/receiver/otlpreceiver v0.86.0
- gomod: go.opentelemetry.io/collector/receiver/otlpreceiver v0.99.0

processors:
- gomod: go.opentelemetry.io/collector/processor/batchprocessor v0.86.0
- gomod: go.opentelemetry.io/collector/processor/batchprocessor v0.99.0

providers:
- gomod: go.opentelemetry.io/collector/confmap/provider/fileprovider v0.99.0

converters:
- gomod: go.opentelemetry.io/collector/confmap/converter/expandconverter v0.99.0
EOF
$ builder --config=otelcol-builder.yaml
$ cat > /tmp/otelcol.yaml <<EOF
Expand Down
77 changes: 65 additions & 12 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

// Config holds the builder's configuration
type Config struct {
Logger *zap.Logger
Logger *zap.Logger

SkipGenerate bool `mapstructure:"-"`
SkipCompilation bool `mapstructure:"-"`
SkipGetModules bool `mapstructure:"-"`
Expand All @@ -37,22 +38,24 @@
Receivers []Module `mapstructure:"receivers"`
Processors []Module `mapstructure:"processors"`
Connectors []Module `mapstructure:"connectors"`
Providers *[]Module `mapstructure:"providers"`
Replaces []string `mapstructure:"replaces"`
Excludes []string `mapstructure:"excludes"`
}

// Distribution holds the parameters for the final binary
type Distribution struct {
Module string `mapstructure:"module"`
Name string `mapstructure:"name"`
Go string `mapstructure:"go"`
Description string `mapstructure:"description"`
OtelColVersion string `mapstructure:"otelcol_version"`
RequireOtelColModule bool `mapstructure:"-"` // required for backwards-compatibility with builds older than 0.86.0
OutputPath string `mapstructure:"output_path"`
Version string `mapstructure:"version"`
BuildTags string `mapstructure:"build_tags"`
DebugCompilation bool `mapstructure:"debug_compilation"`
Module string `mapstructure:"module"`
Name string `mapstructure:"name"`
Go string `mapstructure:"go"`
Description string `mapstructure:"description"`
OtelColVersion string `mapstructure:"otelcol_version"`
RequireOtelColModule bool `mapstructure:"-"` // required for backwards-compatibility with builds older than 0.86.0
SupportsConfmapFactories bool `mapstructure:"-"` // Required for backwards-compatibility with builds older than 0.99.0
OutputPath string `mapstructure:"output_path"`
Version string `mapstructure:"version"`
BuildTags string `mapstructure:"build_tags"`
DebugCompilation bool `mapstructure:"debug_compilation"`
}

// Module represents a receiver, exporter, processor or extension for the distribution
Expand Down Expand Up @@ -87,12 +90,17 @@

// Validate checks whether the current configuration is valid
func (c *Config) Validate() error {
var providersError error
if c.Providers != nil {
providersError = validateModules(*c.Providers)
}
return multierr.Combine(
validateModules(c.Extensions),
validateModules(c.Receivers),
validateModules(c.Exporters),
validateModules(c.Processors),
validateModules(c.Connectors),
providersError,
)
}

Expand All @@ -112,7 +120,8 @@
return nil
}

func (c *Config) SetRequireOtelColModule() error {
func (c *Config) SetBackwardsCompatibility() error {
// check whether we need to adjust the core API module import
constraint, err := version.NewConstraint(">= 0.86.0")
if err != nil {
return err
Expand All @@ -124,6 +133,20 @@
}

c.Distribution.RequireOtelColModule = constraint.Check(otelColVersion)

// check whether confmap factories are supported
constraint, err = version.NewConstraint(">= 0.99.0")
if err != nil {
return err

Check warning on line 140 in cmd/builder/internal/builder/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/config.go#L140

Added line #L140 was not covered by tests
}

otelColVersion, err = version.NewVersion(c.Distribution.OtelColVersion)
if err != nil {
return err

Check warning on line 145 in cmd/builder/internal/builder/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/config.go#L145

Added line #L145 was not covered by tests
}

c.Distribution.SupportsConfmapFactories = constraint.Check(otelColVersion)

return nil
}

Expand Down Expand Up @@ -156,6 +179,36 @@
return err
}

if c.Providers != nil {
providers, err := parseModules(*c.Providers)
if err != nil {
return err

Check warning on line 185 in cmd/builder/internal/builder/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/config.go#L185

Added line #L185 was not covered by tests
}
c.Providers = &providers
} else {
providers, err := parseModules([]Module{
{
GoMod: "go.opentelemetry.io/collector/confmap/provider/envprovider v" + c.Distribution.OtelColVersion,
},
{
GoMod: "go.opentelemetry.io/collector/confmap/provider/fileprovider v" + c.Distribution.OtelColVersion,
},
{
GoMod: "go.opentelemetry.io/collector/confmap/provider/httpprovider v" + c.Distribution.OtelColVersion,
},
{
GoMod: "go.opentelemetry.io/collector/confmap/provider/httpsprovider v" + c.Distribution.OtelColVersion,
},
{
GoMod: "go.opentelemetry.io/collector/confmap/provider/yamlprovider v" + c.Distribution.OtelColVersion,
},
})
if err != nil {
return err

Check warning on line 207 in cmd/builder/internal/builder/config.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/builder/config.go#L207

Added line #L207 was not covered by tests
}
c.Providers = &providers
}

return nil
}

Expand Down
83 changes: 82 additions & 1 deletion cmd/builder/internal/builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,89 @@ func TestRequireOtelColModule(t *testing.T) {
t.Run(tt.Version, func(t *testing.T) {
cfg := NewDefaultConfig()
cfg.Distribution.OtelColVersion = tt.Version
require.NoError(t, cfg.SetRequireOtelColModule())
require.NoError(t, cfg.SetBackwardsCompatibility())
assert.Equal(t, tt.ExpectedRequireOtelColModule, cfg.Distribution.RequireOtelColModule)
})
}
}

func TestConfmapFactoryVersions(t *testing.T) {
testCases := []struct {
version string
supported bool
err bool
}{
{
version: "x.0.0",
supported: false,
err: true,
},
{
version: "0.x.0",
supported: false,
err: true,
},
{
version: "0.0.0",
supported: false,
},
{
version: "0.98.0",
supported: false,
},
{
version: "0.98.1",
supported: false,
},
{
version: "0.99.0",
supported: true,
},
{
version: "0.99.7",
supported: true,
},
{
version: "0.100.0",
supported: true,
},
{
version: "0.100.1",
supported: true,
},
{
version: "1.0",
supported: true,
},
{
version: "1.0.0",
supported: true,
},
}

for _, tt := range testCases {
t.Run(tt.version, func(t *testing.T) {
cfg := NewDefaultConfig()
cfg.Distribution.OtelColVersion = tt.version
if !tt.err {
require.NoError(t, cfg.SetBackwardsCompatibility())
assert.Equal(t, tt.supported, cfg.Distribution.SupportsConfmapFactories)
} else {
require.Error(t, cfg.SetBackwardsCompatibility())
}
})
}
}

func TestAddsDefaultProviders(t *testing.T) {
cfg := NewDefaultConfig()
cfg.Providers = nil
assert.NoError(t, cfg.ParseModules())
assert.Len(t, *cfg.Providers, 5)
}

func TestSkipsNilFieldValidation(t *testing.T) {
cfg := NewDefaultConfig()
cfg.Providers = nil
assert.NoError(t, cfg.Validate())
}
3 changes: 2 additions & 1 deletion cmd/builder/internal/builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ func (c *Config) allComponents() []Module {
append(c.Receivers,
append(c.Processors,
append(c.Extensions,
c.Connectors...)...)...)...)
append(c.Connectors,
*c.Providers...)...)...)...)...)
}

func (c *Config) readGoModFile() (string, map[string]string, error) {
Expand Down
Loading
Loading