Skip to content

Set current namespace from kubeconfig by default#172

Merged
knative-prow-robot merged 4 commits intoknative:masterfrom
nak3:current-ns
Jun 24, 2019
Merged

Set current namespace from kubeconfig by default#172
knative-prow-robot merged 4 commits intoknative:masterfrom
nak3:current-ns

Conversation

@nak3
Copy link
Copy Markdown
Contributor

@nak3 nak3 commented Jun 9, 2019

kn command does not pick up namespace from kubeconfig but hardcorded
default namespace.

This patch fixes to get namespace from kubeconfig.

Fixes #7

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label Jun 9, 2019
@knative-prow-robot knative-prow-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jun 9, 2019
@knative-prow-robot
Copy link
Copy Markdown
Contributor

Hi @nak3. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@knative-prow-robot knative-prow-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 9, 2019
}

// test with all-namespace flag not defined and no namespace given
func TestGetNamespaceDefaultAllNamespacesNotDefined(t *testing.T) {
Copy link
Copy Markdown
Contributor Author

@nak3 nak3 Jun 9, 2019

Choose a reason for hiding this comment

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

I have removed this test case because it is almost same with TestGetNamespaceDefault. (Please let me know if I should not remove it.)

@rhuss
Copy link
Copy Markdown
Contributor

rhuss commented Jun 9, 2019

Coolio, thanks a lot ! 'was just about to fix that on my own, but even better so :-)

I will have a look later today ...

Copy link
Copy Markdown
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Thanks @nak3 ! Looks good for me with some minor comments. Especially getting rid of KubeCfgFile as a global variable which is initalized in one init method and used in another init method is calling for a race sometime in the future.

Comment thread pkg/kn/commands/types.go Outdated
type KnParams struct {
Output io.Writer
ServingFactory func() (serving.ServingV1alpha1Interface, error)
// CurrentNamespace is the namespace set in the current context.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it's clear what CurrentNamespace is. So please either remove the comment (recommended) or comment on the other parameters, too.

Comment thread pkg/kn/commands/types.go Outdated
c.ServingFactory = GetConfig
}
var err error
c.CurrentNamespace, err = GetCurrentNamespace(KubeCfgFile)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder whether we could get rid of sharing that global variable and use a function instead which could cache a lookup. This would free us for knowing the order when this variable was initialized (which I just checked, looks good, but you don't know what happens when people refactor code).

I.e. introducing a GetKubeCfgFile() function which checks a private variable kubeCfgFile and, if empty, does the actual lookup as it's done in InitializeConfig:

func GetKubeCfgFile() string {
   if kubeCfgFile == "" {
       kubeCfgFile = initKubeConfig()
   }
   return kubeCfgFile
}

(and maybe improve the error handling over there, as initKubeConfig() must not call os.Exit() but should return an error which should be propagated here, too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree, I made it public and hated that. Better to go back to old Java-style getters for this one and hide the var. Thanks for suggesting this.

Comment thread pkg/kn/commands/types.go Outdated
}

// GetCurrentNamespace returns current namespace from kubeconfig.
func GetCurrentNamespace(kubeconfig string) (string, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can't really comment on the logic here as I don't know anything about how to extract a current context from the configuration. Looks complicated though ;-)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn’t it one line in the KUBECONFIG file? Maybe my clusters are super simple...

@knative-prow-robot knative-prow-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 10, 2019
@rhuss
Copy link
Copy Markdown
Contributor

rhuss commented Jun 10, 2019

/ok-to-test

@knative-prow-robot knative-prow-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 10, 2019
@nak3
Copy link
Copy Markdown
Contributor Author

nak3 commented Jun 10, 2019

Thank you @rhuss Updated (and moved KubeCfgFile into KnParams struct.)

Comment thread pkg/kn/core/root.go Outdated
p.Initialize()
err := p.Initialize()
if err != nil {
panic(fmt.Sprintf("Failed to initialize command %v", err))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wondering if panic is the right thing to do here? I can understand the rational but the error to user will be kind of ugly. Returning error with message will be handled like all errors in main.go...

Though I can also see the argument that this is not a common error and panicking is warranted. Conflicted a bit.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 for not panicing but exiting with a "normal" error. I think it's not impossible that initialization fails, e.g. when some .kube config file is not readable. We always strive for nice error messages.

@nak3
Copy link
Copy Markdown
Contributor Author

nak3 commented Jun 11, 2019

Thank you @maximilien and @rhuss. Updated.

I realized that Initalize() should not cause panic nor catch error as current code does. If we handle error, users must set kubeconfig even when we just want to check some help usage like kn -h.

Comment thread pkg/kn/commands/types.go Outdated
}
}
rules := clientcmd.NewDefaultClientConfigLoadingRules()
rules.ExplicitPath = c.KubeCfgFile
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

see https://github.com/k14s/kapp/blob/0c91e8a2208ff9a8f9d92f8c01d20dc9dd21256b/pkg/kapp/cmd/core/config_factory.go#L60 for example. we dont need to specify ExplicitPath unless it's given as an explicit option. also KUBECONFIG env var will be picked up by default.

@nak3
Copy link
Copy Markdown
Contributor Author

nak3 commented Jun 12, 2019

Thank you @cppforlife Updated.

Comment thread pkg/kn/commands/types.go Outdated
return nil, err
}
}
config, err := clientcmd.BuildConfigFromFlags("", c.KubeCfgFile)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

we should also get ClientConfig() from clientcmd.NewNonInteractiveDeferredLoadingClientConfig() instead of doing initKubeConfig(). i dont think we actually need initKubeConfig at all.

@nak3
Copy link
Copy Markdown
Contributor Author

nak3 commented Jun 14, 2019

/test pull-knative-client-go-coverage

@nak3
Copy link
Copy Markdown
Contributor Author

nak3 commented Jun 14, 2019

@cppforlife Thank you! updated.

Comment thread pkg/kn/commands/types.go Outdated
Comment thread pkg/kn/commands/namespaced_test.go Outdated
expectedNamespace := "current"
testCmd.Execute()
actualNamespace, err := GetNamespace(testCmd)
kp := &KnParams{CurrentNamespace: "current"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

i think you should add this line to every test case since it's implied that CurrentNamespace() from clientcmd is set. additionally is there an easy way to test GetConfig(). if not in unit test land, can it be done in e2e?

Comment thread pkg/kn/commands/types.go Outdated
}
c.ClientConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
var err error
c.CurrentNamespace, _, err = c.ClientConfig.Namespace()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

not idea that GetConfig() mutates KnParams. is there a better pattern we can have?

@nak3
Copy link
Copy Markdown
Contributor Author

nak3 commented Jun 19, 2019

Thank you @cppforlife ! Updated.

nak3 added 3 commits June 20, 2019 19:39
Currently kn command does not pick up namespace from kubeconfig but
hardcorded default namespace.

This patch fixes to get namespace from kubeconfig.

Fixes #7
@navidshaikh
Copy link
Copy Markdown
Contributor

@nak3 : oops

➜  client git:(current-ns) ./kn version
Version:      v20190620-local-0ce1486
Build Date:   2019-06-20 12:10:25
Git Revision: 0ce1486
Dependencies:
- serving:    v0.6.0

➜  client git:(current-ns) ./kn service create current-ns --image gcr.io/knative-samples/helloworld-go                                                                               
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0xf3124a]

goroutine 1 [running]:
github.com/knative/client/pkg/kn/commands.(*KnParams).CurrentNamespace(0xc0002c1740, 0xc0002d0d50, 0x120eadc, 0xe, 0x1d00b60)
	/home/nshaikh/go/src/github.com/knative/client/pkg/kn/commands/types.go:47 +0x2a
