Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add timeout function for check approval #257

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions apis/meta/v1alpha1/check_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@ func (as ApprovalStatuses) GetBySubject(subject rbacv1.Subject) *ApprovalStatus
}
return nil
}

// IsTimeout determine if the current is timeout.
// Take the current time as a reference, and compare it to the start time and the timeout duration.
func IsTimeout(startTime *metav1.Time, timeout metav1.Duration) bool {
if startTime.IsZero() || timeout.Duration == 0 {
return false
}
runtime := time.Since(startTime.Time)
return runtime >= timeout.Duration
}
38 changes: 38 additions & 0 deletions apis/meta/v1alpha1/check_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
package v1alpha1

import (
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("Test.Check", func() {
Expand Down Expand Up @@ -83,3 +86,38 @@ var _ = Describe("Test.Check", func() {
),
)
})

var _ = Describe("Test.IsTimeout", func() {
DescribeTable("IsTimeout",
func(startTime *metav1.Time, timeout time.Duration, expected bool) {
v1time := metav1.Duration{Duration: timeout}
actual := IsTimeout(startTime, v1time)
Expect(actual).To(Equal(expected))
},
Entry("nil startTime",
nil,
1*time.Second,
false,
),
Entry("timeout is zero",
&metav1.Time{Time: time.Now()},
0*time.Second,
false,
),
Entry("not timeout",
&metav1.Time{Time: time.Now().Add(1 * time.Minute)},
30*time.Second,
false,
),
Entry("timeout",
&metav1.Time{Time: time.Now().Add(-1 * time.Minute)},
30*time.Second,
true,
),
Entry("just a timeout",
&metav1.Time{Time: time.Now().Add(-1 * time.Minute)},
60*time.Second,
true,
),
)
})