Skip to content

Commit

Permalink
fix(STONEINTG-469): add SetSnapshotIntegrationStatusAsInvalidandNoRequeu
Browse files Browse the repository at this point in the history
Signed-off-by: Jing Qi <[email protected]>
  • Loading branch information
jinqi7 committed Jul 28, 2023
1 parent 96c948e commit 2c2602f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
3 changes: 3 additions & 0 deletions controllers/pipeline/pipeline_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/redhat-appstudio/integration-service/gitops"
h "github.com/redhat-appstudio/integration-service/helpers"
"github.com/redhat-appstudio/integration-service/loader"
"github.com/redhat-appstudio/integration-service/metrics"
"github.com/redhat-appstudio/integration-service/status"
"github.com/redhat-appstudio/integration-service/tekton"
"github.com/redhat-appstudio/operator-goodies/reconciler"
Expand Down Expand Up @@ -117,6 +118,7 @@ func (a *Adapter) EnsureSnapshotExists() (reconciler.OperationResult, error) {
a.logger.Error(err, "Failed to create Snapshot")
return reconciler.RequeueWithError(err)
}
go metrics.RegisterNewSnapshot()

Check warning on line 121 in controllers/pipeline/pipeline_adapter.go

View check run for this annotation

Codecov / codecov/patch

controllers/pipeline/pipeline_adapter.go#L121

Added line #L121 was not covered by tests

