Skip to content
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

Update DataImportCron CronJob when needed #2316

Merged

Conversation

arnongilboa
Copy link
Collaborator

@arnongilboa arnongilboa commented Jun 7, 2022

Signed-off-by: Arnon Gilboa agilboa@redhat.com

What this PR does / why we need it:
When CDI is being upgraded, the DataImportCron CronJobs are kept as is, and refer cdi-importer image of the previous version, rather than the current (upgraded) one.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Release note:

Update DataImportCron CronJob if needed due to upgrade

Signed-off-by: Arnon Gilboa <agilboa@redhat.com>
@kubevirt-bot kubevirt-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. labels Jun 7, 2022
var cronJob batchv1.CronJob
cronJobNamespacedName := types.NamespacedName{Namespace: r.cdiNamespace, Name: GetCronJobName(cron)}
return r.uncachedClient.Get(ctx, cronJobNamespacedName, &cronJob) == nil
Copy link
Collaborator

@akalenyu akalenyu Jun 7, 2022

Choose a reason for hiding this comment

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

Why were we doing uncached client here btw? (only a GET so should be ok, just curious)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

afair r.client.Get() sometime returned NotFound after creating CronJob. However, I'll try again r.client for Get() and Update() here and check if it's consistent.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah looks like that uncached client snuck in. Michael mentioned it in the original PR, but we didn't address it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed. So far no surprises.

err := r.uncachedClient.Get(ctx, cronJobNamespacedName, &cronJob)
podSpec := &cronJob.Spec.JobTemplate.Spec.Template.Spec
if err == nil && len(podSpec.Containers) > 0 && podSpec.Containers[0].Image != r.image {
podSpec.Containers[0].Image = r.image
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe Update() over newCronJob().Spec != foundCron.Spec?
It is possible at some point in the future we decided to change some fields in the cronjob object too, and want to sync existing cronjobs to those values

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, seems much smarter. However, creating the spec for every check seems a bit expansive, and I see no clean way for memoizing it. Any idea?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@akalenyu tried this change and it seems that when CronJob is created based on newCronJob(), k8s patches several fields of the spec (dnsPolicy, schedulerName, serviceAccount etc.) so we cannot simply compare (DeepEqual) the current and the desired spec.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this might help here

merged, err := sdk.MergeObject(desired, current, LastAppliedConfigAnnotation)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Used your tip, thanks @akalenyu !

podSpec := &cronJob.Spec.JobTemplate.Spec.Template.Spec
if err == nil && len(podSpec.Containers) > 0 && podSpec.Containers[0].Image != r.image {
podSpec.Containers[0].Image = r.image
err = r.uncachedClient.Update(ctx, &cronJob)
Copy link
Member

Choose a reason for hiding this comment

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

uncached client here seems wrong as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@arnongilboa arnongilboa force-pushed the update_dic_cronjob_container_image branch from f1e89d2 to a95b392 Compare June 9, 2022 14:27
@kubevirt-bot kubevirt-bot added size/L and removed size/M labels Jun 9, 2022
@arnongilboa
Copy link
Collaborator Author

/hold

@kubevirt-bot kubevirt-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 9, 2022
Signed-off-by: Arnon Gilboa <agilboa@redhat.com>
@arnongilboa arnongilboa force-pushed the update_dic_cronjob_container_image branch from a95b392 to a6718df Compare June 9, 2022 20:35
@arnongilboa
Copy link
Collaborator Author

/hold remove

@arnongilboa
Copy link
Collaborator Author

/unhold

@kubevirt-bot kubevirt-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 9, 2022
@arnongilboa arnongilboa changed the title Update DataImportCron CronJob container image if needed Update DataImportCron CronJob when needed Jun 9, 2022
@arnongilboa
Copy link
Collaborator Author

/retest

@awels
Copy link
Member

awels commented Jun 10, 2022

/lgtm
/approve

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Jun 10, 2022
@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: awels

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

The pull request process is described 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

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 10, 2022
@kubevirt-bot kubevirt-bot merged commit ea86148 into kubevirt:main Jun 10, 2022
arnongilboa added a commit to arnongilboa/containerized-data-importer that referenced this pull request Jun 13, 2022
Manual backport of kubevirt#2316

Signed-off-by: Arnon Gilboa <agilboa@redhat.com>
kubevirt-bot pushed a commit that referenced this pull request Jun 13, 2022
Manual backport of #2316

Signed-off-by: Arnon Gilboa <agilboa@redhat.com>
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. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants