How kubernetes/kube-openapi works
Project type and purpose
• This is a Go-based CLI tool (“Kube OpenAPI”) that discovers and generates OpenAPI specs for Kubernetes-style APIs.
• It consists of two main functional areas outlined in the README:
• Model generator: parses Go source to extract type definitions.
• Spec generator: combines routes or existing JSON/YAML specs into a final OpenAPI document.
• There are two CLI front-ends: openapi-gen and openapi2smd, plus conversion subcommands and test harnesses under cmd/.
File structure
• cmd/…: entrypoints (openapi-gen, openapi2smd).
• pkg/…: the heart of the tool, split into subpackages:
• aggregator: merges multiple OpenAPI fragments
• builder & builder3: turn Go AST/markers into schema definitions
• handler & handler3: HTTP/webservice route introspection
• generators: lints, enforces rules, emits JSON/YAML
• openapiconv, schemaconv, schemamutation: convert between spec versions, mutate schemas
• spec3: in-memory representation of OpenAPI v3
• validation: verify schema/JSON conformance
• util & cached & common: shared abstractions, caching, JSON adapter, RESTful route adapter
• internal/third_party: vendored JSON and validator libraries
• hack/: scripts to update libraries
• test/ & pkg/*_test.go: unit and integration tests
Key interactions & dependencies (from README + imports)
• CLI layer (cmd/) invokes:
• Model generation: builder → handler → spec3 models
• Aggregation: aggregator walks spec fragments
• Conversion: openapiconv, schemaconv
• Validation: validation packages run post-generation checks
• Utilities provide JSON serialization/parsing and caching.
• Third-party libs live in internal/third_party and feed into JSON marshalling.