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

scripts: add --go-ldflags argument to operator-sdk build command #1582

Merged
merged 2 commits into from
Jun 24, 2019
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Scaffold a `pkg/apis/<group>/group.go` package file to avoid `go/build` errors when running Kubernetes code generators. ([#1401](https://github.com/operator-framework/operator-sdk/pull/1401))
- Adds a new extra variable containing the unmodified CR spec for ansible based operators. [#1563](https://github.com/operator-framework/operator-sdk/pull/1563)
- New flag `--repo` for subcommands [`new`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#new) and [`migrate`](https://github.com/operator-framework/operator-sdk/blob/master/doc/sdk-cli-reference.md#migrate) specifies the repository path to be used in Go source files generated by the SDK. This flag can only be used with [Go modules](https://github.com/golang/go/wiki/Modules). ([#1475](https://github.com/operator-framework/operator-sdk/pull/1475))
- Adds `--go-build-args` flag to `operator-sdk build` for providing additional Go build arguments. ([#1582](https://github.com/operator-framework/operator-sdk/pull/1582))

### Changed

Expand Down
11 changes: 10 additions & 1 deletion cmd/operator-sdk/build/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
var (
imageBuildArgs string
imageBuilder string
goBuildArgs string
)

func NewCmd() *cobra.Command {
Expand All @@ -54,6 +55,7 @@ For example:
}
buildCmd.Flags().StringVar(&imageBuildArgs, "image-build-args", "", "Extra image build arguments as one string such as \"--build-arg https_proxy=$https_proxy\"")
buildCmd.Flags().StringVar(&imageBuilder, "image-builder", "docker", "Tool to build OCI images. One of: [docker, podman, buildah]")
buildCmd.Flags().StringVar(&goBuildArgs, "go-build-args", "", "Extra Go build arguments as one string such as \"-ldflags -X=main.xyz=abc\"")
return buildCmd
}

Expand Down Expand Up @@ -99,10 +101,17 @@ func buildFunc(cmd *cobra.Command, args []string) error {
// Don't need to build Go code if a non-Go Operator.
if projutil.IsOperatorGo() {
trimPath := fmt.Sprintf("all=-trimpath=%s", filepath.Dir(absProjectPath))
args := []string{"-gcflags", trimPath, "-asmflags", trimPath}

if goBuildArgs != "" {
splitArgs := strings.Fields(goBuildArgs)
args = append(args, splitArgs...)
}

opts := projutil.GoCmdOptions{
BinName: filepath.Join(absProjectPath, scaffold.BuildBinDir, projectName),
PackagePath: path.Join(projutil.GetGoPkg(), filepath.ToSlash(scaffold.ManagerDir)),
Args: []string{"-gcflags", trimPath, "-asmflags", trimPath},
Args: args,
Env: goBuildEnv,
GoMod: projutil.IsDepManagerGoMod(),
}
Expand Down
1 change: 1 addition & 0 deletions doc/sdk-cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Usage:

* `--image-build-args` string - extra, optional image build arguments as one string such as `"--build-arg https_proxy=$https_proxy"` (default "")
* `--image-builder` string - tool to build OCI images. One of: `[docker, podman, buildah]` (default "docker")
* `--go-build-args` string - extra Go build arguments as one string such as `"-ldflags -X=main.xyz=abc"`
* `-h, --help` - help for build

### Use
Expand Down