Skip to content

Instantly share code, notes, and snippets.

View jpbetz's full-sized avatar

Joe Betz jpbetz

  • Google
  • Annapolis, MD
View GitHub Profile

Adding cross validation support to validation-gen

Overview

This document outlines recommendations for adding cross-field validation capabilities to the existing validation-gen framework in Kubernetes. By enhancing the current tag-based approach with more expressive validation constructs, we can enable complex validation scenarios while maintaining backward compatibility.

Design Principles

  1. Declarative Validation: Express complex validation rules through declarative tags
  2. Type-Safety: Leverage Go's type system for validation definitions

Kubernetes API Validation Rules - Migration Notes

Resource Analysis for Validation Migration

After analyzing the comprehensive validation rules from all Kubernetes resources, I've identified several resource kinds with diverse validation rules, particularly focusing on those with cross-field validations. These resources are prioritized based on both validation diversity and relative simplicity.

Prioritized Resources for Validation Migration

1. Service Resource

Kubernetes API Validation Rules

Introduction

Kubernetes API validation ensures that objects created or updated through the API server meet specific formatting requirements, constraints, and semantic rules before being stored in etcd. Validation occurs in multiple layers:

  1. OpenAPI Schema Validation - Validates basic structure and data types
  2. AdmissionWebhooks - Allow custom validation via webhooks
  3. Built-in Validation - The internal logic specific to each resource type
  4. CEL Validation Rules - For CustomResourceDefinitions
@jpbetz
jpbetz / kubernetes-validations.md
Last active February 4, 2025 22:34
Some high level analysis of kubernetes validations

Admissionregistration API Group Validations (from pkg/apis/admissionregistration/validation/validation.go)

Validation Name JSON Path Validation Type Error Types Cross-field Update-specific
ResourceValidation spec.resources array-unique, non-empty Required, Invalid No No
MatchConditionsValidation spec.matchConditions maxItems=64, unique-names TooMany, Duplicate No No
ValidateParamKind spec.paramKind.apiVersion format=dns1123subdomain Required, Invalid No No
ValidateParamKind spec.paramKind.kind format=dns1035label Required, Invalid No No
MatchResourcesValidation spec.matchResources.matchPolicy enum=Exact,Equivalent Required, NotSupported No No
@jpbetz
jpbetz / squash.sh
Last active January 27, 2025 20:38
#!/bin/bash
# Check if input file is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <input-file>"
exit 1
fi
input_file="$1"
@jpbetz
jpbetz / benchstats.txt
Last active January 23, 2025 16:47
Benchmarking structured-merge-diff for unsetting fields
goos: linux
goarch: amd64
pkg: sigs.k8s.io/structured-merge-diff/v4/fieldpath
cpu: Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
│ old.txt │ new.txt │
│ sec/op │ sec/op vs base │
FieldSet/insert-20-8 12.02µ ± ∞ ¹ 11.00µ ± ∞ ¹ ~ (p=1.000 n=1) ²
FieldSet/has-20-8 434.6n ± ∞ ¹ 541.8n ± ∞ ¹ ~ (p=1.000 n=1) ²
FieldSet/serialize-20-8 6.667µ ± ∞ ¹ 8.090µ ± ∞ ¹ ~ (p=1.000 n=1) ²
FieldSet/deserialize-20-8 22.98µ ± ∞ ¹ 21.10µ ± ∞ ¹ ~ (p=1.000 n=1) ²
@jpbetz
jpbetz / main.go
Created October 8, 2024 23:56
Go lambda hack
package main
import (
"fmt"
)
// An interface can be implemented by a function type definition.
// This can be convenient for allowing inline function declarations
// of interfaces with only a single function.
@jpbetz
jpbetz / after
Last active August 30, 2024 01:17
hack/update-codegen.sh: FormatOnly change performance
+++ [0829 20:58:12] Generating protobufs for 70 targets
+++ [0829 20:58:12] protoc 23.4 not found (can install with hack/install-protoc.sh); generating containerized...
+++ [0829 20:58:12] Verifying Prerequisites....
+++ [0829 20:58:13] Building Docker image kube-build:build-41f60316fe-5-v1.31.0-go1.22.5-bullseye.0
+++ [0829 20:58:14] Syncing sources to container
+++ [0829 20:58:23] Output from this container will be rsynced out upon completion. Set KUBE_RUN_COPY_OUTPUT=n to disable.
+++ [0829 20:58:23] Running build command...
+++ [0829 20:58:48] Syncing out of container
+++ [0829 20:58:51] Generating deepcopy code for 254 targets
+++ [0829 20:59:02] Generating swagger for 58 targets
@jpbetz
jpbetz / _REPRODUCTION.md
Last active March 8, 2024 18:19
CRD with invalid conversion webhook CRBundle fails on reads and writes for versions not requiring conversion

What happens:

Create a mult-version CRD with a conversion webhook configured with an invalid CABundle:

$ kubectl apply -f crd.yaml
customresourcedefinition.apiextensions.k8s.io/replicant.stable.example.com created

Read the CRD back:

# config for 1 control plane node and 2 workers (necessary for conformance)
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
ipFamily: ipv4
kubeProxyMode: iptables
# don't pass through host search paths
# TODO: possibly a reasonable default in the future for kind ...
dnsSearch: []
nodes: