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 annotation refreshed-at to delay TTL #97

Merged
merged 7 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ a value such as `30m`, `24h` and `7d`.
The resource is deleted after the current timestamp surpasses the sum of the resource's `metadata.creationTimestamp` and
the duration specified by the `k8s-ttl-controller.twin.sh/ttl` annotation.

If the resource is annotated with `k8s-ttl-controller.twin.sh/refreshed-at`, the TTL will be calculated from the value of
this annotation instead of the `metadata.creationTimestamp`.

## Usage
### Setting a TTL on a resource
Expand Down
24 changes: 19 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

const (
AnnotationTTL = "k8s-ttl-controller.twin.sh/ttl"
AnnotationTTL = "k8s-ttl-controller.twin.sh/ttl"
AnnotationRefreshedAt = "k8s-ttl-controller.twin.sh/refreshed-at"

MaximumFailedExecutionBeforePanic = 10 // Maximum number of allowed failed executions before panicking
ExecutionTimeout = 20 * time.Minute // Maximum time for each reconciliation before timing out
Expand Down Expand Up @@ -90,6 +91,18 @@ func Reconcile(kubernetesClient kubernetes.Interface, dynamicClient dynamic.Inte
}
}

func getStartTime(item unstructured.Unstructured) metav1.Time {
refreshedAt, exists := item.GetAnnotations()[AnnotationRefreshedAt]
if exists {
t, err := time.Parse(time.RFC3339, refreshedAt)
if err == nil {
return metav1.NewTime(t)
}
log.Printf("Failed to parse refreshed-at timestamp '%s' for %s/%s: %s", refreshedAt, item.GetKind(), item.GetName(), err)
}
return item.GetCreationTimestamp()
}

// DoReconcile goes over all API resources specified, retrieves all sub resources and deletes those who have expired
func DoReconcile(dynamicClient dynamic.Interface, eventManager *kevent.EventManager, resources []*metav1.APIResourceList) bool {
for _, resource := range resources {
Expand All @@ -116,6 +129,7 @@ func DoReconcile(dynamicClient dynamic.Interface, eventManager *kevent.EventMana
gvr.Resource = apiResource.Name
var list *unstructured.UnstructuredList
var continueToken string
var ttlInDuration time.Duration
var err error
for list == nil || continueToken != "" {
list, err = dynamicClient.Resource(gvr).List(context.TODO(), metav1.ListOptions{TimeoutSeconds: &listTimeoutSeconds, Continue: continueToken, Limit: ListLimit})
Expand All @@ -134,16 +148,16 @@ func DoReconcile(dynamicClient dynamic.Interface, eventManager *kevent.EventMana
if !exists {
continue
}
ttlInDuration, err := str2duration.ParseDuration(ttl)
ttlInDuration, err = str2duration.ParseDuration(ttl)
if err != nil {
log.Printf("[%s/%s] has an invalid TTL '%s': %s\n", apiResource.Name, item.GetName(), ttl, err)
continue
}
ttlExpired := time.Now().After(item.GetCreationTimestamp().Add(ttlInDuration))
ttlExpired := time.Now().After(getStartTime(item).Add(ttlInDuration))
if ttlExpired {
durationSinceExpired := time.Since(item.GetCreationTimestamp().Add(ttlInDuration)).Round(time.Second)
durationSinceExpired := time.Since(getStartTime(item).Add(ttlInDuration)).Round(time.Second)
log.Printf("[%s/%s] is configured with a TTL of %s, which means it has expired %s ago", apiResource.Name, item.GetName(), ttl, durationSinceExpired)
err := dynamicClient.Resource(gvr).Namespace(item.GetNamespace()).Delete(context.TODO(), item.GetName(), metav1.DeleteOptions{})
err = dynamicClient.Resource(gvr).Namespace(item.GetNamespace()).Delete(context.TODO(), item.GetName(), metav1.DeleteOptions{})
if err != nil {
log.Printf("[%s/%s] failed to delete: %s\n", apiResource.Name, item.GetName(), err)
eventManager.Create(item.GetNamespace(), item.GetKind(), item.GetName(), "FailedToDeleteExpiredTTL", "Unable to delete expired resource:"+err.Error(), true)
Expand Down
47 changes: 47 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,53 @@ func TestReconcile(t *testing.T) {
},
expectedResourcesLeftAfterReconciliation: 2,
},
{
name: "expired-pod-is-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 0,
},
{
name: "not-expired-pod-is-not-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "unannotated-pod-is-not-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "unannotated-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "one-out-of-two-pods-is-deleted-because-only-one-expired-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 1,
},
{
name: "multiple-expired-pods-are-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-1", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-2", time.Now().Add(-72*time.Hour), map[string]interface{}{AnnotationTTL: "2d", AnnotationRefreshedAt: time.Now().Add(-49 * time.Hour).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 0,
},
{
name: "only-expired-pods-are-deleted-by-refreshed-at",
podsToCreate: []*unstructured.Unstructured{
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-1", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "5m", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "not-expired-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationTTL: "3d", AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "expired-pod-name-2", time.Now().Add(-72*time.Hour), map[string]interface{}{AnnotationTTL: "2d", AnnotationRefreshedAt: time.Now().Add(-49 * time.Hour).Format(time.RFC3339)}),
newUnstructuredWithAnnotations("v1", "Pod", "default", "unannotated-pod-name", time.Now().Add(-time.Hour), map[string]interface{}{AnnotationRefreshedAt: time.Now().Add(-10 * time.Minute).Format(time.RFC3339)}),
},
expectedResourcesLeftAfterReconciliation: 2,
},
}

// Run scenarios
Expand Down
Loading