github.com/knative/client/pkg/kn/commands.(*KnParams).CurrentNamespace-fm(0xc0000f6f00, 0x120eadc, 0xe, 0x0)
	/home/nshaikh/go/src/github.com/knative/client/pkg/kn/commands/types.go:42 +0x2a
github.com/knative/client/pkg/kn/commands.(*KnParams).GetNamespace(0xc0002c1740, 0xc0002e9180, 0x32, 0x1d00b60, 0x0, 0x0)
	/home/nshaikh/go/src/github.com/knative/client/pkg/kn/commands/namespaced.go:58 +0xe1
github.com/knative/client/pkg/kn/commands/service.NewServiceCreateCommand.func1(0xc0002e9180, 0xc0002d1140, 0x1, 0x3, 0x0, 0x0)
	/home/nshaikh/go/src/github.com/knative/client/pkg/kn/commands/service/service_create.go:62 +0xde
github.com/spf13/cobra.(*Command).execute(0xc0002e9180, 0xc0002d10e0, 0x3, 0x3, 0xc0002e9180, 0xc0002d10e0)
	/home/nshaikh/go/src/github.com/knative/client/vendor/github.com/spf13/cobra/command.go:762 +0x473
github.com/spf13/cobra.(*Command).ExecuteC(0xc0002e8780, 0xc0002f6280, 0xc0002e9900, 0xc0002e8a00)
	/home/nshaikh/go/src/github.com/knative/client/vendor/github.com/spf13/cobra/command.go:852 +0x2fd
github.com/spf13/cobra.(*Command).Execute(0xc0002e8780, 0x0, 0x0)
	/home/nshaikh/go/src/github.com/knative/client/vendor/github.com/spf13/cobra/command.go:800 +0x2b
main.main()
	/home/nshaikh/go/src/github.com/knative/client/cmd/kn/main.go:25 +0x44

➜  client git:(current-ns) git checkout master && hack/build.sh -f && ./kn service create current-ns --image gcr.io/knative-samples/helloworld-go
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
🚧 Compile
────────────────────────────────────────────
Version:      v20190620-local-32ccc33
Build Date:   2019-06-20 12:14:09
Git Revision: 32ccc33
Dependencies:
- serving:    v0.6.0
Service 'current-ns' successfully created in namespace 'default'.

@knative-metrics-robot
Copy link
Copy Markdown

The following is the coverage report on pkg/.
Say /test pull-knative-client-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/kn/commands/namespaced.go 92.3% 87.5% -4.8

Copy link
Copy Markdown
Contributor

@navidshaikh navidshaikh left a comment

Choose a reason for hiding this comment

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

/lgtm

@knative-prow-robot
Copy link
Copy Markdown
Contributor

@navidshaikh: changing LGTM is restricted to assignees, and only knative/client repo collaborators may be assigned issues.

Details

In response to this:

/lgtm

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@navidshaikh
Copy link
Copy Markdown
Contributor

Well! pull-knative-client-integration-tests dashboard shows SUCCESS but here its Job failed!

@navidshaikh
Copy link
Copy Markdown
Contributor

/retest

@mattmoor mattmoor removed their request for review June 21, 2019 03:16
@csantanapr
Copy link
Copy Markdown
Member

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jun 24, 2019
@cppforlife
Copy link
Copy Markdown

/approve
/lgtm

@knative-prow-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cppforlife, nak3, navidshaikh

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 24, 2019
@knative-prow-robot knative-prow-robot merged commit 13ff277 into knative:master Jun 24, 2019
@nak3 nak3 deleted the current-ns branch June 24, 2019 22:52
maximilien pushed a commit to maximilien/client that referenced this pull request Jul 1, 2019
* Set current namespace from kubeconfig by default

Currently kn command does not pick up namespace from kubeconfig but
hardcorded default namespace.

This patch fixes to get namespace from kubeconfig.

Fixes knative#7

* Use NamespaceFactory to get current namespace

* Update unit tests

* Add nil check for ClientConfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pick up default namespace from .kube/config instead of hardcoding it in kn

9 participants