Skip to content

Commit

Permalink
Introduce IsWithinGracePeriod function
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoxhaa committed Jun 2, 2024
1 parent c317e4e commit 9e6760c
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/schema/v1/contracts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package v1

import (
"fmt"
"github.com/icinga/icinga-go-library/types"
kmetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ktypes "k8s.io/apimachinery/pkg/types"
kcache "k8s.io/client-go/tools/cache"
"time"
)

type Resource interface {
Expand Down Expand Up @@ -58,6 +61,25 @@ func (m *Meta) SetOwnerReferences([]kmetav1.OwnerReference) { panic("Not expe
func (m *Meta) GetManagedFields() []kmetav1.ManagedFieldsEntry { panic("Not expected to be called") }
func (m *Meta) SetManagedFields([]kmetav1.ManagedFieldsEntry) { panic("Not expected to be called") }

func IsWithinGracePeriod(k kmetav1.Object) *string {
const gracePeriod = 5 * time.Minute

deadline := k.GetCreationTimestamp().Add(gracePeriod)
now := time.Now()
if now.Before(deadline) {
key, _ := kcache.MetaNamespaceKeyFunc(k)
reason := fmt.Sprintf(
"%s %s is within grace period until %s, so its state is not yet evaluated.",
types.Name(k),
key,
deadline)

return &reason
}

return nil
}

// Assert interface compliance.
var (
_ kmetav1.Object = (*Meta)(nil)
Expand Down

0 comments on commit 9e6760c

Please sign in to comment.