Skip to content

Commit

Permalink
Add autoscaling using HorizontalPodAutoscaler (close #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
collimarco committed Jun 20, 2024
1 parent bdbec67 commit 5324dc2
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/cuber/cuberfile_validator.rb
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ def validate_procs
@options[:procs].each do |procname, proc|
@errors << "proc \"#{procname}\" name can only include lowercase letters" if procname !~ /\A[a-z]+\z/
@errors << "proc \"#{procname}\" command must be present" if proc[:cmd].to_s.strip.empty?
@errors << "proc \"#{procname}\" scale must be a positive number" unless proc[:scale].is_a?(Integer) && proc[:scale] > 0
@errors << "proc \"#{procname}\" scale must be a positive number or a range" unless (proc[:scale].is_a?(Integer) && proc[:scale] > 0) || (proc[:scale].is_a?(Range) && proc[:scale].minmax.all? { |m| m.is_a?(Integer) && m > 0 })
@errors << "proc \"#{procname}\" cpu must be a positive number" unless proc[:cpu].nil? || proc[:cpu].is_a?(Numeric) && proc[:cpu] > 0
@errors << "proc \"#{procname}\" ram must be a positive number" unless proc[:ram].nil? || proc[:ram].is_a?(Numeric) && proc[:ram] > 0
@errors << "proc \"#{procname}\" term must be a positive number" unless proc[:term].is_a?(Integer) && proc[:term] > 0
30 changes: 29 additions & 1 deletion lib/cuber/templates/deployment.yml.erb
Original file line number Diff line number Diff line change
@@ -131,7 +131,7 @@ metadata:
app.kubernetes.io/managed-by: cuber
spec:
revisionHistoryLimit: 0
replicas: <%= proc[:scale] %>
replicas: <%= proc[:scale].is_a?(Range) ? proc[:scale].min : proc[:scale] %>
selector:
matchLabels:
app: <%= procname %>-proc
@@ -225,6 +225,34 @@ spec:
terminationGracePeriodSeconds: <%= proc[:term] %>
<%- end -%>

<%- @options[:procs].select { |procname, proc| proc[:scale].is_a?(Range) }.each do |procname, proc| -%>
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: scale-<%= procname %>
namespace: <%= @options[:app] %>
labels:
app.kubernetes.io/name: <%= @options[:app].to_s.to_json %>
app.kubernetes.io/instance: <%= @options[:instance].to_s.to_json %>
app.kubernetes.io/version: <%= @options[:release].to_s.to_json %>
app.kubernetes.io/managed-by: cuber
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: <%= procname %>
minReplicas: <%= proc[:scale].min %>
maxReplicas: <%= proc[:scale].max %>
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
<%- end -%>

<%- @options[:cron].each do |jobname, cron| -%>
---
apiVersion: batch/v1
2 changes: 1 addition & 1 deletion lib/cuber/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Cuber
VERSION = '1.10.0'.freeze
VERSION = '1.11.0'.freeze
end

0 comments on commit 5324dc2

Please sign in to comment.