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

🐛 Skip updating VMOp immutable fields #2566

Merged

Conversation

srm09
Copy link
Contributor

@srm09 srm09 commented Dec 22, 2023

What this PR does / why we need it:
VMOp mutation webhook updates the VMOp Spec.ImageName to a user-friendly name prefixed vmi- or cvmi-. This causes the reconciliation of the VSphereMachine to fail since the controllers copy the all spec fields to the VMOp VirtualMachine on every reconcile. Since imageName is an immutable field we see the below error:

Name:         guestcluster1-control-plane-bz7k9-c9whq
Namespace:    gctest
Labels:       cluster.x-k8s.io/cluster-name=guestcluster1
              cluster.x-k8s.io/control-plane=
              cluster.x-k8s.io/control-plane-name=guestcluster1-control-plane
              topology.cluster.x-k8s.io/owned=
Annotations:  cluster.x-k8s.io/cloned-from-groupkind: VSphereMachineTemplate.vmware.infrastructure.cluster.x-k8s.io
              cluster.x-k8s.io/cloned-from-name: guestcluster1-control-plane-bz7k9
              run.tanzu.vmware.com/resolve-os-image: os-name=photon
API Version:  vmware.infrastructure.cluster.x-k8s.io/v1beta1
Kind:         VSphereMachine
Metadata:
  Creation Timestamp:  2023-12-14T20:01:42Z
  Finalizers:
    vspheremachine.infrastructure.cluster.x-k8s.io
  Generation:  2
  Owner References:
    API Version:           cluster.x-k8s.io/v1beta1
    Block Owner Deletion:  true
    Controller:            true
    Kind:                  Machine
    Name:                  guestcluster1-control-plane-wr5w2
    UID:                   fd9da4eb-8b09-4484-9e1a-90a284861f4c
  Resource Version:        415153
  UID:                     1ba496ef-ccc4-4a6a-9e29-4a8009ec7957
Spec:
  Class Name:      best-effort-xsmall
  Failure Domain:  vmware-system-legacy
  Image Name:      tkgs-ova-photon-3-v1.23.8---vmware.3-tkg.1
  Storage Class:   wcpglobalstorageprofile
Status:
  Conditions:
    Last Transition Time:  2023-12-14T20:01:46Z
    Message:               failed to create or update VirtualMachine: admission webhook "default.validating.virtualmachine.v1alpha1.vmoperator.vmware.com" denied the request: spec.imageName: Invalid value: "tkgs-ova-photon-3-v1.23.8---vmware.3-tkg.1": field is immutable
    Reason:                VMCreationFailed
    Severity:              Warning
    Status:                False
    Type:                  Ready
    Last Transition Time:  2023-12-14T20:01:46Z
    Message:               failed to create or update VirtualMachine: admission webhook "default.validating.virtualmachine.v1alpha1.vmoperator.vmware.com" denied the request: spec.imageName: Invalid value: "tkgs-ova-photon-3-v1.23.8---vmware.3-tkg.1": field is immutable
    Reason:                VMCreationFailed
    Severity:              Warning
    Status:                False
    Type:                  VMProvisioned
  Vmstatus:                pending

This patch ensures the immutable fields on the VMop VM Spec do not get set if these fields on the existing VM object are not empty.

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 #2430

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Dec 22, 2023
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 22, 2023
Copy link

codecov bot commented Dec 22, 2023

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (d8bb2e3) 64.67% compared to head (99f247e) 64.32%.
Report is 12 commits behind head on main.

Files Patch % Lines
pkg/services/vmoperator/vmopmachine.go 86.36% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2566      +/-   ##
==========================================
- Coverage   64.67%   64.32%   -0.36%     
==========================================
  Files         118      118              
  Lines        8539     8585      +46     
==========================================
- Hits         5523     5522       -1     
- Misses       2595     2630      +35     
- Partials      421      433      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@srm09 srm09 force-pushed the vmop/ignore-immutable-fields branch 2 times, most recently from b6bd18c to 178bf0c Compare December 22, 2023 03:42
@srm09
Copy link
Contributor Author

srm09 commented Dec 22, 2023

/override pull-cluster-api-provider-vsphere-apidiff-main

@k8s-ci-robot
Copy link
Contributor

@srm09: Overrode contexts on behalf of srm09: pull-cluster-api-provider-vsphere-apidiff-main

In response to this:

/override pull-cluster-api-provider-vsphere-apidiff-main

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/test-infra repository.

@srm09 srm09 force-pushed the vmop/ignore-immutable-fields branch from 178bf0c to b7a724c Compare December 22, 2023 05:44
Copy link
Member

@chrischdi chrischdi left a comment

Choose a reason for hiding this comment

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

First of all thanks for the PR!

This PR also contains some optimizations we might not want to cherry-pick.

Should we reduce this PR to the bare minimum required to solve the related issue and do the other changes on a separate PR?

That would come down to the changes in:
https://github.com/kubernetes-sigs/cluster-api-provider-vsphere/pull/2566/files#diff-c2e693327f8831614b68401c025d9322280d92938f4a064bcd949656feb52770R179-R337

if I did not miss a point here.

This way it would also be more easily cherry-pickable.

pkg/services/vimmachine.go Outdated Show resolved Hide resolved
@@ -47,8 +47,31 @@ type VimMachineService struct {
Client client.Client
}

// GetMachinesInCluster returns a list of VSphereMachine objects belonging to the cluster.
func (v *VimMachineService) GetMachinesInCluster(
Copy link
Member

Choose a reason for hiding this comment

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

Don't know if we should make this part of the VimMachineService. We don't even use v.Client :think:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, since the client is a part of the VimMachineService and VMopMachineService, no use passing them in the function call. Missed that since most of it was copying over the function as is from the util package.

pkg/services/vmoperator/constants.go Outdated Show resolved Hide resolved
pkg/services/vmoperator/vmopmachine_test.go Outdated Show resolved Hide resolved
Copy link
Member

@fabriziopandini fabriziopandini left a comment

Choose a reason for hiding this comment

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

just a couple of nits from my side

pkg/services/vmoperator/vmoperator_suite_test.go Outdated Show resolved Hide resolved
pkg/services/vmoperator/vmopmachine_test.go Outdated Show resolved Hide resolved
pkg/services/vmoperator/vmopmachine_test.go Outdated Show resolved Hide resolved
@srm09 srm09 force-pushed the vmop/ignore-immutable-fields branch 2 times, most recently from 76f70ab to 4daa7e7 Compare December 26, 2023 22:44
Copy link
Member

@chrischdi chrischdi left a comment

Choose a reason for hiding this comment

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

Some more nits + please move the predicate relevant changes to a separate PR :-)

pkg/services/vmoperator/vmopmachine.go Outdated Show resolved Hide resolved
pkg/services/vmoperator/vmopmachine.go Outdated Show resolved Hide resolved
pkg/util/testutil.go Outdated Show resolved Hide resolved
controllers/vspheremachine_controller.go Outdated Show resolved Hide resolved
controllers/vspheremachine_controller.go Outdated Show resolved Hide resolved
@srm09 srm09 force-pushed the vmop/ignore-immutable-fields branch from 4daa7e7 to 99f247e Compare December 27, 2023 18:53
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Dec 27, 2023
@srm09
Copy link
Contributor Author

srm09 commented Dec 27, 2023

Reverted all the irrelavant changes so that this one just handles skipping immutable fields

Copy link
Member

@chrischdi chrischdi left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 28, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: ce01bd84254186927aea547b9ac17a39d3909431

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chrischdi

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 28, 2023
@chrischdi
Copy link
Member

/cherry-pick release-1.8

@k8s-infra-cherrypick-robot

@chrischdi: once the present PR merges, I will cherry-pick it on top of release-1.8 in a new PR and assign it to you.

