Skip to content

Commit

Permalink
Add a default build configuration to ocb.
Browse files Browse the repository at this point in the history
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 <jpeach@cloudflare.com>
  • Loading branch information
jpeach committed Jul 26, 2022
1 parent 74cca75 commit 1034358
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🧰

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ genotelcorecol:

.PHONY: ocb
ocb:
$(MAKE) -C cmd/builder config
$(MAKE) -C cmd/builder ocb

DEPENDABOT_PATH=".github/dependabot.yml"
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions cmd/builder/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 19 additions & 11 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
}

Expand All @@ -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
}
32 changes: 32 additions & 0 deletions cmd/builder/internal/config/default.go
Original file line number Diff line number Diff line change
@@ -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")
}
28 changes: 28 additions & 0 deletions cmd/builder/internal/config/default.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1034358

Please sign in to comment.