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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
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 @@ -14,6 +14,9 @@ require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/config/configopaque v1.19.0
go.opentelemetry.io/collector/config/configtls v1.19.0
go.opentelemetry.io/collector/confmap v1.19.0
go.opentelemetry.io/collector/confmap/provider/envprovider v1.19.0
go.opentelemetry.io/collector/confmap/provider/fileprovider v1.19.0
go.opentelemetry.io/collector/semconv v0.113.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
Expand All @@ -25,9 +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.0.0-alpha.1 // 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.1 // 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
12 changes: 10 additions & 2 deletions cmd/opampsupervisor/go.sum

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

40 changes: 26 additions & 14 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,18 +14,18 @@ import (
"runtime"
"time"

"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 @@ -36,18 +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 {
resolverSettings := confmap.ResolverSettings{
URIs: []string{configFile},
ProviderFactories: []confmap.ProviderFactory{
fileprovider.NewFactory(),
envprovider.NewFactory(),
},
ConverterFactories: []confmap.ConverterFactory{},
DefaultScheme: "env",
}

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

decodeConf := koanf.UnmarshalConf{
Tag: "mapstructure",
conf, err := resolver.Resolve(context.Background())
if err != nil {
return Supervisor{}, err
}

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

if err := cfg.Validate(); err != nil {
Expand Down Expand Up @@ -120,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 @@ -150,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