stack

package
v0.1.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

Stack - Core Domain Model

The stack package defines the hierarchical domain model at the heart of Kure. It provides the Cluster, Node, Bundle, and Application abstractions used to describe a complete Kubernetes deployment topology.

Overview

Kure models Kubernetes infrastructure as a four-level hierarchy:

Cluster
  └── Node (tree structure)
        └── Bundle (deployment unit)
              └── Application (workload)

Each level maps to a concept in GitOps deployment:

Level Purpose GitOps Mapping
Cluster Target cluster Root directory
Node Organizational grouping (e.g., infrastructure, apps) Subdirectory tree
Bundle Deployment unit with dependencies Flux Kustomization / ArgoCD Application
Application Individual workload or resource set Kubernetes manifests

Key Types

Cluster

The root of the hierarchy, representing a complete cluster configuration.

cluster := stack.NewCluster("production", rootNode)
cluster.SetGitOps(&stack.GitOpsConfig{
    Type: "flux",
})
Node

A tree structure for organizing bundles into logical groups. Nodes can have children (sub-nodes) and a package reference for multi-source deployments.

node := &stack.Node{
    Name:     "infrastructure",
    Children: []*stack.Node{childNode},
    Bundle:   []*stack.Bundle{monitoringBundle},
}
Bundle

A deployment unit corresponding to a single GitOps resource (e.g., a Flux Kustomization). Bundles support dependency ordering via DependsOn.

bundle, err := stack.NewBundle("monitoring", apps, labels)
bundle.DependsOn = []string{"cert-manager"}
bundle.Interval = "10m"
Application

An individual Kubernetes workload. Applications use the ApplicationConfig interface to generate their resources.

app := stack.NewApplication("prometheus", "monitoring", prometheusConfig)
resources, err := app.Generate()
ApplicationConfig Interface

Implement this interface to define how an application generates its Kubernetes resources:

type ApplicationConfig interface {
    Generate(*Application) ([]*client.Object, error)
}
Optional Validation

ApplicationConfig implementations can optionally implement the Validator interface to validate configuration before resource generation:

type Validator interface {
    Validate() error
}

When present, Application.Generate() calls Validate() automatically before Generate(). If validation fails, generation stops and the error is returned with application context:

type myConfig struct { Port int }

func (c *myConfig) Validate() error {
    if c.Port <= 0 {
        return errors.New("port must be positive")
    }
    return nil
}

func (c *myConfig) Generate(app *stack.Application) ([]*client.Object, error) {
    // Only called if Validate() passes (or is not implemented)
    ...
}

Validation errors are wrapped with application name and namespace:

validation failed for application "web" in namespace "prod": port must be positive

Configs that do not implement Validator continue to work without changes.

Fluent Builder API

For ergonomic cluster construction, use the fluent builder:

cluster := stack.NewClusterBuilder("production").
    WithNode("infrastructure").
        WithBundle("monitoring").
            WithApplication("prometheus", appConfig).
        End().
    End().
    Build()

Workflow System

The package provides a pluggable workflow abstraction for GitOps tool integration:

// Create a workflow for your GitOps tool
wf, err := stack.NewWorkflow("flux")

// Generate GitOps resources from the cluster definition
objects, err := wf.GenerateFromCluster(cluster)

Supported workflow providers: "flux" / "fluxcd" and "argo" / "argocd".

Source References

Bundles and nodes can reference different source types for multi-source deployments:

node.SetPackageRef(&stack.SourceRef{
    Kind:      "OCIRepository",
    Name:      "my-registry",
    Namespace: "flux-system",
    URL:       "oci://registry.example.com/manifests",
    Tag:       "v1.0.0",
})

Documentation

Overview

Package stack provides the core domain model for defining and generating Kubernetes cluster configurations with GitOps tooling (Flux CD or ArgoCD).

Overview

The stack package models a Kubernetes cluster as a hierarchical tree of nodes, where each node can contain bundles of applications. This structure maps directly to the directory layouts expected by GitOps tools, enabling declarative generation of the complete repository structure needed for Flux Kustomizations or ArgoCD Applications.

Domain Model

The core types form a hierarchical structure:

	Cluster
	  └── Node (tree structure)
	        ├── Bundle
	        │     └── Applications
	        └── Children (nested Nodes)

  - [Cluster]: Top-level configuration including GitOps settings
  - [Node]: Hierarchical structure for organizing deployment units
  - [Bundle]: Collection of applications deployed together
  - [Application]: Individual Kubernetes workload or component

