Skip to content

Commit

Permalink
controller-runtime currently adds the --kubeconfig flag if
Browse files Browse the repository at this point in the history
pkg/client/config is imported (set up in an init() function).
The SDK's test framework also adds this flag, which will panic
if both are imported and the test framework is initialized.
This commit only registers --kubeconfig if it is not already
registered, and gets its value if so.

pkg/test: only register --kubeconfig if not registered, get value
if already registered
  • Loading branch information
estroz committed Mar 27, 2020
1 parent b0b67a4 commit ef61373
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 0 additions & 1 deletion pkg/test/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (opts *frameworkOpts) addToFlagSet(flagset *flag.FlagSet) {
flagset.StringVar(&opts.namespacedManPath, NamespacedManPathFlag, "", "path to rbac manifest")
flagset.BoolVar(&opts.isLocalOperator, LocalOperatorFlag, false,
"enable if operator is running locally (not in cluster)")
flagset.StringVar(&opts.kubeconfigPath, KubeConfigFlag, "", "path to kubeconfig")
flagset.StringVar(&opts.globalManPath, GlobalManPathFlag, "", "path to operator manifest")
flagset.StringVar(&opts.localOperatorArgs, LocalOperatorArgs, "",
"flags that the operator needs (while using --up-local). example: \"--flag1 value1 --flag2=value2\"")
Expand Down
14 changes: 14 additions & 0 deletions pkg/test/main_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,22 @@ import (
func MainEntry(m *testing.M) {
fopts := &frameworkOpts{}
fopts.addToFlagSet(flag.CommandLine)
// controller-runtime registers the --kubeconfig flag in client config
// package:
// https://github.com/kubernetes-sigs/controller-runtime/blob/v0.5.2/pkg/client/config/config.go#L39
//
// If this flag is not registered, do so. Otherwise retrieve its value.
kcFlag := flag.Lookup(KubeConfigFlag)
if kcFlag == nil {
flag.StringVar(&fopts.kubeconfigPath, KubeConfigFlag, "", "path to kubeconfig")
}

flag.Parse()

if kcFlag != nil {
fopts.kubeconfigPath = kcFlag.Value.String()
}

f, err := newFramework(fopts)
if err != nil {
log.Fatalf("Failed to create framework: %v", err)
Expand Down

0 comments on commit ef61373

Please sign in to comment.