Skip to content

Commit

Permalink
Merge pull request konflux-ci#565 from Josh-Everett/751
Browse files Browse the repository at this point in the history
fix(STONEINTG-751): add backoff threshold for GH authentication
  • Loading branch information
Josh-Everett committed Feb 27, 2024
2 parents b37833f + 3b2e25a commit cfd157e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
12 changes: 4 additions & 8 deletions controllers/snapshot/snapshot_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import (
"context"
"errors"
"fmt"
clienterrors "k8s.io/apimachinery/pkg/api/errors"
"reflect"
ctrl "sigs.k8s.io/controller-runtime"
"strings"
"time"

clienterrors "k8s.io/apimachinery/pkg/api/errors"
ctrl "sigs.k8s.io/controller-runtime"

applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"github.com/redhat-appstudio/integration-service/api/v1beta1"
"github.com/redhat-appstudio/integration-service/gitops"
Expand All @@ -44,7 +43,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const RetryReleaseTimeout = time.Duration(3 * time.Hour)
const SnapshotRetryTimeout = time.Duration(3 * time.Hour)

// configuration options for scenario
type ScenarioOptions struct {
Expand Down Expand Up @@ -940,10 +939,7 @@ func GetDetailsFromStatusError(err error) (string, string) {
// to requeue the object and the error message passed to the function. If not, the function returns
// an operation result instructing the reconciler NOT to requeue the object.
func (a *Adapter) RequeueIfYoungerThanThreshold(retErr error) (controller.OperationResult, error) {
snapshotCreationTime := a.snapshot.GetCreationTimestamp().Time
durationSinceSnapshotCreation := time.Since(snapshotCreationTime)

if durationSinceSnapshotCreation < RetryReleaseTimeout {
if h.IsObjectYoungerThanThreshold(a.snapshot, SnapshotRetryTimeout) {
return controller.RequeueWithError(retErr)
}
return controller.ContinueProcessing()
Expand Down
2 changes: 1 addition & 1 deletion controllers/snapshot/snapshot_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
// Set snapshot creation time to 3 hours ago
// time.Sub takes a time.Time and returns a time.Duration. Time.Add takes a time.Duration
// and returns a time.Time. Why? Who knows. We want the latter, so we add -3 hours here
hasSnapshot.CreationTimestamp = metav1.NewTime(time.Now().Add(-1 * RetryReleaseTimeout))
hasSnapshot.CreationTimestamp = metav1.NewTime(time.Now().Add(-1 * SnapshotRetryTimeout))

adapter = NewAdapter(hasSnapshot, hasApp, hasComp, log, loader.NewMockLoader(), k8sClient, ctx)
testErr := fmt.Errorf("something went wrong with the release")
Expand Down
12 changes: 10 additions & 2 deletions controllers/statusreport/statusreport_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package statusreport
import (
"context"
"fmt"

"github.com/redhat-appstudio/integration-service/api/v1beta1"
"github.com/redhat-appstudio/integration-service/gitops"
"github.com/redhat-appstudio/integration-service/metrics"
"github.com/redhat-appstudio/operator-toolkit/metadata"
"time"

applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"github.com/redhat-appstudio/integration-service/helpers"
Expand All @@ -35,6 +35,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const SnapshotRetryTimeout = time.Duration(3 * time.Hour)

// Adapter holds the objects needed to reconcile a snapshot's test status report.
type Adapter struct {
snapshot *applicationapiv1alpha1.Snapshot
Expand Down Expand Up @@ -76,7 +78,13 @@ func (a *Adapter) EnsureSnapshotTestStatusReportedToGitHub() (controller.Operati

err := a.status.ReportSnapshotStatus(a.context, reporter, a.snapshot)
if err != nil {
return controller.RequeueWithError(fmt.Errorf("failed to report status: %w", err))
a.logger.Error(err, "failed to report test status to github for snapshot",
"snapshot.Namespace", a.snapshot.Namespace, "snapshot.Name", a.snapshot.Name)
if helpers.IsObjectYoungerThanThreshold(a.snapshot, SnapshotRetryTimeout) {
return controller.RequeueWithError(err)
} else {
return controller.ContinueProcessing()
}
}

if gitops.IsSnapshotMarkedAsPassed(a.snapshot) || gitops.IsSnapshotMarkedAsFailed(a.snapshot) || gitops.IsSnapshotMarkedAsInvalid(a.snapshot) {
Expand Down
8 changes: 8 additions & 0 deletions helpers/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"github.com/redhat-appstudio/integration-service/api/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
"sort"
"time"
Expand Down Expand Up @@ -546,3 +547,10 @@ func AddFinalizerToScenario(adapterClient client.Client, logger IntegrationLogge

return nil
}

func IsObjectYoungerThanThreshold(obj metav1.Object, threshold time.Duration) bool {
objectCreationTime := obj.GetCreationTimestamp().Time
durationSinceObjectCreation := time.Since(objectCreationTime)

return durationSinceObjectCreation < threshold
}
11 changes: 11 additions & 0 deletions helpers/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,4 +1072,15 @@ var _ = Describe("Pipeline Adapter", Ordered, func() {
logEntry = "Removed Finalizer from the Component"
Expect(buf.String()).Should(ContainSubstring(logEntry))
})

It("Returns RequeueWithError if the object is less than timeout threshold", func() {
hasCompTimeout := time.Duration(3 * time.Hour)

result := helpers.IsObjectYoungerThanThreshold(hasComp, hasCompTimeout)
Expect(result).To(BeTrue())
hasComp.CreationTimestamp = metav1.NewTime(time.Now().Add(-1 * hasCompTimeout))
result = helpers.IsObjectYoungerThanThreshold(hasComp, hasCompTimeout)
Expect(result).To(BeFalse())
})

})

0 comments on commit cfd157e

Please sign in to comment.