Skip to content

Commit

Permalink
Merge pull request #247 from jinqi7/STONEINTG-469
Browse files Browse the repository at this point in the history
fix(STONEINTG-469): add SetSnapshotIntegrationStatusAsInvalidandNoRequ
  • Loading branch information
jinqi7 authored Jul 28, 2023
2 parents de87b51 + 2c2602f commit fbdb95c
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()

a.logger.LogAuditEvent("Created new Snapshot", expectedSnapshot, h.LogActionAdd,
"snapshot.Name", expectedSnapshot.Name,
Expand Down Expand Up @@ -559,6 +561,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())
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())
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())
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())
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())
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)
}
if condition.Reason == AppStudioIntegrationStatusErrorOccured {
return true
}
return false
}

// 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 fbdb95c

Please sign in to comment.