Skip to content

Conversation

@rikatz
Copy link
Member

@rikatz rikatz commented Sep 4, 2025

Kubernetes resources should bump their generation every time a field on spec is changed. This allows controllers to keep track of the desired state and the current state.

This change:

  • initializes the generation of a resource if it does not exist
  • updates the generation of a resource when a field on spec is changed
  • adds some unit tests to assure the desired behavior

Summary by CodeRabbit

  • New Features

    • Route resources now initialize Generation to 1 on creation and increment only when the spec changes.
  • Bug Fixes

    • Metadata or status updates no longer affect Route Generation, improving observability and controller workflows that track spec changes.
  • Tests

    • Added unit tests validating generation behavior across create/update scenarios and hostname allocation during validation.

Kubernetes resources should bump their generation every time
a field on spec is changed. This allows controllers to keep track
of the desired state and the current state.

This change:
* initializes the generation of a resource if it does not exist
* updates the generation of a resource when a field on spec is changed
* adds some unit tests to assure the desired behavior
@openshift-ci-robot openshift-ci-robot added jira/severity-moderate Referenced Jira bug's severity is moderate for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Sep 4, 2025
@coderabbitai
Copy link

coderabbitai bot commented Sep 4, 2025

Walkthrough

Implements generation tracking for Route resources: set Generation=1 on create and increment Generation on Spec changes during updates; metadata or status-only changes do not affect Generation. Adds unit tests validating allocation, initial generation, metadata-no-op, and spec-change increment.

Changes

Cohort / File(s) Summary
Route generation handling
pkg/route/apiserver/registry/route/strategy.go
Add import k8s.io/apimachinery/pkg/api/equality; set Generation = 1 in PrepareForCreate; in PrepareForUpdate use apiequality.Semantic.DeepEqual to compare old.Spec vs new.Spec and increment Generation only when Spec differs; add clarifying comments.
Unit tests for generation
pkg/route/apiserver/registry/route/strategy_test.go
Add TestRouteGenerationManagement: sets up mocked hostname allocator and permissive SAR; verifies hostname allocation, Generation is 0 before create, PrepareForCreate sets Generation=1, metadata-only updates do not bump generation, and a Spec update increments generation to 2.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    actor Client
    participant Strategy as Route Strategy
    participant Alloc as Hostname Allocator

    rect rgb(240,248,255)
    note over Client,Strategy: Create Route
    Client->>Strategy: PrepareForCreate(route)
    Strategy->>Alloc: AllocateHostname(route)
    Alloc-->>Strategy: mygeneratedhost.com
    Strategy-->>Client: route.Generation = 1
    end

    rect rgb(245,245,245)
    note over Client,Strategy: Update Route
    Client->>Strategy: PrepareForUpdate(newRoute, oldRoute)
    Strategy->>Strategy: Semantic.DeepEqual(oldRoute.Spec, newRoute.Spec)
    alt Specs equal
        Strategy-->>Client: Keep Generation (unchanged)
    else Specs differ
        Strategy-->>Client: newRoute.Generation = oldRoute.Generation + 1
    end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I twitch my whiskers, routes align,
A spec-shift nudges gen by design.
Labels stay, status stays the same,
Only spec changes win the game.
Hop—tests pass, and hosts proclaim. 🥕


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 8f06fc0 and 2407878.

📒 Files selected for processing (2)
  • pkg/route/apiserver/registry/route/strategy.go (3 hunks)
  • pkg/route/apiserver/registry/route/strategy_test.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • pkg/route/apiserver/registry/route/strategy_test.go
  • pkg/route/apiserver/registry/route/strategy.go
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@openshift-ci-robot
Copy link

@rikatz: This pull request references Jira Issue OCPBUGS-61228, which is invalid:

  • expected the bug to target the "4.21.0" version, but no target version was set

Comment /jira refresh to re-evaluate validity if changes to the Jira bug are made, or edit the title of this pull request to link to a different bug.

The bug has been updated to refer to the pull request using the external bug tracker.

