Skip to content

Commit

Permalink
Update storage capacity in StatefulSetBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Feb 16, 2021
1 parent b784c3f commit 994fca7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
16 changes: 14 additions & 2 deletions internal/resource/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
const (
initContainerCPU string = "100m"
initContainerMemory string = "500Mi"
defaultPVCName string = "persistence"
DeletionMarker string = "skipPreStopChecks"
)

Expand Down Expand Up @@ -121,6 +122,9 @@ func (builder *StatefulSetBuilder) Update(object client.Object) error {
//Labels
sts.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)

// PVC storage capacity
updatePersistenceStorageCapacity(&sts.Spec.VolumeClaimTemplates, builder.Instance.Spec.Persistence.Storage)

// pod template
sts.Spec.Template = builder.podTemplateSpec(sts.Spec.Template.Annotations)

Expand All @@ -141,6 +145,14 @@ func (builder *StatefulSetBuilder) Update(object client.Object) error {
return nil
}

func updatePersistenceStorageCapacity(templates *[]corev1.PersistentVolumeClaim, capacity *k8sresource.Quantity) {
for _, t := range *templates {
if t.Name == defaultPVCName {
t.Spec.Resources.Requests[corev1.ResourceStorage] = *capacity
}
}
}

func applyStsOverride(sts *appsv1.StatefulSet, stsOverride *rabbitmqv1beta1.StatefulSet) error {
if stsOverride.EmbeddedLabelsAnnotations != nil {
copyLabelsAnnotations(&sts.ObjectMeta, *stsOverride.EmbeddedLabelsAnnotations)
Expand Down Expand Up @@ -178,7 +190,7 @@ func applyStsOverride(sts *appsv1.StatefulSet, stsOverride *rabbitmqv1beta1.Stat
func persistentVolumeClaim(instance *rabbitmqv1beta1.RabbitmqCluster, scheme *runtime.Scheme) ([]corev1.PersistentVolumeClaim, error) {
pvc := corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "persistence",
Name: defaultPVCName,
Namespace: instance.GetNamespace(),
Labels: metadata.Label(instance.Name),
Annotations: metadata.ReconcileAndFilterAnnotations(map[string]string{}, instance.Annotations),
Expand Down Expand Up @@ -275,7 +287,7 @@ func sortVolumeMounts(mounts []corev1.VolumeMount) {
mounts[0], mounts[i] = mounts[i], mounts[0]
continue
}
if m.Name == "persistence" {
if m.Name == defaultPVCName {
mounts[1], mounts[i] = mounts[i], mounts[1]
}
}
Expand Down
30 changes: 28 additions & 2 deletions internal/resource/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ var _ = Describe("StatefulSet", func() {
Expect(statefulSet.Spec.UpdateStrategy).To(Equal(updateStrategy))
})

It("updates tolerations", func() {
It("updates toleration", func() {
newToleration := corev1.Toleration{
Key: "update",
Operator: "equals",
Expand Down Expand Up @@ -1283,7 +1283,7 @@ var _ = Describe("StatefulSet", func() {
Expect(statefulSet.Spec.Template.Spec.Containers[0].Lifecycle.PreStop.Exec.Command).To(Equal(expectedPreStopCommand))
})

It("checks mirror and querum queue status in preStop hook", func() {
It("checks mirror and quorum queue status in preStop hook", func() {
stsBuilder := builder.StatefulSet()
Expect(stsBuilder.Update(statefulSet)).To(Succeed())

Expand Down Expand Up @@ -1372,6 +1372,32 @@ var _ = Describe("StatefulSet", func() {
Expect(*statefulSet.Spec.Replicas).To(Equal(int32(3)))
})

It("updates the PersistentVolumeClaim storage capacity", func() {
defaultCapacity, _ := k8sresource.ParseQuantity("10Gi")

statefulSet.Spec.VolumeClaimTemplates = []corev1.PersistentVolumeClaim{
{
ObjectMeta: v1.ObjectMeta{
Name: "persistence",
Namespace: instance.Namespace,
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Requests: map[corev1.ResourceName]k8sresource.Quantity{
corev1.ResourceStorage: defaultCapacity,
},
},
},
},
}

newCapacity, _ := k8sresource.ParseQuantity("21Gi")
stsBuilder.Instance.Spec.Persistence.Storage = &newCapacity
Expect(stsBuilder.Update(statefulSet)).To(Succeed())
Expect(statefulSet.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests["storage"]).To(Equal(newCapacity))
})

When("stateful set override are provided", func() {
It("overrides statefulSet.ObjectMeta.Annotations", func() {
instance.Annotations = map[string]string{
Expand Down

0 comments on commit 994fca7

Please sign in to comment.