upgrade

package
v1.11.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

CUE Upgrade System

This package provides an extensible system for upgrading CUE templates to handle version compatibility as KubeVela evolves.

Architecture

  • upgrade.go: Core interface, registry, and main Upgrade() function
  • upgrade_1_11.go: KubeVela 1.11+ specific upgrades (list concatenation)
  • upgrade_test.go: Comprehensive test suite
  • Future: upgrade_1_12.go, upgrade_1_13.go, etc.

Usage

import "github.com/oam-dev/kubevela/pkg/cue/upgrade"

// Upgrade using current KubeVela CLI version (auto-detected)
result, err := upgrade.Upgrade(cueTemplate)
if err != nil {
    // If version detection fails, you'll get a helpful error message
    // suggesting to use the --target-version flag
    log.Fatal(err)
}

// Upgrade to specific KubeVela version
result, err := upgrade.Upgrade(cueTemplate, "1.11")

// Future: upgrade to newer versions
result, err := upgrade.Upgrade(cueTemplate, "1.12")

Adding New Version Upgrades

To add support for KubeVela 1.12 (example):

  1. Create upgrade_1_12.go:
package upgrade

import "fmt"

func init() {
    // Register your new upgrade functions
    RegisterUpgrade("1.12", upgradeSomeNewFeature)
    RegisterUpgrade("1.12", upgradeAnotherFeature)
}

// upgradeSomeNewFeature handles a breaking change in CUE 0.15
func upgradeSomeNewFeature(cueStr string) (string, error) {
    // Your upgrade logic here
    // Parse, transform, format, return
    return transformedCue, nil
}
  1. Update upgrade.go supportedVersions:
// In the Upgrade() function
supportedVersions := []string{"1.11", "1.12"}
  1. Add tests to upgrade_test.go for the new functionality

  2. Done! The system automatically applies all relevant upgrades

Current Upgrades

1.11 - List Concatenation
  • Problem: list1 + list2 syntax deprecated in underlying CUE version
  • Solution: Converts to list.Concat([list1, list2])
  • Auto-imports: Adds import "list" when needed

Features

  • Version-aware: Only applies upgrades up to target version
  • Composable: Multiple upgrades can be registered per version
  • Safe: Falls back gracefully on errors
  • Extensible: Easy to add new version support
  • Well-tested: Comprehensive test coverage

Examples

Before (Old CUE):
myList1: [1, 2, 3]
myList2: [4, 5, 6]
combined: myList1 + myList2
After (CUE 0.14+):
import "list"

myList1: [1, 2, 3]
myList2: [4, 5, 6] 
combined: list.Concat([myList1, myList2])

This system ensures KubeVela templates remain compatible as CUE continues to evolve.

CLI Usage

The upgrade functionality is available through the KubeVela CLI:

# Upgrade using current KubeVela CLI version (auto-detected)
vela def upgrade my-definition.cue

# Save upgraded definition to a file
vela def upgrade my-definition.cue -o upgraded-definition.cue

# Upgrade for specific KubeVela version
vela def upgrade my-definition.cue --target-version=v1.11

# Also works with just the version number
vela def upgrade my-definition.cue --target-version=1.11

Version Detection:

  • When no --target-version is specified, the system automatically detects the current KubeVela CLI version
  • If version detection fails (e.g., in development builds), you'll get a helpful error message suggesting to use --target-version=1.11
  • Note: Use --target-version= (with equals sign) for the version flag

Documentation

Overview

Package upgrade provides CUE version-compatibility helpers for KubeVela. The core engine lives in github.com/kubevela/pkg/cue/upgrade; this package wires KubeVela-specific concerns (version provider, Prometheus metrics) and re-exports the public API so existing call sites need no import change.

Index

Constants

This section is empty.

Variables

View Source
var (
	// EnableListConcatUpgrade controls the list-arithmetic compatibility rewrite pass.
	EnableListConcatUpgrade = true
	// EnableErrorFieldLabelUpgrade controls quoting of legacy unquoted error labels.
	EnableErrorFieldLabelUpgrade = true
	// EnableBoolDefaultGuardUpgrade controls the bool default-guard hazard rewrite pass.
	EnableBoolDefaultGuardUpgrade = false
	// EnableGenericDefaultGuardUpgrade controls generic (non-bool) default-guard hazard rewrites.
	EnableGenericDefaultGuardUpgrade = false
	// EnableKeepValidatorsSingletonUpgrade controls singleton keepvalidators concretization rewrites.
	EnableKeepValidatorsSingletonUpgrade = false
	// EnableEvalv3SelfRefGuardUpgrade controls evalv3 self-reference default-guard rewrites.
	EnableEvalv3SelfRefGuardUpgrade = false
)
View Source
var (
	ParseVersion         = pkgupgrade.ParseVersion
	RegisterUpgrade      = pkgupgrade.RegisterUpgrade
	GetSupportedVersions = pkgupgrade.GetSupportedVersions
)
View Source
var CUECompatCacheEvictionsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
	Name: "kubevela_cue_compat_cache_evictions_total",
	Help: "Total number of CUE compatibility cache evictions, by reason (capacity or ttl).",
}, []string{"reason"})

CUECompatCacheEvictionsTotal counts cache entries evicted, by reason. Labels:

  • reason: "capacity" (LRU eviction when cache is full) or "ttl" (idle entry swept by background loop)

A sustained capacity eviction rate means the working set exceeds cache size; increase --cue-compatibility-cache-size or migrate definitions to compliant CUE syntax (run `vela def compat` to identify which definitions need remediation).

View Source
var CUECompatRewriteTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
	Name: "kubevela_cue_compat_rewrite_total",
	Help: "Total number of CUE compatibility rewrites applied at render time, by upgrade ID, version introduced, definition kind, and template area.",
}, []string{"upgrade_id", "upgrade_version", "definition_kind", "template_area"})

CUECompatRewriteTotal counts the number of CUE compatibility rewrites applied at render time. Labels:

  • upgrade_id: stable fix identifier, e.g. "list-arithmetic"
  • upgrade_version: KubeVela version that introduced the incompatibility, e.g. "1.11"
  • definition_kind: definition type, e.g. "Component"
  • template_area: which part of the definition was rewritten, e.g. "template", "health"

Use this metric to track which legacy syntax fixes are still active cluster-wide. The metric is the smoke detector; run `vela def compat definitions` to identify which specific definitions need remediation.

View Source
var CUECompatUpgradeDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
	Name:    "kubevela_cue_compat_upgrade_duration_seconds",
	Help:    "Duration of CUE version compatibility checks at render time (cache misses only), by definition kind.",
	Buckets: []float64{0.00001, 0.00005, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1},
}, []string{"definition_kind"})

CUECompatUpgradeDuration measures how long EnsureCueVersionCompatibility takes per call. Only recorded on cache misses (i.e. when the actual upgrade logic runs). Labels:

  • definition_kind: definition type, e.g. "Component"

Use histogram_quantile() in PromQL to compute aggregated latency percentiles across replicas. Cache hits are not recorded — they are too cheap to be meaningful.

View Source
var CompatibilityCacheSize = pkgupgrade.CompatibilityCacheSize
View Source
var EnableCUEVersionCompatibility = &pkgupgrade.EnableCUEVersionCompatibility

Functions

func EnsureCueVersionCompatibility

func EnsureCueVersionCompatibility(cueStr, defName string, defKind DefinitionKind, area TemplateArea) (string, bool)

EnsureCueVersionCompatibility returns cueStr upgraded to the current CUE version where it needs to be. A template nested too deeply for CUE's parser, which a stored definition admitted without the webhook's check can be, is never parsed: in its place is a template that fails to compile with the reason, so its render fails rather than the controller.

func InitCompatibilityCache

func InitCompatibilityCache(ctx context.Context, size int)

func RequiresUpgrade

func RequiresUpgrade(cueStr string, targetVersion ...Version) (bool, []string, error)

func SetCacheEntryTTL

func SetCacheEntryTTL(d time.Duration)

func Upgrade

func Upgrade(cueStr string, targetVersion ...Version) (string, error)

Types

type CUEUpgradeFunc

type CUEUpgradeFunc = pkgupgrade.CUEUpgradeFunc

type DefinitionKind

type DefinitionKind = pkgupgrade.DefinitionKind
const (
	ComponentKind    DefinitionKind = "Component"
	TraitKind        DefinitionKind = "Trait"
	PolicyKind       DefinitionKind = "Policy"
	WorkflowStepKind DefinitionKind = "WorkflowStep"
)

type KubeVelaUpgradeFunc

type KubeVelaUpgradeFunc = pkgupgrade.KubeVelaUpgradeFunc

type TemplateArea

type TemplateArea = pkgupgrade.TemplateArea
const (
	TemplateAreaMain         TemplateArea = "template"
	TemplateAreaHealth       TemplateArea = "health"
	TemplateAreaCustomStatus TemplateArea = "custom_status"
	TemplateAreaStatusDetail TemplateArea = "status_detail"
)

type UpgradeFunc

type UpgradeFunc = pkgupgrade.UpgradeFunc //nolint:revive

type Version

type Version = pkgupgrade.Version

Jump to

Keyboard shortcuts

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