kubeconform Documentation

repository·master·Indexed 25 days ago

https://github.com/yannh/kubeconform

A high-performance Kubernetes manifest validation tool that validates configurations against OpenAPI schemas. It supports custom resources (CRDs), offline validation, and multiple output formats including JSON, text, and pretty. Key features include configurable schema locations via Go templates, support for OpenShift manifests, and integration options for GitHub Actions and GitLab-CI.

Tokens
5.1K
Snippets
14
Records
29
Agent score
78%

What's inside kubeconform

  1. Overview of Kubeconform

    master

    Kubeconform is a high-performance Kubernetes manifest validation tool. It checks whether your Kubernetes manifests are valid according to Kubernetes resource definitions.

    Key features include:

    • High performance: Validates and downloads manifests using multiple routines and caches downloaded files in memory.
    • Configurable schema locations: Supports both remote and local schema locations, enabling validation of Custom Resource Definitions (CRDs) and offline validation.
    • Up-to-date schemas: Uses a self-updating fork of the kubernetes-json-schema registry, ensuring support for all recent Kubernetes versions.
    • Improved logging: Supports multiple output formats including Tap, Junit, and JSON.
  2. Install kubeconform on Linux

    master

    To install kubeconform on Linux, download the latest release from the GitHub releases page and move the binary to your local bin directory. For x86_64 architecture, you can use the following command to download, extract, and install it to /usr/local/bin/.

    curl -L https://github.com/yannh/kubeconform/releases/latest/download/kubeconform-linux-amd64.tar.gz | tar xvzf - && \
    sudo mv kubeconform /usr/local/bin/
  3. Validate a file with JSON output and a summary

    master

    To get machine-readable results, use the -output json flag. Combining this with the -summary flag provides a JSON object containing the status of each resource and a high-level summary of valid, invalid, error, and skipped counts. An invalid file will return a non-zero exit code.

    $ ./bin/kubeconform -summary -output json fixtures/invalid.yaml
    {
      "resources": [
        {
          "filename": "fixtures/invalid.yaml",
          "kind": "ReplicationController",
          "version": "v1",
          "status": "INVALID",
          "msg": "Additional property templates is not allowed - Invalid type. Expected: [integer,null], given: string"
        }
      ],
      "summary": {
        "valid": 0,
        "invalid": 1,
        "errors": 0,
        "skipped": 0
      }
    }
    $ echo $?
    1
  4. Validate Kubernetes manifests

    master
    Use kubeconform to validate individual files, directories, or input provided via stdin. You can specify the Kubernetes version to validate against using -kubernetes-version and control the output format with -output (e.g., json, text, pretty).
  5. Convert CustomResourceDefinitions to JSON Schema

    master

    Kubeconform requires JSON schemas to validate Kubernetes resources. To validate Custom Resources (CRDs), you must first convert the CRD's OpenAPI specification to a JSON Schema using the provided openapi2jsonschema.py script.

    Pass the URL or path to the CRD YAML file as an argument to the script.

    $ ./scripts/openapi2jsonschema.py https://raw.githubusercontent.com/aws/amazon-sagemaker-operator-for-k8s/master/config/crd/bases/sagemaker.aws.amazon.com_trainingjobs.yaml
    JSON schema written to trainingjob_v1.json
  6. Configure schema locations for Custom Resource Definitions (CRDs)

    master

    To validate CRDs, you must provide a local or remote registry containing the converted JSON schemas. Kubeconform supports multiple -schema-location flags; it will search through them in order and stop at the first match.

    To use a local folder (e.g., schemas/) as a fallback for resources not found in the default registry, pass both the default location and your local path using Go templated strings.

    # If the resource Kind is not found in kubernetesjsonschema.dev, also lookup in the schemas/ folder for a matching file
    $ ./bin/kubeconform -schema-location default -schema-location 'schemas/{{ .ResourceKind }}{{ .KindSuffix }}.json' fixtures/custom-resource.yaml
  7. Integrate kubeconform in CI (GitHub Actions and GitLab-CI)

    master

    You can run kubeconform in your CI pipelines using its official Docker images from ghcr.io.

    GitHub Actions

    Use the docker:// syntax in a step. Note that you may need to log in to ghcr.io first.

    GitLab-CI

    Use the ghcr.io/yannh/kubeconform:latest-alpine image. Ensure you override the entrypoint to allow running commands via script.

    # GitHub Actions Example
    - uses: docker://ghcr.io/yannh/kubeconform:latest
      with:
        entrypoint: '/kubeconform'
        args: "-summary -output json kubeconfigs/"
    
    # GitLab-CI Example
    lint-kubeconform:
      stage: validate
      image:
        name: ghcr.io/yannh/kubeconform:latest-alpine
        entrypoint: [""]
      script:
      - /kubeconform -summary -output json kubeconfigs/