Skip to content
Ramilito edited this page Feb 10, 2025 · 10 revisions

Alias

Create an alias that opens kubectl.nvim directly from the terminal:

alias k8s="nvim -c 'lua require(\"kubectl\").toggle({tab=true})'"

Toggle faults

If you want to be able to see only unhealthy pods, you can set a keymap like so:

local group = vim.api.nvim_create_augroup('kubectl_mappings', { clear = false })
vim.api.nvim_create_autocmd('FileType', {
  group = group,
  pattern = 'k8s_*',
  callback = function(ev)
    vim.keymap.set('n', 'Z', function()
      local state = require 'kubectl.state'
      local current = state.getFilter()
      local faults_filter = '!1/1,!2/2,!3/3,!4/4,!5/5,!6/6,!7/7,!Completed,!Terminating'
      if current == faults_filter then
        state.setFilter ''
        return
      end
      state.setFilter(faults_filter)
    end, { buffer = ev.buf, desc = 'Toggle Faults' })
  end,
})

With this filter you would still see pods with 8 or more containers but it gives you a general idea of how you can customize and add your own filters

Kill pod with prompt

Sometimes we want to be able to have more control over the underlying kubectl command that is running. For that case, we can customize with an action menu. For example, to kill pod but control the flags before you can add this to your ftplugin/k8s_pods.lua:

local ResourceBuilder = require 'kubectl.resourcebuilder'
local commands = require 'kubectl.actions.commands'
local pod_view = require 'kubectl.views.pods'

vim.schedule(function()
  vim.api.nvim_buf_set_keymap(0, 'n', 'gk', '', {
    noremap = true,
    silent = true,
    desc = 'Kill prompt',
    callback = function()
      local name, ns = pod_view.getCurrentSelection()
      local builder = ResourceBuilder:new 'kubectl_drain'
      local pod_def = {
        ft = 'k8s_kill_pod',
        display = string.format('Kill pod: %s/%s?', ns, name),
        resource = ns .. '/' .. name,
        cmd = { 'delete', 'pod', name, '-n', ns },
      }
      local data = {
        { text = 'cascade:', value = 'background', options = { 'background', 'orphan', 'foreground' }, cmd = '--cascade', type = 'option' },
        { text = 'dry run:', value = 'none', options = { 'none', 'server', 'client' }, cmd = '--dry-run', type = 'option' },
        { text = 'grade period:', value = '-1', cmd = '--grace-period', type = 'option' },
        { text = 'timeout:', value = '0s', cmd = '--timeout', type = 'option' },
        { text = 'force:', value = 'false', options = { 'false', 'true' }, cmd = '--force', type = 'flag' },
      }

      builder:action_view(pod_def, data, function(args)
        commands.shell_command_async('kubectl', args)
      end)
    end,
  })
end)

The result is something like this: image

Dynamic titlestring

the vim.o.titlestring allows us to change the terminal's title (if the terminal allows it). We can utilize our k8s_* filetype pattern to change the titlestring if we're on the plugin to see relevant info in the title.

Set up this autocmd (I did it in the lazy.nvim conf of the plugin under init = function() end:

local group = vim.api.nvim_create_augroup('kubectl_user', { clear = true })
vim.api.nvim_create_autocmd('FileType', {
  group = group,
  pattern = 'k8s_*',
  callback = function()
    vim.opt.titlestring = 'k8s: %t'
  end,
})
image

Copy container exec command

Some users don't find it really useful using Neovim's terminal, you can create ftplugin/k8s_containers.lua to contain this gx keymap that copies the entire kubectl exec command to your clipboard:

local mappings = require 'kubectl.mappings'
local pod_view = require 'kubectl.views.pods'
local tables = require 'kubectl.utils.tables'

vim.api.nvim_buf_set_keymap(0, 'n', '<Plug>(kubectl.browse)', '', {
  noremap = true,
  silent = true,
  desc = 'Copy exec command',
  callback = function()
    local container_name = tables.getCurrentSelection(unpack { 1 })
    local exec_cmd = string.format('kubectl exec -it %s -n %s -c %s -- /bin/sh', pod_view.selection.pod, pod_view.selection.ns, container_name)
    vim.notify(exec_cmd)
    vim.fn.setreg('+', exec_cmd)
  end,
})

vim.schedule(function()
  mappings.map_if_plug_not_set('n', 'gx', '<Plug>(kubectl.browse)')
end)

Direnv together with AWS

This setup ensures that kubectl runs with the correct AWS credentials by pulling environment variables managed by Direnv, you can read more here: #432

local function get_aws_env_vars()
    local result = {}
    local varNames = { "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_DEFAULT_REGION", "AWS_DEFAULT_OUTPUT" }
    for _, var in ipairs(varNames) do
        result[var] = os.getenv(var)
    end
    return result
end

return {
    {
        "ramilito/kubectl.nvim",
        keys = {
            { "<C-k>", function () require("kubectl").toggle() end, desc = "Toggle K8s", mode = "n" },
        },
        opts = {
            kubectl_cmd = {
                cmd = "kubectl",
                env = get_aws_env_vars(),
            }
        },
    },
}
Clone this wiki locally