Fluent Builder API

The package provides a fluent builder API for constructing cluster configurations in a type-safe, readable manner:

cluster, err := stack.NewClusterBuilder("production").
	WithGitOps(&stack.GitOpsConfig{Type: "flux"}).
	WithNode("infrastructure").
		WithBundle("monitoring").
			WithApplication("prometheus", prometheusConfig).
			End().
		End().
	Build()

The fluent API uses a copy-on-write pattern where each method returns a new builder instance, allowing safe branching and concurrent construction. Build() returns (*Cluster, error) to surface any validation errors.

Workflow Integration

The Workflow interface abstracts the generation of GitOps-specific resources. Implementations exist for both Flux CD and ArgoCD:

Use the workflow to generate all manifests for a cluster:

workflow := fluxcd.NewFluxWorkflow()
manifests, err := workflow.Generate(cluster)

Layout Generation

The github.com/go-kure/kure/pkg/stack/layout subpackage handles writing the generated manifests to disk following the conventions expected by GitOps tools.

Package References

Nodes can specify a [PackageRef] to indicate that a subtree should be packaged as a separate OCI artifact or kurel package. When undefined, the PackageRef is inherited from the parent node.

Example

Complete example creating a cluster with infrastructure and applications:

// Define the cluster structure
cluster, err := stack.NewClusterBuilder("prod-cluster").
	WithGitOps(&stack.GitOpsConfig{
		Type: "flux",
		Bootstrap: &stack.BootstrapConfig{
			Enabled:   true,
			FluxMode:  "gitops-toolkit",
		},
	}).
	WithNode("infrastructure").
		WithBundle("cert-manager").
			WithApplication("cert-manager", certManagerConfig).
			End().
		WithChild("applications").
			WithBundle("web-app").
				WithApplication("frontend", frontendConfig).
				WithApplication("backend", backendConfig).
				End().
			End().
		End().
	Build()

// Generate Flux manifests
workflow := fluxcd.NewFluxWorkflow()
manifests, _ := workflow.Generate(cluster)

// Write to disk using layout package
layout.WriteCluster(cluster, "./clusters/prod", manifests)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasApplicationConfigGVK

func HasApplicationConfigGVK(apiVersion, kind string) bool

HasApplicationConfigGVK checks if an ApplicationConfig is registered for the given apiVersion and kind

func ListApplicationConfigGVKs

func ListApplicationConfigGVKs() []gvk.GVK

ListApplicationConfigGVKs returns all registered ApplicationConfig GVKs

func RegisterApplicationConfig

func RegisterApplicationConfig(gvk gvk.GVK, factory func() ApplicationConfig)

RegisterApplicationConfig registers an ApplicationConfig type with the stack registry

func RegisterArgoWorkflow

func RegisterArgoWorkflow(factory func() Workflow)

RegisterArgoWorkflow registers the ArgoCD workflow factory. This is called by the argocd package during init.

func RegisterFluxWorkflow

func RegisterFluxWorkflow(factory func() Workflow)

RegisterFluxWorkflow registers the Flux workflow factory. This is called by the fluxcd package during init.

Types

type Application

type Application struct {
	Name      string
	Namespace string
	Config    ApplicationConfig
}

Application represents a deployable application with a configuration.

func NewApplication

func NewApplication(name, namespace string, cfg ApplicationConfig) *Application

NewApplication constructs an Application with the provided parameters.

func (*Application) Generate

func (a *Application) Generate() ([]*client.Object, error)

Generate returns the resources for this application. If the Config implements the Validator interface, Validate() is called before Generate(). A validation error stops generation immediately.

func (*Application) SetConfig

func (a *Application) SetConfig(cfg ApplicationConfig)

SetConfig replaces the application configuration.

func (*Application) SetName

func (a *Application) SetName(name string)

SetName updates the application name.

func (*Application) SetNamespace

func (a *Application) SetNamespace(ns string)

SetNamespace updates the target namespace.

type ApplicationConfig

type ApplicationConfig interface {
	Generate(*Application) ([]*client.Object, error)
}

ApplicationConfig describes the behaviour of specific application types.

func CreateApplicationConfig

func CreateApplicationConfig(apiVersion, kind string) (ApplicationConfig, error)

CreateApplicationConfig creates a new ApplicationConfig instance for the given apiVersion and kind

type ApplicationMetadata

type ApplicationMetadata struct {
	Name      string            `yaml:"name" json:"name"`
	Namespace string            `yaml:"namespace,omitempty" json:"namespace,omitempty"`
	Labels    map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"`
}

ApplicationMetadata contains common metadata fields

type ApplicationWrapper

type ApplicationWrapper struct {
	APIVersion string              `yaml:"apiVersion" json:"apiVersion"`
	Kind       string              `yaml:"kind" json:"kind"`
	Metadata   ApplicationMetadata `yaml:"metadata" json:"metadata"`
	Spec       ApplicationConfig   `yaml:"-" json:"-"`
}

ApplicationWrapper provides type detection and unmarshaling for ApplicationConfig

func (*ApplicationWrapper) MarshalYAML

func (w *ApplicationWrapper) MarshalYAML() (interface{}, error)

MarshalYAML implements custom YAML marshaling

func (*ApplicationWrapper) ToApplication

func (w *ApplicationWrapper) ToApplication() *Application

ToApplication converts the wrapper to a stack.Application

func (*ApplicationWrapper) UnmarshalYAML

func (w *ApplicationWrapper) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling with type detection

type ApplicationWrappers

type ApplicationWrappers []ApplicationWrapper

ApplicationWrappers is a slice of ApplicationWrapper for unmarshaling multiple configs

func (ApplicationWrappers) ToApplications

func (ws ApplicationWrappers) ToApplications() []*Application

ToApplications converts all wrappers to Applications

type BootstrapConfig

type BootstrapConfig struct {
	// Common fields
	Enabled bool `yaml:"enabled"`

	// Flux-specific
	FluxMode        string   `yaml:"fluxMode,omitempty"` // "gitops-toolkit" or "flux-operator"
	FluxVersion     string   `yaml:"fluxVersion,omitempty"`
	Components      []string `yaml:"components,omitempty"`
	Registry        string   `yaml:"registry,omitempty"`
	ImagePullSecret string   `yaml:"imagePullSecret,omitempty"`

	// Source configuration
	SourceURL string `yaml:"sourceURL,omitempty"` // OCI/Git repository URL
	SourceRef string `yaml:"sourceRef,omitempty"` // Tag/branch/ref

	// ArgoCD-specific (mock for now)
	ArgoCDVersion   string `yaml:"argoCDVersion,omitempty"`
	ArgoCDNamespace string `yaml:"argoCDNamespace,omitempty"`
}

BootstrapConfig defines the bootstrap configuration for GitOps tools

type Bundle

type Bundle struct {
	// Name identifies the application set.
	Name string
	// ParentPath is the hierarchical path to the parent bundle (e.g., "cluster/infrastructure")
	// Empty for root bundles. This avoids circular references while maintaining hierarchy.
	ParentPath string
	// DependsOn lists other bundles this bundle depends on
	DependsOn []*Bundle
	// Interval controls how often Flux reconciles the bundle.
	Interval string
	// SourceRef specifies the source for the bundle.
	SourceRef *SourceRef
	// Applications holds the Kubernetes objects that belong to the application.
	Applications []*Application
	// Labels are common labels that should be applied to each resource.
	Labels map[string]string
	// Annotations are common annotations applied to the generated Kustomization resource.
	Annotations map[string]string
	// Description provides a human-readable description of the bundle.
	Description string
	// Prune enables garbage collection of resources removed from the bundle.
	Prune *bool
	// Wait causes the Kustomization to wait for resources to become ready.
	Wait *bool
	// Timeout is the maximum duration to wait for resources to be ready (e.g. "5m").
	Timeout string
	// RetryInterval is the interval between retry attempts for failed reconciliations (e.g. "2m").
	RetryInterval string
	// contains filtered or unexported fields
}

Bundle represents a unit of deployment, typically the resources that are reconciled by a single Flux Kustomization.

func NewBundle

func NewBundle(name string, resources []*Application, labels map[string]string) (*Bundle, error)

NewBundle constructs a Bundle with the given name, resources and labels. It returns an error if validation fails.

func (*Bundle) Generate

func (a *Bundle) Generate() ([]*client.Object, error)

func (*Bundle) GetParent

func (b *Bundle) GetParent() *Bundle

GetParent returns the runtime parent reference (may be nil).

func (*Bundle) GetParentPath

func (b *Bundle) GetParentPath() string

GetParentPath returns the hierarchical path to the parent bundle.

func (*Bundle) GetPath

func (b *Bundle) GetPath() string

GetPath returns the full hierarchical path of this bundle.

func (*Bundle) InitializePathMap

func (b *Bundle) InitializePathMap(allBundles []*Bundle)

InitializePathMap builds the runtime path lookup map for efficient hierarchy navigation. This should be called on the root bundle after the tree structure is complete.

func (*Bundle) SetParent

func (b *Bundle) SetParent(parent *Bundle)

SetParent sets the parent bundle and updates the ParentPath accordingly. This method maintains both the serializable path and runtime reference.

func (*Bundle) Validate

func (a *Bundle) Validate() error

Validate performs basic sanity checks on the Bundle.

type BundleBuilder

type BundleBuilder interface {
	WithApplication(name string, appConfig ApplicationConfig) BundleBuilder
	WithDependency(bundle *Bundle) BundleBuilder
	WithSourceRef(sourceRef *SourceRef) BundleBuilder
	End() NodeBuilder
	Build() (*Cluster, error)
}

BundleBuilder provides fluent interface for building Bundle configurations.

type Cluster

type Cluster struct {
	Name   string        `yaml:"name"`
	Node   *Node         `yaml:"node,omitempty"`
	GitOps *GitOpsConfig `yaml:"gitops,omitempty"`
}

Cluster describes a cluster configuration. A cluster configuration is a set of configurations that are packaged in one or more package units

func NewCluster

func NewCluster(name string, tree *Node) *Cluster

NewCluster creates a Cluster with the provided metadata.

func (*Cluster) GetGitOps

func (c *Cluster) GetGitOps() *GitOpsConfig

func (*Cluster) GetName

func (c *Cluster) GetName() string

GetName Helper getters.

func (*Cluster) GetNode

func (c *Cluster) GetNode() *Node

func (*Cluster) SetGitOps

func (c *Cluster) SetGitOps(g *GitOpsConfig)

func (*Cluster) SetName

func (c *Cluster) SetName(n string)

SetName Setters for metadata fields.

func (*Cluster) SetNode

func (c *Cluster) SetNode(t *Node)

type ClusterBuilder

type ClusterBuilder interface {
	WithNode(name string) NodeBuilder
	WithGitOps(gitops *GitOpsConfig) ClusterBuilder
	Build() (*Cluster, error)
}

ClusterBuilder provides fluent interface for building Cluster configurations.

func NewClusterBuilder

func NewClusterBuilder(name string) ClusterBuilder

NewClusterBuilder creates a new fluent cluster builder.

type GitOpsConfig

type GitOpsConfig struct {
	Type      string           `yaml:"type"` // "flux" or "argocd"
	Bootstrap *BootstrapConfig `yaml:"bootstrap,omitempty"`
}

GitOpsConfig defines the GitOps tool configuration for the cluster

type Node

type Node struct {
	// Name identifies the application set.
	Name string `yaml:"name"`
	// ParentPath is the hierarchical path to the parent node (e.g., "cluster/infrastructure")
	// Empty for root nodes. This avoids circular references while maintaining hierarchy.
	ParentPath string `yaml:"parentPath,omitempty"`
	// Children list child bundles
	Children []*Node `yaml:"children,omitempty"`
	// PackageRef identifies in which package the tree of resources get bundled together
	// If undefined, the PackageRef of the parent is inherited
	PackageRef *schema.GroupVersionKind `yaml:"packageref,omitempty"`
	// Bundle holds the applications that get deployed on this level
	Bundle *Bundle `yaml:"bundle,omitempty"`
	// contains filtered or unexported fields
}

Node represents a hierarchic structure holding all deployment bundles each tree has a list of children, which can be a deployment, or a subtree It could match a kubernetes cluster's full configuration, or it could be just a part of that, when parts are e.g. packaged in different OCI artifacts Tree's with a common PackageRef are packaged together

func (*Node) GetBundle

func (n *Node) GetBundle() *Bundle

func (*Node) GetChildren

func (n *Node) GetChildren() []*Node

func (*Node) GetName

func (n *Node) GetName() string

func (*Node) GetPackageRef

func (n *Node) GetPackageRef() *schema.GroupVersionKind

func (*Node) GetParent

func (n *Node) GetParent() *Node

func (*Node) GetParentPath

func (n *Node) GetParentPath() string

func (*Node) GetPath

func (n *Node) GetPath() string

GetPath returns the full hierarchical path of this node.

func (*Node) InitializePathMap

func (n *Node) InitializePathMap()

InitializePathMap builds the runtime path lookup map for efficient hierarchy navigation. This should be called on the root node after the tree structure is complete.

func (*Node) SetBundle

func (n *Node) SetBundle(bundle *Bundle)

func (*Node) SetChildren

func (n *Node) SetChildren(children []*Node)

func (*Node) SetName

func (n *Node) SetName(name string)

func (*Node) SetPackageRef

func (n *Node) SetPackageRef(ref *schema.GroupVersionKind)

func (*Node) SetParent

func (n *Node) SetParent(parent *Node)

SetParent sets the parent node and updates the ParentPath accordingly. This method maintains both the serializable path and runtime reference.

func (*Node) SetParentPath

func (n *Node) SetParentPath(path string)

type NodeBuilder

type NodeBuilder interface {
	WithChild(name string) NodeBuilder
	WithBundle(name string) BundleBuilder
	WithPackageRef(ref *schema.GroupVersionKind) NodeBuilder
	End() ClusterBuilder
	Build() (*Cluster, error)
}

NodeBuilder provides fluent interface for building Node configurations.

type SourceRef

type SourceRef struct {
	Kind      string
	Name      string
	Namespace string
	// URL is the repository URL (OCI or Git). When set, the resource generator
	// creates the source CRD in addition to referencing it.
	URL string
	// Tag is the tag or semver reference for OCI sources.
	Tag string
	// Branch is the branch reference for Git sources.
	Branch string
}

SourceRef defines a reference to a Flux source. When Kind, Name and Namespace are set, the Kustomization will reference an existing source. When URL is also set, the resource generator will create the source CRD.

type Validator

type Validator interface {
	Validate() error
}

Validator is an optional interface that ApplicationConfig implementations can implement to validate their configuration before generation. If an ApplicationConfig also implements Validator, Application.Generate() calls Validate() automatically before calling Generate().

type Workflow

type Workflow interface {
	// GenerateFromCluster creates GitOps resources from a cluster definition.
	// This is the primary entry point for resource generation.
	GenerateFromCluster(*Cluster) ([]client.Object, error)

	// CreateLayoutWithResources creates a new manifest layout that includes
	// both the application manifests and the GitOps resources needed to
	// deploy them. This combines manifest generation with GitOps resource
	// generation in a single operation.
	// The rules parameter is expected to be of type layout.LayoutRules
	CreateLayoutWithResources(*Cluster, interface{}) (interface{}, error)

	// GenerateBootstrap creates bootstrap resources for initializing the
	// GitOps system itself. This is used to set up the GitOps controller
	// (Flux, ArgoCD, etc.) in the cluster.
	GenerateBootstrap(*BootstrapConfig, *Node) ([]client.Object, error)
}

Workflow defines the core interface for GitOps workflow implementations. This interface provides a minimal abstraction for converting stack definitions into GitOps-specific resources (Flux Kustomizations, ArgoCD Applications, etc.).

func NewWorkflow

func NewWorkflow(provider string) (Workflow, error)

NewWorkflow creates a workflow implementation based on the provider type. Supported providers: "flux", "argocd"

Directories

Path Synopsis
Package generators provides a pluggable system for generating Kubernetes resources from different configuration formats.
Package generators provides a pluggable system for generating Kubernetes resources from different configuration formats.
appworkload
Package appworkload provides generators for creating standard Kubernetes workloads (Deployments, StatefulSets, DaemonSets) along with their associated resources (Services, Ingresses, PersistentVolumeClaims).
Package appworkload provides generators for creating standard Kubernetes workloads (Deployments, StatefulSets, DaemonSets) along with their associated resources (Services, Ingresses, PersistentVolumeClaims).
fluxhelm
Package fluxhelm provides generators for creating Flux HelmRelease resources along with their associated source resources (HelmRepository, GitRepository, OCIRepository, or Bucket).
Package fluxhelm provides generators for creating Flux HelmRelease resources along with their associated source resources (HelmRepository, GitRepository, OCIRepository, or Bucket).
kurelpackage
Package kurelpackage provides a generator for creating kurel packages from Kubernetes manifests with support for values substitution, patches, and conditional extensions.
Package kurelpackage provides a generator for creating kurel packages from Kubernetes manifests with support for values substitution, patches, and conditional extensions.
Package layout provides utilities for generating cluster directory layouts and for writing Kubernetes and Flux manifests to disk.
Package layout provides utilities for generating cluster directory layouts and for writing Kubernetes and Flux manifests to disk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL