Skip to content

Commit

Permalink
Merge pull request #28 from gianlucam76/expiration-date
Browse files Browse the repository at this point in the history
Identify and delete resources based on expiration date
  • Loading branch information
gianlucam76 authored Dec 22, 2023
2 parents 8b0e437 + 0277226 commit 522716f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,14 @@ spec:
end
```
## Delete Kubernetes resources on a configured time to live
## Delete Kubernetes resources on a configured time to live or expiration date
Finds resources that have a __cleaner/ttl__ annotation, which specifies their maximum lifespan. Deletes resources that have lived longer than their specified TTL.
Finds resources that have the __cleaner/ttl__ annotation, which specifies their maximum lifespan. Deletes resources that have lived longer than their specified TTL.
YAML can be found [here](https://github.com/gianlucam76/k8s-cleaner/blob/main/examples/time_based_delete/delete_resource_based_on_ttl_annotation.yaml).
Find resources that have the __cleaner_expires__ annotation, which specifies their expiration date. Deletes resources that have that have surpassed their expiration date.
YAML can be found [here](https://github.com/gianlucam76/k8s-cleaner/blob/main/examples/time_based_delete/delete_resource_based_on_expire_date.yaml).
## DryRun
To preview which resources match the __Cleaner__'s criteria, set the __DryRun__ flag to true. The Cleaner will still execute its logic but will not actually delete or update any resources. To identify matching resources, search the controller logs for the message "resource is a match for cleaner".
Expand Down
106 changes: 106 additions & 0 deletions examples/time_based_delete/delete_resource_based_on_expire_date.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# This Cleaner automatically manages resource lifespans:
# It executes hourly tasks to:
# - Identify eligible resources: Discover Deployments, StatefulSets, and Services bearing the cleaner/expires annotation, indicating their expire date.
# - Execute resource removal: Purge resources that have surpassed their expiration date, ensuring a streamlined environment.
#
# Example cleaner/expires values: 2023-12-12T09:35:56Z
#
# If you want to filter resources based on namespace =>
# - kind: Deployment
# group: "apps"
# version: v1
# namespace: <YOUR_NAMESPACE>
#
# If you want to filter resources based on labels =>
# - kind: Deployment
# group: "apps"
# version: v1
# labelFilters:
# - key: app
# operation: Equal
# value: nginx
# - key: environment
# operation: Different
# value: prouction
#
# If you need further filtering modify `function evaluate` you can access any
# field of obj
#
# If you want to remove any other resource including your own custom resources
# replace/add kind/group/version to resourceSelectors
#
apiVersion: apps.projectsveltos.io/v1alpha1
kind: Cleaner
metadata:
name: expire-date-based-cleaner
spec:
schedule: "0 * * * *"
dryRun: false
resourcePolicySet:
resourceSelectors:
- kind: Deployment
group: "apps"
version: v1
- kind: StatefulSet
group: "apps"
version: v1
- kind: Service
group: ""
version: v1
aggregatedSelection: |
-- Convert creationTimestamp "2023-12-12T09:35:56Z"
function convertTimestampString(timestampStr)
local convertedTimestamp = string.gsub(
timestampStr,
'(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)Z',
function(y, mon, d, h, mi, s)
return os.time({
year = tonumber(y),
month = tonumber(mon),
day = tonumber(d),
hour = tonumber(h),
min = tonumber(mi),
sec = tonumber(s)
})
end
)
return convertedTimestamp
end
function getResourceKey(resource)
return resource.kind .. ":" .. resource.metadata.namespace .. ":" .. resource.metadata.name
end
-- Any resources that have surpassed their expiration date, will be deleted
function evaluate()
hs = {}
local expiredResources = {}
currentTime = os.time()
print("current time:" .. currentTime)
for _, resource in ipairs(resources) do
if resource.metadata.annotations ~= nil then
if resource.metadata.annotations["cleaner/expires"] then
resourceKey = getResourceKey(resource)
expireDate = convertTimestampString(resource.metadata.annotations["cleaner/expires"])
print(resourceKey .. ' expireDate: ' .. expireDate)
timeDifference = os.difftime(currentTime, tonumber(expireDate))
print(resourceKey .. ' timeDifference: ' .. timeDifference)
-- if resource has been running for over ttl
if timeDifference > 0 then
table.insert(expiredResources, resource)
end
end
end
end
if #expiredResources > 0 then
hs.resources = expiredResources
end
return hs
end
action: Delete

0 comments on commit 522716f

Please sign in to comment.