Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/Allaman/kustomize.nvim

A Neovim plugin with some useful functions for working with Kustomize
https://github.com/Allaman/kustomize.nvim

kubernetes kustomize neovim neovim-plugin yaml

Last synced: 8 days ago
JSON representation

A Neovim plugin with some useful functions for working with Kustomize

Lists

README

        

kustomize.nvim



Neovim
Lua





CI
size
issues
last commit
license
release


I work a lot with [Kustomize](https://kustomize.io/) and I love Neovim. So why not write a plugin for some tasks for that I usually switch to a shell.
Jump to the [use cases](#use-cases) to check out what this plugin can do!

## Requirements

- Neovim >= 0.9
- `kustomize` in your PATH to [build manifests](#build-manifests)
- [kubeconform](https://github.com/yannh/kubeconform) in your PATH to [validate manifests](#validate-resources)
- [kubent](https://github.com/doitintl/kube-no-trouble) in your PATH to [check for deprecations](#check-for-deprecations)
- [plenary.nvim](https://github.com/nvim-lua/plenary.nvim)
- [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) and `yaml` parser
- (optionally) [LuaSnip](https://github.com/L3MON4D3/LuaSnip) snippets (default is disabled)

## Quickstart

With [Lazy.nvim](https://github.com/folke/lazy.nvim):

```lua
{
"allaman/kustomize.nvim",
requires = "nvim-lua/plenary.nvim",
ft = "yaml",
opts = {}
}
```

Run `:checkhealth kustomize` for a health check.

## Default mappings

| Mode | Mapping | Action | Lua | Command |
| ---- | ------------ | --------------------- | -------------------------------------------- | -------------------------- |
| n | \kb | Kustomize build | `lua require("kustomize").build()` | `:KustomizeBuild` |
| n | \kk | List kinds | `lua require("kustomize").kinds()` | `:KustomizeListKinds` |
| n | \kr | Print resources | `lua require("kustomize").print_resources()` | `:KustomizePrintResources` |
| n | \ko | List 'resources' | `lua require("kustomize").list_resources()` | `:KustomizeListResources` |
| n | \kv | Validate file | `lua require("kustomize").validate()` | `:KustomizeValidate` |
| n | \kd | Check API deprecation | `lua require("kustomize").deprecations()` | `:KustomizeDeprecations` |

You can define your own keybindings/override the default mappings, for instance:

```lua
use({
"allaman/kustomize.nvim",
requires = "nvim-lua/plenary.nvim",
ft = "yaml",
opts = { enable_key_mappings = false },
config = function(opts)
require('kustomize').setup({opts})
-- default keybindings, adjust to your needs
vim.keymap.set("n", "kb", "lua require('kustomize').build()", { noremap = true })
vim.keymap.set("n", "kk", "lua require('kustomize').kinds()", { noremap = true })
vim.keymap.set("n", "kl", "lua require('kustomize').list_resources()", { noremap = true })
vim.keymap.set("n", "kp", "lua require('kustomize').print_resources()", { noremap = true })
vim.keymap.set("n", "kv", "lua require('kustomize').validate()", { noremap = true })
vim.keymap.set("n", "kd", "lua require('kustomize').deprecations()", { noremap = true })
end,
})
```

## Default configuration

This is the default configuration that can be (partially) overwritten by you.

```lua
{
enable_key_mappings = true,
enable_lua_snip = false,
validate = { kubeconform_args = { "--strict", "--ignore-missing-schemas" } },
build = { additional_args = {} },
deprecations = { kube_version = "1.25" },
kinds = { show_filepath = true, show_line = true, exclude_pattern = "" },
}
```

With Lazy.nvim for instance:

```lua
opts = { validate = { kubeconform_args = { "--strict" } } },
```

And some command / Lua APIs support arguments. See [List "kinds"](#list-kinds) and [Check for deprecations](#check-for-deprecations).

## Use cases

### Snippets

If enabled, kustomize.nvim includes some useful snippets for LuaSnip. All snippets start with `kust`.

Showcase

### Build manifests

Showcase

This command will run `kustomize build .` in the current buffer's directory. The generated YAML will be printed to a new buffer. The new buffer can be closed by just typing `q`.
This allows me to quickly inspect the YAML that Kustomize generates (and ultimately is applied to the cluster). In addition, I get fast feedback on any errors in my YAML sources.

You can add additional arguments to the build call via config file:

```lua
build = {
additional_args = {"--enable-helm", "--load-restrictor=LoadRestrictionsNone"}
},
```

You can also dynamically overwrite the values of your config file with

```
lua require("kustomize").build({additional_args={"--enable-helm", "--load-restrictor=LoadRestrictionsNone"}})
```

```
:KustomizeBuild additional_args={"--enable-helm", "--load-restrictor=LoadRestrictionsNone"}
```

### List "kinds"

Showcase

Sometimes, I just want to roughly check the YAMLs generated by Kustomize. A good hint is to check the `kind:` key of the generated YAML manifests. This command will parse all `kind:` keys in the current buffer with the help of tree-sitter and prints their values to a loclist allowing you to easily jump around all resources. You could use it on any YAML file with multiple resources, for instance generated by [Build manifests](#build-manifests). Only Resources with `metadata.name` are recognized, e.g. `Kustomization` resources are not detected.

The output consists of ` || `. Cluster-wide resources omit the namespace value. You can hide the buffer name and line number by adding the following snippet to the opts table:

If [Telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) is installed, you can toggle `Telescope loclist` with `kt`

```lua
kinds = {
-- setting those to false removes "clutter" but you cannot "jump" to a resource anymore
show_filepath = false,
show_line = false,
-- filter resources you are not interested in
exclude_pattern = {"Namespace", "Ingress"}
},
```

You can also dynamically overwrite the values of your config file with

```
:lua require("kustomize").kinds({show_line=true, show_filepath=true, exclude_pattern={"Namespace", "Ingress"}})
```

```
:KustomizeListKinds show_line=true, show_filepath=true, exclude_pattern={"Namespace", "Ingress"}
```

### Open file/directory

Showcase

You list your YAMLs that should be included by Kustomize for the build of the final manifests like so:

```yaml
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- service1/
- grafana-dashboard.yaml
- ../../base/namespace.yaml
```

In order to quickly check/edit those included YAMLs this command will go through all items in `resources:` and populate a loclist with them.

### Print resource files

Showcase

When writing a new deployment I usually split the resources into files according to their type, for instance `deployment.yaml`, `cm.yaml`, or `sa.yaml`. When writing my `kustomization.yaml` I must add all resource files which does this command for me.

### Validate resources

Showcase

[kubeconform](https://github.com/yannh/kubeconform) is a Kubernetes manifests validator that can detect a misconfiguration before you apply your manifests to your cluster. This command runs `kubeconform --strict --ignore-missing-schemas` on the current buffer. The buffer's content may be a file on disk or content not (yet) saved, e.g. the output of [Build manifests](#build-manifests).

### Check for deprecations

Showcase

[kubent](https://github.com/doitintl/kube-no-trouble) is a tool to search for deprecated Kubernetes APIs. This plugin utilizes the plugin to check the manifests in the current buffer for deprecated Kubernetes APIs. The buffer's content may be a file on disk or content not (yet) saved, e.g. the output of [Build manifests](#build-manifests). The Kubernetes target version can be set with `deprecations = { kube_version = "1.25" }`.

You can also dynamically overwrite the values of your config file with

```
:lua require("kustomize").deprecations({kube_version=1.25})
```

```
:KustomizeDeprecations kube_version=1.16
```