Skip to content

feat: allow kb layout works on operator-sdk run local command#2948

Merged
camilamacedo86 merged 7 commits intooperator-framework:masterfrom
camilamacedo86:cmd-run
May 6, 2020
Merged

feat: allow kb layout works on operator-sdk run local command#2948
camilamacedo86 merged 7 commits intooperator-framework:masterfrom
camilamacedo86:cmd-run

Conversation

@camilamacedo86
Copy link
Copy Markdown
Contributor

@camilamacedo86 camilamacedo86 commented Apr 30, 2020

Description of the change:

To maintain backwards compatibility and allow deprecation for it’s Kubebuilder Makefile equivalent “make run", the "operator-sdk run —local” command should work in Kubebuilder project layouts.

Note: All flags can work successfully with kb layout as well. However, we decide at the first moment hidden the --namespace (deprecated) and the --watch-namespace flags.

Motivation for the change:
KB integration

Local Tests
Following a summary.

  • Checking the command with a kb layout:

Screenshot 2020-04-30 at 19 15 07

  • Checking the command with old layouts:

Screenshot 2020-04-30 at 19 44 01

  • Flags

Screenshot 2020-05-01 at 02 57 42


log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just sort the imports and added the kbutils one.

Comment thread cmd/operator-sdk/run/local.go Outdated
func (c runLocalArgs) run() error {
log.Infof("Running the operator locally in namespace %s.", c.watchNamespace)

projutil.MustInProjectRoot()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it was been called inside of each run. It is a required check for all types.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This can actually be removed from here I think. We call this first thing in the operator-sdk run command here:

projutil.MustInProjectRoot()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Verified. Yes, you are right, we can remove it.

Comment thread cmd/operator-sdk/run/local.go Outdated
var dc *exec.Cmd
// Get the args that will be used to exec the binary.
// Users are allowed to use the flag operator-flags to pass any value that they may wish
args := c.buildArgsBasedOnOperatorFlag()
Copy link
Copy Markdown
Contributor Author

@camilamacedo86 camilamacedo86 Apr 30, 2020

Choose a reason for hiding this comment

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

Since I need to check how it works, I extracted the logic in funcs and then, added comments to make easier we understand and keep it maintained.

Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread internal/util/projutil/project_util.go Outdated
Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread cmd/operator-sdk/run/local.go Outdated
@camilamacedo86 camilamacedo86 requested a review from joelanford May 1, 2020 02:00
@camilamacedo86 camilamacedo86 added the kubebuilder-integration Relates to rewriting the SDK in Kubebuilder plugin form label May 1, 2020
Copy link
Copy Markdown
Contributor

@hasbro17 hasbro17 left a comment

Choose a reason for hiding this comment

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

Some nits on the refactoring not related to the alignment but the actual alignment seems okay.
LGTM

Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread internal/util/kubebuilder/constants.go Outdated
Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread internal/util/projutil/project_util.go Outdated
Comment thread internal/util/projutil/project_util.go Outdated
@camilamacedo86 camilamacedo86 requested a review from joelanford May 5, 2020 14:33
Copy link
Copy Markdown
Member

@joelanford joelanford left a comment

Choose a reason for hiding this comment

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

A few more comments :)

Comment on lines +60 to +62
fs.StringVar(&c.watchNamespace, "watch-namespace", "",
prefix+"The namespace where the operator watches for changes. Set \"\" for AllNamespaces, "+
"set \"ns1,ns2\" for MultiNamespace")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like more changes related to this are required around here and here

Copy link
Copy Markdown
Contributor Author

@camilamacedo86 camilamacedo86 May 6, 2020

Choose a reason for hiding this comment

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

For the first contidional here It will set watch-namepace == namespace if the flag is set. Since the flag does not exist when it is in the new layout all is fine. Anyway, we can be redundant and add a check as well to ensure case something change.

However, for the second conditional here you are right we need set it for cluster-scoped.

Comment thread cmd/operator-sdk/run/local.go Outdated
cmd = exec.Command(binName, args...)
}

// Kills the binary execution, if the exit signal be raised from the container
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

operator-sdk run --local is not running in a container.

Suggested change
// Kills the binary execution, if the exit signal be raised from the container
// Kill the command if an exit signal is received.

Comment thread cmd/operator-sdk/run/local.go Outdated
// Add default env vars and values informed via flags
c.addEnvVars(cmd)
if err := projutil.ExecCmd(cmd); err != nil {
return err
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Keep the error context

Suggested change
return err
return fmt.Errorf("failed to run operator locally: %v", err)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed it before because it will be : failed to run operator locally: failed to run operator locally: twice see here. However, I did the change as you suggested.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah! Didn't realize this was already duplicated. In that case, feel free to remove, either here or in a follow-up.

Comment thread cmd/operator-sdk/run/local.go Outdated
var outputBinName, mainPath string
if kbutil.HasProjectFile() {
outputBinName = filepath.Join(kbutil.BinBuildDir, projutil.GetProjectName()+"-local")
mainPath = kbutil.MainPath
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We compile the entire main package, not just main.go since it is possible for multiple go files to exist in one directory and be in the main package.

We should use projutil.GetGoPkg() as the path (following the pattern for what we're doing for SDK-scaffolded projects), and for clarity, I suggest renaming the variable to packagePath to align with the GoCmdOptions struct

Suggested change
mainPath = kbutil.MainPath
packagePath = projutil.GetGoPkg()

Comment thread cmd/operator-sdk/run/local.go Outdated
Args: c.getBuildRunLocalArgs(),
}
if err := projutil.GoBuild(opts); err != nil {
return outputBinName, err
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we failed to build the binary, we shouldn't return the outputBinName

Suggested change
return outputBinName, err
return "", err

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

make sense

@camilamacedo86 camilamacedo86 requested a review from joelanford May 6, 2020 15:52
Copy link
Copy Markdown
Member

@joelanford joelanford left a comment

Choose a reason for hiding this comment

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

A few more nits/fixes. LGTM once they're addressed.

Comment thread cmd/operator-sdk/run/local.go Outdated
Comment thread internal/util/kubebuilder/constants.go Outdated
Comment thread cmd/operator-sdk/run/local.go Outdated
@camilamacedo86 camilamacedo86 merged commit 305656e into operator-framework:master May 6, 2020
@camilamacedo86 camilamacedo86 deleted the cmd-run branch May 6, 2020 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kubebuilder-integration Relates to rewriting the SDK in Kubebuilder plugin form

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants