Skip to content

Commit

Permalink
fix: GcpNfsVolume Status.State is empty for long time if IpRange is g…
Browse files Browse the repository at this point in the history
…etting created
  • Loading branch information
ravi-shankar-sap committed Jul 10, 2024
1 parent 263b9d2 commit 51479b9
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/skr/gcpnfsvolume/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (r *Reconciler) newAction() composed.Action {
composed.ComposeActions(
"crGcpNfsVolumeValidateSpec",
validateIpRange, validateFileShareName, validateCapacity, validatePV, validatePVC),
setProcessing,
defaultiprange.New(),
addFinalizer,
loadKcpIpRange,
Expand Down
33 changes: 33 additions & 0 deletions pkg/skr/gcpnfsvolume/setProcessing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gcpnfsvolume

import (
"context"
cloudresourcesv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
"github.com/kyma-project/cloud-manager/pkg/composed"
)

func setProcessing(ctx context.Context, st composed.State) (error, context.Context) {
state := st.(*State)
logger := composed.LoggerFromCtx(ctx)
nfsVolume := state.ObjAsGcpNfsVolume()

//If deleting, continue with next steps.
deleting := composed.IsMarkedForDeletion(state.Obj())
if deleting {
return nil, nil
}

logger.WithValues("GcpNfsVolume :", nfsVolume.Name).Info("Checking States")

//If state is not empty, continue
if nfsVolume.Status.State != "" {
return nil, nil
}

//Set the state to processing
nfsVolume.Status.State = cloudresourcesv1beta1.StateProcessing
return composed.UpdateStatus(nfsVolume).
SetExclusiveConditions().
SuccessError(composed.StopWithRequeue).
Run(ctx, state)
}
124 changes: 124 additions & 0 deletions pkg/skr/gcpnfsvolume/setProcessing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package gcpnfsvolume

import (
"context"
"github.com/go-logr/logr"
cloudcontrolv1beta1 "github.com/kyma-project/cloud-manager/api/cloud-control/v1beta1"
"github.com/kyma-project/cloud-manager/api/cloud-resources/v1beta1"
"github.com/kyma-project/cloud-manager/pkg/composed"
"github.com/stretchr/testify/suite"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/log"
"testing"
)

type setProcessingSuite struct {
suite.Suite
ctx context.Context
}

func (suite *setProcessingSuite) SetupTest() {
suite.ctx = log.IntoContext(context.Background(), logr.Discard())
}

func (suite *setProcessingSuite) TestSetProcessingWhenDeleting() {

obj := deletedGcpNfsVolume.DeepCopy()
factory, err := newTestStateFactory()
suite.Nil(err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

//Get state object with GcpNfsVolume
state := factory.newStateWith(obj)
suite.Nil(err)

err, ctx = setProcessing(ctx, state)
suite.Nil(err)
suite.Nil(ctx)
}

func (suite *setProcessingSuite) TestSetProcessingWhenStateDone() {

obj := gcpNfsVolume.DeepCopy()
factory, err := newTestStateFactory()
suite.Nil(err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

//Get state object with GcpNfsVolume
obj.Status.State = v1beta1.StateReady
meta.SetStatusCondition(&obj.Status.Conditions, metav1.Condition{
Type: cloudcontrolv1beta1.ConditionTypeReady,
Status: metav1.ConditionTrue,
Reason: "test",
Message: "test",
})
state := factory.newStateWith(obj)
suite.Nil(err)

err, ctx = setProcessing(ctx, state)
suite.Nil(err)
suite.Nil(ctx)
}

func (suite *setProcessingSuite) TestSetProcessingWhenStateError() {

obj := gcpNfsVolume.DeepCopy()
factory, err := newTestStateFactory()
suite.Nil(err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

//Get state object with GcpNfsVolume
obj.Status.State = v1beta1.StateError
meta.SetStatusCondition(&obj.Status.Conditions, metav1.Condition{
Type: cloudcontrolv1beta1.ConditionTypeError,
Status: metav1.ConditionTrue,
Reason: "test",
Message: "test",
})
state := factory.newStateWith(obj)
suite.Nil(err)

err, ctx = setProcessing(ctx, state)
suite.Nil(err)
suite.Nil(ctx)
}

func (suite *setProcessingSuite) TestSetProcessingWhenStateEmpty() {

obj := gcpNfsVolume.DeepCopy()
factory, err := newTestStateFactory()
suite.Nil(err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

//Set the Status.State to empty.
obj.Status.State = ""

//Get state object with GcpNfsVolume
state := factory.newStateWith(obj)
suite.Nil(err)

err, ctx = setProcessing(ctx, state)
suite.Equal(composed.StopWithRequeue, err)
fromK8s := &v1beta1.GcpNfsVolume{}
err = factory.skrCluster.K8sClient().Get(ctx,
types.NamespacedName{Name: obj.Name,
Namespace: obj.Namespace},
fromK8s)
suite.Nil(err)
suite.Equal(v1beta1.GcpNfsVolumeProcessing, fromK8s.Status.State)
suite.Nil(fromK8s.Status.Conditions)
}

func TestSetProcessing(t *testing.T) {
suite.Run(t, new(setProcessingSuite))
}

0 comments on commit 51479b9

Please sign in to comment.