Skip to content

Commit 4d6c52e

Browse files
committed
Adding validation rules into types
1 parent 39cf838 commit 4d6c52e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

client/apis/volumegroupsnapshot/v1alpha1/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type VolumeGroupSnapshotSpec struct {
3636
// class will be used.
3737
// Empty string is not allowed for this field.
3838
// +optional
39+
// +kubebuilder:validation:XValidation:rule="size(self) > 0",message="VolumeGroupSnapshotClassName must not be the empty string when set"
3940
VolumeGroupSnapshotClassName *string `json:"volumeGroupSnapshotClassName,omitempty" protobuf:"bytes,2,opt,name=volumeGroupSnapshotClassName"`
4041
}
4142

@@ -44,6 +45,8 @@ type VolumeGroupSnapshotSpec struct {
4445
// object should be used.
4546
// Exactly one of its members must be set.
4647
// Members in VolumeGroupSnapshotSource are immutable.
48+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.selector) || has(self.selector)", message="selector is required once set"
49+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.volumeGroupSnapshotContentName) || has(self.volumeGroupSnapshotContentName)", message="volumeGroupSnapshotContentName is required once set"
4750
type VolumeGroupSnapshotSource struct {
4851
// Selector is a label query over persistent volume claims that are to be
4952
// grouped together for snapshotting.
@@ -53,6 +56,7 @@ type VolumeGroupSnapshotSource struct {
5356
// Once a VolumeGroupSnapshotContent is created and the sidecar starts to process
5457
// it, the volume list will not change with retries.
5558
// +optional
59+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.Source.Selector is immutable"
5660
Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,1,opt,name=selector"`
5761

5862
// VolumeGroupSnapshotContentName specifies the name of a pre-existing VolumeGroupSnapshotContent
@@ -61,6 +65,7 @@ type VolumeGroupSnapshotSource struct {
6165
// only needs a representation in Kubernetes.
6266
// This field is immutable.
6367
// +optional
68+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.Source.VolumeGroupSnapshotContentName is immutable"
6469
VolumeGroupSnapshotContentName *string `json:"volumeGroupSnapshotContentName,omitempty" protobuf:"bytes,2,opt,name=volumeGroupSnapshotContentName"`
6570
}
6671

@@ -265,6 +270,9 @@ type VolumeGroupSnapshotContentSpec struct {
265270
// VolumeGroupSnapshot object MUST be provided for binding to happen.
266271
// This field is immutable after creation.
267272
// Required.
273+
// +kubebuilder:validation:XValidation:rule="has(self.name) && has(self.__namespace__)",message="both Spec.VolumeGroupSnapshotRef.Name and Spec.VolumeGroupSnapshotRef.Namespace must be set"
274+
// +kubebuilder:validation:XValidation:rule="self.name == oldSelf.name",message="Spec.VolumeGroupSnapshotRef.Name is immutable"
275+
// +kubebuilder:validation:XValidation:rule="self.__namespace__ == oldSelf.__namespace__",message="Spec.VolumeGroupSnapshotRef.Namespace is immutable"
268276
VolumeGroupSnapshotRef core_v1.ObjectReference `json:"volumeGroupSnapshotRef" protobuf:"bytes,1,opt,name=volumeGroupSnapshotRef"`
269277

270278
// DeletionPolicy determines whether this VolumeGroupSnapshotContent and the
@@ -347,11 +355,14 @@ type VolumeGroupSnapshotContentStatus struct {
347355
// VolumeGroupSnapshotContentSource represents the CSI source of a group snapshot.
348356
// Exactly one of its members must be set.
349357
// Members in VolumeGroupSnapshotContentSource are immutable.
358+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.volumeHandle) || has(self.volumeHandle)", message="volumeHandle is required once set"
359+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.groupSnapshotHandles) || has(self.groupSnapshotHandles)", message="groupSnapshotHandles is required once set"
350360
type VolumeGroupSnapshotContentSource struct {
351361
// VolumeHandles is a list of volume handles on the backend to be snapshotted
352362
// together. It is specified for dynamic provisioning of the VolumeGroupSnapshot.
353363
// This field is immutable.
354364
// +optional
365+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.Source.VolumeHandle is immutable"
355366
VolumeHandles []string `json:"volumeHandles,omitempty" protobuf:"bytes,1,opt,name=volumeHandles"`
356367

357368
// GroupSnapshotHandles specifies the CSI "group_snapshot_id" of a pre-existing
@@ -360,6 +371,7 @@ type VolumeGroupSnapshotContentSource struct {
360371
// representation was (or should be) created.
361372
// This field is immutable.
362373
// +optional
374+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.Source.GroupSnapshotHandles is immutable"
363375
GroupSnapshotHandles *GroupSnapshotHandles `json:"groupSnapshotHandles,omitempty" protobuf:"bytes,2,opt,name=groupSnapshotHandles"`
364376
}
365377

client/apis/volumesnapshot/v1/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type VolumeSnapshotSpec struct {
9191
// CreateSnapshot will fail and generate an event.
9292
// Empty string is not allowed for this field.
9393
// +optional
94+
// +kubebuilder:validation:XValidation:rule="size(self) > 0",message="volumeSnapshotClassName must not be the empty string when set"
9495
VolumeSnapshotClassName *string `json:"volumeSnapshotClassName,omitempty" protobuf:"bytes,2,opt,name=volumeSnapshotClassName"`
9596
}
9697

@@ -99,6 +100,8 @@ type VolumeSnapshotSpec struct {
99100
// object should be used.
100101
// Exactly one of its members must be set.
101102
// Members in VolumeSnapshotSource are immutable.
103+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.persistentVolumeClaimName) || has(self.persistentVolumeClaimName)", message="persistentVolumeClaimName is required once set"
104+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.volumeSnapshotContentName) || has(self.volumeSnapshotContentName)", message="volumeSnapshotContentName is required once set"
102105
type VolumeSnapshotSource struct {
103106
// persistentVolumeClaimName specifies the name of the PersistentVolumeClaim
104107
// object representing the volume from which a snapshot should be created.
@@ -108,13 +111,15 @@ type VolumeSnapshotSource struct {
108111
// created.
109112
// This field is immutable.
110113
// +optional
114+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.Source.PersistentVolumeClaimName is immutable"
111115
PersistentVolumeClaimName *string `json:"persistentVolumeClaimName,omitempty" protobuf:"bytes,1,opt,name=persistentVolumeClaimName"`
112116

113117
// volumeSnapshotContentName specifies the name of a pre-existing VolumeSnapshotContent
114118
// object representing an existing volume snapshot.
115119
// This field should be set if the snapshot already exists and only needs a representation in Kubernetes.
116120
// This field is immutable.
117121
// +optional
122+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.Source.VolumeSnapshotContentName is immutable"
118123
VolumeSnapshotContentName *string `json:"volumeSnapshotContentName,omitempty" protobuf:"bytes,2,opt,name=volumeSnapshotContentName"`
119124
}
120125

@@ -289,6 +294,7 @@ type VolumeSnapshotContentList struct {
289294
}
290295

291296
// VolumeSnapshotContentSpec is the specification of a VolumeSnapshotContent
297+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.sourceVolumeMode) || has(self.sourceVolumeMode)", message="sourceVolumeMode is required once set"
292298
type VolumeSnapshotContentSpec struct {
293299
// volumeSnapshotRef specifies the VolumeSnapshot object to which this
294300
// VolumeSnapshotContent object is bound.
@@ -298,6 +304,7 @@ type VolumeSnapshotContentSpec struct {
298304
// VolumeSnapshot object MUST be provided for binding to happen.
299305
// This field is immutable after creation.
300306
// Required.
307+
// +kubebuilder:validation:XValidation:rule="has(self.name) && has(self.__namespace__)",message="both spec.volumeSnapshotRef.name and spec.volumeSnapshotRef.namespace must be set"
301308
VolumeSnapshotRef core_v1.ObjectReference `json:"volumeSnapshotRef" protobuf:"bytes,1,opt,name=volumeSnapshotRef"`
302309

303310
// deletionPolicy determines whether this VolumeSnapshotContent and its physical snapshot on
@@ -340,24 +347,29 @@ type VolumeSnapshotContentSpec struct {
340347
// This field is immutable.
341348
// This field is an alpha field.
342349
// +optional
350+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.sourceVolumeMode is immutable"
343351
SourceVolumeMode *core_v1.PersistentVolumeMode `json:"sourceVolumeMode" protobuf:"bytes,6,opt,name=sourceVolumeMode"`
344352
}
345353

346354
// VolumeSnapshotContentSource represents the CSI source of a snapshot.
347355
// Exactly one of its members must be set.
348356
// Members in VolumeSnapshotContentSource are immutable.
357+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.volumeHandle) || has(self.volumeHandle)", message="volumeHandle is required once set"
358+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.snapshotHandle) || has(self.snapshotHandle)", message="snapshotHandle is required once set"
349359
type VolumeSnapshotContentSource struct {
350360
// volumeHandle specifies the CSI "volume_id" of the volume from which a snapshot
351361
// should be dynamically taken from.
352362
// This field is immutable.
353363
// +optional
364+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.Source.VolumeHandle is immutable"
354365
VolumeHandle *string `json:"volumeHandle,omitempty" protobuf:"bytes,1,opt,name=volumeHandle"`
355366

356367
// snapshotHandle specifies the CSI "snapshot_id" of a pre-existing snapshot on
357368
// the underlying storage system for which a Kubernetes object representation
358369
// was (or should be) created.
359370
// This field is immutable.
360371
// +optional
372+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Spec.Source.SnapshotHandle is immutable"
361373
SnapshotHandle *string `json:"snapshotHandle,omitempty" protobuf:"bytes,2,opt,name=snapshotHandle"`
362374
}
363375

0 commit comments

Comments
 (0)