a.logger.LogAuditEvent("Created new Snapshot", expectedSnapshot, h.LogActionAdd,
"snapshot.Name", expectedSnapshot.Name,
Expand Down Expand Up @@ -590,6 +592,7 @@ func (a *Adapter) createCompositeSnapshotsIfConflictExists(application *applicat
if err != nil {
return nil, err
}
go metrics.RegisterNewSnapshot()
a.logger.LogAuditEvent("CompositeSnapshot created", compositeSnapshot, h.LogActionAdd,
"snapshot.Spec.Components", compositeSnapshot.Spec.Components)
return compositeSnapshot, nil
Expand Down
10 changes: 5 additions & 5 deletions controllers/snapshot/snapshot_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (a *Adapter) EnsureAllIntegrationTestPipelinesExist() (reconciler.Operation
if err != nil {
a.logger.Error(err, "Failed to get all required IntegrationTestScenarios")
patch := client.MergeFrom(a.snapshot.DeepCopy())
gitops.SetSnapshotIntegrationStatusAsInvalid(a.snapshot, "Failed to get all required IntegrationTestScenarios: "+err.Error())
gitops.SetSnapshotIntegrationStatusAsError(a.snapshot, "Failed to get all required IntegrationTestScenarios: "+err.Error())

Check warning on line 132 in controllers/snapshot/snapshot_adapter.go

View check run for this annotation

Codecov / codecov/patch

controllers/snapshot/snapshot_adapter.go#L132

Added line #L132 was not covered by tests
a.logger.LogAuditEvent("Snapshot integration status marked as Invalid. Failed to get all required IntegrationTestScenarios",
a.snapshot, h.LogActionUpdate)
return reconciler.RequeueOnErrorOrStop(a.client.Status().Patch(a.context, a.snapshot, patch))
Expand Down Expand Up @@ -310,7 +310,7 @@ func (a *Adapter) EnsureAllReleasesExist() (reconciler.OperationResult, error) {
if err != nil {
a.logger.Error(err, "Failed to get all ReleasePlans")
patch := client.MergeFrom(a.snapshot.DeepCopy())
gitops.SetSnapshotIntegrationStatusAsInvalid(a.snapshot, "Failed to get all ReleasePlans: "+err.Error())
gitops.SetSnapshotIntegrationStatusAsError(a.snapshot, "Failed to get all ReleasePlans: "+err.Error())

Check warning on line 313 in controllers/snapshot/snapshot_adapter.go

View check run for this annotation

Codecov / codecov/patch

controllers/snapshot/snapshot_adapter.go#L313

Added line #L313 was not covered by tests
a.logger.LogAuditEvent("Snapshot integration status marked as Invalid. Failed to get all ReleasePlans",
a.snapshot, h.LogActionUpdate)
return reconciler.RequeueOnErrorOrStop(a.client.Status().Patch(a.context, a.snapshot, patch))
Expand All @@ -320,7 +320,7 @@ func (a *Adapter) EnsureAllReleasesExist() (reconciler.OperationResult, error) {
if err != nil {
a.logger.Error(err, "Failed to create new Releases")
patch := client.MergeFrom(a.snapshot.DeepCopy())
gitops.SetSnapshotIntegrationStatusAsInvalid(a.snapshot, "Failed to create new Releases: "+err.Error())
gitops.SetSnapshotIntegrationStatusAsError(a.snapshot, "Failed to create new Releases: "+err.Error())

Check warning on line 323 in controllers/snapshot/snapshot_adapter.go

View check run for this annotation

Codecov / codecov/patch

controllers/snapshot/snapshot_adapter.go#L323

Added line #L323 was not covered by tests
a.logger.LogAuditEvent("Snapshot integration status marked as Invalid. Failed to create new Releases",
a.snapshot, h.LogActionUpdate)
return reconciler.RequeueOnErrorOrStop(a.client.Status().Patch(a.context, a.snapshot, patch))
Expand Down Expand Up @@ -362,7 +362,7 @@ func (a *Adapter) EnsureSnapshotEnvironmentBindingExist() (reconciler.OperationR
"snapshotEnvironmentBinding.Environment", snapshotEnvironmentBinding.Spec.Environment,
"snapshotEnvironmentBinding.Snapshot", snapshotEnvironmentBinding.Spec.Snapshot)
patch := client.MergeFrom(a.snapshot.DeepCopy())
gitops.SetSnapshotIntegrationStatusAsInvalid(a.snapshot, "Failed to update SnapshotEnvironmentBinding: "+err.Error())
gitops.SetSnapshotIntegrationStatusAsError(a.snapshot, "Failed to update SnapshotEnvironmentBinding: "+err.Error())

Check warning on line 365 in controllers/snapshot/snapshot_adapter.go

View check run for this annotation

Codecov / codecov/patch

controllers/snapshot/snapshot_adapter.go#L365

Added line #L365 was not covered by tests
a.logger.LogAuditEvent("Snapshot integration status marked as Invalid. Failed to update SnapshotEnvironmentBinding",
a.snapshot, h.LogActionUpdate)
return reconciler.RequeueOnErrorOrStop(a.client.Status().Patch(a.context, a.snapshot, patch))
Expand All @@ -379,7 +379,7 @@ func (a *Adapter) EnsureSnapshotEnvironmentBindingExist() (reconciler.OperationR
"environment", availableEnvironment.Name,
"snapshot", a.snapshot.Name)
patch := client.MergeFrom(a.snapshot.DeepCopy())
gitops.SetSnapshotIntegrationStatusAsInvalid(a.snapshot, "Failed to create SnapshotEnvironmentBinding: "+err.Error())
gitops.SetSnapshotIntegrationStatusAsError(a.snapshot, "Failed to create SnapshotEnvironmentBinding: "+err.Error())

Check warning on line 382 in controllers/snapshot/snapshot_adapter.go

View check run for this annotation

Codecov / codecov/patch

controllers/snapshot/snapshot_adapter.go#L382

Added line #L382 was not covered by tests
a.logger.LogAuditEvent("Snapshot integration status marked as Invalid. Failed to create SnapshotEnvironmentBinding",
a.snapshot, h.LogActionUpdate)
return reconciler.RequeueOnErrorOrStop(a.client.Status().Patch(a.context, a.snapshot, patch))
Expand Down
29 changes: 27 additions & 2 deletions gitops/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ const (
// AppStudioIntegrationStatusInvalid is the reason that's set when the AppStudio integration gets into an invalid state.
AppStudioIntegrationStatusInvalid = "Invalid"

// AppStudioIntegrationStatusErrorOccured is the reason that's set when the AppStudio integration gets into an error state.
AppStudioIntegrationStatusErrorOccured = "ErrorOccured"

// AppStudioIntegrationStatusValid is the reason that's set when the AppStudio integration gets into an valid state.
AppStudioIntegrationStatusValid = "Valid"

Expand Down Expand Up @@ -169,7 +172,18 @@ func SetSnapshotIntegrationStatusAsInvalid(snapshot *applicationapiv1alpha1.Snap
Message: message,
}
meta.SetStatusCondition(&snapshot.Status.Conditions, condition)
go metrics.RegisterInvalidSnapshot(condition.Type, condition.Reason)
go metrics.RegisterInvalidSnapshot(AppStudioIntegrationStatusCondition, AppStudioIntegrationStatusInvalid)
}

// SetSnapshotIntegrationStatusAsError sets the AppStudio integration status condition for the Snapshot to error.
func SetSnapshotIntegrationStatusAsError(snapshot *applicationapiv1alpha1.Snapshot, message string) {
condition := metav1.Condition{
Type: AppStudioIntegrationStatusCondition,
Status: metav1.ConditionFalse,
Reason: AppStudioIntegrationStatusErrorOccured,
Message: message,
}
meta.SetStatusCondition(&snapshot.Status.Conditions, condition)
}

// MarkSnapshotIntegrationStatusAsInProgress sets the AppStudio integration status condition for the Snapshot to In Progress.
Expand Down Expand Up @@ -227,6 +241,18 @@ func IsSnapshotNotStarted(snapshot *applicationapiv1alpha1.Snapshot) bool {
return false
}

// IsSnapshotError if the AppStudio Integration Status condition is in ErrorOcurred status.
func IsSnapshotError(snapshot *applicationapiv1alpha1.Snapshot) bool {
condition := meta.FindStatusCondition(snapshot.Status.Conditions, AppStudioIntegrationStatusCondition)
if condition == nil {
condition = meta.FindStatusCondition(snapshot.Status.Conditions, LegacyIntegrationStatusCondition)
}

Check warning on line 249 in gitops/snapshot.go

View check run for this annotation

Codecov / codecov/patch

gitops/snapshot.go#L248-L249

Added lines #L248 - L249 were not covered by tests
if condition.Reason == AppStudioIntegrationStatusErrorOccured {
return true
}
return false

Check warning on line 253 in gitops/snapshot.go

View check run for this annotation

Codecov / codecov/patch

gitops/snapshot.go#L253

Added line #L253 was not covered by tests
}

// IsSnapshotValid checks if the AppStudio Integration Status condition is not invalid.
func IsSnapshotValid(snapshot *applicationapiv1alpha1.Snapshot) bool {
condition := meta.FindStatusCondition(snapshot.Status.Conditions, AppStudioIntegrationStatusCondition)
Expand Down Expand Up @@ -294,7 +320,6 @@ func NewSnapshot(application *applicationapiv1alpha1.Application, snapshotCompon
Components: *snapshotComponents,
},
}
go metrics.RegisterNewSnapshot()
return snapshot
}

Expand Down
5 changes: 3 additions & 2 deletions gitops/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ var _ = Describe("Gitops functions for managing Snapshots", Ordered, func() {
Expect(meta.IsStatusConditionTrue(updatedSnapshot.Status.Conditions, gitops.AppStudioTestSuceededCondition)).To(BeFalse())
})

It("ensures the Snapshots status can be marked as invalid", func() {
gitops.SetSnapshotIntegrationStatusAsInvalid(hasSnapshot, "Test message")
It("ensures the Snapshots status can be marked as error", func() {
gitops.SetSnapshotIntegrationStatusAsError(hasSnapshot, "Test message")
Expect(hasSnapshot).NotTo(BeNil())
Expect(hasSnapshot.Status.Conditions).NotTo(BeNil())
Expect(meta.IsStatusConditionTrue(hasSnapshot.Status.Conditions, gitops.AppStudioIntegrationStatusCondition)).To(BeFalse())
Expect(gitops.IsSnapshotError(hasSnapshot)).To(BeTrue())
})

It("ensures the Snapshots status can be marked as finished", func() {
Expand Down

0 comments on commit 2c2602f

Please sign in to comment.