Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GcpNfsVolume): no longer tries to edit capacity in spec #1022

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions pkg/skr/gcpnfsvolume/modifyPersistentVolumeClaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func modifyPersistentVolumeClaim(ctx context.Context, st composed.State) (error,
}

nfsVolume := state.ObjAsGcpNfsVolume()
capacity := gcpNfsVolumeCapacityToResourceQuantity(nfsVolume)

if !meta.IsStatusConditionTrue(nfsVolume.Status.Conditions, v1beta1.ConditionTypeReady) {
return nil, nil
Expand All @@ -29,12 +28,6 @@ func modifyPersistentVolumeClaim(ctx context.Context, st composed.State) (error,
return nil, nil
}

capacityChanged := !capacity.Equal(state.PVC.Spec.Resources.Requests["storage"])
if capacityChanged {
state.PVC.Spec.Resources.Requests["storage"] = *capacity
logger.Info("Detected modified PVC capacity")
}

expectedLabels := getVolumeClaimLabels(nfsVolume)
labelsChanged := !areLabelsEqual(state.PVC.Labels, expectedLabels)
if labelsChanged {
Expand All @@ -49,7 +42,7 @@ func modifyPersistentVolumeClaim(ctx context.Context, st composed.State) (error,
logger.Info("Detected desynced PVC annotations")
}

if !(capacityChanged || labelsChanged || annotationsDesynced) {
if !(labelsChanged || annotationsDesynced) {
return nil, nil
}

Expand Down
53 changes: 41 additions & 12 deletions pkg/skr/gcpnfsvolume/modifyPersistentVolumeClaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
"testing"
"time"

"github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
cloudresourcesv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
composed "github.com/kyma-project/cloud-manager/pkg/composed"
spy "github.com/kyma-project/cloud-manager/pkg/testinfra/clientspy"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -26,6 +24,7 @@ func TestModifyPersistentVolumeClaim(t *testing.T) {
t.Run("modifyPersistentVolumeClaim", func(t *testing.T) {

var gcpNfsVolume *cloudresourcesv1beta1.GcpNfsVolume
var actualPVC *corev1.PersistentVolumeClaim
var state *State
var k8sClient client.WithWatch

Expand All @@ -38,7 +37,7 @@ func TestModifyPersistentVolumeClaim(t *testing.T) {

setupTest := func() {
gcpNfsVolume = &cloudresourcesv1beta1.GcpNfsVolume{
ObjectMeta: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gcpnfsvol",
Namespace: "test-ns",
},
Expand All @@ -54,19 +53,19 @@ func TestModifyPersistentVolumeClaim(t *testing.T) {
},
},
Status: cloudresourcesv1beta1.GcpNfsVolumeStatus{
Conditions: []v1.Condition{
Conditions: []metav1.Condition{
{
Type: v1beta1.ConditionTypeReady,
Type: cloudresourcesv1beta1.ConditionTypeReady,
Status: metav1.ConditionTrue,
Reason: v1beta1.ConditionReasonReady,
Reason: cloudresourcesv1beta1.ConditionReasonReady,
Message: "Volume is ready",
},
},
},
}

actualPVC := &corev1.PersistentVolumeClaim{
ObjectMeta: v1.ObjectMeta{
actualPVC = &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "test-gcpnfsvol",
Namespace: "test-ns",
Labels: getVolumeClaimLabels(gcpNfsVolume),
Expand Down Expand Up @@ -96,7 +95,7 @@ func TestModifyPersistentVolumeClaim(t *testing.T) {
state.PVC = actualPVC
}

t.Run("Should: modify PVC when actual state diverges from desired state", func(t *testing.T) {
t.Run("Should: modify PVC when labels change", func(t *testing.T) {
setupTest()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand All @@ -105,26 +104,56 @@ func TestModifyPersistentVolumeClaim(t *testing.T) {
"foo": "bar-modified",
"oof": "rab",
},
}

err, res := modifyPersistentVolumeClaim(ctx, state)

assert.NotNil(t, err, "should return not-nil err") // not an actual error, but StopWithRequeueDelay
assert.Nil(t, res, "should return nil res")
assert.EqualValues(t, 1, k8sClient.(spy.ClientSpy).UpdateCallCount(), "update should be called")
})

t.Run("Should: modify PVC when annotation change", func(t *testing.T) {
setupTest()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gcpNfsVolume.Spec.PersistentVolumeClaim = &cloudresourcesv1beta1.GcpNfsVolumePvcSpec{
Annotations: map[string]string{
"baz": "qux-modified",
"zab": "xuq",
},
}

err, res := modifyPersistentVolumeClaim(ctx, state)

assert.NotNil(t, err, "should return not-nil err") // not an actual error, but StopWithRequeueDelay
assert.Nil(t, res, "should return nil res")
assert.EqualValues(t, 1, k8sClient.(spy.ClientSpy).UpdateCallCount(), "update should be called")
})

t.Run("Should: modify only PVC label when capacity changes", func(t *testing.T) {
setupTest()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
originalCapacity := actualPVC.Spec.Resources.Requests["storage"].DeepCopy()
gcpNfsVolume.Spec.CapacityGb = 2000

err, res := modifyPersistentVolumeClaim(ctx, state)

postModifyCapacity := actualPVC.Spec.Resources.Requests["storage"].DeepCopy()
assert.NotNil(t, err, "should return not-nil err") // not an actual error, but StopWithRequeueDelay
assert.Nil(t, res, "should return nil res")
assert.EqualValues(t, 1, k8sClient.(spy.ClientSpy).UpdateCallCount(), "update should be called")
assert.True(t, originalCapacity.Equal(postModifyCapacity), "capacity should not change")
assert.Equal(t, "2000Gi", actualPVC.Labels[cloudresourcesv1beta1.LabelStorageCapacity], "label value should be adjusted to match desired value")
})

t.Run("Should: do nothing if GcpNfsVolume is marked for deletion", func(t *testing.T) {
setupTest()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gcpNfsVolume.ObjectMeta = v1.ObjectMeta{
DeletionTimestamp: &v1.Time{
gcpNfsVolume.ObjectMeta = metav1.ObjectMeta{
DeletionTimestamp: &metav1.Time{
Time: time.Now(),
},
}
Expand All @@ -141,7 +170,7 @@ func TestModifyPersistentVolumeClaim(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
state.PVC = nil
gcpNfsVolume.Status.Conditions = []v1.Condition{}
gcpNfsVolume.Status.Conditions = []metav1.Condition{}

err, res := modifyPersistentVolumeClaim(ctx, state)

Expand Down
Loading