Vim with YAML support for Kubernetes

Note. transl.: The original article was written by Josh Rosso, an architect at VMware, who previously worked at companies such as CoreOS and Heptio, and is also a co-author of Kubernetes alb-ingress-controller. The author shares a little recipe that can be very useful for "old school" operations engineers who prefer vim even in the era of the victorious cloud native.

Vim with YAML support for Kubernetes

Writing YAML manifests for Kubernetes in vim? Spent countless hours trying to figure out where the next field should be in this specification? Or maybe you'd like a quick reminder of the difference args ΠΈ command? There is good news! Vim is easy to bind to yaml-language-serverto get automatic completion, validation and other conveniences. In this article, we'll talk about how to configure the language server client for this.

(The original article also is there a video, where the author tells and demonstrates the content of the material.)

Language server

Language servers (language servers) tell editors and IDEs about the possibilities of programming languages, for which they interact with each other using a special protocol - Language Server Protocol (LSP). This is a great approach because it allows one implementation to provide data to multiple editors/IDEs at once. I have already писал about gopls - language server for Golang - and how it can be used in vim. The steps to get autocomplete in YAML for Kubernetes are similar.

Vim with YAML support for Kubernetes

In order for vim to work in the described way, you will need to install a language server client. The two methods I know of are LanguageClient-neovim ΠΈ coc.vim. In the article I will consider coc.vim is the most popular plugin at the moment. You can install it via 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'}

For start coc (and thus the yaml-language-server) will require node.js to be installed:

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

When coc.vim configured, install the server extension coc-yaml from vim:

:CocInstall coc-yaml

Vim with YAML support for Kubernetes

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

Setting up yaml-language-server discovery

That coc could use yaml-language-server, it should be asked to load schema from Kubernetes when editing YAML files. This is done by editing coc-config:

:CocConfig

In the configuration you need to add kubernetes for all files yaml. I additionally use a language server for golangso my overall config looks like this:

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

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

kubernetes is a reserved field that tells the language server to load the Kubernetes schema from the URL defined in this constant. yaml.schemas can be extended with support for additional schemas - see details in relevant documentation.

Now you can create a YAML file and start using autocompletion. pressing + space (or another combination configured in vim) should show the available fields and documentation according to the current context:

Vim with YAML support for Kubernetes
Works here + space because I configured inoremap <silent><expr> <c-space> coc#refresh(). If you haven't done so, see coc.nvim README for example configuration.

Selecting the Kubernetes API version

As of this writing, yaml-language-server ships with Kubernetes 1.14.0 schemas. I didn't find a way to dynamically select a schema, so I opened corresponding GitHub issue. Fortunately, since the language server is written in typescript, it's quite easy to manually change the version. To do this, just find the file server.ts.

To find it on your machine, just 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

Process 2380 is relevant for us: it is what vim uses when editing the YAML file.

It is easy to see that 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 by changing the value KUBERNETES_SCHEMA_URL, for example, for 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 you are using coc-yaml the location of the variable in the code can be different. Please also note that I changed the repository from garethr on instrumenta. It seems that garethr switched to support schemes there.

To verify that the change has taken effect, see if a field appears that didn't exist before [in past versions of Kubernetes]. For example, in the scheme for K8s 1.14 there was no startupProbe:

Vim with YAML support for Kubernetes

Summary

I hope this opportunity pleased you as much as it did me. Happy YAMLing! Be sure to check out these repositories to better understand the utilities mentioned in the article:

PS from translator

And there’s vikube, vim-kubernetes ΠΈ vimkubectl.

Read also on our blog:

Source: habr.com

Add a comment