From 1034358614a7db3eb86787f7b6c10473c4241888 Mon Sep 17 00:00:00 2001 From: James Peach Date: Tue, 26 Jul 2022 15:59:37 +1000 Subject: [PATCH] Add a default build configuration to ocb. Embed the build configuration that is used to build otelcorecol into ocb so that end users can easily generate a useful collector. This makes the `--config` flag optional again. Signed-off-by: James Peach --- CHANGELOG.md | 1 + Makefile | 2 ++ cmd/builder/Makefile | 9 +++++++ cmd/builder/internal/command.go | 30 ++++++++++++++-------- cmd/builder/internal/config/default.go | 32 ++++++++++++++++++++++++ cmd/builder/internal/config/default.yaml | 28 +++++++++++++++++++++ 6 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 cmd/builder/internal/config/default.go create mode 100644 cmd/builder/internal/config/default.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index ee9e141f493..d1f2b3e67d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### 💡 Enhancements 💡 - `ocb` now exits with an error if it fails to load the build configuration. (#5731) +- `ocb` now generates a default Collector when no build configuration is supplied. ### 🧰 Bug fixes 🧰 diff --git a/Makefile b/Makefile index 53652d14390..93814e49845 100644 --- a/Makefile +++ b/Makefile @@ -205,6 +205,7 @@ genotelcorecol: .PHONY: ocb ocb: + $(MAKE) -C cmd/builder config $(MAKE) -C cmd/builder ocb DEPENDABOT_PATH=".github/dependabot.yml" @@ -397,6 +398,7 @@ endif sed -i.bak 's/$(PREVIOUS_VERSION)/$(RELEASE_CANDIDATE)/g' examples/k8s/otel-config.yaml find . -name "*.bak" -type f -delete # regenerate files + $(MAKE) -C cmd/builder config $(MAKE) genotelcorecol # commit changes before running multimod git checkout -b opentelemetry-collector-bot/release-$(RELEASE_CANDIDATE) diff --git a/cmd/builder/Makefile b/cmd/builder/Makefile index 59bde934421..aed509584fa 100644 --- a/cmd/builder/Makefile +++ b/cmd/builder/Makefile @@ -3,3 +3,12 @@ include ../../Makefile.Common .PHONY: ocb ocb: GO111MODULE=on CGO_ENABLED=0 $(GOCMD) build -trimpath -o ../../bin/ocb_$(GOOS)_$(GOARCH) . + +# Generate the default build config from otelcorecol, by removing the +# "replaces" stanza, which is assumed to be at the end of the file. +# +# The default config file is checked in so that `go install` will work +# and so that non-unix builds don't need sed to be installed. +.PHONY: config +config: internal/config/default.yaml + sed '-e/replaces:/,$$d' <../otelcorecol/builder-config.yaml > internal/config/default.yaml diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index 0e9831b165e..5f0bf088c45 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -27,6 +27,7 @@ import ( "go.uber.org/zap" "go.opentelemetry.io/collector/cmd/builder/internal/builder" + "go.opentelemetry.io/collector/cmd/builder/internal/config" ) var ( @@ -44,7 +45,8 @@ func Command() (*cobra.Command, error) { Long: fmt.Sprintf("OpenTelemetry Collector Builder (%s)", version) + ` ocb generates a custom OpenTelemetry Collector binary using the -build configuration given by the "--config" argument. +build configuration given by the "--config" argument. If no build +configuration is provided, ocb will generate a default Collector. `, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { @@ -65,13 +67,6 @@ build configuration given by the "--config" argument. cmd.Flags().StringVar(&cfgFile, "config", "", "build configuration file") - // A build configuration file is always required, and there's no - // default. We can relax this in future by embedding the default - // config that is used to build otelcorecol. - if err := cmd.MarkFlagRequired("config"); err != nil { - panic(err) // Only fails if the usage message is empty, which is a programmer error. - } - // 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") @@ -96,8 +91,18 @@ func initConfig() error { cfg.Logger.Info("OpenTelemetry Collector Builder", zap.String("version", version), zap.String("date", date)) - // load the config file - if err := k.Load(file.Provider(cfgFile), yaml.Parser()); err != nil { + var provider koanf.Provider + + if cfgFile != "" { + // load the config file + provider = file.Provider(cfgFile) + } else { + // or the default if the config isn't provided + provider = config.DefaultProvider() + cfg.Logger.Info("Using default build configuration") + } + + if err := k.Load(provider, yaml.Parser()); err != nil { return fmt.Errorf("failed to load configuration file: %w", err) } @@ -112,6 +117,9 @@ func initConfig() error { return fmt.Errorf("failed to unmarshal configuration: %w", err) } - cfg.Logger.Info("Using config file", zap.String("path", cfgFile)) + if cfgFile != "" { + cfg.Logger.Info("Using config file", zap.String("path", cfgFile)) + } + return nil } diff --git a/cmd/builder/internal/config/default.go b/cmd/builder/internal/config/default.go new file mode 100644 index 00000000000..9ac8fd52561 --- /dev/null +++ b/cmd/builder/internal/config/default.go @@ -0,0 +1,32 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package config + +import ( + "embed" + + "github.com/knadh/koanf" + "github.com/knadh/koanf/providers/fs" +) + +//go:embed *.yaml +var configs embed.FS + +// DefaultProvider returns a koanf.Provider that provides the default build +// configuration file. This is the same configuration that otelcorecol is +// built with. +func DefaultProvider() koanf.Provider { + return fs.Provider(configs, "default.yaml") +} diff --git a/cmd/builder/internal/config/default.yaml b/cmd/builder/internal/config/default.yaml new file mode 100644 index 00000000000..2182f717e65 --- /dev/null +++ b/cmd/builder/internal/config/default.yaml @@ -0,0 +1,28 @@ +dist: + module: go.opentelemetry.io/collector/cmd/otelcorecol + name: otelcorecol + description: Local OpenTelemetry Collector binary, testing only. + version: 0.56.0-dev + otelcol_version: 0.56.0 + +receivers: + - import: go.opentelemetry.io/collector/receiver/otlpreceiver + gomod: go.opentelemetry.io/collector v0.56.0 +exporters: + - import: go.opentelemetry.io/collector/exporter/loggingexporter + gomod: go.opentelemetry.io/collector v0.56.0 + - import: go.opentelemetry.io/collector/exporter/otlpexporter + gomod: go.opentelemetry.io/collector v0.56.0 + - import: go.opentelemetry.io/collector/exporter/otlphttpexporter + gomod: go.opentelemetry.io/collector v0.56.0 +extensions: + - import: go.opentelemetry.io/collector/extension/ballastextension + gomod: go.opentelemetry.io/collector v0.56.0 + - import: go.opentelemetry.io/collector/extension/zpagesextension + gomod: go.opentelemetry.io/collector v0.56.0 +processors: + - import: go.opentelemetry.io/collector/processor/batchprocessor + gomod: go.opentelemetry.io/collector v0.56.0 + - import: go.opentelemetry.io/collector/processor/memorylimiterprocessor + gomod: go.opentelemetry.io/collector v0.56.0 +