Skip to content

operator run --local: --go-ldflags not effective #2477

@benoit-a

Description

@benoit-a

Bug Report

What did you do?

I am trying to run locally an operator and pass the current version to the binary using a LDFLAG to the go build stage like this:

operator-sdk run --local --go-ldflags "-X=main.version=1.2.3.4"

What did you expect to see?

I expected the version to be set and displayed properly in the log (at start)

What did you see instead? Under which circumstances?

The operator logs the placeholder value.

Environment

  • operator-sdk version:
operator-sdk version: "v0.15.0", commit: "21a93ca379b887ab2303b0d148a399bf205c3231", go version: "go1.13.6 darwin/amd64"
  • go version:
go version go1.13.6 darwin/amd64
  • Kubernetes version information:
❯ kubectl version
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.2", GitCommit:"59603c6e503c87169aea6106f57b9f242f64df89", GitTreeState:"clean", BuildDate:"2020-01-23T14:21:36Z", GoVersion:"go1.13.6", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.3", GitCommit:"2d3c76f9091b6bec110a5e63777c332469e0cba2", GitTreeState:"clean", BuildDate:"2019-08-19T11:05:50Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
  • Kubernetes cluster kind:

Installed on bare metal.

  • Are you writing your operator in ansible, helm, or go?

go

Possible Solution

After a quick review of PR #2441, I suspect this is due to the change in cmd/operator-sdk/run/local.go ; previously the flag variables were declared globally. They are now part of a new structure called runLocalArgs.

Specifically, the function addToFlags is not getting a pointer to the object but is receiving a copy and so the StringVar declaration is pointing to a copy of ldFlags

I had some success with this patch:

diff --git cmd/operator-sdk/run/local.go cmd/operator-sdk/run/local.go
index 593159c9..b570c589 100644
--- cmd/operator-sdk/run/local.go
+++ cmd/operator-sdk/run/local.go
@@ -48,7 +48,7 @@ type runLocalArgs struct {
        helmOperatorFlags    *hoflags.HelmOperatorFlags
 }

-func (c runLocalArgs) addToFlags(fs *pflag.FlagSet) {
+func (c *runLocalArgs) addToFlags(fs *pflag.FlagSet) {
        prefix := "[local only] "
        fs.StringVar(&c.operatorFlags, "operator-flags", "",
                prefix+"The flags that the operator needs. Example: \"--flag1 value1 --flag2=value2\"")

Metadata

Metadata

Assignees

Labels

kind/bugCategorizes issue or PR as related to a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions