← Back to Pull requests

gen-apidocs: include create-only resources in the generated ToC #473

Open
abhinav-phi opened this pull request with 28 lines diff across 2 files
View on GitHub
abhinav-phi abhinav-phi commented on Sep 04, 2026 14:54 UTC
Author

Problem

With --auto-detect, IsTopLevelResource only recognized definitions that have a List operation ("Read Operations" category, List/List All Namespaces). Resources that only support Create were therefore never classified as top-level, and buildGroupBasedCategories dropped them entirely — their API group's category/directory and per-resource pages were never emitted.

This silently dropped two whole groups from the generated navigation (kubernetes/website#57313):

  • authentication.k8s.io: TokenRequest, TokenReview, SelfSubjectReview

  • authorization.k8s.io review types: SubjectAccessReview, LocalSubjectAccessReview, SelfSubjectAccessReview, SelfSubjectRulesReview
  • Both groups are listed on the generated group-versions.md page but had no pages. Up to the v1.35 docs these pages existed (e.g. /docs/reference/kubernetes-api/authentication-resources/token-request-v1/); the migration to the markdown backend removed them because the generator no longer produced them. Verified against the v1.36 spec: baseline run produces no authentication/ or authorization/ directory.

    Fix

    1. IsTopLevelResource also accepts a top-level Create operation ("Write Operations" category, Create type) as evidence of a top-level resource.
    2. In the TokenRequest special case (its operation is createCoreV1NamespacedServiceAccountToken, mapped in mapOperationsToDefinitions), o.Type is now set so the typed operation is recorded, but the write category is deliberately not appended to d.OperationCategories. IsTopLevelResource therefore does not see the Create operation, and TokenRequest keeps a definitions/ page instead of becoming an authentication resource page.

    Result

    With --auto-detect --backend=hugo-md on the v1.36 spec:

  • authentication/: _index.md, token-review-v1.md, self-subject-review-v1.md (TokenRequest stays on definitions/token-request-v1-authentication.md)

  • authorization/: _index.md, subject-access-review-v1.md, local-subject-access-review-v1.md, self-subject-access-review-v1.md, self-subject-rules-review-v1.md
  • matching the per-group structure of the other groups. The group _index/weights of existing groups shift by two steps (Authentication and Authorization now occupy their alphabetical slots); all page content of existing resources is unchanged.

    Notes

  • Downstream effect for kubernetes/website: after this lands, make apimd-hugo && copyapimd restores the missing group directories. The new resource pages for authentication/authorization land in content/en/docs/reference/kubernetes-api/authentication/ and .../authorization/.

  • Fixes kubernetes/website#57313 (together with a website-side commit syncing the regenerated content, coming separately).
  • Known gap this leaves

    Running the generator at this head shows the POST /api/v1/namespaces/{namespace}/serviceaccounts/{name}/token operation is not documented anywhere in the output: the definitions page for TokenRequest carries the object type and its spec and status fields but no API operations section, and no orphaned-operations page picks the endpoint up. It is a consequence of keeping TokenRequest off the top-level classification, so it is raised here rather than fixed silently. Happy to take whichever shape is agreed.

    AI assistance

    This PR description was revised with the assistance of generative AI. The code change is mine and I have read every line of the diff.

    Diff: 28 lines (+20 / -8) across 2 files Reviewer: Caesarsage Review duration: 131774m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.32) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.32 · 28 lines diff across 2 files
    0.32
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 5 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.201
    Real PR Conversation Timeline
    23 items
    kubernetes-prow[bot] kubernetes-prow[bot] commented on Sep 04, 2026 14:54 UTC
    Bot

    Welcome @abhinav-phi! <br><br>It looks like this is your first PR to <a href='https://github.com/kubernetes-sigs/reference-docs'>kubernetes-sigs/reference-docs</a> 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval. <br><br>You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation. <br><br>You can also check if kubernetes-sigs/reference-docs has its own contribution guidelines. <br><br>You may want to refer to our testing guide if you run into trouble with your tests not passing. <br><br>If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs! <br><br>Thank you, and welcome to Kubernetes. :smiley:

    kubernetes-prow[bot] kubernetes-prow[bot] commented on Sep 04, 2026 14:54 UTC
    Bot

    [APPROVALNOTIFIER] This PR is NOT APPROVED

    This pull-request has been approved by: <a href="https://github.com/kubernetes-sigs/reference-docs/pull/473#" title="Author self-approved">abhinav-phi</a>
    Once this PR has been reviewed and has the lgtm label, please assign natalisucks for approval. For more information see the Code Review Process.

    The full list of commands accepted by this bot can be found here.

    <details open>
    Needs approval from an approver in each of these files:

  • OWNERS
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":["natalisucks"]} -->

    Caesarsage Caesarsage commented on code on Sep 06, 2026 01:14 UTC
    Code Review
    gen-apidocs/generators/api/definition.go:1
    @@ -337,6 +337,8 @@ func (s *Definitions) FindNewestVersion(group, kind string) string {
     // IsTopLevelResource returns true if this definition represents a top-level
     // API resource (one that has its own List endpoint), as opposed to a
     // subresource (Scale, Eviction) or utility type (Status, WatchEvent).
    +// Some resources, such as TokenRequest and the SubjectAccessReview family,
    +// only support Create; they are top-level resources too.

    The first sentence is now the opposite of what the function does, and Binding (a pods/binding subresource) gets promoted by this change, so the "as opposed to a subresource" part doesn't hold either.

    // IsTopLevelResource reports whether this definition is a top-level API
    // resource, as opposed to a utility type (Status, WatchEvent) or a field type
    // that never appears on the wire by itself.
    //
    // A List endpoint is the usual marker, but create-only resources such as
    // TokenRequest and the SubjectAccessReview family qualify too.
    

    Caesarsage Caesarsage commented on code on Sep 06, 2026 01:16 UTC
    Code Review
    gen-apidocs/generators/api/definition.go:1
    @@ -351,6 +353,19 @@ func (d *Definition) IsTopLevelResource() bool {
     			}
     		}
     	}
    +	for _, c := range d.OperationCategories {
    +		if c == nil || c.Name != "Write Operations" {
    +			continue
    +		}
    +		for _, op := range c.Operations {
    +			if op == nil {
    +				continue
    +			}
    +			if op.Type.Name == "Create" {
    +				return true
    +			}
    +		}
    +	}

    
    

    There is no need for this extra loop.

    This can be one pass instead of walking OperationCategories twice, same conditions, same result:

    for _, c := range d.OperationCategories {
    		if c == nil {
    			continue
    		}
    		for _, op := range c.Operations {
    			if op == nil {
    				continue
    			}
    			switch c.Name {
    			case "Read Operations":
    				if op.Type.Name == "List" || op.Type.Name == "List All Namespaces" {
    					return true
    				}
    			case "Write Operations":
    				if op.Type.Name == "Create" {
    					return true
    				}
    			}
    		}
    	}
    	return false
    
    

    Caesarsage Caesarsage commented on code on Sep 06, 2026 01:17 UTC
    Code Review
    gen-apidocs/generators/api/config.go:1
    @@ -831,10 +831,14 @@ func (c *Config) mapOperationsToDefinitions() error {
     
     				o.Definition = d
     				o.Definition.InToc = true

    Now that the category is attached and buildGroupBasedCategories marks the definition, this line is redundant and it's the other half of what broke TokenRequest in the first place: it told writeDefinitions to skip the page while the missing category kept it out of the ToC, so it fell through both. Worth dropping so the fix reads on its own.

    Caesarsage Caesarsage commented on code on Sep 06, 2026 01:19 UTC
    Code Review
    gen-apidocs/generators/api/config.go:1

    nit: Since you're here, this XXX: reads like a bug marker but it's just describing the operation ID. Something like "TokenRequest is created through a ServiceAccount subresource, so its operation ID doesn't follow the ${group}${version}${resource} pattern the generic matcher below uses" would save the next person a detour

    Caesarsage Caesarsage commented on Sep 06, 2026 01:27 UTC
    Reviewer

    Nice find, the dropped OperationCategory is easy to miss. I reproduced your result on the v1.37 spec: both groups come back, 162 pages either way.

    Have you copied the output into k/website and built it locally?

    https://deploy-preview-57160--kubernetes-io-main-staging.netlify.app/docs/contribute/generate-ref-docs/release-generation/

    Mine failed with 18 REF_NOT_FOUND errors. Three of the admission-policy pages relref the definitions/ pages you're removing, so the website change needs to go in at the same time as this one.

    While you're there, look at the page you just created, authentication/token-request-v1.md. Its request body and all three responses link to the CSI driver page instead of to itself. There are two different TokenRequest kinds, authentication.v1 and storage.v1, and the link map in markdown.go is keyed on the kind name alone, so one overwrites the other. Same thing makes Pod's eviction op point at the wrong Eviction today. So this needs a fix in markdown.go as well. That can be a follow-up PR, but please hold this one until it lands.

    Last thing: the golden tests pass on your base. That golden came in with #460, which you're already on, so that note in the description can go.

    Left a few smaller things inline.

    abhinav-phi abhinav-phi commented on Sep 07, 2026 18:10 UTC
    Author

    Thanks @Caesarsage for the quick review and for reproducing it on the v1.37 spec — all four suggestions are applied in 9cecf0f:

  • IsTopLevelResource is now a single pass with a switch, and the doc comment no longer claims List-only resources are excluded (I re-verified the regenerated output after the rework: authentication/ and authorization/ still come out with the same pages, and Binding is still promoted to a resource page, consistent with the v1.35 docs where workload-resources/binding-v1 existed).

  • Dropped the redundant o.Definition.InToc = true — agreed, attaching the category is what places the definition, and the stray flag was exactly what made TokenRequest fall through both writeDefinitions and the ToC before.

  • Reworded the XXX: comment to explain the operation-ID mismatch.
  • To answer your question: I did not copy the output into k/website and build it there. Two reasons:

    1. Format drift between reference-docs master and the site. The content committed on kubernetes/website main still matches the hugo-md output as of the backend switch (May 13): import: string front matter, HTML tables, no preamble shortcodes. Master's output today (after 31032f6) emits code_import:, {{< api-object-preamble >}} and friends — and none of those shortcodes are defined in kubernetes/website's layouts (or docsy), so pasting master-format pages today would break the Hugo build. I checked this while preparing the link-repair PR (kubernetes/website#57414), which fixes the 16 stranded inbound links in the meantime; the full page restore should ride the next scheduled regen once the shortcode templates land on the site side (issue #440 territory).

    2. A pre-existing nondeterminism I hit while diffing runs (not from this change — reproduces on master without it, verified by stashing): two consecutive runs of --backend=hugo-md can differ in the core vs events category weights (130/140 swap in _index.md front matter) and in the ref-link disambiguation of the colliding event-v1.md filenames ({{< ref "event-v1#Event" >}} vs {{< ref "../events/event-v1#Event" >}} in core/event-v1.md). Looks like a map-iteration-order dependency in the link map / weight assignment when two groups have same-named filenames. Worth a follow-up issue since it will make every website regen PR noisy — happy to file and take a stab at it if you agree.

    abhinav-phi abhinav-phi commented on Sep 12, 2026 19:23 UTC
    Author

    @Caesarsage all four of your suggestions are applied in 9cecf0f (single-pass switch + corrected doc comment, redundant InToc dropped, XXX: comment reworded). I re-ran --backend=hugo-md on the v1.36 spec after the rework — authentication/ and authorization/ still emit with the same page set as before, so behavior is unchanged.

    Could I ask for a re-review when you have a moment? Happy to iterate on anything else.

    abhinav-phi abhinav-phi commented on Sep 18, 2026 08:57 UTC
    Author

    Hi @Caesarsage, a consolidated update since the re-review request, with one correction from my side.

    First, the golden-test caveat in the PR notes was inaccurate as worded, so I have corrected it in the description rather than leaving it. I ran TestWriteOperationGolden and TestWriteOperationGoldenHugoMode locally against both the merge-base and this PR head: both pass identically in both states, so no golden refresh is needed here. Whatever failure I saw earlier does not reproduce against current master, and I did not want a stale caveat confusing reviewers.

    Second, two items from my earlier note still sit outside this PR. The colliding event-v1.md ref-link nondeterminism I reported has no follow-up issue and no owner; I offered to file and attempt it, but only if you agree, so please tell me whether you want that filed (by me or someone else) or left alone. On the website side, the link-repair PR (kubernetes/website#57414) and the shortcode-templates track (issue #440) are both still open, so the coordinated regen and any website build validation remain blocked behind those and cannot be completed from here.

    Nothing in the code changed with this update. Could I ask for a re-review when you have a moment? Happy to iterate on anything else.

    abhinav-phi abhinav-phi commented on Oct 04, 2026 17:25 UTC
    Author

    hi @Caesarsage @natalisucks, friendly nudge.. all four of your suggestions from the sep 6 review went into 9cecf0f and nothing has changed since. could you re review when you have a moment?

    abhinav-phi abhinav-phi commented on code on Oct 05, 2026 23:44 UTC
    Code Review
    gen-apidocs/generators/api/definition.go:1
    @@ -337,6 +337,8 @@ func (s *Definitions) FindNewestVersion(group, kind string) string {
     // IsTopLevelResource returns true if this definition represents a top-level
     // API resource (one that has its own List endpoint), as opposed to a
     // subresource (Scale, Eviction) or utility type (Status, WatchEvent).
    +// Some resources, such as TokenRequest and the SubjectAccessReview family,
    +// only support Create; they are top-level resources too.

    applied in 9cecf0f68, definition.go:337-342 is your suggested block, the as opposed to a subresource clause is gone

    abhinav-phi abhinav-phi commented on code on Oct 05, 2026 23:44 UTC
    Code Review
    gen-apidocs/generators/api/definition.go:1
    @@ -351,6 +353,19 @@ func (d *Definition) IsTopLevelResource() bool {
     			}
     		}
     	}
    +	for _, c := range d.OperationCategories {
    +		if c == nil || c.Name != "Write Operations" {
    +			continue
    +		}
    +		for _, op := range c.Operations {
    +			if op == nil {
    +				continue
    +			}
    +			if op.Type.Name == "Create" {
    +				return true
    +			}
    +		}
    +	}

    applied in 9cecf0f68, definition.go:343-365 is the single pass with the switch, no second walk of OperationCategories

    abhinav-phi abhinav-phi commented on code on Oct 05, 2026 23:44 UTC
    Code Review
    gen-apidocs/generators/api/config.go:1
    @@ -831,10 +831,14 @@ func (c *Config) mapOperationsToDefinitions() error {
     
     				o.Definition = d
     				o.Definition.InToc = true

    applied in 9cecf0f68, the InToc line is deleted from the TokenRequest case, config.go:842 only attaches the category now

    abhinav-phi abhinav-phi commented on code on Oct 05, 2026 23:44 UTC
    Code Review
    gen-apidocs/generators/api/config.go:1

    applied in 9cecf0f68, config.go:819-821 carries your rewording and no XXX: is left in either file

    lmktfy lmktfy commented on code on Oct 09, 2026 12:54 UTC
    Code Review
    gen-apidocs/generators/api/definition.go:1
    @@ -334,20 +334,30 @@ func (s *Definitions) FindNewestVersion(group, kind string) string {
     	return newest
     }
     
    -// IsTopLevelResource returns true if this definition represents a top-level
    -// API resource (one that has its own List endpoint), as opposed to a
    -// subresource (Scale, Eviction) or utility type (Status, WatchEvent).
    +// IsTopLevelResource reports whether this definition is a top-level API
    +// resource, as opposed to a utility type (Status, WatchEvent) or a field type
    +// that never appears on the wire by itself.
    +//
    +// A List endpoint is the usual marker, but create-only resources such as
    +// TokenRequest and the SubjectAccessReview family qualify too.

    Well, TokenRequest isn't a top level resource. SubjectAccessReview is, though (and is already documented at https://kubernetes.io/docs/reference/kubernetes-api/definitions/subject-access-review-v1-authorization/).

    I think SubjectAccessReview could move to https://kubernetes.io/docs/reference/kubernetes-api/authorization/subject-access-review-v1-authorization/ because it is in the authorization.k8s.io API group.

    Caesarsage Caesarsage commented on code on Oct 11, 2026 17:10 UTC
    Code Review
    gen-apidocs/generators/api/config.go:1
    @@ -830,11 +832,14 @@ func (c *Config) mapOperationsToDefinitions() error {
     				}
     
     				o.Definition = d
    -				o.Definition.InToc = true
    +				o.Type = ot
     				if err := o.initExample(c); err != nil {
     					return fmt.Errorf("failed to init example: %w", err)
     				}
     				oc.Operations = append(oc.Operations, o)
    +				// Attach the category to the definition so IsTopLevelResource
    +				// and the resource pages see this Create operation.
    +				d.OperationCategories = append(d.OperationCategories, &oc)

    TokenRequest isn't top-level, so don't attach the category here. Keep the InToc removal so it still lands in definitions/.

    Caesarsage Caesarsage commented on code on Oct 11, 2026 17:12 UTC
    Code Review
    gen-apidocs/generators/api/definition.go:1
    @@ -334,20 +334,30 @@ func (s *Definitions) FindNewestVersion(group, kind string) string {
     	return newest
     }
     
    -// IsTopLevelResource returns true if this definition represents a top-level
    -// API resource (one that has its own List endpoint), as opposed to a
    -// subresource (Scale, Eviction) or utility type (Status, WatchEvent).
    +// IsTopLevelResource reports whether this definition is a top-level API
    +// resource, as opposed to a utility type (Status, WatchEvent) or a field type
    +// that never appears on the wire by itself.
    +//
    +// A List endpoint is the usual marker, but create-only resources such as
    +// TokenRequest and the SubjectAccessReview family qualify too.

    // A List endpoint is the usual marker, but create-only resources such as
    // TokenReview and the SubjectAccessReview family qualify too.
    

    Caesarsage Caesarsage commented on Oct 11, 2026 17:12 UTC
    Reviewer

    +1 to @lmktfy. TokenRequest is the body of the serviceaccounts/token subresource, not a resource of its own, so it should stay in definitions/. Two small changes inline. Please check that TokenRequest still gets a page in definitions/ after them.

    With that change, this PR no longer causes the TokenRequest link collision, so I'll track the markdown.go fix separately rather than block on it.

    abhinav-phi abhinav-phi commented on code on Oct 16, 2026 20:53 UTC
    Code Review
    gen-apidocs/generators/api/config.go:1
    @@ -830,11 +832,14 @@ func (c *Config) mapOperationsToDefinitions() error {
     				}
     
     				o.Definition = d
    -				o.Definition.InToc = true
    +				o.Type = ot
     				if err := o.initExample(c); err != nil {
     					return fmt.Errorf("failed to init example: %w", err)
     				}
     				oc.Operations = append(oc.Operations, o)
    +				// Attach the category to the definition so IsTopLevelResource
    +				// and the resource pages see this Create operation.
    +				d.OperationCategories = append(d.OperationCategories, &oc)

    dropped the append in 0f2064c, both comment lines with it. the InToc removal stays so TokenRequest still lands in definitions/.

    ran the generator on the v1.36 spec at both heads rather than assume it. before, TokenRequest came out as authentication/token-request-v1.md. now it is definitions/token-request-v1-authentication.md, so it does keep a definitions page. authentication/ goes from four resource pages to three, authorization/ is unchanged.

    one cost though, and I would rather put it in front of you than decide it myself. the definitions page carries the object type and spec and status but no api operations section, so the POST /api/v1/namespaces/{namespace}/serviceaccounts/{name}/token call ends up documented nowhere in the output. nothing picks it up as an orphaned operation either. that follows directly from keeping TokenRequest off the top level classification.

    abhinav-phi abhinav-phi commented on code on Oct 16, 2026 20:53 UTC
    Code Review
    gen-apidocs/generators/api/definition.go:1
    @@ -334,20 +334,30 @@ func (s *Definitions) FindNewestVersion(group, kind string) string {
     	return newest
     }
     
    -// IsTopLevelResource returns true if this definition represents a top-level
    -// API resource (one that has its own List endpoint), as opposed to a
    -// subresource (Scale, Eviction) or utility type (Status, WatchEvent).
    +// IsTopLevelResource reports whether this definition is a top-level API
    +// resource, as opposed to a utility type (Status, WatchEvent) or a field type
    +// that never appears on the wire by itself.
    +//
    +// A List endpoint is the usual marker, but create-only resources such as
    +// TokenRequest and the SubjectAccessReview family qualify too.

    took the suggestion text in 0f2064c, the comment names TokenReview now.

    on SubjectAccessReview, it already comes out under authorization/ once the classification fix lands, so authorization/ gets subject-access-review-v1.md, local-subject-access-review-v1.md, self-subject-access-review-v1.md and self-subject-rules-review-v1.md. If you meant moving it somewhere past that, tell me and I will do it here or as a follow up.

    the open question is where TokenRequest itself should sit, since the answer here and the one on website#57313 point different ways. I have put both readings on that issue and will build whichever gets picked.

    abhinav-phi abhinav-phi commented on Nov 07, 2026 09:29 UTC
    Author

    friendly ping @lmktfy @Caesarsage — i addressed all four suggestions from sep 6 plus the TokenRequest placement question in the oct 16 commits. happy to rebase onto master if that helps. thanks.

    Reviewers
    Assignees
    abhinav-phi abhinav-phi
    Labels
    risk: low confidence: 89.3% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 31.6% (LOW) · Higher = larger blast radius
    31.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.3% · Lower = rushed or shallow review
    89.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.4% · Safe to merge
    3.4%