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/opampsupervisor] feat: Support environment variable expansion in supervisor config #36270

Merged
Merged
27 changes: 27 additions & 0 deletions .chloggen/opamp_supervisor_env_vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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. filelogreceiver)
component: cmd/opampsupervisor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support environment variable expansion in the OpAMP supervisor config.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [36269]

# (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 your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# 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]
6 changes: 5 additions & 1 deletion cmd/opampsupervisor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.22.0

require (
github.com/cenkalti/backoff/v4 v4.3.0
github.com/go-viper/mapstructure/v2 v2.2.1
github.com/google/uuid v1.6.0
github.com/knadh/koanf/maps v0.1.1
github.com/knadh/koanf/parsers/yaml v0.1.0
Expand All @@ -15,6 +14,9 @@ require (
github.com/stretchr/testify v1.10.0
go.opentelemetry.io/collector/config/configopaque v1.20.1-0.20241202231142-b9ff1bc54c99
go.opentelemetry.io/collector/config/configtls v1.20.1-0.20241202231142-b9ff1bc54c99
go.opentelemetry.io/collector/confmap v1.20.1-0.20241202231142-b9ff1bc54c99
go.opentelemetry.io/collector/confmap/provider/envprovider v1.20.1-0.20241202231142-b9ff1bc54c99
go.opentelemetry.io/collector/confmap/provider/fileprovider v1.20.1-0.20241202231142-b9ff1bc54c99
go.opentelemetry.io/collector/semconv v0.114.1-0.20241202231142-b9ff1bc54c99
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
Expand All @@ -26,8 +28,10 @@ require (
require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down
8 changes: 8 additions & 0 deletions cmd/opampsupervisor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 27 additions & 24 deletions cmd/opampsupervisor/supervisor/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package config

import (
"context"
"errors"
"fmt"
"net/http"
Expand All @@ -13,19 +14,18 @@ import (
"runtime"
"time"

"github.com/go-viper/mapstructure/v2"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/open-telemetry/opamp-go/protobufs"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/provider/envprovider"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
"go.uber.org/zap/zapcore"
)

// Supervisor is the Supervisor config file format.
type Supervisor struct {
Server OpAMPServer
Agent Agent
Server OpAMPServer `mapstructure:"server"`
Agent Agent `mapstructure:"agent"`
Capabilities Capabilities `mapstructure:"capabilities"`
Storage Storage `mapstructure:"storage"`
Telemetry Telemetry `mapstructure:"telemetry"`
Expand All @@ -37,26 +37,29 @@ func Load(configFile string) (Supervisor, error) {
return Supervisor{}, errors.New("path to config file cannot be empty")
}

k := koanf.New("::")
if err := k.Load(file.Provider(configFile), yaml.Parser()); err != nil {
return Supervisor{}, err
resolverSettings := confmap.ResolverSettings{
URIs: []string{configFile},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{},
DefaultScheme: "env",
}

cfg := DefaultSupervisor()
resolver, err := confmap.NewResolver(resolverSettings)
if err != nil {
return Supervisor{}, err
}

decodeConf := koanf.UnmarshalConf{
Tag: "mapstructure",
DecoderConfig: &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeDurationHookFunc()),
Result: &cfg,
WeaklyTypedInput: true,
ErrorUnused: true,
},
conf, err := resolver.Resolve(context.Background())
if err != nil {
return Supervisor{}, err
}

if err := k.UnmarshalWithConf("", &cfg, decodeConf); err != nil {
return Supervisor{}, fmt.Errorf("cannot parse %s: %w", configFile, err)
cfg := DefaultSupervisor()
if err = conf.Unmarshal(&cfg); err != nil {
return Supervisor{}, err
}

if err := cfg.Validate(); err != nil {
Expand Down Expand Up @@ -129,8 +132,8 @@ func (c Capabilities) SupportedCapabilities() protobufs.AgentCapabilities {
}

type OpAMPServer struct {
Endpoint string
Headers http.Header
Endpoint string `mapstructure:"endpoint"`
Headers http.Header `mapstructure:"headers"`
TLSSetting configtls.ClientConfig `mapstructure:"tls,omitempty"`
}

Expand Down Expand Up @@ -159,7 +162,7 @@ func (o OpAMPServer) Validate() error {
}

type Agent struct {
Executable string
Executable string `mapstructure:"executable"`
OrphanDetectionInterval time.Duration `mapstructure:"orphan_detection_interval"`
Description AgentDescription `mapstructure:"description"`
ConfigApplyTimeout time.Duration `mapstructure:"config_apply_timeout"`
Expand Down
Loading
Loading