Skip to content

Commit

Permalink
Add automated nightly scaling of Deployments/StatefulSets/DaemonSets
Browse files Browse the repository at this point in the history
  • Loading branch information
mgianluc committed Feb 16, 2024
1 parent 87de7cd commit b8b18ec
Show file tree
Hide file tree
Showing 29 changed files with 167 additions and 11 deletions.
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,32 @@ Each notification contains list of all resources successfully deleted (or modifi

- 👉 For feature requests and bugs, file an [issue](https://github.com/gianlucam76/k8s-cleaner/issues).
- 👉 To get updates [⭐️ star](https://github.com/gianlucam76/k8s-cleaner/stargazers) this repository.
- 👉 Working examples can be found in the [examples](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples) section.
- 👉 Working examples can be found in the [examples](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource) section.

Currently k8s-cleaner has rich set of working examples to identify and list unused:

- [ConfigMaps](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/configmaps)/[Secrets](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/secrets)
- [ClusterRoles](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/clusterroles)/[Roles](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/roles)
- [ServiceAccounts](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/service-accounts)
- [PersistentVolumes](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/presistent-volumes)/[PersistentVolumeClaims](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/persistent-volume-claims)
- [Deployments](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/deployments)/[StatefulSets](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/stateful-sets)
- [Identify resources based on annotation indicating the maximum lifespan or the expiration date](https://github.com/gianlucam76/k8s-cleaner/tree/main/unused-resources-examples/time_based_delete)
- [ConfigMaps](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/configmaps)/[Secrets](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/secrets)
- [ClusterRoles](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/clusterroles)/[Roles](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/roles)
- [ServiceAccounts](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/service-accounts)
- [PersistentVolumes](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/presistent-volumes)/[PersistentVolumeClaims](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/persistent-volume-claims)
- [Deployments](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/deployments)/[StatefulSets](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/stateful-sets)
- [Identify resources based on annotation indicating the maximum lifespan or the expiration date](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unused-resource/time_based_delete)
- many more

There are also examples to identify unhealthy resources:

- [Pods Mounting Secrets with Old Content](https://github.com/gianlucam76/k8s-cleaner/tree/main/unhealthy-resources-examples/pod-with-outdated-secrets): Detect pods that are not utilizing the most recent Secret data.
- [Pods Using Expired Certificates](https://github.com/gianlucam76/k8s-cleaner/tree/main/unhealthy-resources-examples/pod-with-expired-certificates): Pinpoint pods that are operating with expired security certificates.
- [Pods Mounting Secrets with Old Content](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unhealthy-resources/pod-with-outdated-secrets): Detect pods that are not utilizing the most recent Secret data.
- [Pods Using Expired Certificates](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-unhealthy-resources/pod-with-expired-certificates): Pinpoint pods that are operating with expired security certificates.

# Manage and Automate Resource Operations:

K8s-cleaner doesn't just help you identify unused or unhealthy resources; it can also automate various operations to enhance your cluster's efficiency and management. The examples-operations directory showcases practical scripts and configurations covering key tasks:

## Scaling Deployments/DaemonSets/StatefulSets with Nightly Downtime:

This [example](https://github.com/gianlucam76/k8s-cleaner/tree/main/examples-automated-operations/scheduled-scaling) demonstrates how to automatically scale down Deployments, DaemonSets, and StatefulSets with a specified annotation at a desired time (e.g., 8 PM nightly).
Before scaling down, the replica count is stored in another annotation for later retrieval.
At the configured scale-up time (e.g., 8 AM), resources are restored to their original replica count, ensuring efficient resource utilization during off-peak hours

# Contribute to Cleaner Examples

Expand Down Expand Up @@ -411,10 +421,10 @@ spec:
## Delete Kubernetes resources on a configured time to live or expiration date
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/unused-resources-examples/time_based_delete/delete_resource_based_on_ttl_annotation.yaml).
YAML can be found [here](https://github.com/gianlucam76/k8s-cleaner/blob/main/examples-unused-resource/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/unused-resources-examples/time_based_delete/delete_resource_based_on_expire_date.yaml).
YAML can be found [here](https://github.com/gianlucam76/k8s-cleaner/blob/main/examples-unused-resource/time_based_delete/delete_resource_based_on_expire_date.yaml).
## DryRun
Expand Down
74 changes: 74 additions & 0 deletions examples-automated-operations/scheduled-scaling/pause.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# This cleaner:
# - runs at 8PM every day
# - finds all Deployments/StatefulSet/DaemonSet with
# annotation "pause-resume"
#
# For any such resource:
# - store current replicas in the annotation "previous-replicas"
# - set their replicas to zero (scale down and pause)
#
apiVersion: apps.projectsveltos.io/v1alpha1
kind: Cleaner
metadata:
name: scale-down-deployment-statefulset-daemonset
spec:
schedule: "* 20 * * *"
action: Transform
transform: |
-- Set replicas to 0
function transform()
hs = {}
if obj.metadata.annotations == nil then
obj.metadata.annotations = {}
end
-- store in the annotation current replicas value
obj.metadata.annotations["previous-replicas"] = tostring(obj.spec.replicas)
-- reset replicas to 0
obj.spec.replicas = 0
hs.resource = obj
return hs
end
resourcePolicySet:
resourceSelectors:
- kind: Deployment
group: apps
version: v1
- kind: StatefulSet
group: "apps"
version: v1
- kind: DaemonSet
group: "apps"
version: v1
aggregatedSelection: |
function evaluate()
local hs = {}
-- returns true if object has annotaiton "pause-resume"
function hasPauseAnnotation(obj)
if obj.metadata.annotations ~= nil then
if obj.metadata.annotations["pause-resume"] then
return true
end
return false
end
return
end
local resourceToPause = {}
for _, resource in ipairs(resources) do
if hasPauseAnnotation(resource) then
table.insert(resourceToPause, {resource = resource})
end
end
if #resourceToPause > 0 then
hs.resources = resourceToPause
end
return hs
end
72 changes: 72 additions & 0 deletions examples-automated-operations/scheduled-scaling/resume.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# This cleaner:
# - runs at 8AM every day
# - finds all Deployments/StatefulSet/DaemonSet with
# annotation "pause-resume"
#
# For any such resource:
# - get old replicas in the annotation "previous-replicas"
# - set their replicas to such value (scale deployment/statefulset/daemonset up)
#
apiVersion: apps.projectsveltos.io/v1alpha1
kind: Cleaner
metadata:
name: scale-up-deployment-statefulset-daemonset
spec:
schedule: "* 8 * * *"
action: Transform
transform: |
-- Set replicas to 0
function transform()
hs = {}
if obj.metadata.annotations == nil then
return
end
if not obj.metadata.annotations["previous-replicas"] then
return
end
-- reset replicas
obj.spec.replicas = tonumber(obj.metadata.annotations["previous-replicas"])
hs.resource = obj
return hs
end
resourcePolicySet:
resourceSelectors:
- kind: Deployment
group: apps
version: v1
- kind: StatefulSet
group: "apps"
version: v1
- kind: DaemonSet
group: "apps"
version: v1
aggregatedSelection: |
function evaluate()
local hs = {}
-- returns true if object has annotaiton "pause-resume"
function hasPauseAnnotation(obj)
if obj.metadata.annotations ~= nil then
if obj.metadata.annotations["pause-resume"] then
return true
end
return false
end
return false
end
local resourceToUnPause = {}
for _, resource in ipairs(resources) do
if hasPauseAnnotation(resource) then
table.insert(resourceToUnPause, {resource = resource})
end
end
if #resourceToUnPause > 0 then
hs.resources = resourceToUnPause
end
return hs
end

0 comments on commit b8b18ec

Please sign in to comment.