Skip to content

Commit

Permalink
Adding validation rules into types
Browse files Browse the repository at this point in the history
  • Loading branch information
cici37 committed Apr 29, 2024
1 parent 39cf838 commit 4d6c52e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions client/apis/volumegroupsnapshot/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type VolumeGroupSnapshotSpec struct {
// class will be used.
// Empty string is not allowed for this field.
// +optional
// +kubebuilder:validation:XValidation:rule="size(self) > 0",message="VolumeGroupSnapshotClassName must not be the empty string when set"
VolumeGroupSnapshotClassName *string `json:"volumeGroupSnapshotClassName,omitempty" protobuf:"bytes,2,opt,name=volumeGroupSnapshotClassName"`
}

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

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

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

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

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

Expand Down
12 changes: 12 additions & 0 deletions client/apis/volumesnapshot/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type VolumeSnapshotSpec struct {
// CreateSnapshot will fail and generate an event.
// Empty string is not allowed for this field.
// +optional
// +kubebuilder:validation:XValidation:rule="size(self) > 0",message="volumeSnapshotClassName must not be the empty string when set"
VolumeSnapshotClassName *string `json:"volumeSnapshotClassName,omitempty" protobuf:"bytes,2,opt,name=volumeSnapshotClassName"`
}

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

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

Expand Down Expand Up @@ -289,6 +294,7 @@ type VolumeSnapshotContentList struct {
}

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

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

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

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

Expand Down

0 comments on commit 4d6c52e

Please sign in to comment.