In response to this:

/cherry-pick release-1.8

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/test-infra repository.

@chrischdi
Copy link
Member

/cherry-pick release-1.7

@k8s-infra-cherrypick-robot

@chrischdi: once the present PR merges, I will cherry-pick it on top of release-1.7 in a new PR and assign it to you.

In response to this:

/cherry-pick release-1.7

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot merged commit c60d8ba into kubernetes-sigs:main Dec 28, 2023
19 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.9 milestone Dec 28, 2023
@k8s-infra-cherrypick-robot

@chrischdi: #2566 failed to apply on top of branch "release-1.8":

Applying: Skip updating immutable VMOp fields
Using index info to reconstruct a base tree...
M	pkg/services/vmoperator/vmopmachine.go
M	pkg/services/vmoperator/vmopmachine_test.go
M	pkg/util/testutil.go
Falling back to patching base and 3-way merge...
Auto-merging pkg/util/testutil.go
Auto-merging pkg/services/vmoperator/vmopmachine_test.go
CONFLICT (content): Merge conflict in pkg/services/vmoperator/vmopmachine_test.go
Auto-merging pkg/services/vmoperator/vmopmachine.go
CONFLICT (content): Merge conflict in pkg/services/vmoperator/vmopmachine.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Skip updating immutable VMOp fields
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

In response to this:

/cherry-pick release-1.8

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/test-infra repository.

@k8s-infra-cherrypick-robot

@chrischdi: #2566 failed to apply on top of branch "release-1.7":

Applying: Skip updating immutable VMOp fields
Using index info to reconstruct a base tree...
M	pkg/services/vmoperator/vmopmachine.go
M	pkg/services/vmoperator/vmopmachine_test.go
M	pkg/util/testutil.go
Falling back to patching base and 3-way merge...
Auto-merging pkg/util/testutil.go
Auto-merging pkg/services/vmoperator/vmopmachine_test.go
CONFLICT (content): Merge conflict in pkg/services/vmoperator/vmopmachine_test.go
Auto-merging pkg/services/vmoperator/vmopmachine.go
CONFLICT (content): Merge conflict in pkg/services/vmoperator/vmopmachine.go
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Skip updating immutable VMOp fields
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

In response to this:

/cherry-pick release-1.7

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/test-infra repository.

Comment on lines 294 to 295
// NOTE: Set field-by-field in order to preserve changes made directly
// to the VirtualMachine spec by other sources (e.g. the cloud provider)
Copy link
Member

Choose a reason for hiding this comment

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

On the issue we wrote that the following fields are immutable:

  • ImageName
  • ClassName
  • StorageClass
  • ResourcePolicyName

Here we stop mutating / reconciling:

  • ImageName
  • ClassName
  • StorageClass
  • MinHardwareVersion

(MinHardwareVersion instead of ResourcePolicyName)

Q: Is this intended?

Additionally, @srm09 @chrischdi @fabriziopandini What do we think about making those 4 fields immutable on the VSphereMachine? (and also updating the godoc comments accordingly)

I think it's confusing if folks can update the fields and then they are just silently not reconciled.

Copy link
Member

@chrischdi chrischdi Jan 8, 2024

Choose a reason for hiding this comment

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

According vm-operator code it is als MinHardwareVersion:
https://github.com/vmware-tanzu/vm-operator/blob/main/webhooks/virtualmachine/v1alpha1/validation/virtualmachine_validator.go#L143-L149

but great catch, we should also have the same behaviour for ResourcePolicyName!

Also +1 for making these immutable on CAPV side. We will have to introduce webhooks for vm-operator mode capv first!

Copy link
Member

Choose a reason for hiding this comment

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

created #2595 to track this validating webhook part.

Copy link
Member

Choose a reason for hiding this comment

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

Thank you!

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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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.

Remove immutable fields when patching VM Service VMs after creation
6 participants