hypershift / api
AGENTS.md551 starsChanged 6 months agoApache-2.0View on GitHub
This module (github.com/openshift/hypershift/api) is consumed by external Go clients that vendor it directly. Changes to types in this module affect not only the HyperShift operator but also downstream consumers that embed, serialize, and deserialize these types independently of the Kubernetes API server. These are non-obvious behaviors of the Kubernetes API machinery that affect how CRD types in api/ must be written. These are not style preferences or conventions — they are fundamental facts and reasoning about how the system works.
Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about code style.
Of the AGENTS.md files we read, the 31,774 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository26 other files it gives its agents.
- AGENTS.mdAGENTS.md
- control-plane-operator/AGENTS.mdAGENTS.md
- .cursor/rules/100-go-mistakes.mdcCursor rule
- .cursor/rules/code-formatting.mdcCursor rule
- .cursor/rules/code-review.mdcCursor rule
- .cursor/rules/create-jira-items.mdcCursor rule
- .cursor/rules/debug-aro-hcp-e2e.mdcCursor rule
- .cursor/rules/git-commit-format.mdcCursor rule
- .cursor/rules/restructure-commits.mdcCursor rule
- create-cpo-overrideSkill
- Debug ClusterSkill
- e2e-analyzeSkill
- Effective GoSkill
- feature-developmentSkill
- fix-hypershift-repo-robot-prSkill
- Git Commit FormatSkill
- konflux-buildSkill
- Konflux Archived PipelineRunsSkill
- pr-reportSkill
- restructure-commitsSkill
- test-tag-pipelineSkill
- triage-leaked-infraSkill
- update-konflux-tasksSkill
- validate-pr-override-imagesSkill
- Git Commit FormatSkill
- restructure-commitsSkill
The file
We split this AGENTS.md into the rules it gives an agent
11 rules. About 0 are common, 9 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈9
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 15
Use feature gates for experimental functionality
- Distinctive: line 39
Ratcheting validation: when adding new validation to existing fields, verify that existing clusters with values that predate the new …
- Distinctive: line 47
When graduating a field from overflow to a typed struct field:
- Distinctive: line 57
Match the Go type from upstream kubelet/Karpenter: Use the same Go type (pointer vs value, map key/value types) to keep the mapping trivial …
- Very specific: line 72
Use api/karpenter/v1/karpenter_types.go and api/hypershift/v1beta1/etcdbackup_types.go as examples of best practices and patterns.
- Distinctive: line 74
Don't use the other existing APIs as examples as they might have many legacy constraints.
- Distinctive: line 82
Every change to an API type must be safe for both:
- Distinctive: line 93
Changing a field's type: Ensure the JSON representation is compatible in both directions.
- Distinctive: line 97
When modifying API types, add serialization compatibility tests that:
- Very specific: line 106
All API CEL validations must be covered with envtests, see test/envtest/README.md for details
- Distinctive: line 120
All these must pass for any change before creating a PR
≈ “Common” is an estimate: at least 1 in 5 of the 40,246 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
HyperShift API Module
This module (github.com/openshift/hypershift/api) is consumed by external Go clients that vendor it directly. Changes to types in this module affect not only the HyperShift operator but also downstream consumers that embed, serialize, and deserialize these types independently of the Kubernetes API server.
CRD API Machinery Fundamentals
These are non-obvious behaviors of the Kubernetes API machinery that affect how CRD types in api/ must be written. These are not style preferences or conventions — they are fundamental facts and reasoning about how the system works.
For conventions, always trust the kube-api-linter (make api-lint-fix). Do not question its findings and do not add exclusions unless explicitly told to by a human reviewer. The linter encodes the OpenShift API conventions (authoritative) and upstream Kubernetes API conventions (informational, downstream wins on conflicts). Do not fetch or read those documents — the linter already enforces them, and the actionable rules are captured in this file.
API Versioning
- APIs primarily in v1beta1
- Any new API should GA as v1
- Use feature gates for experimental functionality
- CRD generation via controller-gen with OpenShift-specific tooling
Serialization
- Always set
omitemptyoromitzeroon every field, regardless of whether it is+requiredor+optional.omitempty/omitzerotags control serialization, not validation.+requiredis a schema constraint enforced at admission time; the serialization tag controls what goes on the wire. Without a tag, a zero-value field serializes as an explicit value (e.g.,"pullSecret": {"name": ""}), which makes the API server unable to distinguish "not set" from "explicitly set to empty." This breaks defaulting, server-side apply field ownership, and strategic merge patch — all of which rely on field absence to mean "don't touch this." Additionally, without omission a structured client serializes the empty object, which passes the+requiredcheck (based on key presence) without validating the value — so a user can forget to set a required field, it passes admission, and the reader sees a required field with an unexpected empty value. omitemptydoes nothing for non-pointer structs. Onlyomitzerocorrectly omits a struct field when it equals its zero value. This is a Go encoding/json behavior, not a Kubernetes convention. Useomitemptyfor scalar fields (string, int, bool) and slices/maps. Useomitzerofor struct fields (available since Go 1.24; this repo requires Go 1.25+).- The only reason to use a pointer in a CRD is when the zero value is a valid, distinct user choice. If the struct has a required field,
{}can never be valid user input, so there is no ambiguity to resolve and no pointer is needed.omitzeroon a non-pointer struct will correctly omit the key from serialized output.MinProperties/MaxPropertieson the parent counts serialized keys — it has no concept of whether the Go field is a pointer. // +defaultmust be paired with// +optionalbecause the required check runs before defaulting. A required field with a default will be rejected before the default is ever applied.
Validation Execution
- OpenAPI schema validation only runs when a key is present in the serialized object. if a field is omitted, the validation never executes. This is why
MinLength=1on an optional field is safe: the constraint only fires when the user actually provides a value. - Optionality and min constraints are independent concerns. An optional field with
MinLength=1means "you don't have to set this, but if you do, it can't be empty." These do not conflict. - Admission-time validation rejects the write immediately. Controller-time validation accepts the write, the user assumes success, and discovers the problem later via conditions or logs. Always prefer admission-time via CEL over controller-time validation.
- Use CEL rules, not webhooks, for new validation. HyperShift deliberately avoids admission webhooks because they add latency (especially in hosted-on-hosted topologies like IBM Cloud where requests traverse konnectivity), require TLS cert management, and couple resource availability to a running webhook service. The existing webhook is scoped to KubeVirt platform defaults and is not a general extension point. Add new validation as CEL markers on the API types in this module.
Immutability
- Immutable + optional creates a two-step bypass. A user can (1) remove the optional field, then (2) re-add it with a different value. To prevent this, add a CEL rule at the parent level that forbids removing the field once set:
oldSelf.has(field) ? self.has(field) : true. - "Once set, cannot be removed" and "once set, cannot be changed" are separate constraints. You typically need both together, and they require separate CEL rules.
Defaulting and Transitions
- Ratcheting validation: when adding new validation to existing fields, verify that existing clusters with values that predate the new validation can still be updated. CRD validation ratchets (allows unchanged invalid values through), but only for fields that are literally unchanged in the update.
KubeletConfiguration Field Graduation (Karpenter)
The KubeletConfiguration type in api/karpenter/v1/kubelet_config.go accepts arbitrary kubelet
configuration fields via a PreserveUnknownFields schema and a runtime.RawExtension overflow
mechanism. A subset of these fields are promoted to explicitly typed struct fields with CEL validation.
When graduating a field from overflow to a typed struct field:
- Match upstream kubelet's field name and JSON tag exactly. The primary compatibility target is
k8s.io/kubelet/config/v1beta1.KubeletConfiguration. Users today set kubelet fields that land in our overflow using kubelet field names. Changing a name would be a breaking API change requiring a new API version, because existing serialized data (and user-supplied YAML) uses the kubelet name. - Use upstream Karpenter as a secondary reference. Upstream Karpenter
(
github.com/aws/karpenter-provider-aws/pkg/apis/v1.KubeletConfiguration) currently mirrors kubelet field names. If Karpenter ever diverges from kubelet naming, follow kubelet and add a translation layer inkarpenterKubeletConfigurationFromNodeClassSpec(). - Match the Go type from upstream kubelet/Karpenter. Use the same Go type (pointer vs value, map
key/value types) to keep the mapping trivial and avoid serialization incompatibilities. Minor
differences (e.g., bare
int32vs*int32) are acceptable only when they don't change the JSON representation. - Add stricter CEL validation as needed. Admission-time validation does not affect serialization
compatibility. We can add constraints beyond what upstream provides (e.g.,
Minimum=1instead ofMinimum=0, cross-field rules likeimageGCHigh > imageGCLow) without breaking the API. - Do not rename fields for OpenShift conventions. Normally OpenShift API conventions may prefer different naming. For KubeletConfiguration fields, upstream kubelet compatibility takes precedence over convention to avoid breaking changes.
API Type Change Guidelines
Best Practices and Patterns
Use api/karpenter/v1/karpenter_types.go and api/hypershift/v1beta1/etcdbackup_types.go as examples of best practices and patterns.
Don't use the other existing APIs as examples as they might have many legacy constraints.
Field Grouping
When multiple fields on a spec share a common prefix or relate to the same feature, they MUST be grouped into a dedicated struct. Top-level specs like HostedClusterSpec and NodePoolSpec should only contain fields that are independently meaningful. If removing one field would make another field meaningless, they belong together in a sub-struct. A common signal is fields that share a name prefix (e.g., BarEndpoint, BarConfig, BarID all relate to "Bar" and should be a single Bar field with a BarSpec struct).
N-1 and N+1 Compatibility
Every change to an API type must be safe for both:
- N+1 (forward): New code reading data written by old code
- N-1 (rollback): Old code reading data written by new code
This matters because consumers like ARO-HCP embed these types directly into their own structs and serialize them to storage (e.g., Cosmos DB) outside CRD validation. If a consumer must revert to a previous code level, they need to deserialize data that was written by the newer version without errors or data corruption.
Common Pitfalls
- Changing a value type to a pointer (e.g.,
int32to*int32): Withoutomitempty, a nil pointer serializes asnull, which cannot be deserialized back into the non-pointer type. Always pair pointer types withomitemptyon required fields. - Removing or renaming a field: Old code will fail to deserialize the new format if it expects the field.
- Changing a field's type: Ensure the JSON representation is compatible in both directions.
Required Tests
When modifying API types, add serialization compatibility tests that:
- Define a struct matching the previous version of the type
- Serialize the current type and verify the previous version can deserialize it
- Serialize the previous type and verify the current version can deserialize it
- Cover edge cases: zero values, nil pointers, omitted fields
See api/hypershift/v1beta1/nodepool_types_test.go for an example of this pattern.
All API CEL validations must be covered with envtests, see test/envtest/README.md for details
Key make targets for API work
make api # Regenerate all CRDs, deepcopy, clients
make api-lint-fix # Run API linter and auto-fix violations
make verify # Full verification (includes api, fmt, vet, lint)
make update # Full update (api-deps, workspace-sync, deps, api, api-docs, clients)
make test-envtest-ocp # Run envtest for CEL validations
# To target a specific k8s version, check api/go.mod (e.g. k8s.io/api v0.35.1 → 1.35):
# ENVTEST_OCP_K8S_VERSIONS=1.35.0 make test-envtest-ocpAll these must pass for any change before creating a PR
Cluster Capabilities
The OptionalCapability type and Capabilities struct in api/hypershift/v1beta1/hostedcluster_types.go control which OpenShift operators are deployed in the hosted control plane. When adding a new capability constant or modifying CEL validation rules on the Capabilities struct, see Adding a New Cluster Capability for the full implementation checklist and reference PRs.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

