kubechecks

repository·main·Indexed 20 days ago

https://github.com/zapier/kubechecks

A tool that integrates with GitHub, GitLab, and ArgoCD to provide automated feedback on Pull/Merge Requests. It helps developers understand the impact of Kubernetes manifest changes on live deployments by running checks, linting, and conformance test suites, posting the results directly to the PR/MR.

Tokens
15.9K
Snippets
41
Records
64
Agent score
68%

What's inside kubechecks

  1. What is kubechecks?

    main
    kubechecks is a tool designed for GitHub and GitLab users to visualize the impact of Pull/Merge Requests (PR/MR) on existing ArgoCD deployments. It automates the process of determining how changes in a PR will affect the state of your main/default branch and runs conformance test suites, linting, and Kubernetes manifest checks before a merge occurs. The results are posted directly as comments on the PR/MR.
  2. Overview of kubechecks

    main

    kubechecks is a tool designed to provide visibility into Kubernetes manifest changes before they are merged into your main branch. It acts similarly to terraform plan for Kubernetes by detecting the difference between your proposed changes in a Pull Request (PR) or Merge Request (MR) and your live deployments (e.g., those managed via ArgoCD).

    Key capabilities include:

    • Change Detection: Identifies exactly what will change in your live cluster based on your PR/MR.
    • Automated Validation: Runs linting and validation reports using kubepug, kubeconform, and conftest to ensure manifests are production-ready.
    • Platform Support: Works with both GitHub and GitLab.
  3. How Archive Mode works for GitHub and GitLab

    main

    Archive mode replaces Git protocol operations with HTTPS REST API calls to the respective Version Control System (VCS).

    GitHub Implementation

    • Mechanism: Downloads a ZIP archive of the merge commit SHA via the GitHub API.
    • Endpoint: https://api.github.com/repos/{owner}/{repo}/zipball/{sha}
    • Authentication: Uses Authorization: Bearer <token>.
    • File Detection: Uses the GET /repos/{owner}/{repo}/pulls/{number}/files endpoint to identify changed files.

    GitLab Implementation

    • Mechanism: Downloads a preview merge via a special merge ref.
    • Endpoint: https://gitlab.com/api/v4/projects/{project_encoded}/repository/archive.zip?sha=refs/merge-requests/{iid}/merge
    • Authentication: Uses PRIVATE-TOKEN: <token>.
    • File Detection: Uses the GET /projects/{id}/merge_requests/{iid}/diffs endpoint.
    • Note: GitLab uses the special refs/merge-requests/<iid>/merge ref because the merge_commit_sha is only available after the MR is merged.
  4. How kubechecks works

    main

    kubechecks is a system that automates Kubernetes application checks by reacting to Pull Request (PR) or Merge Request (MR) events from Version Control Systems (VCS) like GitHub or GitLab.

    The high-level workflow is:

    1. Webhook Trigger: A remote VCS provider sends a webhook event to the kubechecks server.
    2. Parsing & Cloning: A VCS-specific Client parses the payload and clones the repository to a local Repo at the specific commit SHA.
    3. Check Execution: A CheckEvent identifies which applications in the repository were affected by the changes and runs the configured check suites concurrently.
    4. Reporting: As checks complete, kubechecks compiles reports and uses the Client to post/update a single, dynamic comment on the PR/MR. To prevent clutter, previous comments from prior runs are deleted when a new run starts.
  5. Core components of kubechecks

    main

    The kubechecks architecture is built on three primary abstractions that allow it to remain VCS-agnostic:

    Client

    Acts as the entry point for the process. A Client implementation is responsible for:

    • Validating incoming webhooks and their payloads.
    • Converting VCS-specific webhook payloads into the internal Repo representation.
    • Communicating back to the remote VCS (e.g., posting or updating comments on a PR/MR).
    • Supported providers: GitHub, GitLab.

    Repo

    An internal representation of a Pull/Merge Request. It contains metadata required to perform local git operations, including:

    • The HEAD SHA of the PR/MR branch.
    • The base/target branch information.
    • Git metadata (username/email).
    • The ability to locally clone the repository at the specified SHA.

    CheckEvent

    The orchestrator of a single execution run. It takes a Client and a Repo and:

    • Determines which applications within the repository are affected by the PR/MR.
    • Runs the check suites for affected applications concurrently.
    • Aggregates individual application reports into a single comprehensive report.
    • Instructs the Client to update the PR/MR comment with the latest status.
  6. How the Kubechecks queue system works

    main

    The Kubechecks queue system uses a hybrid concurrency model to manage PR check requests. It provides per-repository sequential processing and cross-repository parallelism.

    Mental Model

    • Sequential per Repo: For any single repository, PR checks are processed one after another in a FIFO (First-In-First-Out) manner. This prevents git conflicts and cache contention within a single repository.
    • Parallel across Repos: Checks for different repositories (e.g., Repo A and Repo B) run in parallel, as they are managed by independent RepoQueue instances.

    Workflow

    1. Request Arrival: A webhook triggers an Enqueue operation via the QueueManager.
    2. Queue Management: The QueueManager looks up the existing queue for the repository's URL or creates a new RepoQueue on-demand.
    3. Worker Processing: Each RepoQueue maintains a single worker goroutine that pulls requests from a buffered channel and processes them one by one.
    4. Shutdown: During a graceful shutdown, the system stops accepting new requests, allows in-flight work to finish, and notifies users of any dropped requests via PR comments.
                        Webhook Arrives
                             |
                             v
                      ┌──────────────┐
                      │ QueueManager │
                      │   (Global)   │
                      └──────┬───────┘
                             |
                  Lookup or Create Queue
                             |
            ┌────────────────┼────────────────┐
            v                v                v
       ┌─────────┐      ┌─────────┐     ┌─────────┐
       │ Queue A │      │ Queue B │     │ Queue C │
       │ Repo A  │      │ Repo B  │     │ Repo C  │
       └────┬────┘      └────┬────┘     └────┬────┘
            │                │                │
       Buffered Chan   Buffered Chan   Buffered Chan
       [PR3, PR2]       [PR5]           [PR7]
            │                │                │
            v                v                v
       ┌─────────┐      ┌─────────┐     ┌─────────┐
       │Worker A │      │Worker B │     │Worker C │
       │(1 goro) │      │(1 goro) │     │(1 goro) │
       └─────────┘      └─────────┘     └─────────┘
       Sequential       Sequential      Sequential
       Processing       Processing       Processing
  7. Deploy kubechecks-rbac for external installations

    main

    The kubechecks-rbac Helm chart is used to deploy the necessary ClusterRole and ClusterRoleBinding when running kubechecks from outside the Kubernetes cluster where the target resources reside.

    Note: This chart is not required if you are running kubechecks within the same cluster that you are checking.

  8. Multi-Architecture Builds and Registry Pushing

    main

    Kubechecks supports multi-architecture builds (defaulting to linux/amd64 and linux/arm64) using Go's native cross-compilation for high performance.

    • Build for multiple architectures: make build-multiarch
    • Build and push to a registry: make push-multiarch IMAGE_REGISTRY=<registry_url>

    Example: Pushing to a custom registry:

    make push-multiarch IMAGE_REGISTRY=docker.io/myorg
    make build-multiarch
    make push-multiarch IMAGE_REGISTRY=ghcr.io/zapier
  9. Configure webhook integration for kubechecks

    main

    To use kubechecks, you must configure your remote repository (GitHub, GitLab, etc.) to send PullRequest or MergeRequest webhook events to the address where your kubechecks instance is listening.

    Requirement: You must manually register the webhook in your VCS provider's settings pointing to the kubechecks webhook endpoint.

  10. Set up the local development environment with Tilt

    main

    The local development environment is powered by Tilt.dev. Tilt manages the building, testing, and running of kubechecks within a local Kubernetes cluster. It also handles deploying ArgoCD, demo applications, and ngrok for webhook testing.

    1. Store secrets in your OS keychain

    It is highly recommended to store secrets in your OS keychain rather than using a .secret file to avoid keeping plaintext on disk.

    Required: You must provide either GITLAB_TOKEN or GITHUB_TOKEN. Optional: KUBECHECKS_WEBHOOK_SECRET, OPENAI_API_TOKEN, or ANTHROPIC_API_KEY.

    macOS (Keychain Access): Use the security command. Use the -w flag without a value to prompt for the secret interactively, which prevents the secret from appearing in your shell history.

    Linux (pass): Install pass (e.g., sudo apt install pass), initialize it with your GPG key using pass init "your-email@example.com", and then use pass insert to add secrets.

    2. Configure VCS type

    If testing with GitHub, create a tilt_config.json file in the root directory to specify the version control system (the default is gitlab).

    3. Start Tilt

    Run tilt up from the root directory of the repository. You can then press the space bar in the terminal to open the Tilt web UI.

    // tilt_config.json
    {
        "vcs-type": "github"
    }
  11. Sign commits using the Developer Certificate of Origin (DCO)

    main

    To contribute to kubechecks, every commit must be signed off to comply with the Developer Certificate of Origin (DCO). This is a requirement for all Pull Requests.

    To sign off on a new commit, use the -s or --signoff flag.

    If you need to fix a previous commit that was not signed, you can use git commit --amend --signoff for the most recent commit, or git rebase --signoff HEAD~N (where N is the number of commits to go back) to sign off on multiple commits.

    # Sign a new commit
    git commit -s -m "your message"
    
    # Amend the last commit to add sign-off
    git commit --amend --signoff
    
    # Sign off on the last 2 commits
    git rebase --signoff HEAD~2
  12. Install kubechecks via Helm

    main

    kubechecks is officially supported for deployment to a Kubernetes cluster using Helm.

    Requirements

    1. A Kubernetes Cluster
    2. A GitHub or GitLab token (for repository authentication)
    3. ArgoCD

    Installation Steps

    1. Add the kubechecks Helm repository:
    helm repo add kubechecks https://zapier.github.io/kubechecks/
    1. Install the chart:
    helm install kubechecks charts/kubechecks -n kubechecks --create-namespace

    Note: You must provide required secrets (such as VCS tokens or AI API keys) via your values.yaml. Refer to the Helm chart documentation for specific required secret keys.

    helm repo add kubechecks https://zapier.github.io/kubechecks/
    helm install kubechecks charts/kubechecks -n kubechecks --create-namespace