Skip to content

Commit f6164a1

Browse files
authored
Merge pull request kubernetes-csi#1073 from cici37/master
Adding validation rules into CRDs for validation
2 parents 39cf838 + 982f11e commit f6164a1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

client/apis/volumegroupsnapshot/v1alpha1/types.go

Lines changed: 13 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,9 @@ 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"
50+
// +kubebuilder:validation:XValidation:rule="(has(self.selector) && !has(self.volumeGroupSnapshotContentName)) || (!has(self.selector) && has(self.volumeGroupSnapshotContentName))", message="exactly one of selector and volumeGroupSnapshotContentName must be set"
4751
type VolumeGroupSnapshotSource struct {
4852
// Selector is a label query over persistent volume claims that are to be
4953
// grouped together for snapshotting.
@@ -53,6 +57,7 @@ type VolumeGroupSnapshotSource struct {
5357
// Once a VolumeGroupSnapshotContent is created and the sidecar starts to process
5458
// it, the volume list will not change with retries.
5559
// +optional
60+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="selector is immutable"
5661
Selector *metav1.LabelSelector `json:"selector,omitempty" protobuf:"bytes,1,opt,name=selector"`
5762

5863
// VolumeGroupSnapshotContentName specifies the name of a pre-existing VolumeGroupSnapshotContent
@@ -61,6 +66,7 @@ type VolumeGroupSnapshotSource struct {
6166
// only needs a representation in Kubernetes.
6267
// This field is immutable.
6368
// +optional
69+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="volumeGroupSnapshotContentName is immutable"
6470
VolumeGroupSnapshotContentName *string `json:"volumeGroupSnapshotContentName,omitempty" protobuf:"bytes,2,opt,name=volumeGroupSnapshotContentName"`
6571
}
6672

@@ -265,6 +271,8 @@ type VolumeGroupSnapshotContentSpec struct {
265271
// VolumeGroupSnapshot object MUST be provided for binding to happen.
266272
// This field is immutable after creation.
267273
// Required.
274+
// +kubebuilder:validation:XValidation:rule="has(self.name) && has(self.__namespace__)",message="both volumeGroupSnapshotRef.name and volumeGroupSnapshotRef.namespace must be set"
275+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="volumeGroupSnapshotRef 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,15 @@ 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.volumeHandles) || has(self.volumeHandles)", message="volumeHandles is required once set"
359+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.groupSnapshotHandles) || has(self.groupSnapshotHandles)", message="groupSnapshotHandles is required once set"
360+
// +kubebuilder:validation:XValidation:rule="(has(self.volumeHandles) && !has(self.groupSnapshotHandles)) || (!has(self.volumeHandles) && has(self.groupSnapshotHandles))", message="exactly one of volumeHandles and groupSnapshotHandles must be set"
350361
type VolumeGroupSnapshotContentSource struct {
351362
// VolumeHandles is a list of volume handles on the backend to be snapshotted
352363
// together. It is specified for dynamic provisioning of the VolumeGroupSnapshot.
353364
// This field is immutable.
354365
// +optional
366+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="volumeHandles is immutable"
355367
VolumeHandles []string `json:"volumeHandles,omitempty" protobuf:"bytes,1,opt,name=volumeHandles"`
356368

357369
// GroupSnapshotHandles specifies the CSI "group_snapshot_id" of a pre-existing
@@ -360,6 +372,7 @@ type VolumeGroupSnapshotContentSource struct {
360372
// representation was (or should be) created.
361373
// This field is immutable.
362374
// +optional
375+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="groupSnapshotHandles is immutable"
363376
GroupSnapshotHandles *GroupSnapshotHandles `json:"groupSnapshotHandles,omitempty" protobuf:"bytes,2,opt,name=groupSnapshotHandles"`
364377
}
365378

client/apis/volumesnapshot/v1/types.go

Lines changed: 14 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,9 @@ 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"
105+
// +kubebuilder:validation:XValidation:rule="(has(self.volumeSnapshotContentName) && !has(self.persistentVolumeClaimName)) || (!has(self.volumeSnapshotContentName) && has(self.persistentVolumeClaimName))", message="exactly one of volumeSnapshotContentName and persistentVolumeClaimName must be set"
102106
type VolumeSnapshotSource struct {
103107
// persistentVolumeClaimName specifies the name of the PersistentVolumeClaim
104108
// object representing the volume from which a snapshot should be created.
@@ -108,13 +112,15 @@ type VolumeSnapshotSource struct {
108112
// created.
109113
// This field is immutable.
110114
// +optional
115+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="persistentVolumeClaimName is immutable"
111116
PersistentVolumeClaimName *string `json:"persistentVolumeClaimName,omitempty" protobuf:"bytes,1,opt,name=persistentVolumeClaimName"`
112117

113118
// volumeSnapshotContentName specifies the name of a pre-existing VolumeSnapshotContent
114119
// object representing an existing volume snapshot.
115120
// This field should be set if the snapshot already exists and only needs a representation in Kubernetes.
116121
// This field is immutable.
117122
// +optional
123+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="volumeSnapshotContentName is immutable"
118124
VolumeSnapshotContentName *string `json:"volumeSnapshotContentName,omitempty" protobuf:"bytes,2,opt,name=volumeSnapshotContentName"`
119125
}
120126

@@ -289,6 +295,7 @@ type VolumeSnapshotContentList struct {
289295
}
290296

291297
// VolumeSnapshotContentSpec is the specification of a VolumeSnapshotContent
298+
// +kubebuilder:validation:XValidation:rule="!has(oldSelf.sourceVolumeMode) || has(self.sourceVolumeMode)", message="sourceVolumeMode is required once set"
292299
type VolumeSnapshotContentSpec struct {
293300
// volumeSnapshotRef specifies the VolumeSnapshot object to which this
294301
// VolumeSnapshotContent object is bound.
@@ -298,6 +305,7 @@ type VolumeSnapshotContentSpec struct {
298305
// VolumeSnapshot object MUST be provided for binding to happen.
299306
// This field is immutable after creation.
300307
// Required.
308+
// +kubebuilder:validation:XValidation:rule="has(self.name) && has(self.__namespace__)",message="both spec.volumeSnapshotRef.name and spec.volumeSnapshotRef.namespace must be set"
301309
VolumeSnapshotRef core_v1.ObjectReference `json:"volumeSnapshotRef" protobuf:"bytes,1,opt,name=volumeSnapshotRef"`
302310

303311
// deletionPolicy determines whether this VolumeSnapshotContent and its physical snapshot on
@@ -340,24 +348,30 @@ type VolumeSnapshotContentSpec struct {
340348
// This field is immutable.
341349
// This field is an alpha field.
342350
// +optional
351+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="sourceVolumeMode is immutable"
343352
SourceVolumeMode *core_v1.PersistentVolumeMode `json:"sourceVolumeMode" protobuf:"bytes,6,opt,name=sourceVolumeMode"`
344353
}
345354

346355
// VolumeSnapshotContentSource represents the CSI source of a snapshot.
347356
// Exactly one of its members must be set.
348357
// Members in VolumeSnapshotContentSource 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.snapshotHandle) || has(self.snapshotHandle)", message="snapshotHandle is required once set"
360+
// +kubebuilder:validation:XValidation:rule="(has(self.volumeHandle) && !has(self.snapshotHandle)) || (!has(self.volumeHandle) && has(self.snapshotHandle))", message="exactly one of volumeHandle and snapshotHandle must be set"
349361
type VolumeSnapshotContentSource struct {
350362
// volumeHandle specifies the CSI "volume_id" of the volume from which a snapshot
351363
// should be dynamically taken from.
352364
// This field is immutable.
353365
// +optional
366+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="volumeHandle is immutable"
354367
VolumeHandle *string `json:"volumeHandle,omitempty" protobuf:"bytes,1,opt,name=volumeHandle"`
355368

356369
// snapshotHandle specifies the CSI "snapshot_id" of a pre-existing snapshot on
357370
// the underlying storage system for which a Kubernetes object representation
358371
// was (or should be) created.
359372
// This field is immutable.
360373
// +optional
374+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="snapshotHandle is immutable"
361375
SnapshotHandle *string `json:"snapshotHandle,omitempty" protobuf:"bytes,2,opt,name=snapshotHandle"`
362376
}
363377

0 commit comments

Comments
 (0)