Skip to content

ftplugin Customizations

Moshe Avni edited this page Feb 9, 2025 · 4 revisions

You can expand kubectl.nvim by adding your own ftplugins for the k8s_* filetypes.

ArgoCD

In this example, I add to my config's location ~/.config/nvim/ftplugin/k8s_fallback.lua It defines the <Plug>(kubectl.select) keymap to open the application's page on our browser using the server's ingress host.

Warning

This might not work for everyone, the ingress may not return a host or your configuration is a bit different. Anyway, it gives a basic idea.

local commands = require 'kubectl.actions.commands'
local tables = require 'kubectl.utils.tables'

vim.schedule(function()
  vim.api.nvim_buf_set_keymap(0, 'n', '<Plug>(kubectl.select)', '', {
    noremap = true,
    silent = true,
    desc = 'Go to application',
    callback = function()
      local _, buf_name = pcall(vim.api.nvim_buf_get_var, 0, 'buf_name')
      local lower_buf_name = string.lower(buf_name)

      if lower_buf_name == 'applications' or lower_buf_name == 'applications.argoproj.io' then
        local name = tables.getCurrentSelection(2)
        if not name then
          return
        end
        local ingress_host = commands.shell_command(
          'kubectl',
          { 'get', 'ingress', '-n', 'argocd', '-l', 'app.kubernetes.io/component=server', '-o', 'jsonpath={.items[].spec.rules[].host}' }
        )

        local final_host = string.format('https://%s/applications/argocd/%s', ingress_host, name)
        vim.notify('Opening ' .. final_host)
        vim.ui.open(final_host)
      end
    end,
  })
end)
Clone this wiki locally