Ramilito / kubectl.nvim

Apache License 2.0
165 stars 5 forks source link

Error when trying to change namespace #143

Closed Elyviere closed 3 weeks ago

Elyviere commented 1 month ago

Describe the bug I'm unable to change namespaces. When pressing <C-n>, nothing happens for about 1 minute, then I get the below error:

 ¯¬ç  Error  15:34:57 msg_show.lua_error Error executing luv callback:
.../kubectl.nvim/lua/kubectl/views/namespace/definition.lua:8: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
    [C]: in function 'pairs'
    .../kubectl.nvim/lua/kubectl/views/namespace/definition.lua:8: in function 'processFunc'
    ...e/nvim/lazy/kubectl.nvim/lua/kubectl/resourcebuilder.lua:102: in function 'process'
    ...m/lazy/kubectl.nvim/lua/kubectl/views/namespace/init.lua:12: in function 'callback'
    ...e/nvim/lazy/kubectl.nvim/lua/kubectl/resourcebuilder.lua:74: in function 'on_exit'
    .../nvim/lazy/kubectl.nvim/lua/kubectl/actions/commands.lua:79: in function 'on_exit'
    ...lar/neovim/0.10.1/share/nvim/runtime/lua/vim/_system.lua:300: in function <...lar/neovim/0.10.1/share/nvim/runtime/lua/vim/_system.lua:270>

To Reproduce

  1. Open the kubectl window with <leader>k
  2. Press <C-n>
  3. Wait for it..... No not yet, keep waiting...
  4. See error

Expected behavior I assume some sort of pop-up with available namespaces to select?

Screenshots image

Environment (please complete the following information):

Additional context I assume the error may be caused by my user not having full access to the cluster. I only have access to a few namespaces, and so any commands using --all-namespaces will return an error. Perhaps this addon is attempting to find all available namespaces and getting an error instead?

For context, running kubectl get namespaces in terminal gives the following error: Error from server (Forbidden): namespaces is forbidden: User "b76729a2-1a1e-4625-93a4-7828836dc696" cannot list resource "namespaces" in API group "" at the cluster scope: access denied

Ramilito commented 1 month ago

Thank you for the excellent issue, it really helps! Unfortunately I'm a bit stumped here. Since you can't list all namespaces available, you would need to know them beforehand.

Atm, I can only come up with workarounds. One of them is to specify a namespace in the config (the default is All), the second one is for me to add a Kubens command so you can change namespace using :Kubens

There are probably more places this might affect since I haven't thought about that at all and would love it if you could help me find those, it's hard to replicate this behaviour for me currently.

Do you have any other suggestions?

Elyviere commented 1 month ago

The "optimal" way in my opinion would be something like this:

Further improvements to this could then be:

This way, the end result for the user is barely any different than a user with full access. You always change namespaces with <C-n> and the UI is essentially the same.

I will add that in my use-case I have access to several namespaces, I simply cannot list them. So, I think it's important to keep the ability to switch between namespaces on the fly, rather than defining a single namespace in config.

Ramilito commented 1 month ago

I agree, this sounds great.

kubectl.nvim remembers previously input namespaces and shows them as suggestions in the namespace window

I will probably wait and evaluate if this is needed but the rest sounds great.

Will start with this soon so hopefully we have this setup during the weekend.

Ramilito commented 1 month ago

Did something very close to your suggestion now, I can see further improvement in making it closer to how the filter (c-f) works with a combined input and suggestions but this should take care of the immediate issue.

Like your suggested workflow

Upon getting a "nul" in return, it shows a description like "Access to namespaces denied, please input your desired namespace:"

Implemented!

The user gets a free-text input field to type their desired namespace

Implemented!

Upon pressing enter, kubectl tries to change namespace to namespace input by the user

Implemented

Config allows defining a list of default namespaces that will always be available to choose in the namespace window

Implemented, although the input field will disappear when setting the namespace_fallback config.

I will add that in my use-case I have access to several namespaces, I simply cannot list them. So, I think it's important to keep the ability to switch between namespaces on the fly, rather than defining a single namespace in config.

I understand and agree, although, I would suggest setting a different default than "All" for namespacevalue in combination to the other settings.

Please test it out and see if it works as expected!

Ramilito commented 3 weeks ago

Closing since I believe this is working! Let me know otherwise!

Elyviere commented 3 weeks ago

Sorry I completely forgot about trying this. I updated and am now getting the following error:

 ¯¬ç  Error  11:38:43 notify.error lazy.nvim Failed to run `config` for kubectl.nvim

...ocal/share/nvim/lazy/kubectl.nvim/lua/kubectl/config.lua:49: attempt to index local 'options' (a nil value)

