Menu

Overview

Relevant source files

Purpose and Scope

This document provides a high-level overview of the KCL (Kubernetes Configuration Language) compiler system, its architecture, and core capabilities. KCL is a constraint-based record and functional programming language designed for writing and managing complex configurations, particularly in cloud-native environments.

This overview introduces the major system components, execution flow, and workspace organization. For detailed information about specific subsystems, refer to:

Sources: README.md22-24 README.md42-57

What is KCL?

KCL is an open-source, constraint-based configuration language implemented primarily in Rust. It combines features from high-level languages like Python and Golang with functional programming principles to provide a type-safe, declarative approach to configuration management.

Key Characteristics:

AspectDescription
Language ParadigmConstraint-based record and functional language
Primary Use CaseCloud-native configuration, especially Kubernetes
Type SystemStatic typing with inference, constraints, and validation rules
Execution ModelsAST interpretation (FastRunner) and LLVM native compilation
ImplementationRust with C/LLVM for high-performance runtime
Output FormatsJSON, YAML, or programmatic API results

Sources: README.md24 README.md44-51

System Architecture

The KCL system follows a layered architecture with clear separation between external interfaces, service orchestration, compilation pipeline, and execution backends.

High-Level Component Organization

Sources: kclvm/Cargo.toml38-63 Diagram 1 from high-level architecture

Core Components

Workspace Structure

The KCL compiler is organized as a Rust Cargo workspace with 20+ specialized crates. The workspace root is defined in kclvm/Cargo.toml38-63

Primary Crates:

CratePurposeLocation
kclvm-apiService API implementation and protobuf interfaceskclvm/api
kclvm-cmdCommand-line interface entry pointskclvm/cmd
kclvm-parserLexer and parser producing ASTkclvm/parser
kclvm-astAbstract Syntax Tree definitionskclvm/ast
kclvm-semaSemantic analysis, type checking, symbol resolutionkclvm/sema
kclvm-compilerLLVM code generationkclvm/compiler
kclvm-runnerExecution orchestrationkclvm/runner
kclvm-evaluatorAST interpretation enginekclvm/evaluator
kclvm-runtimeRuntime library and built-in functionskclvm/runtime
kclvm-driverWorkspace discovery and build coordinationkclvm/driver
kclvm-errorError reporting and diagnosticskclvm/error
kclvm-toolsFormatting, linting, testing, documentationkclvm/tools
kcl-language-serverLSP implementation for IDE supportkclvm/tools/src/LSP

Sources: kclvm/Cargo.toml1-72 kclvm/api/Cargo.toml kclvm/parser/Cargo.toml kclvm/sema/Cargo.toml kclvm/runner/Cargo.toml kclvm/tools/src/LSP/Cargo.toml

Compilation Pipeline Flow

Sources: Diagram 2 from high-level architecture, kclvm/driver kclvm/parser kclvm/sema kclvm/runner

Execution Models

KCL supports two execution backends, selectable via feature flags in kclvm/Cargo.toml65-71:

1. FastRunner (Default)

  • Module: kclvm-evaluator
  • Mechanism: Direct AST interpretation
  • Use Case: Development, debugging, environments without LLVM
  • Performance: Faster compilation, slower runtime

2. LLVM Backend (Optional)

  • Module: kclvm-compiler
  • Mechanism: Compiles KCL to LLVM IR, then to native code
  • Use Case: Production deployments requiring maximum runtime performance
  • Performance: Slower compilation, faster runtime
  • Activation: Build with --features llvm

Both backends share the same kclvm-runtime library for built-in functions and FFI support.

Sources: kclvm/Cargo.toml65-71 kclvm/runner/Cargo.toml50-51 kclvm/api/Cargo.toml50-51 Diagram 1 from high-level architecture

Integration Points

API Layers

Key Integration Components:

  1. C API (FFI Boundary): Defined in kclvm/Cargo.toml8-11 as cdylib and staticlib outputs named kclvm_cli_cdylib
  2. Service API: KclvmServiceImpl in kclvm-api provides unified interface for all operations
  3. Multi-language SDKs: Wrap C API for language-specific ergonomics
  4. Cloud-native Plugins: Integrate KCL into Kubernetes tooling ecosystem

Sources: kclvm/Cargo.toml8-11 kclvm/api/Cargo.toml README.md55-56 Diagram 4 from high-level architecture

Developer Experience

Language Server Protocol (LSP)

The kcl-language-server crate (kclvm/tools/src/LSP) provides IDE features:

  • Code completion via semantic analysis
  • Goto definition using symbol resolution
  • Hover information from type system
  • Find references through def-ref tracking
  • Diagnostics from error subsystem

Sources: kclvm/tools/src/LSP/Cargo.toml README.md53 Diagram 3 from high-level architecture

Command-Line Tools

The kclvm-tools crate provides:

  • Formatting: Code style enforcement
  • Linting: Style checking
  • Testing: Unit test framework
  • Validation: Configuration validation
  • Documentation: Auto-generated docs

Sources: kclvm/tools/Cargo.toml README.md53 Diagram 7 from high-level architecture

Platform Support

Target Platforms

KCL supports multiple platforms through CI/CD workflows:

PlatformArchitectureBuild Environment
macOSAMD64, ARM64Native
WindowsAMD64MSVC, MinGW
LinuxAMD64, ARM64Ubuntu native
LinuxAMD64CentOS7 (Docker)
LinuxAMD64 (musl)Alpine (Docker)
WebAssemblyWASM32WASI target

The default target is configured at build time in kclvm/runner/build.rs6-10

Sources: kclvm/runner/build.rs1-12 Diagram 6 from high-level architecture, .devcontainer/devcontainer.json

Example Usage

Simple Configuration

A basic KCL program (samples/fib.k):

Kubernetes Configuration

A Kubernetes deployment (samples/kubernetes.k):

Sources: samples/fib.k1-15 samples/kubernetes.k1-19

System Dependencies

External Dependencies

Key external libraries and their purposes:

CategoryDependenciesPurpose
Parsingrustc_lexer, petgraphLexical analysis, dependency graphs
Serializationserde, serde_json, serde_yaml_ngJSON/YAML output
CompilationLLVM libraries (optional)Native code generation
LSPlsp-server, lsp-typesIDE integration
Error Reportingcompiler_base_error, annotate-snippetsDiagnostics
Concurrencytokio, crossbeam, parking_lotAsync runtime, thread safety

Sources: kclvm/Cargo.lock1-300 kclvm/parser/Cargo.toml8-36 kclvm/tools/src/LSP/Cargo.toml35-43 kclvm/runtime/Cargo.toml6-37

Project Organization

The project follows standard Rust conventions:

  • Source code: kclvm/ directory containing all crates
  • Documentation: docs/ directory with development guides
  • Examples: samples/ directory with example .k files
  • Build artifacts: Generated during cargo build in target/
  • Development environment: .devcontainer/ for GitHub Codespaces support

For contribution guidelines, see docs/dev_guide/1.about_this_guide.md

Sources: docs/dev_guide/1.about_this_guide.md1-21 .devcontainer/devcontainer.json1-14 README.md75-78

Next Steps

This overview has introduced the KCL system architecture and major components. For deeper dives into specific subsystems:

Sources: Table of contents provided in prompt