-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Make set manifest state more understandable (#2074)
* make set manifest state more understandable * fix test * add unit test * fix lint
- Loading branch information
Showing
7 changed files
with
144 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package status_test | ||
|
||
import ( | ||
"testing" | ||
|
||
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/kyma-project/lifecycle-manager/api/v1beta2" | ||
"github.com/kyma-project/lifecycle-manager/internal/manifest/status" | ||
) | ||
|
||
func TestConfirmInstallationCondition(t *testing.T) { | ||
manifest := &v1beta2.Manifest{} | ||
manifest.SetGeneration(1) | ||
|
||
status.ConfirmInstallationCondition(manifest) | ||
|
||
conditions := manifest.GetStatus().Conditions | ||
if len(conditions) != 1 { | ||
t.Fatalf("expected 1 condition, got %d", len(conditions)) | ||
} | ||
|
||
condition := conditions[0] | ||
if condition.Type != string(status.ConditionTypeInstallation) { | ||
t.Errorf("expected condition type %s, got %s", status.ConditionTypeInstallation, condition.Type) | ||
} | ||
if condition.Reason != string(status.ConditionReasonReady) { | ||
t.Errorf("expected condition reason %s, got %s", status.ConditionReasonReady, condition.Reason) | ||
} | ||
if condition.Status != apimetav1.ConditionTrue { | ||
t.Errorf("expected condition status %s, got %s", apimetav1.ConditionTrue, condition.Status) | ||
} | ||
if condition.Message != "installation is ready and resources can be used" { | ||
t.Errorf("expected condition message %s, got %s", "installation is ready and resources can be used", | ||
condition.Message) | ||
} | ||
if condition.ObservedGeneration != manifest.GetGeneration() { | ||
t.Errorf("expected observed generation %d, got %d", manifest.GetGeneration(), condition.ObservedGeneration) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,27 @@ | ||
package status | ||
|
||
import ( | ||
"errors" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/kyma-project/lifecycle-manager/api/shared" | ||
"github.com/kyma-project/lifecycle-manager/api/v1beta2" | ||
) | ||
|
||
const waitingForResourcesMsg = "waiting for resources to become ready" | ||
|
||
var ErrInstallationConditionRequiresUpdate = errors.New("installation condition needs an update") | ||
|
||
func SetManifestState(manifest *v1beta2.Manifest, newState shared.State) error { | ||
status := manifest.GetStatus() | ||
|
||
if newState == shared.StateProcessing { | ||
manifest.SetStatus(status.WithState(shared.StateProcessing).WithOperation(waitingForResourcesMsg)) | ||
return ErrInstallationConditionRequiresUpdate | ||
} | ||
|
||
installationCondition := initInstallationCondition(manifest) | ||
if newState != status.State || !meta.IsStatusConditionTrue(status.Conditions, installationCondition.Type) { | ||
installationCondition.Status = apimetav1.ConditionTrue | ||
meta.SetStatusCondition(&status.Conditions, installationCondition) | ||
const ( | ||
ResourcesAreReadyMsg = "resources are ready" | ||
WaitingForResourcesMsg = "waiting for resources to become ready" | ||
) | ||
|
||
manifest.SetStatus(status.WithState(newState).WithOperation(installationCondition.Message)) | ||
return ErrInstallationConditionRequiresUpdate | ||
func RequireManifestStateUpdateAfterSyncResource(manifest *v1beta2.Manifest, newState shared.State) bool { | ||
manifestStatus := manifest.GetStatus() | ||
|
||
if newState != manifestStatus.State { | ||
if newState == shared.StateProcessing || newState == shared.StateError { | ||
manifest.SetStatus(manifestStatus.WithState(newState).WithOperation(WaitingForResourcesMsg)) | ||
} else { | ||
ConfirmInstallationCondition(manifest) | ||
manifest.SetStatus(manifestStatus.WithState(newState).WithOperation(ResourcesAreReadyMsg)) | ||
} | ||
return true | ||
} | ||
|
||
return nil | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package status_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kyma-project/lifecycle-manager/api/shared" | ||
"github.com/kyma-project/lifecycle-manager/api/v1beta2" | ||
"github.com/kyma-project/lifecycle-manager/internal/manifest/status" | ||
) | ||
|
||
func TestRequireManifestStateUpdateAfterSyncResource(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
newState shared.State | ||
expectedState shared.State | ||
expectedOp string | ||
expectUpdate bool | ||
}{ | ||
{ | ||
name: "State changes to Processing", | ||
newState: shared.StateProcessing, | ||
expectedState: shared.StateProcessing, | ||
expectedOp: status.WaitingForResourcesMsg, | ||
expectUpdate: true, | ||
}, | ||
{ | ||
name: "State changes to Error", | ||
newState: shared.StateError, | ||
expectedState: shared.StateError, | ||
expectedOp: status.WaitingForResourcesMsg, | ||
expectUpdate: true, | ||
}, | ||
{ | ||
name: "State changes to Ready", | ||
newState: shared.StateReady, | ||
expectedState: shared.StateReady, | ||
expectedOp: status.ResourcesAreReadyMsg, | ||
expectUpdate: false, | ||
}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
manifest := &v1beta2.Manifest{} | ||
manifest.SetGeneration(1) | ||
manifestStatus := shared.Status{ | ||
State: shared.StateReady, | ||
} | ||
manifestStatus = manifestStatus.WithOperation(status.ResourcesAreReadyMsg) | ||
manifest.SetStatus(manifestStatus) | ||
|
||
updated := status.RequireManifestStateUpdateAfterSyncResource(manifest, testCase.newState) | ||
if updated != testCase.expectUpdate { | ||
t.Errorf("expected update to be %v, got %v", testCase.expectUpdate, updated) | ||
} | ||
|
||
if manifest.GetStatus().State != testCase.expectedState { | ||
t.Errorf("expected state to be %v, got %v", testCase.expectedState, manifest.GetStatus().State) | ||
} | ||
|
||
if manifest.GetStatus().Operation != testCase.expectedOp { | ||
t.Errorf("expected operation to be %v, got %v", testCase.expectedOp, manifest.GetStatus().Operation) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters