Skip to content

Conversation

@chrischdi
Copy link

@chrischdi chrischdi commented Dec 9, 2025

This PR adds an improved version of ApplyCustomResourceDefinitionV1, analog to the pattern used in other packages, named ApplyCustomResourceDefinitionV1Improved.

The only change is to also copy over the caBundle of a conversion webhook, to preserve an injected ca bundle and to not cause indefinite reconciliations between some controller and service-ca-bundle operator.

This was found for https://github.com/openshift/cluster-capi-operator which makes use of this library.

CVO already today does this: https://github.com/openshift/cluster-version-operator/blob/0e6c916f99e05983190202575bb530200560acb9/lib/resourcemerge/apiext.go#L34

I adjusted the code though to match what library-go does for MutatingWebhookConfigurations.

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Dec 9, 2025

@chrischdi: 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.

@damdo
Copy link
Member

damdo commented Dec 9, 2025

/assign @JoelSpeed

@damdo
Copy link
Member

damdo commented Dec 9, 2025

/assign @bertinatto @jsafrane

@damdo
Copy link
Member

damdo commented Dec 9, 2025

/cc @wking

As I think CVO does implement/vendor this same change to resourceapply

@openshift-ci openshift-ci bot requested a review from wking December 9, 2025 12:35
@JoelSpeed
Copy link
Contributor

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Dec 9, 2025
@openshift-ci
Copy link
Contributor

openshift-ci bot commented Dec 9, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

The full list of commands accepted by this bot can be found 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

)

// ApplyCustomResourceDefinitionV1Improved applies the required CustomResourceDefinition to the cluster, preserving an injected ca bundle.
func ApplyCustomResourceDefinitionV1Improved(ctx context.Context, client apiextclientv1.CustomResourceDefinitionsGetter, recorder events.Recorder, required *apiextensionsv1.CustomResourceDefinition) (*apiextensionsv1.CustomResourceDefinition, bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please double-check but I think that the *Improved versions let callers reuse a cache to avoid unnecessary writes when nothing has changed.

)

// ApplyCustomResourceDefinitionV1Improved applies the required CustomResourceDefinition to the cluster, preserving an injected ca bundle.
func ApplyCustomResourceDefinitionV1Improved(ctx context.Context, client apiextclientv1.CustomResourceDefinitionsGetter, recorder events.Recorder, required *apiextensionsv1.CustomResourceDefinition) (*apiextensionsv1.CustomResourceDefinition, bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please also add a unit test

return existing, false, nil
}

if klog.V(2).Enabled() {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not to use klog.V(2).Infof(...) ?


// copyCustomResourceDefinitionConversionWebhookCABundle populates spec.conversion.webhook.clientConfig.caBundle fields from
// existing resource if it was set before and is not set in present. This provides upgrade compatibility with service-ca-bundle operator.
func copyCustomResourceDefinitionConversionWebhookCABundle(existing *apiextensionsv1.CustomResourceDefinition, required *apiextensionsv1.CustomResourceDefinition) {
Copy link
Contributor

Choose a reason for hiding this comment

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

could you rename the params to from and to ? it would be easier to read the code.

modified := false
existingCopy := existing.DeepCopy()

copyCustomResourceDefinitionConversionWebhookCABundle(existing, required)
Copy link
Contributor

Choose a reason for hiding this comment

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

you pass the original pointer to the required obj which is modified. you need to make a copy of that obj and pass the pointer to the copy

}

// copyCustomResourceDefinitionConversionWebhookCABundle populates spec.conversion.webhook.clientConfig.caBundle fields from
// existing resource if it was set before and is not set in present. This provides upgrade compatibility with service-ca-bundle operator.
Copy link
Contributor

Choose a reason for hiding this comment

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

since this function provides upgrade compatibility with service-ca-bundle operator why shouldn't the ApplyCustomResourceDefinitionV1 receive this improvement ?

Copy link
Author

Choose a reason for hiding this comment

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

I thought about creating a separate function to not "surprisingly" change behavior of the existing function.

I'm okay with doing the adjustment to ApplyCustomResourceDefinitionV1

Copy link
Member

Choose a reason for hiding this comment

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

I think creating an "Improved" function has generally been the approach around here right?
To not surprise importers with a "breaking change" in behaviour.
That said if we think we are ok with that, +1

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that the *Improved versions also let callers reuse a cache to avoid unnecessary writes when nothing has changed. Usually by accepting the cache ResourceCache param.

I'm ok with a new function but I think the new function should also take the cache ResourceCache param.

We should also consider deprecating the old function. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds reasonable thanks

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

Labels

lgtm Indicates that a PR is ready to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants