Skip to content

Commit

Permalink
don't try to apply patches below current pvc size
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed Oct 27, 2023
1 parent 0fd1671 commit 26279fb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
35 changes: 27 additions & 8 deletions internal/fullnode/pvc_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ var (
)

// BuildPVCs outputs desired PVCs given the crd.
func BuildPVCs(crd *cosmosv1.CosmosFullNode, dataSources map[int32]*dataSource) []diff.Resource[*corev1.PersistentVolumeClaim] {
func BuildPVCs(
crd *cosmosv1.CosmosFullNode,
dataSources map[int32]*dataSource,
currentPVCs []*corev1.PersistentVolumeClaim,
) []diff.Resource[*corev1.PersistentVolumeClaim] {
base := corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Expand All @@ -46,8 +50,16 @@ func BuildPVCs(crd *cosmosv1.CosmosFullNode, dataSources map[int32]*dataSource)
pvc.Labels[kube.InstanceLabel] = instanceName(crd, i)

var dataSource *corev1.TypedLocalObjectReference
var existingSize resource.Quantity
if ds, ok := dataSources[i]; ok && ds != nil {
dataSource = ds.ref
} else {
for _, pvc := range currentPVCs {
if pvc.Name == name {
existingSize = pvc.Spec.Resources.Requests[corev1.ResourceStorage]
break
}
}
}

tpl := crd.Spec.VolumeClaimTemplate
Expand All @@ -59,7 +71,7 @@ func BuildPVCs(crd *cosmosv1.CosmosFullNode, dataSources map[int32]*dataSource)

pvc.Spec = corev1.PersistentVolumeClaimSpec{
AccessModes: sliceOrDefault(tpl.AccessModes, defaultAccessModes),
Resources: pvcResources(crd, name, dataSources[i]),
Resources: pvcResources(crd, name, dataSources[i], existingSize),
StorageClassName: ptr(tpl.StorageClassName),
VolumeMode: valOrDefault(tpl.VolumeMode, ptr(corev1.PersistentVolumeFilesystem)),
DataSource: dataSource,
Expand All @@ -85,11 +97,13 @@ func pvcName(crd *cosmosv1.CosmosFullNode, ordinal int32) string {
return kube.ToName(name)
}

func pvcResources(crd *cosmosv1.CosmosFullNode, name string, dataSource *dataSource) corev1.ResourceRequirements {
var (
reqs = crd.Spec.VolumeClaimTemplate.Resources
size = reqs.Requests[corev1.ResourceStorage]
)
func pvcResources(
crd *cosmosv1.CosmosFullNode,
name string,
dataSource *dataSource,
existingSize resource.Quantity,
) corev1.ResourceRequirements {
var reqs = crd.Spec.VolumeClaimTemplate.Resources

if dataSource != nil {
reqs.Requests[corev1.ResourceStorage] = dataSource.size
Expand All @@ -101,10 +115,15 @@ func pvcResources(crd *cosmosv1.CosmosFullNode, name string, dataSource *dataSou
requestedSize := status.RequestedSize.DeepCopy()
newSize := requestedSize.AsDec()
sizeWithPadding := resource.NewDecimalQuantity(*newSize.Mul(newSize, inf.NewDec(snapshotGrowthFactor, 2)), resource.DecimalSI)
if sizeWithPadding.Cmp(size) > 0 {
if sizeWithPadding.Cmp(reqs.Requests[corev1.ResourceStorage]) > 0 {
reqs.Requests[corev1.ResourceStorage] = *sizeWithPadding
}
}
}

if existingSize.Cmp(reqs.Requests[corev1.ResourceStorage]) > 0 {
reqs.Requests[corev1.ResourceStorage] = existingSize
}

return reqs
}
23 changes: 14 additions & 9 deletions internal/fullnode/pvc_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ func TestBuildPVCs(t *testing.T) {
"juno-0": {},
}

for i, r := range BuildPVCs(&crd, map[int32]*dataSource{}) {
initial := BuildPVCs(&crd, map[int32]*dataSource{}, nil)
for i, r := range initial {
require.Equal(t, int64(i), r.Ordinal())
require.NotEmpty(t, r.Revision())
}

pvcs := lo.Map(BuildPVCs(&crd, map[int32]*dataSource{}), func(r diff.Resource[*corev1.PersistentVolumeClaim], _ int) *corev1.PersistentVolumeClaim {
initialPVCs := lo.Map(initial, func(r diff.Resource[*corev1.PersistentVolumeClaim], _ int) *corev1.PersistentVolumeClaim {
return r.Object()
})

pvcs := lo.Map(BuildPVCs(&crd, map[int32]*dataSource{}, initialPVCs), func(r diff.Resource[*corev1.PersistentVolumeClaim], _ int) *corev1.PersistentVolumeClaim {
return r.Object()
})

Expand Down Expand Up @@ -95,7 +100,7 @@ func TestBuildPVCs(t *testing.T) {
0: {
ref: crd.Spec.VolumeClaimTemplate.DataSource,
},
})
}, nil)
require.NotEmpty(t, pvcs)

got := pvcs[0].Object()
Expand Down Expand Up @@ -133,7 +138,7 @@ func TestBuildPVCs(t *testing.T) {
},
}

pvcs := BuildPVCs(&crd, map[int32]*dataSource{})
pvcs := BuildPVCs(&crd, map[int32]*dataSource{}, nil)
require.Equal(t, 2, len(pvcs))

got1, got2 := pvcs[0].Object(), pvcs[1].Object()
Expand All @@ -148,7 +153,7 @@ func TestBuildPVCs(t *testing.T) {
crd.Spec.Replicas = 3
crd.Name = strings.Repeat("Y", 300)

pvcs := BuildPVCs(&crd, map[int32]*dataSource{})
pvcs := BuildPVCs(&crd, map[int32]*dataSource{}, nil)
require.NotEmpty(t, pvcs)

for _, got := range pvcs {
Expand Down Expand Up @@ -176,7 +181,7 @@ func TestBuildPVCs(t *testing.T) {
},
}

pvcs := BuildPVCs(&crd, map[int32]*dataSource{})
pvcs := BuildPVCs(&crd, map[int32]*dataSource{}, nil)
require.Len(t, pvcs, 1, tt)

want := corev1.ResourceList{corev1.ResourceStorage: resource.MustParse(tt.WantQuant)}
Expand All @@ -203,7 +208,7 @@ func TestBuildPVCs(t *testing.T) {
},
}

pvcs := BuildPVCs(&crd, map[int32]*dataSource{})
pvcs := BuildPVCs(&crd, map[int32]*dataSource{}, nil)
require.Len(t, pvcs, 1, tt)

want := corev1.ResourceList{corev1.ResourceStorage: resource.MustParse(tt.WantQuant)}
Expand All @@ -230,7 +235,7 @@ func TestBuildPVCs(t *testing.T) {
},
}

pvcs := BuildPVCs(&crd, map[int32]*dataSource{})
pvcs := BuildPVCs(&crd, map[int32]*dataSource{}, nil)
require.Len(t, pvcs, 1, tt)

want := corev1.ResourceList{corev1.ResourceStorage: resource.MustParse(tt.WantQuant)}
Expand All @@ -240,7 +245,7 @@ func TestBuildPVCs(t *testing.T) {
})

test.HasTypeLabel(t, func(crd cosmosv1.CosmosFullNode) []map[string]string {
pvcs := BuildPVCs(&crd, map[int32]*dataSource{})
pvcs := BuildPVCs(&crd, map[int32]*dataSource{}, nil)
labels := make([]map[string]string, 0)
for _, pvc := range pvcs {
labels = append(labels, pvc.Object().Labels)
Expand Down
2 changes: 1 addition & 1 deletion internal/fullnode/pvc_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (control PVCControl) Reconcile(ctx context.Context, reporter kube.Reporter,
}

var (
wantPVCs = BuildPVCs(crd, dataSources)
wantPVCs = BuildPVCs(crd, dataSources, currentPVCs)
diffed = diff.New(currentPVCs, wantPVCs)
)

Expand Down
8 changes: 4 additions & 4 deletions internal/fullnode/pvc_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestPVCControl_Reconcile(t *testing.T) {
crd.Name = "hub"
crd.Namespace = namespace
crd.Spec.Replicas = 1
existing := diff.New(nil, BuildPVCs(&crd, map[int32]*dataSource{})).Creates()[0]
existing := diff.New(nil, BuildPVCs(&crd, map[int32]*dataSource{}, nil)).Creates()[0]
existing.Status.Phase = corev1.ClaimBound

var mClient mockPVCClient
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestPVCControl_Reconcile(t *testing.T) {
crd.Namespace = namespace
crd.Name = "hub"
crd.Spec.Replicas = 1
existing := BuildPVCs(&crd, map[int32]*dataSource{})[0].Object()
existing := BuildPVCs(&crd, map[int32]*dataSource{}, nil)[0].Object()

var mClient mockPVCClient
mClient.ObjectList = corev1.PersistentVolumeClaimList{
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestPVCControl_Reconcile(t *testing.T) {
crd.Spec.Replicas = 1

var mClient mockPVCClient
existing := BuildPVCs(&crd, map[int32]*dataSource{})[0].Object()
existing := BuildPVCs(&crd, map[int32]*dataSource{}, nil)[0].Object()
existing.Status.Phase = corev1.ClaimBound
mClient.ObjectList = corev1.PersistentVolumeClaimList{
Items: []corev1.PersistentVolumeClaim{*existing},
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestPVCControl_Reconcile(t *testing.T) {
crd.Namespace = namespace
crd.Spec.Replicas = 1

existing := BuildPVCs(&crd, map[int32]*dataSource{})[0].Object()
existing := BuildPVCs(&crd, map[int32]*dataSource{}, nil)[0].Object()
existing.Status.Phase = corev1.ClaimPending
var mClient mockPVCClient
mClient.ObjectList = corev1.PersistentVolumeClaimList{
Expand Down

0 comments on commit 26279fb

Please sign in to comment.