# siderolabs/bldr architecture

> Architecture diagram of the GitHub repository siderolabs/bldr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/siderolabs/bldr
- Repository: https://github.com/siderolabs/bldr

## Overview

##### Main Components
- CLI Frontend: Under cmd/bldr, this is the entry point for users—handling commands like build, validate, graph, and llb.
- Package Loader & Parser: This component is responsible for loading “Pkgfile”, “pkg.yaml”, and “vars.yaml” files from the project directory. This is implemented in the internal/pkg/pkgfile and related conversion modules (under internal/pkg/convert).
- Dependency Resolver & Graph Generator: Found in modules under internal/pkg/solver and internal/pkg/integration (for tests and graph generation). This piece resolves both internal package dependencies and external image dependencies, and creates an LLB dependency graph (which can be rendered with Graphviz).
- Build Flow Manager: This component orchestrates the multiple build phases (sources, prepare, build, install, test, finalize) as described in the README. It coordinates the integration with BuildKit (and docker buildx) and manages the build context and environment setup (including environment variables and caching via internal/pkg/environment).
- Update & Upgrade Subsystems: Modules such as internal/pkg/update and internal/pkg/upgrade handle versioning, update operations, and integration with external repositories (like GitHub).
- Utility Modules: Under internal/pkg/util, shared helper functions (including testing utilities) are spread out to support the primary components.

##### Relationships and Data Flow
- The CLI Frontend receives the user’s command and parameters.
- It reads the “Pkgfile” at the root (global build configuration) and then scans the subdirectories for package directories by detecting pkg.yaml files.
- The Package Loader then parses these definitions (with templating using Go’s text/template) and hands off the data.
- The Build Flow Manager uses this data to generate an LLB graph that orchestrates the entire build process: downloading resources, setting up the environment, executing build steps, and finally finalizing the output.
- Upon completion, the result can be saved to cache, pushed to a container registry, or otherwise output as specified.

##### Architectural Patterns and Design Principles
- Modular Design: The system is clearly divided into modules (conversion, solver, update, integration, environment) making it easier to test, maintain, and extend.
- Pipeline Processing: The build flow is set up as a series of phases (download, prepare, build, install, test, finalize) that form a pipeline following BuildKit’s LLB stages.
- Declarative Configuration: Using YAML files for defining build instructions promotes a declarative approach.
- Dependency Injection and Template Engine Usage: Integrating variables from global (Pkgfile) and local (vars.yaml) contexts using Go’s templating engine is a key design aspect.
- Graph-based Build Dependency Resolution: The creation and rendering of dependency graphs (in Graphviz format) is central to understanding the build flow.

##### Relevant Technologies, Frameworks, or Libraries
- Golang (for the implementation)
- BuildKit (and docker buildx) for backend builds
- Graphviz for dependency graph rendering (as shown by the “bldr graph” command)
- (Go) Text/template and functions from the Sprig library for templating across YAML files

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend"
        A["CLI Frontend"]:::cli
    end

    %% Configuration Parsing & Template Engine
    subgraph "Configuration Parsing & Template Engine"
        B["Pkgfile Loader"]:::config
        C["Conversion Modules"]:::config
    end

    %% Dependency Resolution & Graph Generation
    subgraph "Dependency Resolution & Graph Generation"
        D["Dependency Resolver"]:::dependency
        X["Integration Support"]:::dependency
        E["Graph Generator"]:::dependency
    end

    %% Build Orchestration
    subgraph "Build Orchestration"
        F["Build Flow Manager"]:::orchestration
        K["Build Phases: download, prepare, build, install, test, finalize"]:::annotation
    end

    %% Update Subsystems
    subgraph "Update Subsystems"
        G["Update Module"]:::update
        H["Upgrade Module"]:::update
    end

    %% Utility Modules
    subgraph "Utilities"
        I["Utility Modules"]:::utility
    end

    %% External Systems
    subgraph "External Systems"
        J["BuildKit/docker buildx & Container Registry"]:::external
    end

    %% Data Flow Connections
    A -->|"reads_config"| B
    B -->|"parses_YAML"| C
    C -->|"provides_package_definitions"| D
    D -->|"builds_dependency_graph"| E
    X -->|"supports_graph"| E
    E -->|"generates_LLB_graph"| F
    F -->|"invokes_build"| J
    A -->|"triggers_update"| G
    A -->|"triggers_upgrade"| H
    F -.->|"executes"| K
    I -.-> B
    I -.-> D
    I -.-> F

    %% Click Events
    click A "https://github.com/siderolabs/bldr/tree/main/cmd/bldr"
    click B "https://github.com/siderolabs/bldr/tree/main/internal/pkg/pkgfile"
    click C "https://github.com/siderolabs/bldr/tree/main/internal/pkg/convert"
    click D "https://github.com/siderolabs/bldr/tree/main/internal/pkg/solver"
    click E "https://github.com/siderolabs/bldr/blob/main/internal/pkg/convert/graph.go"
    click X "https://github.com/siderolabs/bldr/tree/main/internal/pkg/integration"
    click F "https://github.com/siderolabs/bldr/tree/main/internal/pkg/environment"
    click G "https://github.com/siderolabs/bldr/tree/main/internal/pkg/update"
    click H "https://github.com/siderolabs/bldr/tree/main/internal/pkg/upgrade"
    click I "https://github.com/siderolabs/bldr/tree/main/internal/pkg/util"

    %% Styles
    classDef cli fill:#FFCC80,stroke:#FB8C00,stroke-width:2px;
    classDef config fill:#FFF59D,stroke:#F9A825,stroke-width:2px;
    classDef dependency fill:#80DEEA,stroke:#039BE5,stroke-width:2px;
    classDef orchestration fill:#A5D6A7,stroke:#43A047,stroke-width:2px;
    classDef update fill:#CE93D8,stroke:#8E24AA,stroke-width:2px;
    classDef utility fill:#E1BEE7,stroke:#AB47BC,stroke-width:2px;
    classDef external fill:#B0BEC5,stroke:#78909C,stroke-width:2px;
    classDef annotation fill:#EEEEEE,stroke:#9E9E9E,stroke-dasharray: 5, 5,stroke-width:1px;
```
