Skip to content

Commit

Permalink
update webhook to reject decreasing storage size
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Kvapil <[email protected]>
  • Loading branch information
kvaps committed Jul 22, 2024
1 parent 1ea2eae commit 9756858
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/v1alpha1/etcdcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,26 @@ func (r *EtcdCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, er
etcdclusterlog.Info("validate update", "name", r.Name)
var warnings admission.Warnings
oldCluster := old.(*EtcdCluster)

// Check if replicas are being resized
if *oldCluster.Spec.Replicas != *r.Spec.Replicas {
warnings = append(warnings, "cluster resize is not currently supported")
}

var allErrors field.ErrorList

// Check if storage size is being decreased
oldStorage := oldCluster.Spec.Storage.VolumeClaimTemplate.Spec.Resources.Requests[corev1.ResourceStorage]
newStorage := r.Spec.Storage.VolumeClaimTemplate.Spec.Resources.Requests[corev1.ResourceStorage]
if newStorage.Cmp(oldStorage) < 0 {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "storage", "volumeClaimTemplate", "resources", "requests", "storage"),
newStorage.String(),
"decreasing storage size is not allowed"),
)
}

// Check if storage type is changing
if oldCluster.Spec.Storage.EmptyDir == nil && r.Spec.Storage.EmptyDir != nil ||
oldCluster.Spec.Storage.EmptyDir != nil && r.Spec.Storage.EmptyDir == nil {
allErrors = append(allErrors, field.Invalid(
Expand All @@ -121,6 +136,7 @@ func (r *EtcdCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, er
)
}

// Validate PodDisruptionBudget
pdbWarnings, pdbErr := r.validatePdb()
if pdbErr != nil {
allErrors = append(allErrors, pdbErr...)
Expand All @@ -129,11 +145,13 @@ func (r *EtcdCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, er
warnings = append(warnings, pdbWarnings...)
}

// Validate Security
securityErr := r.validateSecurity()
if securityErr != nil {
allErrors = append(allErrors, securityErr...)
}

// Validate Options
if errOptions := validateOptions(r); errOptions != nil {
allErrors = append(allErrors, field.Invalid(
field.NewPath("spec", "options"),
Expand Down
40 changes: 40 additions & 0 deletions api/v1alpha1/etcdcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,46 @@ var _ = Describe("EtcdCluster Webhook", func() {
}
})

It("Should reject decreasing storage size", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
Storage: StorageSpec{
VolumeClaimTemplate: EmbeddedPersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{
Resources: corev1.VolumeResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceStorage: resource.MustParse("5Gi"),
},
},
},
},
},
},
}
oldCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
Storage: StorageSpec{
VolumeClaimTemplate: EmbeddedPersistentVolumeClaim{
Spec: corev1.PersistentVolumeClaimSpec{
Resources: corev1.VolumeResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceStorage: resource.MustParse("10Gi"),
},
},
},
},
},
},
}
_, err := etcdCluster.ValidateUpdate(oldCluster)
if Expect(err).To(HaveOccurred()) {
statusErr := err.(*errors.StatusError)
Expect(statusErr.ErrStatus.Message).To(ContainSubstring("decreasing storage size is not allowed"))
}
})

It("Should allow changing emptydir size", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Expand Down

0 comments on commit 9756858

Please sign in to comment.