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

add ldflags builder cmd option #6946

Merged
merged 5 commits into from
Feb 1, 2023

Conversation

kristinapathak
Copy link
Contributor

Description: Adding a feature - ldflags option in builder cmd, to be passed to go build

Link to tracking Issue: #6940

Testing: manually built a collector with custom components who have build variables. Passing ldflags set the values of these variables.

Documentation: I added a description for the --help command, but I can add a small explanation to the README if people would prefer.

@kristinapathak kristinapathak requested review from a team and jpkrohling January 14, 2023 00:02
Copy link
Member

@jpkrohling jpkrohling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks alright, but this PR is missing the tests.

@codecov
Copy link

codecov bot commented Jan 17, 2023

Codecov Report

Base: 90.35% // Head: 90.35% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (2b9ad71) compared to base (1bfb180).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6946   +/-   ##
=======================================
  Coverage   90.35%   90.35%           
=======================================
  Files         243      243           
  Lines       14567    14570    +3     
=======================================
+ Hits        13162    13165    +3     
  Misses       1136     1136           
  Partials      269      269           
Impacted Files Coverage Δ
cmd/builder/internal/builder/config.go 68.67% <ø> (ø)
cmd/builder/internal/builder/main.go 59.57% <100.00%> (+0.87%) ⬆️
cmd/builder/internal/command.go 61.36% <100.00%> (+0.29%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@kristinapathak kristinapathak force-pushed the ldflags branch 2 times, most recently from 3f43f3e to 562c1ad Compare January 17, 2023 22:22
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this change, just one question

cmd/builder/internal/builder/main_test.go Outdated Show resolved Hide resolved
SkipGetModules bool `mapstructure:"-"`
SkipCompilation bool `mapstructure:"-"`
SkipGetModules bool `mapstructure:"-"`
LDFlags string `mapstructure:"-"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you expect this to be configured?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a command line argument. Ex:

builder --ldflags='-X "<pkg>.gitVersion=d49d45c039a2a89a26526afa3658de6429099bef" -X "<pkg>.gitTag=local-testing" -X "<pkg>.whoami=kristina.pathak"'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is the "--ldflags" flag defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmd/builder/internal/builder/main.go Outdated Show resolved Hide resolved
SkipGetModules bool `mapstructure:"-"`
SkipCompilation bool `mapstructure:"-"`
SkipGetModules bool `mapstructure:"-"`
LDFlags string `mapstructure:"-"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is the "--ldflags" flag defined?

SkipGetModules bool `mapstructure:"-"`
SkipCompilation bool `mapstructure:"-"`
SkipGetModules bool `mapstructure:"-"`
LDFlags string `mapstructure:"-"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be part of "Distribution" and we should not have a flag but be part of the yaml config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there we have other flags, and because we want to go with "configuration as a code". Which means to pass the configuration to the builder as a config file merged into the code repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my example here, you can see that ldflags include git information. The moment these values are committed and pushed, they are incorrect since it will be a new commit hash with new git tags (our tags include datetimes). If ldflags become a configuration value, my automation would have to add it as part of the build process, and the value won't be able to be committed with the rest of the configuration file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bogdandrutu, would you be open to moving this value under "Distribution" in the yaml config and allowing it to be set through a flag?

assert.NoError(t, cfg.SetGoPath())
require.NoError(t, GenerateAndCompile(cfg))

// Sleep for 1 second to make sure all processes using the files are completed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Contributor Author

@kristinapathak kristinapathak Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic was taken from pre-existing main tests:
https://github.com/open-telemetry/opentelemetry-collector/blame/80cabdd33c50eaf386293f90fd8ac57cb3a10dc9/cmd/builder/internal/builder/main_test.go#L106
I see that now they have been refactored so will rebase and include my test case, but this logic still exists in those tests.

@kristinapathak
Copy link
Contributor Author

TestServiceTelemetryRestart has failed in the test-coverage github action, which is unrelated to my changes in this PR.

@jpkrohling
Copy link
Member

I just triggered the failed test again.

@bogdandrutu bogdandrutu merged commit c0a08e3 into open-telemetry:main Feb 1, 2023
@kristinapathak kristinapathak deleted the ldflags branch February 1, 2023 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants