Skip to content

Commit

Permalink
fix update validator of spec.storage (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
sircthulhu committed Mar 25, 2024
1 parent b3547a0 commit 3088a5a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion api/v1alpha1/etcdcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (r *EtcdCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, er
}

var allErrors field.ErrorList
if oldCluster.Spec.Storage.EmptyDir.String() != r.Spec.Storage.EmptyDir.String() {
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(
field.NewPath("spec", "storage", "emptyDir"),
r.Spec.Storage.EmptyDir,
Expand Down
20 changes: 18 additions & 2 deletions api/v1alpha1/etcdcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,28 @@ var _ = Describe("EtcdCluster Webhook", func() {
Storage: StorageSpec{EmptyDir: nil},
},
}
w, err := etcdCluster.ValidateUpdate(oldCluster)
gomega.Expect(w).To(gomega.BeEmpty())
_, err := etcdCluster.ValidateUpdate(oldCluster)
if gomega.Expect(err).To(gomega.HaveOccurred()) {
statusErr := err.(*errors.StatusError)
gomega.Expect(statusErr.ErrStatus.Message).To(gomega.ContainSubstring("field is immutable"))
}
})

It("Should allow changing emptydir size", func() {
etcdCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
Storage: StorageSpec{EmptyDir: &corev1.EmptyDirVolumeSource{SizeLimit: ptr.To(resource.MustParse("4Gi"))}},
},
}
oldCluster := &EtcdCluster{
Spec: EtcdClusterSpec{
Replicas: ptr.To(int32(1)),
Storage: StorageSpec{EmptyDir: &corev1.EmptyDirVolumeSource{SizeLimit: ptr.To(resource.MustParse("10Gi"))}},
},
}
_, err := etcdCluster.ValidateUpdate(oldCluster)
gomega.Expect(err).To(gomega.Succeed())
})
})
})

0 comments on commit 3088a5a

Please sign in to comment.