Skip to content

Commit bf5a74e

Browse files
authored
🌱 Simplify VirtualMachineSnapshot and related APIs (#1196)
This commit refactors the VirtualMachineSnapshot API to use a simple string field for VM references instead of a complex LocalObjectRef structure, and resolves field conflicts in the VirtualMachine API. API Changes: - Replace VirtualMachineSnapshotSpec.VMRef (LocalObjectRef) with VMName (string) - Rename VirtualMachine.Spec.CurrentSnapshot to CurrentSnapshotName to resolve JSON tag conflict with VirtualMachine.Status.CurrentSnapshot - Simplify VirtualMachineSnapshotReference to use Name field directly - VM status now includes the type (managed / unmanaged) and the name of the snapshot CR) Changes include: - Update virtualmachinesnapshot controller to use VMName instead of VMRef - Update storagepolicyusage and other references in code to handle the above change. - Simplify the validation webhook to handle this change - Add validation to prevent empty/whitespace-only currentSnapshotName - Maintain VKS/TKG node snapshot prevention logic - Provider changes to handle this change Note that I wanted to keep the `spec.currentSnapshot` for the desired snapshot but that's not possible because two fields with different type can't have the same JSON tag (spec being a string, and status being an object).
1 parent 152468c commit bf5a74e

File tree

39 files changed

+261
-799
lines changed

39 files changed

+261
-799
lines changed

api/v1alpha1/zz_generated.conversion.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha2/zz_generated.conversion.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha3/zz_generated.conversion.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha4/virtualmachine_conversion.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
vmopv1a4common "github.com/vmware-tanzu/vm-operator/api/v1alpha4/common"
1616
vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha5"
17-
vmopv1common "github.com/vmware-tanzu/vm-operator/api/v1alpha5/common"
1817
)
1918

2019
func Convert_v1alpha5_VirtualMachineBootstrapCloudInitSpec_To_v1alpha4_VirtualMachineBootstrapCloudInitSpec(
@@ -162,30 +161,29 @@ func Convert_common_LocalObjectRef_To_v1alpha5_VirtualMachineSnapshotReference(
162161
return nil
163162
}
164163

165-
// Convert LocalObjectRef to the nested SnapshotReference field
166-
out.Reference = &vmopv1common.LocalObjectRef{
167-
APIVersion: in.APIVersion,
168-
Kind: in.Kind,
169-
Name: in.Name,
170-
}
164+
// Convert LocalObjectRef to simplified VirtualMachineSnapshotReference
165+
out.Type = vmopv1.VirtualMachineSnapshotReferenceTypeManaged
166+
out.Name = in.Name
171167

172168
return nil
173169
}
174170

175171
func Convert_v1alpha5_VirtualMachineSnapshotReference_To_common_LocalObjectRef(
176172
in *vmopv1.VirtualMachineSnapshotReference, out *vmopv1a4common.LocalObjectRef, s apiconversion.Scope) error {
177-
if in == nil || in.Reference == nil {
173+
if in == nil {
178174
return nil
179175
}
180176

181-
// Convert the nested SnapshotReference back to LocalObjectRef
182-
out.APIVersion = in.Reference.APIVersion
183-
out.Kind = in.Reference.Kind
184-
out.Name = in.Reference.Name
177+
// Convert simplified VirtualMachineSnapshotReference back to LocalObjectRef
178+
// For managed snapshots, assume it's a VirtualMachineSnapshot resource
179+
out.APIVersion = "vmoperator.vmware.com/v1alpha5"
180+
out.Kind = "VirtualMachineSnapshot"
181+
out.Name = in.Name
185182

186183
return nil
187184
}
188185

186+
189187
// ConvertTo converts this VirtualMachine to the Hub version.
190188
func (src *VirtualMachine) ConvertTo(dstRaw ctrlconversion.Hub) error {
191189
dst := dstRaw.(*vmopv1.VirtualMachine)

api/v1alpha4/zz_generated.conversion.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha5/virtualmachine_types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -975,15 +975,14 @@ type VirtualMachineSpec struct {
975975

976976
// +optional
977977

978-
// CurrentSnapshot represents the desired snapshot that the VM
978+
// CurrentSnapshotName represents the desired snapshot that the VM
979979
// should point to. This field can be specified to revert the VM
980980
// to a given snapshot. Once the virtual machine has been
981981
// successfully reverted to the desired snapshot, the value of
982982
// this field is cleared.
983983
//
984-
// The value of this field must be an existing object of
985-
// VirtualMachineSnapshot kind that exists on the API server. All
986-
// other values are invalid.
984+
// The value of this field must be the name of an existing
985+
// VirtualMachineSnapshot resource in the same namespace.
987986
//
988987
// Reverting a virtual machine to a snapshot rolls back the data
989988
// and the configuration of the virtual machine to that of the
@@ -998,7 +997,7 @@ type VirtualMachineSpec struct {
998997
// the power state from the snapshot (i.e., powered on). This can
999998
// be overridden by specifying the PowerState to PoweredOff in the
1000999
// VirtualMachineSpec.
1001-
CurrentSnapshot *vmopv1common.LocalObjectRef `json:"currentSnapshot,omitempty"`
1000+
CurrentSnapshotName string `json:"currentSnapshotName,omitempty"`
10021001

10031002
// +optional
10041003

@@ -1232,6 +1231,7 @@ type VirtualMachineStatus struct {
12321231
// +optional
12331232

12341233
// CurrentSnapshot describes the observed working snapshot of the VirtualMachine.
1234+
// This field contains the name of the current snapshot.
12351235
CurrentSnapshot *VirtualMachineSnapshotReference `json:"currentSnapshot,omitempty"`
12361236

12371237
// +optional

api/v1alpha5/virtualmachinesnapshot_types.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package v1alpha5
77
import (
88
"k8s.io/apimachinery/pkg/api/resource"
99
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10-
11-
vmopv1common "github.com/vmware-tanzu/vm-operator/api/v1alpha5/common"
1210
)
1311

1412
const (
@@ -59,9 +57,9 @@ type VirtualMachineSnapshotSpec struct {
5957

6058
// +optional
6159

62-
// VMRef represents the name of the virtual machine for which the
60+
// VMName represents the name of the virtual machine for which the
6361
// snapshot is requested.
64-
VMRef *vmopv1common.LocalObjectRef `json:"vmRef,omitempty"`
62+
VMName string `json:"vmName,omitempty"`
6563
}
6664

6765
// QuiesceSpec represents specifications that will be used to quiesce
@@ -201,11 +199,9 @@ type VirtualMachineSnapshotReference struct {
201199
// Possible values are: Managed, Unmanaged
202200
Type VirtualMachineSnapshotReferenceType `json:"type"`
203201

204-
// +optional
205-
206-
// Reference is the reference to the snapshot in the Supervisor.
207-
// This should be set only for Managed snapshots.
208-
Reference *vmopv1common.LocalObjectRef `json:"reference,omitempty"`
202+
// Name is the name of the snapshot resource. This field is only set
203+
// for managed snapshots.
204+
Name string `json:"name"`
209205
}
210206

211207
// VirtualMachineSnapshotStorageStatusRequested describes the observed amount of

api/v1alpha5/zz_generated.deepcopy.go

Lines changed: 3 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/vmoperator.vmware.com_virtualmachinereplicasets.yaml

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,17 +2151,16 @@ spec:
21512151
Defaults to true if omitted.
21522152
type: boolean
21532153
type: object
2154-
currentSnapshot:
2154+
currentSnapshotName:
21552155
description: |-
2156-
CurrentSnapshot represents the desired snapshot that the VM
2156+
CurrentSnapshotName represents the desired snapshot that the VM
21572157
should point to. This field can be specified to revert the VM
21582158
to a given snapshot. Once the virtual machine has been
21592159
successfully reverted to the desired snapshot, the value of
21602160
this field is cleared.
21612161
2162-
The value of this field must be an existing object of
2163-
VirtualMachineSnapshot kind that exists on the API server. All
2164-
other values are invalid.
2162+
The value of this field must be the name of an existing
2163+
VirtualMachineSnapshot resource in the same namespace.
21652164
21662165
Reverting a virtual machine to a snapshot rolls back the data
21672166
and the configuration of the virtual machine to that of the
@@ -2176,31 +2175,7 @@ spec:
21762175
the power state from the snapshot (i.e., powered on). This can
21772176
be overridden by specifying the PowerState to PoweredOff in the
21782177
VirtualMachineSpec.
2179-
properties:
2180-
apiVersion:
2181-
description: |-
2182-
APIVersion defines the versioned schema of this representation of an
2183-
object. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
2184-
type: string
2185-
kind:
2186-
description: |-
2187-
Kind is a string value representing the REST resource this object
2188-
represents.
2189-
Servers may infer this from the endpoint the client submits requests to.
2190-
Cannot be updated.
2191-
In CamelCase.
2192-
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
2193-
type: string
2194-
name:
2195-
description: |-
2196-
Name refers to a unique resource in the current namespace.
2197-
More info: http://kubernetes.io/docs/user-guide/identifiers#names
2198-
type: string
2199-
required:
2200-
- apiVersion
2201-
- kind
2202-
- name
2203-
type: object
2178+
type: string
22042179
groupName:
22052180
description: |-
22062181
GroupName indicates the name of the VirtualMachineGroup to which this

0 commit comments

Comments
 (0)