Skip to content

✨ (CLI/Api): Add IfNotExistsAction to machinery for optional file handling #4967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aman4433
Copy link
Contributor

This PR introduces a new mechanism to gracefully handle optional files during scaffolding using a configurable IfNotExistsAction.

Currently, scaffolding fails when an optional file (like test/e2e/e2e_test.go) is missing. This is problematic for plugins/templates that want to inject content only if the file exists.

Fixes: #4960

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 26, 2025
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 26, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @aman4433. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@camilamacedo86 camilamacedo86 changed the title add IfNotExistsAction to machinery for optional file handling ✨ (CLI/Api): Add IfNotExistsAction to machinery for optional file handling Jul 27, 2025
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

That is great 🥇
Thank you for looking on that.

We need to:

  1. Fix lint issues:
    Could you please run make lint-fix to fix the sort/order of the imports?

  2. Ensure that we still handle the scenario.
    We have no automated tests for this one: #4967 (comment)

  3. We will need to fix and adapt the unit tests: https://github.com/kubernetes-sigs/kubebuilder/actions/runs/16542895231/job/46797272326?pr=4967

@aman4433
Copy link
Contributor Author

Hey @camilamacedo86, thanks for the suggestion! I’ll work on the changes and push an update soon.

@aman4433 aman4433 force-pushed the feature/add-if-not-exists-action branch from b54cb0c to acce86a Compare July 27, 2025 16:34
@aman4433
Copy link
Contributor Author

That is great 🥇 Thank you for looking on that.

We need to:

  1. Fix lint issues:
    Could you please run make lint-fix to fix the sort/order of the imports?
  2. Ensure that we still handle the scenario.
    We have no automated tests for this one: #4967 (comment)
  3. We will need to fix and adapt the unit tests: https://github.com/kubernetes-sigs/kubebuilder/actions/runs/16542895231/job/46797272326?pr=4967

Just pushed an update — trimmed the scope to handle only missing optional webhook files and cleaned up lint issues. Verified that the previously failing tests are now passing locally.

@camilamacedo86
Copy link
Member

/pk-to-test

@aman4433
Copy link
Contributor Author

aman4433 commented Jul 30, 2025

/pk-to-test

Its a typo 😅

@camilamacedo86
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 30, 2025
@camilamacedo86 camilamacedo86 requested a review from Copilot August 3, 2025 19:16
Copilot

This comment was marked as outdated.

Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

@aman4433

Thank you a lot for your PR 🥇
Can you address the recommendations made by Copilot here?
So that we can get this one merged?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 4, 2025
@aman4433 aman4433 force-pushed the feature/add-if-not-exists-action branch from 45786ba to 7fa7fdf Compare August 4, 2025 11:20
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 4, 2025
@aman4433 aman4433 force-pushed the feature/add-if-not-exists-action branch from aec3323 to 849a78f Compare August 4, 2025 17:01
@k8s-ci-robot k8s-ci-robot added needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Aug 4, 2025
@camilamacedo86
Copy link
Member

/test pull-kubebuilder-test

@camilamacedo86
Copy link
Member

/approved
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 4, 2025
@camilamacedo86
Copy link
Member

/lgtm cancel

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 4, 2025
@camilamacedo86
Copy link
Member

Hi @aman4433 👋

Thanks so much for the work on this — we're almost there! Just a couple of quick things before we can merge:

  1. Squash the commits — we should keep the history clean with just one commit for this PR.
  2. There’s a linting issue and a failing test — could you take a look and fix those when you get a chance?

Really appreciate your effort on this — it's looking great overall! Let me know if you need a hand with anything 😊

@aman4433 aman4433 force-pushed the feature/add-if-not-exists-action branch from 272c9b2 to dfb18f0 Compare August 4, 2025 21:17
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: aman4433
Once this PR has been reviewed and has the lgtm label, please ask for approval from camilamacedo86. For more information see the Code Review Process.

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

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

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 4, 2025
@aman4433 aman4433 force-pushed the feature/add-if-not-exists-action branch from dfb18f0 to 5dd5c95 Compare August 4, 2025 21:24
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 4, 2025
@aman4433
Copy link
Contributor Author

aman4433 commented Aug 5, 2025

Hi @aman4433 👋

Thanks so much for the work on this — we're almost there! Just a couple of quick things before we can merge:

  1. Squash the commits — we should keep the history clean with just one commit for this PR.
  2. There’s a linting issue and a failing test — could you take a look and fix those when you get a chance?

Really appreciate your effort on this — it's looking great overall! Let me know if you need a hand with anything 😊

Hey @camilamacedo86, Thanks for your support, I think now all the issues are resolved, let me know if this looks good

Copilot

This comment was marked as outdated.

@camilamacedo86
Copy link
Member

Hi @aman4433

We have some fixes

@aman4433 aman4433 force-pushed the feature/add-if-not-exists-action branch from 5dd5c95 to 04f4e7a Compare August 6, 2025 10:10
@camilamacedo86 camilamacedo86 requested a review from Copilot August 6, 2025 20:44
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a configurable IfNotExistsAction mechanism to handle optional files during scaffolding operations. Previously, scaffolding would fail when attempting to inject code into missing optional files (like test files), requiring workarounds. The new system allows templates to specify whether missing files should be ignored or cause errors.

  • Adds IfNotExistsAction enum with ErrorIfNotExist (default) and IgnoreFile options
  • Implements HasIfNotExistsAction interface and IfNotExistsActionMixin for templates to use
  • Updates scaffold logic to check for the new action when files don't exist, replacing hardcoded test file handling

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pkg/machinery/file.go Defines the IfNotExistsAction enum and adds the field to the File struct
pkg/machinery/interfaces.go Adds HasIfNotExistsAction interface for templates to implement
pkg/machinery/mixins.go Provides IfNotExistsActionMixin for easy integration into templates
pkg/machinery/scaffold.go Updates file processing logic to use the new action system instead of hardcoded test file handling
pkg/plugins/golang/v4/scaffolds/internal/templates/webhooks/webhook_test_template.go Example implementation using the new mixin and setting IgnoreFile behavior

return err
}
}
// If inserter doesn't implement HasIfNotExistsAction, return the original error
Copy link
Preview

Copilot AI Aug 6, 2025

Choose a reason for hiding this comment

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

The comment indicates that if the inserter doesn't implement HasIfNotExistsAction, the original error should be returned, but the code will actually fall through to line 256 which wraps the error in a different format. This inconsistency between comment and implementation could lead to confusion about error handling behavior.

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this comment okay?
// If HasIfNotExistsAction is not implemented, fall through and return a wrapped error.

@aman4433 aman4433 force-pushed the feature/add-if-not-exists-action branch from 04f4e7a to 11dcd98 Compare August 7, 2025 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Add IfNotExistsAction to Machinery
3 participants