Details

In response to this:

Kubernetes resources should bump their generation every time a field on spec is changed. This allows controllers to keep track of the desired state and the current state.

This change:

  • initializes the generation of a resource if it does not exist
  • updates the generation of a resource when a field on spec is changed
  • adds some unit tests to assure the desired behavior

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@rikatz
Copy link
Member Author

rikatz commented Sep 4, 2025

/jira refresh

@openshift-ci-robot openshift-ci-robot added jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. and removed jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Sep 4, 2025
@openshift-ci-robot
Copy link

@rikatz: This pull request references Jira Issue OCPBUGS-61228, which is valid. The bug has been moved to the POST state.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (4.21.0) matches configured target version for branch (4.21.0)
  • bug is in the state New, which is one of the valid states (NEW, ASSIGNED, POST)

Requesting review from QA contact:
/cc @gangwgr

Details

In response to this:

/jira refresh

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci openshift-ci bot requested a review from gangwgr September 4, 2025 16:53
@openshift-ci-robot
Copy link

@rikatz: This pull request references Jira Issue OCPBUGS-61228, which is valid.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (4.21.0) matches configured target version for branch (4.21.0)
  • bug is in the state POST, which is one of the valid states (NEW, ASSIGNED, POST)

Requesting review from QA contact:
/cc @gangwgr

Details

In response to this:

Kubernetes resources should bump their generation every time a field on spec is changed. This allows controllers to keep track of the desired state and the current state.

This change:

  • initializes the generation of a resource if it does not exist
  • updates the generation of a resource when a field on spec is changed
  • adds some unit tests to assure the desired behavior

Summary by CodeRabbit

  • New Features
  • Route resources now initialize Generation to 1 on creation and increment it only when the spec changes.
  • Bug Fixes
  • Metadata or status updates no longer affect Route Generation, improving observability and controller workflows that track spec changes.
  • Tests
  • Added unit tests validating generation behavior across create/update scenarios.
  • Tests also verify hostname allocation behavior during validation.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
pkg/route/apiserver/registry/route/strategy.go (1)

153-157: Also freeze Generation for status updates

Status updates must never alter Generation. Mirror old Generation here to prevent spoofing.

 func (routeStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
   newRoute := obj.(*routeapi.Route)
   oldRoute := old.(*routeapi.Route)
   newRoute.Spec = oldRoute.Spec
+  newRoute.Generation = oldRoute.Generation
 }
pkg/route/apiserver/registry/route/strategy_test.go (3)

1146-1148: Fix failure message: printing wrong variable

Use newRoute.Generation in the “got” branch.

-    t.Fatalf("Expected generation after metadata update to still be 1, got %d", simpleRoute.Generation)
+    t.Fatalf("Expected generation after metadata update to still be 1, got %d", newRoute.Generation)

1153-1155: Fix failure message: printing wrong variable

Same issue here; print newRoute.Generation.

-    t.Fatalf("Expected generation after spec update to be 2, got %d", simpleRoute.Generation)
+    t.Fatalf("Expected generation after spec update to be 2, got %d", newRoute.Generation)

1116-1156: Add a guard test for client-supplied Generation on metadata-only updates

Recommend a test that attempts to set newRoute.Generation to a bogus value while leaving Spec unchanged, and asserts it remains equal to old.Generation after PrepareForUpdate. This complements the proposed strategy fix.

I can draft this test if you’d like.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 91face9 and 8f06fc0.

📒 Files selected for processing (2)
  • pkg/route/apiserver/registry/route/strategy.go (3 hunks)
  • pkg/route/apiserver/registry/route/strategy_test.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
pkg/route/apiserver/registry/route/strategy_test.go (2)
pkg/route/apiserver/registry/route/strategy.go (1)
  • NewStrategy (46-55)
vendor/github.com/openshift/api/route/v1/types.go (1)
  • Route (48-61)
🔇 Additional comments (3)
pkg/route/apiserver/registry/route/strategy.go (2)

8-8: Semantic equality import: LGTM

Using apiequality.Semantic.DeepEqual is the right choice for spec comparisons.


70-70: Initialize Generation on create: LGTM

Setting Generation to 1 on create aligns with kube conventions.

pkg/route/apiserver/registry/route/strategy_test.go (1)

1116-1134: Good coverage of generation lifecycle

Nice assertions for: pre-create (0), post-create (1), and no bump on metadata-only update.

@candita
Copy link
Contributor

candita commented Sep 10, 2025

/assign @Miciah
/assign @alebedev87

Copy link

@alebedev87 alebedev87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Sep 15, 2025
@alebedev87
Copy link

I doubt I can approve in this repository but let's give a try.

/approve

@rikatz
Copy link
Member Author

rikatz commented Sep 15, 2025

thanks! I think per https://github.com/openshift/openshift-apiserver/blob/main/pkg/route/OWNERS we need @knobunc to approve it.

Maybe you and Miciah should also be added to approvers? :)

@knobunc
Copy link
Contributor

knobunc commented Sep 16, 2025

/approve

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Sep 16, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alebedev87, knobunc, rikatz

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

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 16, 2025
@rikatz
Copy link
Member Author

rikatz commented Sep 16, 2025

Test plan executed:

  • Created a cluster with 4.20/tp
  • Created a new project/deployment/exposed the route
  • Verified that the route does not have a generation field
  • Changed the route (.spec) and verified that no route was still added
  • Generated a new openshift-apiserver image and replaced on the cluster.
  • Checked the route - No generation was added
  • Updated my current route - A new generation field with value "1" was added
  • Updated the route again - Generation was bumped to "2"
  • Created a new route - Generation field was added as "1"
  • Updated the newly created route - Generation field was bumped to "2"

As a follow up PR, I will use the OTE existing here and add some e2e tests for this case (and others), but for now I am keeping it separated to not overload the PR with non-related changes

@rikatz
Copy link
Member Author

rikatz commented Sep 16, 2025

/verified by "TestRouteGenerationManagement"

@openshift-ci-robot openshift-ci-robot added the verified Signifies that the PR passed pre-merge verification criteria label Sep 16, 2025
@openshift-ci-robot
Copy link

@rikatz: This PR has been marked as verified by "TestRouteGenerationManagement".

Details

In response to this:

/verified by "TestRouteGenerationManagement"

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Sep 16, 2025

@rikatz: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@openshift-merge-bot openshift-merge-bot bot merged commit a187f61 into openshift:main Sep 16, 2025
14 checks passed
@openshift-ci-robot
Copy link

@rikatz: Jira Issue Verification Checks: Jira Issue OCPBUGS-61228
✔️ This pull request was pre-merge verified.
✔️ All associated pull requests have merged.
✔️ All associated, merged pull requests were pre-merge verified.

Jira Issue OCPBUGS-61228 has been moved to the MODIFIED state and will move to the VERIFIED state when the change is available in an accepted nightly payload. 🕓

Details

In response to this:

Kubernetes resources should bump their generation every time a field on spec is changed. This allows controllers to keep track of the desired state and the current state.

This change:

  • initializes the generation of a resource if it does not exist
  • updates the generation of a resource when a field on spec is changed
  • adds some unit tests to assure the desired behavior

Summary by CodeRabbit

  • New Features

  • Route resources now initialize Generation to 1 on creation and increment only when the spec changes.

  • Bug Fixes

  • Metadata or status updates no longer affect Route Generation, improving observability and controller workflows that track spec changes.

  • Tests

  • Added unit tests validating generation behavior across create/update scenarios and hostname allocation during validation.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@alebedev87
Copy link

Maybe you and Miciah should also be added to approvers? :)

#558

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. jira/severity-moderate Referenced Jira bug's severity is moderate for the branch this PR is targeting. jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. lgtm Indicates that a PR is ready to be merged. verified Signifies that the PR passed pre-merge verification criteria

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants