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

WIP [builder] Support for --skip-new-go-module proof of concept #9253

Closed
wants to merge 3 commits into from
Closed
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
41 changes: 41 additions & 0 deletions cmd/builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,44 @@ then commit the code in a git repo. A CI can sync the code and execute
ocb --skip-generate --skip-get-modules --config=config.yaml
```
to only execute the compilation step.

### Avoiding the use of a new go.mod file

There is an additional option that controls one aspect of the build
process, which specifically allows skipping the use of a new `go.mod`
file. When the `--skip-new-go-module` command-line flag is supplied,
the build process issues a `go get` command for each component,
relying on the Go toolchain to update the enclosing Go module
appropriately.

This command will avoid downgrading a dependency in the enclosing
module, according to
[`semver.Compare()`](https://pkg.go.dev/golang.org/x/mod/semver#Compare),
and will instead issue a log indicating that the component was not
upgraded.

This mode cannot be used in conjunction with several features that
control the generated `go.mod` file, including `replaces`, `excludes`,
and the component `path` override. For each of these features, users
are expected to modify the enclosing `go.mod` directly.

### Strict versioning checks

There is an option that controls version strictness applied by the
builder. When the `--strict-versioning` command-line flag is used,
the following additional checks apply to the finished `go.mod` file
after getting all the components and tidying the result.

1. The `dist::otelcol_version` field in the build configuration must
match the core library version calculated by the Go toolchain,
considering all components. A mismatch could happen, for example,
when one of the components depends on a newer release of the core
collector library.
2. For each component in the build configuration, the version included
in the `gomod` module specifier must match the one calculated by
the Go toolchain, considering all components. A mismatch could
happen, for example, when the enclosing Go module uses a newer
release of the core collector library.

When `--skip-new-go-module` is used, these checks apply to the
enclosing Go module; otherwise, they apply to the generated module.
1 change: 1 addition & 0 deletions cmd/builder/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/builder/go.sum

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

44 changes: 31 additions & 13 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ var ErrInvalidGoMod = errors.New("invalid gomod specification for module")

// Config holds the builder's configuration
type Config struct {
Logger *zap.Logger
SkipGenerate bool `mapstructure:"-"`
SkipCompilation bool `mapstructure:"-"`
SkipGetModules bool `mapstructure:"-"`
LDFlags string `mapstructure:"-"`
Verbose bool `mapstructure:"-"`
Logger *zap.Logger
SkipGenerate bool `mapstructure:"-"`
SkipCompilation bool `mapstructure:"-"`
SkipGetModules bool `mapstructure:"-"`
SkipNewGoModule bool `mapstructure:"-"`
StrictVersioning bool `mapstructure:"-"`
LDFlags string `mapstructure:"-"`
Verbose bool `mapstructure:"-"`

Distribution Distribution `mapstructure:"dist"`
Exporters []Module `mapstructure:"exporters"`
Expand Down Expand Up @@ -87,11 +89,12 @@ func NewDefaultConfig() Config {
// Validate checks whether the current configuration is valid
func (c *Config) Validate() error {
return multierr.Combine(
validateModules(c.Extensions),
validateModules(c.Receivers),
validateModules(c.Exporters),
validateModules(c.Processors),
validateModules(c.Connectors),
c.validateModules(c.Extensions),
c.validateModules(c.Receivers),
c.validateModules(c.Exporters),
c.validateModules(c.Processors),
c.validateModules(c.Connectors),
c.validateFlags(),
)
}

Expand Down Expand Up @@ -158,20 +161,35 @@ func (c *Config) ParseModules() error {
return nil
}

func validateModules(mods []Module) error {
func (c *Config) validateFlags() error {
if c.SkipNewGoModule && (len(c.Replaces) != 0 || len(c.Excludes) != 0) {
return fmt.Errorf("cannot combine excludes or replaces with --skip-new-go-module; please modify the enclosing go.mod file directly, in this case")
}
return nil
}

func (c *Config) validateModules(mods []Module) error {
for _, mod := range mods {
if mod.GoMod == "" {
return fmt.Errorf("module %q: %w", mod.GoMod, ErrInvalidGoMod)
}
if mod.Path != "" && c.SkipNewGoModule {
return fmt.Errorf("cannot modify gomod path combined with --skip-new-go-module; please modify the enclosing go.mod file directly, in this case")
}
}
return nil
}

func parseModules(mods []Module) ([]Module, error) {
var parsedModules []Module
for _, mod := range mods {
module, _, found := strings.Cut(mod.GoMod, " ")
if !found {
return mods, fmt.Errorf("go.mod module specifier syntax is <module><space><version>: %q", mod.GoMod)
}

if mod.Import == "" {
mod.Import = strings.Split(mod.GoMod, " ")[0]
mod.Import = module
}

if mod.Name == "" {
Expand Down
Loading
Loading