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] Update builder flags precedence #5726

Merged
merged 7 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### 🧰 Bug fixes 🧰

- Fix bug in ocb where flags did not take precedence. (#5726)

## v0.56.0 Beta

### 💡 Enhancements 💡
Expand Down
73 changes: 61 additions & 12 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ import (
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"go.uber.org/zap"

"go.opentelemetry.io/collector/cmd/builder/internal/builder"
)

const (
SkipCompilationFlag = "skip-compilation"
DistributionNameFlag = "name"
DistributionDescriptionFlag = "description"
DistributionVersionFlag = "version"
DistributionOtelColVersionFlag = "otelcol-version"
DistributionOutputPathFlag = "output-path"
DistributionGoFlag = "go"
DistributionModuleFlag = "module"
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
)

var (
cfgFile string
cfg = builder.NewDefaultConfig()
Expand All @@ -41,7 +53,7 @@ func Command() (*cobra.Command, error) {
Use: "builder",
Long: fmt.Sprintf("OpenTelemetry Collector distribution builder (%s)", version),
RunE: func(cmd *cobra.Command, args []string) error {
if err := initConfig(); err != nil {
if err := initConfig(cmd.Flags()); err != nil {
return err
}
if err := cfg.Validate(); err != nil {
Expand All @@ -62,14 +74,14 @@ func Command() (*cobra.Command, error) {
cmd.Flags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.otelcol-builder.yaml)")

// the distribution parameters, which we accept as CLI flags as well
cmd.Flags().BoolVar(&cfg.SkipCompilation, "skip-compilation", false, "Whether builder should only generate go code with no compile of the collector (default false)")
cmd.Flags().StringVar(&cfg.Distribution.Name, "name", "otelcol-custom", "The executable name for the OpenTelemetry Collector distribution")
cmd.Flags().StringVar(&cfg.Distribution.Description, "description", "Custom OpenTelemetry Collector distribution", "A descriptive name for the OpenTelemetry Collector distribution")
cmd.Flags().StringVar(&cfg.Distribution.Version, "version", "1.0.0", "The version for the OpenTelemetry Collector distribution")
cmd.Flags().StringVar(&cfg.Distribution.OtelColVersion, "otelcol-version", cfg.Distribution.OtelColVersion, "Which version of OpenTelemetry Collector to use as base")
cmd.Flags().StringVar(&cfg.Distribution.OutputPath, "output-path", cfg.Distribution.OutputPath, "Where to write the resulting files")
cmd.Flags().StringVar(&cfg.Distribution.Go, "go", "", "The Go binary to use during the compilation phase. Default: go from the PATH")
cmd.Flags().StringVar(&cfg.Distribution.Module, "module", "go.opentelemetry.io/collector/cmd/builder", "The Go module for the new distribution")
cmd.Flags().BoolVar(&cfg.SkipCompilation, SkipCompilationFlag, false, "Whether builder should only generate go code with no compile of the collector (default false)")
cmd.Flags().StringVar(&cfg.Distribution.Name, DistributionNameFlag, "otelcol-custom", "The executable name for the OpenTelemetry Collector distribution")
cmd.Flags().StringVar(&cfg.Distribution.Description, DistributionDescriptionFlag, "Custom OpenTelemetry Collector distribution", "A descriptive name for the OpenTelemetry Collector distribution")
cmd.Flags().StringVar(&cfg.Distribution.Version, DistributionVersionFlag, "1.0.0", "The version for the OpenTelemetry Collector distribution")
cmd.Flags().StringVar(&cfg.Distribution.OtelColVersion, DistributionOtelColVersionFlag, cfg.Distribution.OtelColVersion, "Which version of OpenTelemetry Collector to use as base")
cmd.Flags().StringVar(&cfg.Distribution.OutputPath, DistributionOutputPathFlag, cfg.Distribution.OutputPath, "Where to write the resulting files")
cmd.Flags().StringVar(&cfg.Distribution.Go, DistributionGoFlag, "", "The Go binary to use during the compilation phase. Default: go from the PATH")
cmd.Flags().StringVar(&cfg.Distribution.Module, DistributionModuleFlag, "go.opentelemetry.io/collector/cmd/builder", "The Go module for the new distribution")

// version of this binary
cmd.AddCommand(versionCommand())
Expand All @@ -81,8 +93,8 @@ func Command() (*cobra.Command, error) {
return cmd, nil
}

func initConfig() error {
cfg.Logger.Info("OpenTelemetry Collector distribution builder", zap.String("version", version), zap.String("date", date))
func initConfig(flags *flag.FlagSet) error {
cfg.Logger.Info("OpenTelemetry Collector distribution builder", zap.String(DistributionVersionFlag, version), zap.String("date", date))
// use the default path if there is no config file being specified
if cfgFile == "" {
cfgFile = "$HOME/.otelcol-builder.yaml"
Expand All @@ -100,11 +112,48 @@ func initConfig() error {
cfg.Logger.Error("failed to load env var", zap.Error(err))
}

if err := k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{Tag: "mapstructure"}); err != nil {
cfgFromFile := builder.Config{}
if err := k.UnmarshalWithConf("", &cfgFromFile, koanf.UnmarshalConf{Tag: "mapstructure"}); err != nil {
cfg.Logger.Error("failed to unmarshal config", zap.Error(err))
return err
}

applyCfgFromFile(flags, cfgFromFile)

cfg.Logger.Info("Using config file", zap.String("path", cfgFile))
return nil
}

func applyCfgFromFile(flags *flag.FlagSet, cfgFromFile builder.Config) {
cfg.Exporters = cfgFromFile.Exporters
cfg.Extensions = cfgFromFile.Extensions
cfg.Receivers = cfgFromFile.Receivers
cfg.Processors = cfgFromFile.Processors
cfg.Replaces = cfgFromFile.Replaces
cfg.Excludes = cfgFromFile.Excludes

if !flags.Changed(SkipCompilationFlag) && cfgFromFile.SkipCompilation {
cfg.SkipCompilation = cfgFromFile.SkipCompilation
}
if !flags.Changed(DistributionNameFlag) && cfgFromFile.Distribution.Name != "" {
cfg.Distribution.Name = cfgFromFile.Distribution.Name
}
if !flags.Changed(DistributionDescriptionFlag) && cfgFromFile.Distribution.Description != "" {
cfg.Distribution.Description = cfgFromFile.Distribution.Description
}
if !flags.Changed(DistributionVersionFlag) && cfgFromFile.Distribution.Version != "" {
cfg.Distribution.Version = cfgFromFile.Distribution.Version
}
if !flags.Changed(DistributionOtelColVersionFlag) && cfgFromFile.Distribution.OtelColVersion != "" {
cfg.Distribution.OtelColVersion = cfgFromFile.Distribution.OtelColVersion
}
if !flags.Changed(DistributionOutputPathFlag) && cfgFromFile.Distribution.OutputPath != "" {
cfg.Distribution.OutputPath = cfgFromFile.Distribution.OutputPath
}
if !flags.Changed(DistributionGoFlag) && cfgFromFile.Distribution.Go != "" {
cfg.Distribution.Go = cfgFromFile.Distribution.Go
}
if !flags.Changed(DistributionModuleFlag) && cfgFromFile.Distribution.Module != "" {
cfg.Distribution.Module = cfgFromFile.Distribution.Module
}
}