Skip to main content

Merge built-in K8s and CRD schemas for yamlls

Project description

k8s-schema-merger

This is a CLI tool to help setting up Kubernetes schema linting with with yamlls.

Motivation

Kubernetes manifest validation can be done by providing the corresponding schema definitions to yamlls. The same can be achieved for CRDs with one of the definitions from the CRD catalog. However, this approach is insufficient when a file contains more than one manifest with a combination of either different CRDs, or a CRDs with Kubernetes primitives, given that yamlls is unable to validate one file with more than one schema definition.

This tool provides a solution to this problem by merging the required definitions into a single file.

Usage

Install this Python module. E.g.:

pip install k8s-schema-merger

You can simply run k8s-schema-merger and let the module detect the Kubernetes version and required CRD definitions by connecting to your Kubernetes cluster.

If cluster credentials are not available, or if you only want a subset of CRD schemas, you can specify the version and required CRD groups yourself, like so:

k8s-schema-merger argoproj.io cert-manager.io k8s.nginx.org --version 1.32.0

The script will then download the schema definitions from the CRD catalog. Note that these CRD groups must correspond with the names of top-level directories from the CRD catalog.

You can also change the output directory with the --output-dir argument. By default, all output files are stored in $XDG_DATA_HOME/k8s-schema-merger.

Also, you can always extend a previously generated schema definition with additional CRDs by using the --extend argument.

IDE setup

On nvim, adapt the nvim-lspconfig configuration to use the newly-generated schemas for your Kubernetes manifests. Below is an example using lazy.nvim.

{
  "neovim/nvim-lspconfig",
  opts = {
    servers = {
      yamlls = {
        settings = {
          yaml = {
            schemas = {
              ["file:/Users/whoever/.local/share/k8s-schema-merger/all.json"] = "k8s/*.y*ml",
            },
          },
        },
      },
    },
  },
}

Caveats

The yaml-language-server project does not work with schemas other than those for Kubernetes v1.22.4, which are hard-coded in its codebase. In order to lint your manifests with custom schemas, you can patch your installation of yamlls as shown in this patch:

index bec1f83..692d8ed 100644
--- a/settingsHandlers.js
+++ b/settingsHandlers.js
@@ -288,12 +288,12 @@ class SettingsHandler {
         else {
             languageSettings.schemas.push({ uri, fileMatch: fileMatch, schema: schema, priority: priorityLevel });
         }
-        if (fileMatch.constructor === Array && uri === schemaUrls_1.KUBERNETES_SCHEMA_URL) {
+        if (fileMatch.constructor === Array && (uri === schemaUrls_1.KUBERNETES_SCHEMA_URL || uri.search(/k8s-yaml-merger/))) {
             fileMatch.forEach((url) => {
                 this.yamlSettings.specificValidatorPaths.push(url);
             });
         }
-        else if (uri === schemaUrls_1.KUBERNETES_SCHEMA_URL) {
+        else if (uri === schemaUrls_1.KUBERNETES_SCHEMA_URL || uri.search(/k8s-yaml-merger/)) {
             this.yamlSettings.specificValidatorPaths.push(fileMatch);
         }
         return languageSettings;

If you installed yamlls with Mason, the aforementioned file may be located at ~/.local/share/nvim/mason/packages/yaml-language-server/node_modules/yaml-language-server/out/server/src/languageserver/handlers/settingsHandlers.js. Remember to change the /k8s-yaml-merger/ regex when using a different output dir.

Contributing

This project is a currently a functional proof of concept. It can certainly be extended and improved, for example, by integrating the generation of schemas directly in the IDE.

If you have ideas or comments about it, go ahead and open an issue to let me know.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

k8s_schema_merger-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

k8s_schema_merger-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file k8s_schema_merger-0.1.1.tar.gz.

File metadata

  • Download URL: k8s_schema_merger-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for k8s_schema_merger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 81548dd4acf8508b7358f86a182695d8cb97d3725282d86f35bd51146c61fd7b
MD5 90c1d151a6bc60c261d11db479f89424
BLAKE2b-256 f4460b00a0db87528aabd97cdc4e79eab2509c3a60ee40897565caaa2f7eac96

See more details on using hashes here.

Provenance

The following attestation bundles were made for k8s_schema_merger-0.1.1.tar.gz:

Publisher: publish.yaml on Funk66/k8s-schema-merger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file k8s_schema_merger-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for k8s_schema_merger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a6797c3f6bf0229cd82ed7fb552612491450afd1b7dd5d2d088e0fad27faa133
MD5 4064423990dfcbf4316a8ecc97a75526
BLAKE2b-256 f3cd7d3583f1d5868f2cae87fce1f3a168d2c8bf0e83ee47ab40a258babc6b48

See more details on using hashes here.

Provenance

The following attestation bundles were made for k8s_schema_merger-0.1.1-py3-none-any.whl:

Publisher: publish.yaml on Funk66/k8s-schema-merger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page