Skip to content

Conversation

@liam-mackie
Copy link

Background:
When working with long names, you will often need to use a shortened (or abbreviated) version of the controller name as the namePrefix in the kustomization.yaml file. This means a shorter name can be used as the prefix to all the files.

When using the v2alpha/helm plugin, this mostly works. The only resource that doesn't use the name generated by Kustomize is the ServiceMonitor. This is problematic, as it will use the name (with the custom prefix), then template the chart name (usually the long name) in front of it. This leads to a broken installation.

The fix:
This PR fixes this by attempting to get the namePrefix from the kustomization config path. If the namePrefix and the projectName are different, we do not template the file for the service monitor. This way, customisations are honoured, but existing behaviour is maintained.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Oct 7, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: liam-mackie
Once this PR has been reviewed and has the lgtm label, please assign varshaprasad96 for approval. 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
Copy link
Contributor

Welcome @liam-mackie!

It looks like this is your first PR to kubernetes-sigs/kubebuilder 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

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.

You can also check if kubernetes-sigs/kubebuilder has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

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!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @liam-mackie. 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.

@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. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 7, 2025
@liam-mackie
Copy link
Author

@Kavinjsir / @varshaprasad96 - is there any possibility of getting this one reviewed/approved to test? I'd love to get this in!

@liam-mackie liam-mackie changed the title feat: ✨ Allow helm plugin to use custom prefixes ✨ Allow helm plugin to use custom prefixes Oct 13, 2025
fs.BoolVar(&p.force, "force", false, "if true, regenerates all the files")
fs.StringVar(&p.manifestsFile, "manifests", DefaultManifestsFile,
"path to the YAML file containing Kubernetes manifests from kustomize output")
fs.StringVar(&p.kustomizePath, "kustomize-path", DefaultKustomizeFile,
Copy link
Member

Choose a reason for hiding this comment

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

I do not think that we should pass the path here, otherwise we will need a flag for each file.
In this case, we might want to get the config from PROJECT file which has the project name instead of check the kustomize file wdyt?

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately, there's no reference to the entrypoint for Kustomize in the PROJECT file, and there's no way to infer the prefix someone uses in Kustomize outside of the kustomization.yaml itself. Kubebuilder conventions put it in config/default/kustomization.yaml. Originally, I left this as unconfigurable, and simply assumed that the kustomization.yaml would always be in this path.

The best we have to get this programatically is the Makefile, which uses it in the build-installer target:

	$(KUSTOMIZE) build config/default > dist/install.yaml

The config/default path is what we'd use, and we'd simply look for a kustomization.yaml in there. I'm not sure if that's any better than simply allowing the desired kustomization file to be passed through though.

Happy to defer to your expertise here!

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.

Thank you for your contribution 🥇
I tried to give the first round of reviews. I think we need to see if we could use the data from install.yaml and check for more generic options. Please, check my comments , let me know wdyt


// NewChartConverter creates a new chart converter with all necessary components
func NewChartConverter(resources *ParsedResources, projectName, outputDir string) *ChartConverter {
func NewChartConverter(resources *ParsedResources, projectName, namePrefix, outputDir string) *ChartConverter {
Copy link
Member

Choose a reason for hiding this comment

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

The projectName and namePrefix here are the same.
So, why would we need both parameters?
Did I miss something?

Copy link
Author

Choose a reason for hiding this comment

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

In the tests they're the same, but in the code itself, the prefix could possibly be different. We estimate it here and pass it in, as the templater needs it.

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.

Hi @liam-mackie

Really thank you for the amazing work 🥇
I think we are very close.

I have just one comment in the latest review — see: https://github.com/kubernetes-sigs/kubebuilder/pull/5119/files#r2434870465

Also, note that for us to get this one merged, we need the commits squashed. Could you please ensure that you squash all commits so that we have only one commit in this PR? Also, could you please rebase this one with master so that it can pass in the tests?

Lastly, I’ll trigger the tests to check if any fail.

@camilamacedo86 camilamacedo86 changed the title ✨ Allow helm plugin to use custom prefixes 🐛 (helm/v2-alpha): Ensure that ServiceAccount has the prefix used in the default kustomize Oct 16, 2025
@camilamacedo86 camilamacedo86 changed the title 🐛 (helm/v2-alpha): Ensure that ServiceAccount has the prefix used in the default kustomize 🐛 (helm/v2-alpha): Fixed an issue where the ServiceMonitor resource name did not include the default prefix applied by Kustomize. Oct 16, 2025
@liam-mackie liam-mackie force-pushed the lm/add-kustomize-prefix branch from 182aa72 to 11d1780 Compare October 20, 2025 07:01
@liam-mackie
Copy link
Author

Hi @liam-mackie

Really thank you for the amazing work 🥇 I think we are very close.

I have just one comment in the latest review — see: #5119 (files)

Also, note that for us to get this one merged, we need the commits squashed. Could you please ensure that you squash all commits so that we have only one commit in this PR? Also, could you please rebase this one with master so that it can pass in the tests?

Lastly, I’ll trigger the tests to check if any fail.

All done! Rebased and squashed - thanks for the help!

Copy link

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 fixes an issue where the ServiceMonitor resource name did not respect custom namePrefix values from Kustomize configuration, causing broken installations when using abbreviated controller names.

Key Changes:

  • Added namePrefix detection from parsed Kustomize resources to distinguish between default and custom prefixes
  • Modified ServiceMonitor template to use the detected prefix instead of always using projectName
  • Updated the templating logic to preserve custom names when prefix differs from project name

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
servicemonitor.go Changed template fields from ProjectNameMixin to explicit NamePrefix and ServiceName fields
parser.go Added EstimatePrefix method to detect custom name prefixes from Kustomize resources
helm_templater.go Added namePrefix field and logic to skip templating when prefix differs from project name
helm_templater_test.go Added tests for ServiceMonitor name generation with matching and differing prefixes
chart_writer.go Updated to use namePrefix instead of projectName for file naming; reformatted function signatures
chart_converter.go Updated constructor to accept namePrefix parameter
chart_converter_test.go Updated test to pass namePrefix to constructor
edit_kustomize.go Added logic to estimate prefix, find metrics service name, and pass both to ServiceMonitor template

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@liam-mackie liam-mackie force-pushed the lm/add-kustomize-prefix branch from 11d1780 to 37219ac Compare October 21, 2025 22:03
@camilamacedo86
Copy link
Member

I will need some time to properly evaluate
I think we might want to only use the logic for the prefix for serviceMonitor

/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 Oct 23, 2025
@k8s-ci-robot
Copy link
Contributor

@liam-mackie: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubebuilder-e2e-k8s-1-34-1 37219ac link true /test pull-kubebuilder-e2e-k8s-1-34-1

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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.

@liam-mackie
Copy link
Author

I think we might want to only use the logic for the prefix for serviceMonitor

That's a good point - we only see the chart name being used at all in the serviceMonitor, but everything else is neatly pulled directly from the rendered Kustomize YAML. Do you think it's a better idea to simply standardise on using the resource names from the rendered Kustomize? I'm honestly not fully sure why we have the {{ chart.name }} replacement, so I'm fully open to pulling it out completely as a part of this change instead, so long as it's a sane change from your end!

@camilamacedo86
Copy link
Member

Hi @liam-mackie 👋

I think the more surgical we are here, the better.
My suggestion is to focus only on getting the data for the ServiceMonitor and keep everything else as it is for now.
If needed, we can revisit and expand later.

Could we try it out this way?
It would also make the review process easier — otherwise, we’d need to start looking into other design aspects as well.

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/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants