forked from gianlucam76/k8s-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add automated nightly scaling of Deployments/StatefulSets/DaemonSets
- Loading branch information
mgianluc
committed
Feb 16, 2024
1 parent
87de7cd
commit b8b18ec
Showing
29 changed files
with
167 additions
and
11 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
74 changes: 74 additions & 0 deletions
74
examples-automated-operations/scheduled-scaling/pause.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,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
72
examples-automated-operations/scheduled-scaling/resume.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,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 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.