# stacktrace:
  - /kubectl.nvim/lua/kubectl/config.lua:49 _in_ **setup**
  - /kubectl.nvim/lua/kubectl/init.lua:53 _in_ **setup**
  - .config/nvim/lua/plugins/kubectl.lua:5 _in_ **config**
  - .config/nvim/lua/config/lazy.lua:9
  - dotfiles/.config/nvim/init.lua:2

I'm still able to open kubectl.nvim with my keymap vim.keymap.set("n", "<leader>k", '<cmd>lua require("kubectl").open()<cr>', { noremap = true, silent = true }), but pressing C-n just moves my cursor to the next line (similar to normal-mode 'j').

Looking in nvim/lua/plugins/kubectl.lua I'm getting the following warning (I split the .setup() to a separate line to show where the warning was):

image

I took another look at the readme for kubectl.nvim and it looks like I've copied everything 1-1, so not sure what's going on there.

Ramilito commented 3 weeks ago

Could you try again? It seems I didn't allow no config params 😅

Elyviere commented 3 weeks ago

Tried again, config error fixed but the namespace fix isn't working.

Pressing <C-n> gives the desired result, but after inputting a namespace and pressing enter the namespace window quickly flashes and reappears with the following text: image

Removing gr and replacing with my desired namespace and pressing enter again gives this error (and moves my cursor to the next line in the pop-up window, without editing the contents):

11:50:49 msg_show RPC[Error] code_name = InternalError, message = "Request getCompletions failed with message: tunneling socket could not be established, cause=socket hang up"
Elyviere commented 3 weeks ago

I will add that after quitting that window, my deployments in the namespace showed up correctly. So it seems like the namespace change was correctly applied! Just the extra pop-up window that needs to be fixed.

Ramilito commented 3 weeks ago

I think I found the issue, could you try again?

PS. thanks for updating this 🙏, some issues like this one seems to be dependant on the computer

Elyviere commented 3 weeks ago

Seems there is at least one related error to the original issue. When entering a deployment (i.e. pods are visable in the UI) and pressing gu (It says <gu> usage in the top, but in the <g?> help menu it says it's <gu> Show Resources Used, not sure which it is) an empty "k8s_top - Top" window pops up and I get the following error:

 ¯¬ç  Error  11:56:51 notify.error Error from server (Forbidden): pods.metrics.k8s.io is forbidden: User "sso:elyviere@<redacted>.com" cannot list resource "pods" in API group "metrics.k8s.io" at the cluster scope

I will attempt to find any other commands that give similar errors.

Elyviere commented 3 weeks ago

Tried again, config error fixed but the namespace fix isn't working.

Pressing <C-n> gives the desired result, but after inputting a namespace and pressing enter the namespace window quickly flashes and reappears with the following text: image

Removing gr and replacing with my desired namespace and pressing enter again gives this error (and moves my cursor to the next line in the pop-up window, without editing the contents):

11:50:49 msg_show RPC[Error] code_name = InternalError, message = "Request getCompletions failed with message: tunneling socket could not be established, cause=socket hang up"

Wow you work fast. Still the same error in commit ebd6dcb I'm afraid.

Ramilito commented 3 weeks ago

The top usage should be fixed in latest (forgot to consider namespace in that one), don't think there are any more hardcoded "-A" anywhere now (hopefully) But that namespace thing is weird...

Elyviere commented 3 weeks ago

Can confirm top thing is fixed. Can I help you debug the gr somehow? Any logs you want me to provide? We can make a branch to debug it so we don't clutter main with logs.

Ramilito commented 3 weeks ago

I'd love that! there is a branch now called fix/make_ns_popup_great_again I added a print where I think the error is but also a potential fix

Elyviere commented 3 weeks ago

It has no idea. 13:15:04 msg_show this should be true otherwise it's not understanding what view we are on false

Ramilito commented 3 weeks ago

Hmm I was almost sure the async call was the problem, how are we testing this? 1: Open neovim 2: Toggle plugin 3: Press c-n 4: Type namespace 5 Press (enter)

Or are you doing it differently?

Elyviere commented 3 weeks ago

That is the exact way I'm doing it.

Elyviere commented 3 weeks ago

Breakthrough: When opening an actual file it works, but when opening nvim without a file I get the error. So in the first of your 5 steps above the difference would be nvim anything → works or just nvim → breaks

So to be clear, I'm running LazyVim. So when I open nvim with just nvim I'm greeted with this page: image

When opening with nvim anything I open an actual file, i.e. no LazyVim start page. I assume that start page is causing the issue here.

Ramilito commented 3 weeks ago

That's really interesting, will setup lazyvim and see if I get the same

Ramilito commented 3 weeks ago

Installed dashboard and indeed did get that behaviour!

Elyviere commented 3 weeks ago

Suddenly I'm unable to open kubectl.nvim any more. Tried returning to the main branch and I'm still unable to open it. Let me know if I should report it as a separate bug. This is the error I'm getting when I try to open the plugin with <leader>k, and I'm left with an empty window:

 ¯¬ç  Error  15:51:37 msg_show.lua_error Error executing vim.schedule lua callback: ...y/kubectl.nvim/lua/kubectl/views/services/definition.lua:40: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
    [C]: in function 'pairs'
    ...y/kubectl.nvim/lua/kubectl/views/services/definition.lua:40: in function 'processFunc'
    ...e/nvim/lazy/kubectl.nvim/lua/kubectl/resourcebuilder.lua:145: in function 'process'
    ...e/nvim/lazy/kubectl.nvim/lua/kubectl/resourcebuilder.lua:299: in function <...e/nvim/lazy/kubectl.nvim/lua/kubectl/resourcebuilder.lua:297>
 ¯¬ç  Error  15:51:37 msg_show.lua_error Error executing vim.schedule lua callback: ...y/kubectl.nvim/lua/kubectl/views/services/definition.lua:40: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
    [C]: in function 'pairs'
    ...y/kubectl.nvim/lua/kubectl/views/services/definition.lua:40: in function 'processFunc'
    ...e/nvim/lazy/kubectl.nvim/lua/kubectl/resourcebuilder.lua:145: in function 'process'
    ...e/nvim/lazy/kubectl.nvim/lua/kubectl/resourcebuilder.lua:299: in function <...e/nvim/lazy/kubectl.nvim/lua/kubectl/resourcebuilder.lua:297>

I've attached a file with the inspection info of the plugin, since I expect it'll be something locally that's become messed up somehow. kubectl-nvim_inspection.txt

Ramilito commented 3 weeks ago

Yeah that's an issue that I've fixed in #171 but haven't merged it yet. I cherry picked the pick for services so it should work now, the other views probably have similar error for you but I'll try to merge it soon. I've merged it to our debug branch as well so should work in both main and branch now.

For the main issue, it's super confusing tbh on why it happens, tried adjusting to solve it but seems I have to rewrite it instead, will try later this evening with that.

Elyviere commented 3 weeks ago

Glad to hear you've already fixed that. I mean the remaining part of this issue is so small now, and only when opening while dashboard is open. It can definitely be delayed or honestly even left like this and just add it as a known issue. Nobody except me will ever run into it anyway lol.

Ramilito commented 3 weeks ago

Yeah that would be the most sane thing to do....however....I rewrote the namespace selection anyways 🤓

The first time I tried fixing this I kind of did a hack to make it work and figured I'd do it properly later. The namespace selection is a lot better now I believe, especially for those that have access to namespaces. There is still the autosuggestions missing but I'll drop it for now. I would love it if you could verify before I merge our branch 🙏

Elyviere commented 3 weeks ago

Very nicely done! Can confirm it all appears to be working now :)

Elyviere commented 3 weeks ago

Oh but I'm afraid I think I found a related error.

Expected result: The pop-up window closes

Actual result: The pop-up windows is cleared of content but does not close

After closing the pop-up window with :q any subsequent opening and closing of the pop-up window with q work as expected.

Ramilito commented 3 weeks ago

Yeah just verified, it seems to be the same issue with the dashboard again but for other buffer types. At least this fix is working, will look into what's causing those other buffers

mosheavni commented 3 weeks ago

for me the latest commit shows this:

image

No namespaces, no completions.

mosheavni commented 3 weeks ago

for me the latest commit shows this: image

No namespaces, no completions.

now it's working. weird. maybe it just took time to load?

What's the right way to choose one? there's no completion so I must write the full name

image
Ramilito commented 3 weeks ago

hmmm maybe, was super confused on why it would look like that. The completion isn't done yet so if you go into normal mode you can use normal selection as we do when selecting pods for example.

Ramilito commented 3 weeks ago

Added a issue to keep track of the completion task: #188

Elyviere commented 3 weeks ago

I added a feature request for the namespace config: https://github.com/Ramilito/kubectl.nvim/issues/189

Let me know if I can help out with it. I don't know lua but how hard can it be.

Ramilito commented 3 weeks ago

Oh but I'm afraid I think I found a related error.

  • Open Nvim dashboard
  • Open kubectl.nvim
  • Open a pop-up window (e.g. press enter on a pod)
  • Press q

Expected result: The pop-up window closes

Actual result: The pop-up windows is cleared of content but does not close

After closing the pop-up window with :q any subsequent opening and closing of the pop-up window with q work as expected.

I created a new issue for the above so we can close this issue!