Skip to main content

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.

Metadata

Release files for k8s-schema-merger 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for k8s-schema-merger 0.1.1
File Size Uploaded
k8s_schema_merger-0.1.1.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for k8s-schema-merger 0.1.1
File Interpreter ABI Platform
k8s_schema_merger-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / k8s_schema_merger-0.1.1.tar.gz

Download URL k8s_schema_merger-0.1.1.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
81548dd4acf8508b7358f86a182695d8cb97d3725282d86f35bd51146c61fd7b
BLAKE2b-256 checksum
How to use checksums
f4460b00a0db87528aabd97cdc4e79eab2509c3a60ee40897565caaa2f7eac96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 28, 2024.

Transparency log

Release files / k8s_schema_merger-0.1.1-py3-none-any.whl

Download URL k8s_schema_merger-0.1.1-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a6797c3f6bf0229cd82ed7fb552612491450afd1b7dd5d2d088e0fad27faa133
BLAKE2b-256 checksum
How to use checksums
f3cd7d3583f1d5868f2cae87fce1f3a168d2c8bf0e83ee47ab40a258babc6b48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 28, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page