-
Notifications
You must be signed in to change notification settings - Fork 249
resourceapply: add improved version which copies over spec.conversion.webhook.clientConfig.caBundle #2064
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
base: master
Are you sure you want to change the base?
resourceapply: add improved version which copies over spec.conversion.webhook.clientConfig.caBundle #2064
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,39 @@ import ( | |
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| // 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add a unit test |
||
| existing, err := client.CustomResourceDefinitions().Get(ctx, required.Name, metav1.GetOptions{}) | ||
| if apierrors.IsNotFound(err) { | ||
| requiredCopy := required.DeepCopy() | ||
| actual, err := client.CustomResourceDefinitions().Create( | ||
| ctx, resourcemerge.WithCleanLabelsAndAnnotations(requiredCopy).(*apiextensionsv1.CustomResourceDefinition), metav1.CreateOptions{}) | ||
| resourcehelper.ReportCreateEvent(recorder, required, err) | ||
| return actual, true, err | ||
| } | ||
| if err != nil { | ||
| return nil, false, err | ||
| } | ||
|
|
||
| modified := false | ||
| existingCopy := existing.DeepCopy() | ||
|
|
||
| copyCustomResourceDefinitionConversionWebhookCABundle(existing, required) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you pass the original pointer to the |
||
| resourcemerge.EnsureCustomResourceDefinitionV1(&modified, existingCopy, *required) | ||
| if !modified { | ||
| return existing, false, nil | ||
| } | ||
|
|
||
| if klog.V(2).Enabled() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not to use |
||
| klog.Infof("CustomResourceDefinition %q changes: %s", existing.Name, JSONPatchNoError(existing, existingCopy)) | ||
| } | ||
|
|
||
| actual, err := client.CustomResourceDefinitions().Update(ctx, existingCopy, metav1.UpdateOptions{}) | ||
| resourcehelper.ReportUpdateEvent(recorder, required, err) | ||
|
|
||
| return actual, true, err | ||
| } | ||
|
|
||
| // ApplyCustomResourceDefinitionV1 applies the required CustomResourceDefinition to the cluster. | ||
| func ApplyCustomResourceDefinitionV1(ctx context.Context, client apiextclientv1.CustomResourceDefinitionsGetter, recorder events.Recorder, required *apiextensionsv1.CustomResourceDefinition) (*apiextensionsv1.CustomResourceDefinition, bool, error) { | ||
| existing, err := client.CustomResourceDefinitions().Get(ctx, required.Name, metav1.GetOptions{}) | ||
|
|
@@ -55,3 +88,29 @@ func DeleteCustomResourceDefinitionV1(ctx context.Context, client apiextclientv1 | |
| resourcehelper.ReportDeleteEvent(recorder, required, err) | ||
| return nil, true, nil | ||
| } | ||
|
|
||
| // 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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this function
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 I'm ok with a new function but I think the new function should also take the We should also consider deprecating the old function. WDYT?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds reasonable thanks |
||
| func copyCustomResourceDefinitionConversionWebhookCABundle(existing *apiextensionsv1.CustomResourceDefinition, required *apiextensionsv1.CustomResourceDefinition) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you rename the params to |
||
| // Skip in case the required resource does not have a webhook configured. | ||
| if required.Spec.Conversion == nil || required.Spec.Conversion.Webhook == nil || required.Spec.Conversion.Webhook.ClientConfig == nil { | ||
| return | ||
| } | ||
|
|
||
| // Skip in case the required resource does not have a webhook configured with a strategy of WebhookConverter. | ||
| if required.Spec.Conversion.Strategy != apiextensionsv1.WebhookConverter { | ||
| return | ||
| } | ||
|
|
||
| // Skip in case the required resource has a webhook configured with its own ca bundle. | ||
| if required.Spec.Conversion.Webhook.ClientConfig.CABundle != nil { | ||
| return | ||
| } | ||
|
|
||
| // Skip in case the existing resource does not have a ca bundle set. | ||
| if existing.Spec.Conversion == nil || existing.Spec.Conversion.Webhook == nil || existing.Spec.Conversion.Webhook.ClientConfig == nil || existing.Spec.Conversion.Webhook.ClientConfig.CABundle == nil { | ||
| return | ||
| } | ||
|
|
||
| required.Spec.Conversion.Webhook.ClientConfig.CABundle = existing.Spec.Conversion.Webhook.ClientConfig.CABundle | ||
| } | ||
There was a problem hiding this comment.
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
*Improvedversions let callers reuse a cache to avoid unnecessary writes when nothing has changed.