expr

package
v0.22.0-rc1 Latest Latest
Warning

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

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

Documentation

Overview

Package expr parses constraint expressions ("<operator> <value>") and evaluates them against a single actual value.

It is a leaf package: it depends only on pkg/errors and pkg/version. The parent pkg/constraints imports pkg/recipe for snapshot-aware evaluation, so pkg/recipe cannot import it back; keeping the pure expression grammar here lets both sides share one parser instead of reimplementing it. The parent re-exports every symbol declared here, so external callers keep using pkg/constraints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompoundConstraint

type CompoundConstraint struct {
	// Alternatives holds the OR clauses; each inner slice is an AND group.
	Alternatives [][]ParsedConstraint
	// contains filtered or unexported fields
}

CompoundConstraint is a disjunction (OR) of conjunction (AND) groups of ParsedConstraints. It evaluates as true when at least one AND group is fully satisfied. This supports per-track GKE version floors such as:

>= 1.34.3-gke.1318000 < 1.35.0 || >= 1.35.0-gke.2745000

which the single-expression ParseConstraintExpression cannot represent.

No overlay or mixin emits compound expressions yet; this is groundwork for per-track GB300 GKE version floors. See #1985.

func ParseCompoundConstraint

func ParseCompoundConstraint(expr string) (*CompoundConstraint, error)

ParseCompoundConstraint parses a compound constraint expression that may contain OR clauses ("||") and AND groups (space-separated sub-expressions). Simple single-term expressions ("ubuntu", ">= 1.32.4") are handled as a degenerate case with one alternative containing one term.

Examples:

">= 1.32.4"
">= 1.34.3-gke.1318000 < 1.35.0 || >= 1.35.0-gke.2745000"

func (*CompoundConstraint) Evaluate

func (cc *CompoundConstraint) Evaluate(actual string) (bool, error)

Evaluate evaluates the compound constraint against an actual value. Returns true if at least one OR alternative is fully satisfied (all AND terms in that group pass). If any individual term evaluation returns an error, the error is propagated immediately (fail-closed).

func (*CompoundConstraint) String

func (cc *CompoundConstraint) String() string

String returns the original expression string.

type Operator

type Operator string

Operator represents a comparison operator in constraint expressions.

const (
	// OperatorGTE represents ">=" (greater than or equal).
	OperatorGTE Operator = ">="

	// OperatorLTE represents "<=" (less than or equal).
	OperatorLTE Operator = "<="

	// OperatorGT represents ">" (greater than).
	OperatorGT Operator = ">"

	// OperatorLT represents "<" (less than).
	OperatorLT Operator = "<"

	// OperatorEQ represents "==" (exact match).
	OperatorEQ Operator = "=="

	// OperatorNE represents "!=" (not equal).
	OperatorNE Operator = "!="

	// OperatorExact represents no operator (exact string match).
	OperatorExact Operator = ""
)

type ParsedConstraint

type ParsedConstraint struct {
	// Operator is the comparison operator (or empty for exact match).
	Operator Operator

	// Value is the expected value after the operator.
	Value string

	// IsVersionComparison indicates if this should be treated as a version comparison.
	IsVersionComparison bool
}

ParsedConstraint represents a parsed constraint expression.

func ParseConstraintExpression

func ParseConstraintExpression(expr string) (*ParsedConstraint, error)

ParseConstraintExpression parses a constraint value expression. Examples:

  • ">= 1.32.4" -> {Operator: ">=", Value: "1.32.4", IsVersionComparison: true}
  • "ubuntu" -> {Operator: "", Value: "ubuntu", IsVersionComparison: false}
  • "== 24.04" -> {Operator: "==", Value: "24.04", IsVersionComparison: true}

func (*ParsedConstraint) Evaluate

func (pc *ParsedConstraint) Evaluate(actual string) (bool, error)

Evaluate evaluates the constraint against an actual value. Returns true if the constraint is satisfied, false otherwise.

func (*ParsedConstraint) String

func (pc *ParsedConstraint) String() string

String returns a string representation of the parsed constraint.

type TightenOutcome

type TightenOutcome int

TightenOutcome reports how two same-named constraint expressions relate.

const (
	// TightenIncomparable means the pair is not a bounded version range on
	// both sides — an exact match, an equality or inequality term, an OR
	// alternative, or a value the version parser cannot read. Callers must
	// not merge these: "stricter" is undefined for them.
	TightenIncomparable TightenOutcome = iota

	// TightenUnchanged means the candidate admits everything the existing
	// expression already admits, so the intersection is the existing one.
	TightenUnchanged

	// TightenNarrowed means the returned expression is the intersection
	// restated, because a bound moved or a term had to be retained. It is
	// never wider than the existing expression, but it is not always
	// strictly narrower: two bounds can differ textually and admit the same
	// versions (">= 1.32 < 1.35" against "<= 1.34"), and restating them is
	// still correct.
	TightenNarrowed

	// TightenUnsatisfiable means the two expressions have no version in
	// common, so no cluster could satisfy both.
	TightenUnsatisfiable

	// TightenPrecisionMismatch means both sides bound the same direction but
	// are written at different precisions (">= 1.34" against ">= 1.34.1"), so
	// pkg/version compares them at the lower precision and reports them equal.
	// Which one is stricter is unknowable from the expressions alone.
	TightenPrecisionMismatch
)

func Tighten

func Tighten(existing, candidate string) (string, TightenOutcome)

Tighten intersects two same-named constraint expressions and returns the combined expression together with how it relates to existing.

Only bounded version ranges intersect: every term on both sides must use >=, >, <=, or < and neither side may carry an OR alternative. That covers the version floors and ceilings recipes actually compose (">= 1.34.1 < 1.36.0" tightened by ">= 1.35" yields ">= 1.35 < 1.36.0") while leaving predicates whose values do not order — the node-set label constraints, exact matches, "!=", and same-direction bounds written at different precisions — reported as TightenIncomparable so the caller keeps rejecting them rather than silently merging a contradiction.

The returned expression is written in the same grammar it was parsed from (a single AND clause), so it round-trips through ParseCompoundConstraint.

Jump to

Keyboard shortcuts

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