Vim with YAML Support for Kubernetes

Note perev. : The original article was written by Josh Rosso, an architect from VMware who previously worked for companies such as CoreOS and Heptio, as well as a co-author of Kubernetes alb-ingress-controller. The author shares a small recipe, which can be very useful for “old school” maintenance engineers who prefer vim even in the era of the victorious cloud native.



Writing YAML manifests for Kubernetes in vim? Have spent countless hours trying to figure out where the next field should be in this specification? Or maybe you'll be happy with a quick reminder of the differenceargsandcommand? There is good news! Vim is easy to bind to yaml-language-serverto get automatic completion, validation and other amenities. In this article, we’ll talk about how to configure the language server client for this.

(The original article also has a video where the author talks and demonstrates the contents of the material.)

Language server


Language servers (language servers) talk about the possibilities of programming languages ​​for editors and IDEs, for which they interact with each other using a special protocol - Language Server Protocol (LSP). This is a great approach: it allows a single implementation to provide multiple editors / IDEs with data at once. I already wrote about gopls - the language server for Golang - and how it can be used in vim . The steps to get auto-completion in YAML for Kubernetes are similar.



In order for vim to work in the described way, you need to install the language server client. Two methods I know are LanguageClient-neovim and coc.vim. In the article I will consider coc.vim- this is the most popular plugin at the moment. You can install it through vim-plug :

" Use release branch (Recommend)
Plug 'neoclide/coc.nvim', {'branch': 'release'}

" Or build from source code by use yarn: https://yarnpkg.com
Plug 'neoclide/coc.nvim', {'do': 'yarn install --frozen-lockfile'}

To start coc(and thus yaml-language-server), you will need installed node.js installed:

curl -sL install-node.now.sh/lts | bash

When coc.vimconfigured, install the server extension coc-yamlfrom vim:

:CocInstall coc-yaml



Finally, you will most likely want to start with the configuration coc-vimpresented as an example . In particular, it activates the combination <Ctrl> + space to call auto-completion.

Configuring yaml-language-server discovery


To cocuse yaml-language-server, you need to ask him to download the scheme from Kubernetes when editing YAML files. This is done by editing coc-config:

:CocConfig

In the configuration, you need to add kubernetesfor all files yaml. I additionally use the language server for golang, so my general config looks like this:

{
  "languageserver": {
      "golang": {
        "command": "gopls",
        "rootPatterns": ["go.mod"],
        "filetypes": ["go"]
      }
  },

  "yaml.schemas": {
      "kubernetes": "/*.yaml"
  }
}

kubernetes- a reserved field that informs the language server about the need to download the Kubernetes scheme using the URL defined in this constant . yaml.schemascan be expanded by supporting additional schemes - for more details, see the relevant documentation .

Now you can create a YAML file and start using auto-completion. Pressing <Ctrl> + space (or another combination configured in vim) should show the available fields and documentation in accordance with the current context:


<Ctrl> + space works here, because I configured it inoremap <silent><expr> <c-space> coc#refresh(). If you have not done so, see coc.nvim README for an example configuration.

Kubernetes API Version Selection


At the time of this writing, yaml-language-server is shipped with Kubernetes 1.14.0 schemas. I did not find a way to dynamically select a scheme, so I opened the corresponding GitHub issue . Fortunately, since the language server is written in typescript, it is very easy to manually change the version. To do this, just find the file server.ts.

To find it on your machine, simply open the YAML file with vim and find the process with yaml-language-server.

ps aux | grep -i yaml-language-server

joshrosso         2380  45.9  0.2  5586084  69324   ??  S     9:32PM   0:00.43 /usr/local/Cellar/node/13.5.0/bin/node /Users/joshrosso/.config/coc/extensions/node_modules/coc-yaml/node_modules/yaml-language-server/out/server/src/server.js --node-ipc --node-ipc --clientProcessId=2379
joshrosso         2382   0.0  0.0  4399352    788 s001  S+    9:32PM   0:00.00 grep -i yaml-language-server

The process 2380 is relevant for us: it is vim that uses it when editing a YAML file.

As you can see, the file is located in /Users/joshrosso/.config/coc/extensions/node_modules/coc-yaml/node_modules/yaml-language-server/out/server/src/server.js. It is enough to edit it, changing the value KUBERNETES_SCHEMA_URL, for example, to version 1.17.0:

// old 1.14.0 schema
//exports.KUBERNETES_SCHEMA_URL = "https://raw.githubusercontent.com/garethr/kubernetes-json-schema/master/v1.14.0-standalone-strict/all.json";
// new 1.17.0 schema in instrumenta repo
exports.KUBERNETES_SCHEMA_URL = "https://raw.githubusercontent.com/instrumenta/kubernetes-json-schema/master/v1.17.0-standalone-strict/all.json";

Depending on the version used, the coc-yamllocation of the variable in the code may be different. Also note that I changed the repository from garethrto instrumenta. It seems that garethrhe switched to supporting circuits there.

To verify that the change has taken effect, look to see if a field appears that wasn’t [in previous versions of Kubernetes]. For example, there was no startupProbe in the circuit for K8s 1.14 :



Summary


I hope this opportunity has pleased you no less than me. Happy YAML'ing! Be sure to check out these repositories to better understand the utilities mentioned in the article:


PS from the translator


And there are vikube , vim-kubernetes and vimkubectl .

Read also in our blog:


All Articles