-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from gianlucam76/expiration-date
Identify and delete resources based on expiration date
- Loading branch information
Showing
2 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
examples/time_based_delete/delete_resource_based_on_expire_date.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |