compat

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package crdcompat provides functionality for comparing Kubernetes CustomResourceDefinition schemas and identifying breaking and non-breaking changes.

The package analyzes OpenAPI v3 schemas from CRDs and generates detailed reports about compatibility issues. It's designed to prevent accidental schema changes that would break existing CRD instances.

Breaking changes detected include:

  • Property removal
  • Type changes
  • Adding required fields
  • Restricting enum values
  • Pattern changes

Non-breaking changes detected include:

  • Adding new properties that are not required
  • Expanding enum values
  • Changing descriptions
  • Changing default values
  • Removing optional fields from 'required' list

Usage:

// Get existing and new CRD objects
oldCRD, newCRD := getCRDs()

// Compare schemas
report, err := crdcompat.CompareVersions(oldCRD.Spec.Versions, newCRD.Spec.Versions)
if err != nil {
    // Handle error
}

// Check for breaking changes
if report.HasBreakingChanges() {
    log.Fatalf("Breaking changes detected: %s", report)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	// Path is the JSON path to the changed property
	Path string
	// ChangeType is the type of change
	ChangeType ChangeType
	// OldValue is the string representation of the old value (if applicable)
	OldValue string
	// NewValue is the string representation of the new value (if applicable)
	NewValue string
}

Change represents a single schema change

func (Change) Description

func (c Change) Description() string

Description generates a human-readable description based on the change type

type ChangeType

type ChangeType string

ChangeType represents the type of schema change

const (
	// Breaking change types
	PropertyRemoved        ChangeType = "PROPERTY_REMOVED"
	TypeChanged            ChangeType = "TYPE_CHANGED"
	RequiredAdded          ChangeType = "REQUIRED_ADDED"
	EnumRestricted         ChangeType = "ENUM_RESTRICTED"
	PatternChanged         ChangeType = "PATTERN_CHANGED"
	PatternAdded           ChangeType = "PATTERN_ADDED"
	RequiredDefaultRemoved ChangeType = "REQUIRED_DEFAULT_REMOVED"

	// Breaking - constraint tightened
	MinimumAdded       ChangeType = "MINIMUM_ADDED"
	MinimumIncreased   ChangeType = "MINIMUM_INCREASED"
	MaximumAdded       ChangeType = "MAXIMUM_ADDED"
	MaximumDecreased   ChangeType = "MAXIMUM_DECREASED"
	MinLengthAdded     ChangeType = "MIN_LENGTH_ADDED"
	MinLengthIncreased ChangeType = "MIN_LENGTH_INCREASED"
	MaxLengthAdded     ChangeType = "MAX_LENGTH_ADDED"
	MaxLengthDecreased ChangeType = "MAX_LENGTH_DECREASED"
	MinItemsAdded      ChangeType = "MIN_ITEMS_ADDED"
	MinItemsIncreased  ChangeType = "MIN_ITEMS_INCREASED"
	MaxItemsAdded      ChangeType = "MAX_ITEMS_ADDED"
	MaxItemsDecreased  ChangeType = "MAX_ITEMS_DECREASED"

	// Non-breaking change types
	PropertyAdded      ChangeType = "PROPERTY_ADDED"
	DescriptionChanged ChangeType = "DESCRIPTION_CHANGED"
	DefaultChanged     ChangeType = "DEFAULT_CHANGED"
	RequiredRemoved    ChangeType = "REQUIRED_REMOVED"
	EnumExpanded       ChangeType = "ENUM_EXPANDED"
	PatternRemoved     ChangeType = "PATTERN_REMOVED"

	// Non-breaking - constraint relaxed
	MinimumRemoved     ChangeType = "MINIMUM_REMOVED"
	MinimumDecreased   ChangeType = "MINIMUM_DECREASED"
	MaximumRemoved     ChangeType = "MAXIMUM_REMOVED"
	MaximumIncreased   ChangeType = "MAXIMUM_INCREASED"
	MinLengthRemoved   ChangeType = "MIN_LENGTH_REMOVED"
	MinLengthDecreased ChangeType = "MIN_LENGTH_DECREASED"
	MaxLengthRemoved   ChangeType = "MAX_LENGTH_REMOVED"
	MaxLengthIncreased ChangeType = "MAX_LENGTH_INCREASED"
	MinItemsRemoved    ChangeType = "MIN_ITEMS_REMOVED"
	MinItemsDecreased  ChangeType = "MIN_ITEMS_DECREASED"
	MaxItemsRemoved    ChangeType = "MAX_ITEMS_REMOVED"
	MaxItemsIncreased  ChangeType = "MAX_ITEMS_INCREASED"
)

type Report

type Report struct {
	// BreakingChanges are changes that break backward compatibility
	BreakingChanges []Change
	// NonBreakingChanges are changes that maintain backward compatibility
	NonBreakingChanges []Change
}

Report contains the full analysis of schema differences

func Compare

func Compare(oldSchema, newSchema *v1.JSONSchemaProps) *Report

Compare compares two OpenAPIV3Schema objects and returns a compatibility report. It identifies breaking and non-breaking changes between the schemas.

func CompareVersions

func CompareVersions(oldVersions, newVersions []v1.CustomResourceDefinitionVersion) (*Report, error)

CompareVersions compares CRD versions and returns a compatibility report. This is a convenience wrapper that extracts schemas from version slices. It expects exactly one version in each slice.

func (*Report) AddBreakingChange

func (r *Report) AddBreakingChange(path string, changeType ChangeType, oldValue, newValue string)

AddBreakingChange adds a breaking change to the result with automatically generated description

func (*Report) AddNonBreakingChange

func (r *Report) AddNonBreakingChange(path string, changeType ChangeType, oldValue, newValue string)

AddNonBreakingChange adds a non-breaking change to the result with automatically generated description

func (*Report) HasBreakingChanges

func (r *Report) HasBreakingChanges() bool

HasBreakingChanges returns true if breaking changes were detected

func (*Report) HasChanges

func (r *Report) HasChanges() bool

HasChanges returns true if any changes were detected

func (*Report) IsCompatible

func (r *Report) IsCompatible() bool

IsCompatible returns true if no breaking changes were detected

func (*Report) String

func (r *Report) String() string

SummarizeBreakingChanges returns a user-friendly summary of breaking changes

Jump to

Keyboard shortcuts

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